Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

casync: compute seed hash on the fly #25046

Merged
merged 4 commits into from
Jul 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions system/hardware/tici/agnos.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import system.hardware.tici.casync as casync

SPARSE_CHUNK_FMT = struct.Struct('H2xI4x')
CAIBX_URL = "https://commadist.azureedge.net/agnosupdate/"


class StreamingDecompressor:
Expand Down Expand Up @@ -103,28 +104,37 @@ def get_partition_path(target_slot_number: int, partition: dict) -> str:
return path


def get_raw_hash(path: str, partition_size: int) -> str:
raw_hash = hashlib.sha256()
pos, chunk_size = 0, 1024 * 1024

with open(path, 'rb+') as out:
while pos < partition_size:
n = min(chunk_size, partition_size - pos)
raw_hash.update(out.read(n))
pos += n

return raw_hash.hexdigest().lower()


def verify_partition(target_slot_number: int, partition: Dict[str, Union[str, int]], force_full_check: bool = False) -> bool:
full_check = partition['full_check'] or force_full_check
path = get_partition_path(target_slot_number, partition)

if not isinstance(partition['size'], int):
return False

partition_size: int = partition['size']

if not isinstance(partition['hash_raw'], str):
return False
partition_hash: str = partition['hash_raw']
with open(path, 'rb+') as out:
if full_check:
raw_hash = hashlib.sha256()

pos, chunk_size = 0, 1024 * 1024
while pos < partition_size:
n = min(chunk_size, partition_size - pos)
raw_hash.update(out.read(n))
pos += n
partition_hash: str = partition['hash_raw']

return raw_hash.hexdigest().lower() == partition_hash.lower()
else:
if full_check:
return get_raw_hash(path, partition_size) == partition_hash.lower()
else:
with open(path, 'rb+') as out:
out.seek(partition_size)
return out.read(64) == partition_hash.lower().encode()

Expand Down Expand Up @@ -177,8 +187,13 @@ def extract_casync_image(target_slot_number: int, partition: dict, cloudlog):
sources: List[Tuple[str, casync.ChunkReader, casync.ChunkDict]] = []

# First source is the current partition. Index file for current version is provided in the manifest
if 'casync_seed_caibx' in partition:
sources += [('seed', casync.FileChunkReader(seed_path), casync.build_chunk_dict(casync.parse_caibx(partition['casync_seed_caibx'])))]
raw_hash = get_raw_hash(seed_path, partition['size'])
caibx_url = f"{CAIBX_URL}{partition['name']}-{raw_hash}.caibx"
try:
cloudlog.info(f"casync fetching {caibx_url}")
sources += [('seed', casync.FileChunkReader(seed_path), casync.build_chunk_dict(casync.parse_caibx(caibx_url)))]
except requests.RequestException:
cloudlog.error(f"casync failed to load {caibx_url}")

# Second source is the target partition, this allows for resuming
sources += [('target', casync.FileChunkReader(path), casync.build_chunk_dict(target))]
Expand Down