예제 #1
0
    def __call__(self, *, parse_result: typing.Any, tmp_dir: str,
                 image_file: str) -> int:
        if not parse_result.efi_device:
            print("No --efi-device provided, stopping.")
            exit(1)
        if not parse_result.image_device:
            print("No --image-device provided, stopping.")
            exit(1)

        with mount.Mount(
                parse_result.image_device,
                os.path.join(tmp_dir, "images"),
                options=parse_result.image_options,
                fs_type=parse_result.image_fs_type,
        ) as images_mnt:
            _copy_file(image_file,
                       images_mnt,
                       overwrite=parse_result.overwrite)

        with mount.Mount(
                parse_result.efi_device,
                os.path.join(tmp_dir, "efi_dest"),
                options=parse_result.efi_options,
                fs_type=parse_result.efi_fs_type,
        ) as efi_dest_mnt:
            return tool.execute_with_system_mounted(
                lambda e, _: _copy_efi(
                    e,
                    efi_dest_mnt,
                    include_bootloader=parse_result.include_bootloader,
                    overwrite=parse_result.overwrite,
                ),
                image_file=image_file,
                tmp_dir=tmp_dir,
            )
예제 #2
0
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
예제 #3
0
파일: tools.py 프로젝트: shizonic/cleanroom
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)
예제 #4
0
파일: tools.py 프로젝트: shizonic/cleanroom
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)
예제 #5
0
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)
예제 #6
0
def create_qemu_image(
    image_path: str,
    *,
    image_size: int,
    image_format: str = "qcow2",
    system_image_file: str,
    tmp_dir: str,
) -> str:
    trace(f"Creating image file {image_path}.")
    with disk.NbdDevice.new_image_file(image_path,
                                       image_size,
                                       disk_format=image_format) as device:
        _create_hdd_image(device)

        debug("mounting data partition for further setup.")
        with mount.Mount(
                device.device(3),
                os.path.join(tmp_dir, "data"),
                fs_type="btrfs",
                options="subvolid=0",
                fallback_cwd=os.getcwd(),
        ) as data_dir:
            _setup_btrfs(data_dir)

            trace("Copying image file")
            copyfile(
                system_image_file,
                os.path.join(data_dir, ".images",
                             os.path.basename(system_image_file)),
            )

            with mount.Mount(
                    device.device(1),
                    os.path.join(tmp_dir, "efi_dest"),
                    options="defaults",
                    fs_type="vfat",
            ) as efi_dest_mnt:
                tool.execute_with_system_mounted(
                    lambda e, _: _copy_efi(
                        e,
                        efi_dest_mnt,
                    ),
                    image_file=system_image_file,
                    tmp_dir=tmp_dir,
                )

    return image_path
    def __call__(self, parse_result: typing.Any) -> None:
        with TemporaryDirectory() as tempdir:
            with mount.Mount(parse_result.image_device,
                             os.path.join(tempdir, 'images'),
                             options=parse_result.image_options,
                             fs_type=parse_result.image_fs_type) as images_mnt:
                exported_file = tool.export_into_directory(
                    parse_result.system_name,
                    images_mnt,
                    version=parse_result.system_version,
                    repository=parse_result.repository)

                tool.copy_efi_partition(image_file=exported_file,
                                        efi_device=parse_result.efi_device,
                                        tempdir=tempdir)
예제 #8
0
def create_qemu_image(image_path: str,
                      *,
                      image_size: str,
                      image_format: str = "qcow2",
                      system_name: str,
                      system_version: str = "",
                      repository: str,
                      tempdir: str) -> str:
    trace("Creating image file {}.".format(image_path))
    with disk.NbdDevice.new_image_file(image_path,
                                       image_size,
                                       disk_format=image_format) as device:
        _create_hdd_image(device)

        debug("mounting data partition for further setup.")
        with mount.Mount(
                device.device(3),
                os.path.join(tempdir, "data"),
                fs_type="btrfs",
                options="subvolid=0",
                fallback_cwd=os.getcwd(),
        ) as data_dir:
            _setup_btrfs(data_dir)

            extract_location = os.path.join(data_dir, ".images")
            verbose("Extracting system image to {}.".format(extract_location))
            extracted_version = tool.write_image(
                system_name,
                extract_location,
                repository=repository,
                version=system_version,
            )

            extracted_image = os.path.join(data_dir, ".images",
                                           "clrm_{}".format(extracted_version))
            assert os.path.isfile(extracted_image)

            tool.copy_efi_partition(
                image_file=extracted_image,
                efi_device=device.device(1),
                tempdir=tempdir,
                kernel_only=False,
            )

    return image_path
예제 #9
0
def create_qemu_image(image_path: str, *,
                      image_size: str, image_format: str = 'qcow2',
                      system_name: str, system_version: str = '',
                      repository: str,
                      tempdir: str) -> str:
    trace('Creating image file {}.'.format(image_path))
    with disk.NbdDevice.new_image_file(image_path, image_size,
            disk_format=image_format) as device:
        _create_hdd_image(device)

        debug('mounting data partition for further setup.')
        with mount.Mount(device.device(3), os.path.join(tempdir, 'data'),
                         fs_type='btrfs',
                         options='subvolid=0',
                         fallback_cwd=os.getcwd()) as data_dir:
            _setup_btrfs(data_dir)

            extract_location = os.path.join(data_dir, '.images')
            verbose('Extracting system image to {}.'
                    .format(extract_location))
            extracted_version \
                = tool.write_image(system_name, extract_location,
                                   repository=repository,
                                   version=system_version)

            extracted_image \
                = os.path.join(data_dir, '.images',
                               'clrm_{}'.format(extracted_version))
            assert os.path.isfile(extracted_image)

            tool.copy_efi_partition(image_file=extracted_image,
                                    efi_device=device.device(1),
                                    tempdir=tempdir,
                                    kernel_only=False)

    return image_path