From 569caa733f9bd99a20c7063b0454b8f764e53e0a Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 23 Oct 2024 17:15:55 +0200 Subject: cache: add code to cache the oglive boot files Add update_live_cache() implementing the legacy script updateBootCache() Copy the ogvmlinuz and oginitrd.img files into cache after a partition and format command with an available cache partition. --- src/live/ogOperations.py | 1 + src/utils/cache.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) (limited to 'src') diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py index 6f66900..64e5532 100644 --- a/src/live/ogOperations.py +++ b/src/live/ogOperations.py @@ -541,6 +541,7 @@ class OgLiveOperations: fs = part["filesystem"].lower() if fs == 'cache': mount_cache() + update_live_cache() logging.info('Partition setup command OK') result = self.refresh(ogRest) diff --git a/src/utils/cache.py b/src/utils/cache.py index ea32f8b..d1ae3d9 100644 --- a/src/utils/cache.py +++ b/src/utils/cache.py @@ -7,6 +7,7 @@ # (at your option) any later version. import logging +import shutil import os from src.utils.fs import mount_mkdir, umount @@ -66,3 +67,50 @@ def generate_cache_txt(): return content = ','.join(files) write_cache_txt(content) + +def _read_checksum(file_path): + res = '' + try: + with open(file_path, 'r') as f: + res = f.read() + except (FileNotFoundError, PermissionError, OSError) as e: + logging.info(f'Cannot read checksum file {file_path}: {e}') + return res + +def update_live_cache(): + live_dir = os.getenv('oglivedir') + + if not live_dir: + logging.warning('No oglivedir environment variable defined') + return + + cache_mnt = mount_cache() + if not cache_mnt: + logging.info(f'No cache partition available, skipping live cache generation') + return + + src_dir = f"/opt/oglive/tftpboot/{live_dir}" + dst_dir = f"{cache_mnt}/boot/{live_dir}" + + if not os.path.exists(src_dir): + logging.info(f'{src_dir} not found') + return + + os.makedirs(dst_dir, exist_ok=True) + + sum_extension = '.sum' + + files = ['ogvmlinuz', 'oginitrd.img'] + + for file_name in files: + server_file = f'{src_dir}/{file_name}' + client_file = f'{dst_dir}/{file_name}' + server_checksum = _read_checksum(server_file + sum_extension) + client_checksum = _read_checksum(client_file + sum_extension) + + if server_checksum and server_checksum != client_checksum: + logging.info(f'Updating live cache {client_file}') + shutil.copyfile(server_file, client_file) + shutil.copyfile(server_file + sum_extension, client_file + sum_extension) + else: + logging.info(f'{client_file} is already up to date') -- cgit v1.2.3-18-g5258