def execute_with_system_mounted(to_execute: typing.Callable[[str, str], int], *, image_file: str, tmp_dir: str) -> int: assert os.path.isfile(image_file) with disk.NbdDevice(image_file, disk_format="raw", read_only=True) as device: verbose("Mounting EFI...") device.wait_for_device_node(partition=1) with mount.Mount( device.device(1), os.path.join(tmp_dir, "EFI"), fs_type="vfat", options="ro", ) as efi: verbose("Mounting root filesystem...") with mount.Mount( device.device(2), os.path.join(tmp_dir, "root"), fs_type="squashfs", options="ro", ) as root: trace(f'Executing with EFI "{efi}" and root "{root}".') result = to_execute(efi, root) return result
def execute_with_system_mounted(to_execute: typing.Callable[[str, str], None], *, repository: str, system_name: str, system_version: str = '') -> None: with TemporaryDirectory(prefix='clrm_qemu_') as tempdir: verbose('Extracting image') image_path \ = export_into_directory(system_name, tempdir, repository=repository, version=system_version) assert os.path.isfile(image_path) with disk.NbdDevice(image_path, disk_format='raw') as device: verbose('Mounting EFI...') device.wait_for_device_node(partition=1) with mount.Mount(device.device(1), os.path.join(tempdir, 'EFI'), fs_type='vfat', options='ro') as efi: verbose('Mounting root filesystem...') with mount.Mount(device.device(2), os.path.join(tempdir, 'root'), fs_type='squashfs', options='ro') as root: verbose('Executing with EFI "{}" and root "{}".' .format(efi, root)) to_execute(efi, root)
def copy_efi_partition(*, image_file: str, efi_device, tempdir: str, kernel_only: bool = True): verbose('Copying EFI configuration out of image file.') with disk.NbdDevice(image_file, disk_format='raw') \ as internal_device: internal_device.wait_for_device_node(partition=1) with mount.Mount(internal_device.device(1), os.path.join(tempdir, '_efi')) \ as int_efi: with mount.Mount(efi_device, os.path.join(tempdir, 'efi'), fs_type='vfat') \ as efi: if kernel_only: img_dir = os.path.join(int_efi, 'EFI', 'Linux') efi_dir = os.path.join(efi, 'EFI', 'Linux') assert os.path.isdir(img_dir) if not os.path.isdir(efi_dir): os.makedirs(efi_dir) for f in [f for f in os.listdir(img_dir) if os.path.isfile(os.path.join(img_dir, f))]: trace('Copying EFI kernel {}.'.format(f)) copyfile(os.path.join(img_dir, f), os.path.join(efi_dir, f)) else: trace('Copying EFI folder into system.') copy_tree(int_efi, efi)
def execute_with_system_mounted(to_execute: typing.Callable[[str, str], None], *, repository: str, system_name: str, system_version: str = "") -> None: with TemporaryDirectory(prefix="clrm_qemu_") as tempdir: verbose("Extracting image") image_path = export_into_directory(system_name, tempdir, repository=repository, version=system_version) assert os.path.isfile(image_path) with disk.NbdDevice(image_path, disk_format="raw") as device: verbose("Mounting EFI...") device.wait_for_device_node(partition=1) with mount.Mount( device.device(1), os.path.join(tempdir, "EFI"), fs_type="vfat", options="ro", ) as efi: verbose("Mounting root filesystem...") with mount.Mount( device.device(2), os.path.join(tempdir, "root"), fs_type="squashfs", options="ro", ) as root: verbose('Executing with EFI "{}" and root "{}".'.format( efi, root)) to_execute(efi, root)
def create_device(dev: str, format: str) -> disk.Device: if os.path.isfile(dev): return disk.NbdDevice(dev, disk_format=format) assert format == "raw" return disk.Device(dev)