casync: compute seed caibx url on the fly (#25046)

* compute seed hash on the fly

* more logging

* partition name in url

* fix comment
pull/25050/head
Willem Melching 3 years ago committed by GitHub
parent 50434d612e
commit 4080f729be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 41
      system/hardware/tici/agnos.py

@ -13,6 +13,7 @@ import requests
import system.hardware.tici.casync as casync import system.hardware.tici.casync as casync
SPARSE_CHUNK_FMT = struct.Struct('H2xI4x') SPARSE_CHUNK_FMT = struct.Struct('H2xI4x')
CAIBX_URL = "https://commadist.azureedge.net/agnosupdate/"
class StreamingDecompressor: class StreamingDecompressor:
@ -103,28 +104,37 @@ def get_partition_path(target_slot_number: int, partition: dict) -> str:
return path 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: 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 full_check = partition['full_check'] or force_full_check
path = get_partition_path(target_slot_number, partition) path = get_partition_path(target_slot_number, partition)
if not isinstance(partition['size'], int): if not isinstance(partition['size'], int):
return False return False
partition_size: int = partition['size'] partition_size: int = partition['size']
if not isinstance(partition['hash_raw'], str): if not isinstance(partition['hash_raw'], str):
return False 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 partition_hash: str = partition['hash_raw']
while pos < partition_size:
n = min(chunk_size, partition_size - pos)
raw_hash.update(out.read(n))
pos += n
return raw_hash.hexdigest().lower() == partition_hash.lower() if full_check:
else: return get_raw_hash(path, partition_size) == partition_hash.lower()
else:
with open(path, 'rb+') as out:
out.seek(partition_size) out.seek(partition_size)
return out.read(64) == partition_hash.lower().encode() return out.read(64) == partition_hash.lower().encode()
@ -177,8 +187,13 @@ def extract_casync_image(target_slot_number: int, partition: dict, cloudlog):
sources: List[Tuple[str, casync.ChunkReader, casync.ChunkDict]] = [] sources: List[Tuple[str, casync.ChunkReader, casync.ChunkDict]] = []
# First source is the current partition. Index file for current version is provided in the manifest # First source is the current partition. Index file for current version is provided in the manifest
if 'casync_seed_caibx' in partition: raw_hash = get_raw_hash(seed_path, partition['size'])
sources += [('seed', casync.FileChunkReader(seed_path), casync.build_chunk_dict(casync.parse_caibx(partition['casync_seed_caibx'])))] 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 # Second source is the target partition, this allows for resuming
sources += [('target', casync.FileChunkReader(path), casync.build_chunk_dict(target))] sources += [('target', casync.FileChunkReader(path), casync.build_chunk_dict(target))]

Loading…
Cancel
Save