Beispiel #1
0
    def create_export_directory(self, system_context: SystemContext) -> str:
        """Return the root directory."""
        export_volume = os.path.join(system_context.scratch_directory,
                                     'export')
        btrfs_helper = self._service('btrfs_helper')
        if btrfs_helper.is_subvolume(export_volume):
            btrfs_helper.delete_subvolume_recursive(export_volume)
        btrfs_helper.create_subvolume(export_volume)

        os_name = system_context.substitution('DISTRO_ID', 'clrm')
        timestamp = system_context.substitution('TIMESTAMP', 'unknown')

        image_filename \
            = os.path.join(export_volume,
                           '{}_{}{}'.format(os_name, timestamp,
                                            '.' + self._image_format
                                            if self._image_format != 'raw'
                                            else ''))
        create_image(image_filename,
                     self._image_format,
                     self._extra_partitions,
                     self._efi_size,
                     self._swap_size,
                     kernel_file=self._kernel_file,
                     root_partition=self._root_partition,
                     verity_partition=self._verity_partition,
                     root_hash=self._root_hash,
                     flock_command=self._binary(Binaries.FLOCK),
                     sfdisk_command=self._binary(Binaries.SFDISK))

        return export_volume
Beispiel #2
0
def _root_part_label(system_context: SystemContext) -> str:
    label = system_context.substitution("ROOTFS_PARTLABEL", "")
    return (
        label
        if label
        else "{}_{}".format(
            system_context.substitution("DISTRO_ID", "clrm"),
            system_context.substitution("DISTRO_VERSION_ID", system_context.timestamp),
        )
    )
Beispiel #3
0
def _validate_installation(location: Location, system_context: SystemContext) -> None:
    hostname = system_context.substitution("HOSTNAME")
    if hostname is None:
        raise GenerateError(
            "Trying to export a system without a hostname.", location=location
        )

    machine_id = system_context.substitution("MACHINE_ID")
    if machine_id is None:
        raise GenerateError(
            "Trying to export a system without " "a machine_id.", location=location
        )
Beispiel #4
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""
        os_release = 'NAME="{}"\n'.format(
            system_context.substitution("DISTRO_NAME", "Arch Linux"))
        os_release += 'PRETTY_NAME="{}"\n'.format(
            system_context.substitution("DISTRO_PRETTY_NAME", "Arch Linux"))
        os_release += 'ID="{}"\n'.format(
            system_context.substitution("DISTRO_ID", "arch"))
        os_release += 'ID_LIKE="arch"\n'
        os_release += 'ANSI_COLOR="0;36"\n'
        os_release += 'HOME_URL="{}"\n'.format(
            system_context.substitution("DISTRO_HOME_URL",
                                        "https://www.archlinux.org/"))
        os_release += 'SUPPORT_URL="{}"\n'.format(
            system_context.substitution("DISTRO_SUPPORT_URL",
                                        "https://bbs.archlinux.org/"))
        os_release += 'BUG_REPORT_URL="{}"\n'.format(
            system_context.substitution("DISTRO_BUG_URL",
                                        "https://bugs.archlinux.org/"))
        os_release += 'VERSION="{}"\n'.format(
            system_context.substitution("DISTRO_VERSION", "unknown"))
        os_release += 'VERSION_ID="{}"\n'.format(
            system_context.substitution("DISTRO_VERSION_ID", "unknown"))

        self._execute(
            location,
            system_context,
            "create",
            "/usr/lib/os-release",
            os_release,
            force=True,
            mode=0o644,
        )
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""
        os_release = 'NAME="{}"\n'\
            .format(system_context.substitution('DISTRO_NAME',
                                                'Arch Linux'))
        os_release += 'PRETTY_NAME=\"{}\"\n'\
            .format(system_context.substitution('DISTRO_PRETTY_NAME',
                                                'Arch Linux'))
        os_release += 'ID=\"{}\"\n'\
            .format(system_context.substitution('DISTRO_ID', 'arch'))
        os_release += 'ID_LIKE=\"arch\"\n'
        os_release += 'ANSI_COLOR=\"0;36\"\n'
        os_release += 'HOME_URL=\"{}\"\n'\
            .format(system_context.substitution('DISTRO_HOME_URL',
                                                'https://www.archlinux.org/'))
        os_release += 'SUPPORT_URL=\"{}\"\n'\
            .format(system_context.substitution('DISTRO_SUPPORT_URL',
                                                'https://bbs.archlinux.org/'))
        os_release += 'BUG_REPORT_URL=\"{}\"\n'\
            .format(system_context.substitution('DISTRO_BUG_URL',
                                                'https://bugs.archlinux.org/'))
        os_release += 'VERSION=\"{}\"\n'\
            .format(system_context.substitution('DISTRO_VERSION',
                                                'unknown'))
        os_release += 'VERSION_ID=\"{}\"\n'\
            .format(system_context.substitution('DISTRO_VERSION_ID',
                                                'unknown'))

        self._execute(location, system_context,
                      'create', '/usr/lib/os-release',
                      os_release, force=True, mode=0o644)
Beispiel #6
0
    def _create_cache_data(
        self, location: Location, system_context: SystemContext, *, has_kernel: bool
    ) -> typing.Tuple[str, str, str, str]:
        squashfs_file = self._create_squashfs(
            system_context, system_context.cache_directory
        )
        (verity_file, verity_uuid, root_hash) = _create_dmverity(
            system_context.cache_directory,
            squashfs_file,
            timestamp=system_context.timestamp,
            veritysetup_command=self._binary(Binaries.VERITYSETUP),
        )

        verity_device = "UUID={}".format(verity_uuid)
        squashfs_device = "PARTLABEL={}".format(_root_part_label(system_context))

        cmdline = system_context.substitution("KERNEL_CMDLINE", "")
        cmdline = " ".join(
            (cmdline, "systemd.volatile=true", "rootfstype=squashfs")
        ).strip()

        kernel_file = ""
        if has_kernel:
            kernel_file = self._create_complete_kernel(
                location,
                system_context,
                cmdline,
                squashfs_device,
                verity_device,
                root_hash,
                system_context.cache_directory,
            )

        return kernel_file, squashfs_file, verity_file, root_hash
Beispiel #7
0
    def __call__(
        self,
        location: Location,
        system_context: SystemContext,
        *args: typing.Any,
        **kwargs: typing.Any,
    ) -> None:
        """Execute command."""
        old_machine_id = system_context.substitution("MACHINE_ID", "")
        if old_machine_id:
            raise GenerateError(
                f'Machine-id was already set to "{old_machine_id}".',
                location=location,
            )

        machine_id = args[0]
        system_context.set_substitution("MACHINE_ID", machine_id)
        machine_id += "\n"
        self._execute(
            location.next_line(),
            system_context,
            "create",
            "/etc/machine-id",
            machine_id,
        )
Beispiel #8
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""

        # Enable KMS:
        self._execute(location, system_context, 'pkg_intel_kms')

        self._execute(location, system_context, 'pkg_xorg')

        # Set some kernel parameters:
        cmdline = system_context.substitution('KERNEL_CMDLINE', '')
        if cmdline:
            cmdline += ' '
        cmdline += 'intel_iommu=igfx_off i915.fastboot=1'
        system_context.set_substitution('KERNEL_CMDLINE', cmdline)

        self._execute(location, system_context, 'pacman', 'libva-intel-driver',
                      'mesa', 'vulkan-intel', 'xf86-video-intel',
                      'intel-media-driver')

        self._execute(location.next_line(), system_context, 'create',
                      '/etc/modprobe.d/i915-guc.conf',
                      'options i915 enable_guc=3')

        self._execute(location.next_line(),
                      system_context,
                      'remove',
                      '/usr/lib/firmware/amdgpu/*',
                      '/usr/lib/firmware/nvidia/*',
                      '/usr/lib/firmware/radeon/*',
                      force=True,
                      recursive=True)
Beispiel #9
0
 def _setup_hooks(
     self,
     location: Location,
     system_context: SystemContext,
     locales: typing.Sequence[str],
 ) -> None:
     if not system_context.substitution("CLRM_LOCALES", ""):
         location.set_description("run locale-gen")
         self._add_hook(
             location,
             system_context,
             "export",
             "run",
             "/usr/bin/locale-gen",
             inside=True,
         )
         location.set_description("Remove locale related data.")
         self._add_hook(
             location,
             system_context,
             "export",
             "remove",
             "/usr/share/locale/*",
             "/etc/locale.gen",
             "/usr/bin/locale-gen",
             "/usr/bin/localedef",
             force=True,
             recursive=True,
         )
         system_context.set_substitution("CLRM_LOCALES", ",".join(locales))
Beispiel #10
0
    def __call__(
        self,
        location: Location,
        system_context: SystemContext,
        *args: typing.Any,
        **kwargs: typing.Any,
    ) -> None:
        """Execute command."""
        static_hostname = args[0]
        pretty_hostname = kwargs.get("pretty", static_hostname)

        if system_context.substitution("HOSTNAME", ""):
            raise GenerateError("Hostname was already set.", location=location)

        system_context.set_substitution("HOSTNAME", static_hostname)
        system_context.set_substitution("PRETTY_HOSTNAME", pretty_hostname)

        self._execute(location, system_context, "create", "/etc/hostname",
                      static_hostname)
        self._execute(
            location.next_line(),
            system_context,
            "sed",
            f'/^PRETTY_HOSTNAME=/ cPRETTY_HOSTNAME="{pretty_hostname}"',
            "/etc/machine.info",
        )
Beispiel #11
0
    def __call__(
        self,
        location: Location,
        system_context: SystemContext,
        *args: typing.Any,
        **kwargs: typing.Any,
    ) -> None:
        """Execute command."""
        ## validate package type:
        if system_context.substitution("CLRM_PACKAGE_TYPE", ""):
            raise GenerateError(
                "Trying to run swupd_init on a system that already has a CLRM_PACKAGE_TYPE defined."
            )
        system_context.set_substitution("CLRM_PACKAGE_TYPE", "swupd")

        run(
            self._binary(Binaries.SWUPD),
            "autoupdate",
            f"--path={system_context.fs_directory}",
            "--disable",
            "--no-progress",
            returncode=28,
        )

        # Setup update-helper so that swupd os-install will actually work:
        os.makedirs(system_context.file_name("/usr/bin"))
        with open(system_context.file_name("/usr/bin/update-helper"), "wb") as fd:
            fd.write(
                dedent(
                    """\
                        #!/usr/bin/sh
                        exit 0
                    """
                ).encode("utf-8")
            )
        os.chmod(system_context.file_name("/usr/bin/update-helper"), 0o755)

        run(
            self._binary(Binaries.SWUPD),
            "os-install",
            f"--path={system_context.fs_directory}",
            "--skip-optional",
            "--no-progress",
        )

        location.set_description("Move systemd files into /usr")
        self._add_hook(location, system_context, "_teardown", "systemd_cleanup")

        with open(system_context.file_name("/usr/lib/os-release"), "r") as osr:
            for l in osr:
                l = l.strip()
                if l.startswith("BUILD_ID="):
                    build_id = l[9:]
                    verbose(f"Installed {build_id}.")
                    system_context.set_substitution("DISTRO_VERSION_ID", build_id)
                    system_context.set_substitution("DISTRO_VERSION", build_id)

        self._execute(location.next_line(), system_context, "create_os_release")
Beispiel #12
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""

        if system_context.substitution("ROOT_DEVICE") is None:
            GenerateError("ROOT_DEVICE must be set when creating EFI kernel.",
                          location=location)

        output = args[0]
        kernel = kwargs.get("kernel", "")
        initrd_directory = kwargs.get(
            "initrd",
            os.path.join(system_context.boot_directory, "initrd-parts"))
        initrd_files = _get_initrd_parts(location, initrd_directory)
        cmdline_input = kwargs.get("commandline", "")
        osrelease_file = system_context.file_name("/usr/lib/os-release")
        efistub = system_context.file_name("/usr/lib/systemd/boot/efi/"
                                           "linuxx64.efi.stub")

        debug("{}: Kernel   : {}.".format(self.name, kernel))
        debug("{}: Initrd   : {}.".format(self.name, ", ".join(initrd_files)))
        debug("{}: cmdline  : {}.".format(self.name, cmdline_input))
        debug("{}: osrelease: {}.".format(self.name, osrelease_file))
        debug("{}: efistub  : {}.".format(self.name, efistub))

        self._validate_files(kernel, *initrd_files, osrelease_file, efistub)
        with tempfile.TemporaryDirectory() as tmp:
            initrd = _create_initrd(tmp, *initrd_files)
            cmdline = _create_cmdline_file(tmp, cmdline_input)

            run(
                self._binary(Binaries.OBJCOPY),
                "--add-section",
                ".osrel={}".format(osrelease_file),
                "--change-section-vma",
                ".osrel=0x20000",
                "--add-section",
                ".cmdline={}".format(cmdline),
                "--change-section-vma",
                ".cmdline=0x30000",
                "--add-section",
                ".linux={}".format(kernel),
                "--change-section-vma",
                ".linux=0x40000",
                "--add-section",
                ".initrd={}".format(initrd),
                "--change-section-vma",
                ".initrd=0x3000000",
                efistub,
                output,
            )

            os.remove(initrd)
            os.remove(cmdline)
Beispiel #13
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""
        device = args[0]

        root_device_key = 'ROOT_DEVICE'

        dev = system_context.substitution(root_device_key)
        if dev is not None:
            raise GenerateError('"{}" root device is already set to "{}".'
                                .format(self.name, dev),
                                location=location)

        system_context.set_substitution(root_device_key, device)
        self._write_file(system_context, 'root_device', root_device_key)

        if ' ' in device:
            device = '"{}"'.format(device)

        cmdline = system_context.substitution('KERNEL_CMDLINE', '')
        cmdline = ' '.join((cmdline, 'root={}'.format(device), 'rootflags=ro'))
        system_context.set_substitution('KERNEL_CMDLINE', cmdline)
Beispiel #14
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""
        if not os.path.exists(system_context.file_name("usr/bin/mkinitcpio")):
            info("Skipping initrd generation: No mkinitcpio binary.")
            return

        if not os.path.exists(
                os.path.join(system_context.boot_directory, "vmlinuz")):
            info("Skipping initrd generation: No vmlinuz in boot directory.")
            return

        self._vg = system_context.substitution("DEFAULT_VG", None)
        if not self._vg:
            self._vg = None

        self._image_fs = system_context.substitution("IMAGE_FS", "ext2")
        self._image_device = _deviceify(
            system_context.substitution("IMAGE_DEVICE", ""))
        self._image_options = system_context.substitution(
            "IMAGE_OPTIONS", "rw")

        name_prefix = system_context.substitution("DISTRO_ID", "clrm")
        name_version = system_context.substitution("DISTRO_VERSION_ID",
                                                   system_context.timestamp)
        self._full_name = "{}_{}".format(name_prefix, name_version)

        initrd = args[0]

        to_clean_up = []  # type: typing.List[str]
        to_clean_up += "/boot/vmlinuz"
        to_clean_up += self._install_extra_binaries(location, system_context)
        to_clean_up += self._create_systemd_units(location, system_context)
        to_clean_up += self._install_mkinitcpio(location, system_context)
        to_clean_up += self._install_mkinitcpio_hooks(location, system_context)

        copy(
            system_context,
            os.path.join(system_context.boot_directory, "vmlinuz"),
            "/boot/vmlinuz",
            from_outside=True,
        )

        run(
            "/usr/bin/mkinitcpio",
            "-p",
            "cleanroom",
            chroot=system_context.fs_directory,
            chroot_helper=self._binary(Binaries.CHROOT_HELPER),
        )

        initrd_directory = os.path.dirname(initrd)
        os.makedirs(initrd_directory, exist_ok=True)
        move(system_context, "/boot/initramfs.img", initrd, to_outside=True)

        _cleanup_extra_files(location, system_context, *to_clean_up)
        self._remove_mkinitcpio(location, system_context)

        assert os.path.isfile(initrd)
Beispiel #15
0
    def create_export_directory(self, system_context: SystemContext) -> str:
        """Return the root directory."""
        export_volume = os.path.join(system_context.scratch_directory, "export")
        btrfs_helper = self._service("btrfs_helper")
        if btrfs_helper.is_subvolume(export_volume):
            btrfs_helper.delete_subvolume_recursive(export_volume)
        btrfs_helper.create_subvolume(export_volume)

        os_name = system_context.substitution("DISTRO_ID", "clrm")
        timestamp = system_context.substitution("TIMESTAMP", "unknown")

        image_filename = os.path.join(
            export_volume,
            "{}_{}{}".format(
                os_name,
                timestamp,
                "." + self._image_format if self._image_format != "raw" else "",
            ),
        )
        create_image(
            image_filename,
            self._image_format,
            self._extra_partitions,
            self._efi_size,
            self._swap_size,
            efi_emulator=self._efi_emulator,
            kernel_file=self._kernel_file,
            root_partition=self._root_partition,
            verity_partition=self._verity_partition,
            root_hash=self._root_hash,
            flock_command=self._binary(Binaries.FLOCK),
            sfdisk_command=self._binary(Binaries.SFDISK),
            nbd_client_command=self._binary(Binaries.NBD_CLIENT),
            sync_command=self._binary(Binaries.SYNC),
            modprobe_command=self._binary(Binaries.MODPROBE),
        )

        return export_volume
Beispiel #16
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""
        uuid = args[0]
        cmdline = system_context.substitution('KERNEL_CMDLINE', '')

        if 'rd.luks.name=' in cmdline:
            raise GenerateError('rd.luks.name already set.', location=location)

        if cmdline:
            cmdline += ' '
        cmdline += 'rd.luks.name={}={} rd.luks.options=discard'.format(
            uuid, args[1])

        system_context.set_substitution('KERNEL_CMDLINE', cmdline)
Beispiel #17
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""
        if not os.path.exists(system_context.file_name('usr/bin/mkinitcpio')):
            info('Skipping initrd generation: No mkinitcpio binary.')
            return

        if not os.path.exists(
                os.path.join(system_context.boot_directory, 'vmlinuz')):
            info('Skipping initrd generation: No vmlinuz in boot directory.')
            return

        self._vg = system_context.substitution('DEFAULT_VG', None)
        if not self._vg:
            self._vg = None

        self._image_fs = system_context.substitution('IMAGE_FS', 'ext2')
        self._image_device = \
            _deviceify(system_context.substitution('IMAGE_DEVICE', ''))
        self._image_options = system_context.substitution(
            'IMAGE_OPTIONS', 'rw')

        name_prefix = system_context.substitution('DISTRO_ID', 'clrm')
        name_version = system_context.substitution('DISTRO_VERSION_ID',
                                                   system_context.timestamp)
        self._full_name = "{}_{}".format(name_prefix, name_version)

        initrd = args[0]

        to_clean_up = []  # type: typing.List[str]
        to_clean_up += '/boot/vmlinuz'
        to_clean_up += self._install_extra_binaries(location, system_context)
        to_clean_up += self._create_systemd_units(location, system_context)
        to_clean_up += self._install_mkinitcpio(location, system_context)
        to_clean_up += self._install_mkinitcpio_hooks(location, system_context)

        copy(system_context,
             os.path.join(system_context.boot_directory, 'vmlinuz'),
             '/boot/vmlinuz',
             from_outside=True)

        run('/usr/bin/mkinitcpio',
            '-p',
            'cleanroom',
            chroot=system_context.fs_directory,
            chroot_helper=self._binary(Binaries.CHROOT_HELPER))

        initrd_directory = os.path.dirname(initrd)
        os.makedirs(initrd_directory, exist_ok=True)
        move(system_context, '/boot/initramfs.img', initrd, to_outside=True)

        _cleanup_extra_files(location, system_context, *to_clean_up)
        self._remove_mkinitcpio(location, system_context)

        assert (os.path.isfile(initrd))
Beispiel #18
0
    def __call__(
        self,
        location: Location,
        system_context: SystemContext,
        *args: typing.Any,
        **kwargs: typing.Any,
    ) -> None:
        """Execute command."""
        uuid = args[0]
        name = args[1]

        cmdline = system_context.substitution("KERNEL_CMDLINE", "")
        if "rd.luks.name=" in cmdline:
            raise GenerateError("rd.luks.name already set.", location=location)

        system_context.set_or_append_substitution(
            "KERNEL_CMDLINE",
            f"rd.luks.name={uuid}={name} rd.luks.options=discard",
        )
Beispiel #19
0
def update_cmdline(system_context: SystemContext, version: str, variant: str):
    with open(
            system_context.file_name(
                f"/usr/lib/kernel/cmdline-{version}.{variant}"), "r") as cmd:
        clr_cmdline = [
            a.strip() for a in cmd.read().split("\n") if a and a != "quiet"
        ]

    in_cmdline = [
        a for a in system_context.substitution("KERNEL_CMDLINE", "").split(" ")
        if a
    ]

    cmdline = [
        *in_cmdline,
        *clr_cmdline,
    ]
    cmdline_str = " ".join([a for a in cmdline if a])

    system_context.set_substitution("KERNEL_CMDLINE", cmdline_str)
Beispiel #20
0
    def _create_rootverity_fsimage(self, location: Location,
                                   system_context: SystemContext, *,
                                   rootfs: str) -> typing.Tuple[str, str]:
        vrty_label = system_context.substitution_expanded(
            "VRTYFS_PARTLABEL", "")
        if not vrty_label:
            raise GenerateError("VRTYFS_PARTLABEL is unset.")
        verity_file = os.path.join(system_context.cache_directory, vrty_label)

        self._execute(
            location,
            system_context,
            "_create_dmverity_fsimage",
            verity_file,
            base_image=rootfs,
        )
        root_hash = system_context.substitution("LAST_DMVERITY_ROOTHASH", "")
        assert root_hash

        return (verity_file, root_hash)
Beispiel #21
0
    def __call__(
        self,
        location: Location,
        system_context: SystemContext,
        *args: typing.Any,
        **kwargs: typing.Any,
    ) -> None:
        """Execute command."""
        # Validate:
        if system_context.substitution("CLRM_PACKAGE_TYPE", "") != "swupd":
            raise GenerateError(
                "Trying to run swupd when other package type has been initialized before."
            )

        run(
            self._binary(Binaries.SWUPD),
            "bundle-add",
            f"--path={system_context.fs_directory}",
            *args,
        )
Beispiel #22
0
    def __call__(self, location: Location, system_context: SystemContext,
                 *args: typing.Any, **kwargs: typing.Any) -> None:
        """Execute command."""

        if system_context.substitution('ROOT_DEVICE') is None:
            GenerateError('ROOT_DEVICE must be set when creating EFI kernel.',
                          location=location)

        output = args[0]
        kernel = kwargs.get('kernel', '')
        initrd_directory \
            = kwargs.get('initrd', os.path.join(system_context.boot_directory,
                                                'initrd-parts'))
        initrd_files = _get_initrd_parts(location, initrd_directory)
        cmdline_input = kwargs.get('commandline', '')
        osrelease_file = system_context.file_name('/usr/lib/os-release')
        efistub = system_context.file_name('/usr/lib/systemd/boot/efi/'
                                           'linuxx64.efi.stub')

        debug('{}: Kernel   : {}.'.format(self.name, kernel))
        debug('{}: Initrd   : {}.'.format(self.name, ', '.join(initrd_files)))
        debug('{}: cmdline  : {}.'.format(self.name, cmdline_input))
        debug('{}: osrelease: {}.'.format(self.name, osrelease_file))
        debug('{}: efistub  : {}.'.format(self.name, efistub))

        self._validate_files(kernel, *initrd_files, osrelease_file, efistub)
        with tempfile.TemporaryDirectory() as tmp:
            initrd = _create_initrd(tmp, *initrd_files)
            cmdline = _create_cmdline_file(tmp, cmdline_input)

            run(self._binary(Binaries.OBJCOPY), '--add-section',
                '.osrel={}'.format(osrelease_file), '--change-section-vma',
                '.osrel=0x20000', '--add-section',
                '.cmdline={}'.format(cmdline), '--change-section-vma',
                '.cmdline=0x30000', '--add-section',
                '.linux={}'.format(kernel), '--change-section-vma',
                '.linux=0x40000', '--add-section', '.initrd={}'.format(initrd),
                '--change-section-vma', '.initrd=0x3000000', efistub, output)

            os.remove(initrd)
            os.remove(cmdline)
Beispiel #23
0
    def __call__(
        self,
        location: Location,
        system_context: SystemContext,
        *args: typing.Any,
        **kwargs: typing.Any,
    ) -> None:
        """Execute command."""
        ## validate package type:
        if system_context.substitution("CLRM_PACKAGE_TYPE", ""):
            raise GenerateError(
                "Trying to run swupd_init on a system that already has a CLRM_PACKAGE_TYPE defined."
            )
        system_context.set_substitution("CLRM_PACKAGE_TYPE", "swupd")
        system_context.set_substitution("DISTRO_PRETTY_NAME",
                                        "Cleanroom - CLR")

        run(
            self._binary(Binaries.SWUPD),
            "autoupdate",
            f"--path={system_context.fs_directory}",
            "--disable",
            "--no-progress",
            returncode=28,
        )

        # Setup update-helper so that swupd os-install will actually work:
        os.makedirs(system_context.file_name("/usr/bin"))
        with open(system_context.file_name("/usr/bin/update-helper"),
                  "wb") as fd:
            fd.write(
                dedent("""\
                        #!/usr/bin/sh
                        exit 0
                    """).encode("utf-8"))
        os.chmod(system_context.file_name("/usr/bin/update-helper"), 0o755)

        run(
            self._binary(Binaries.SWUPD),
            "os-install",
            f"--path={system_context.fs_directory}",
            "--skip-optional",
            "--no-progress",
        )

        system_context.set_substitution("INITRD_GENERATOR", "clr")

        location.set_description("Move systemd files into /usr")
        self._add_hook(location, system_context, "_teardown",
                       "systemd_cleanup")

        location.set_description("Setup Clearlinux triggers as hooks")
        self._add_hook(
            location,
            system_context,
            "export",
            "run",
            "/usr/bin/systemd-tmpfiles",
            "--create",
            inside=True,
        )

        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_catalog_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_fontconfig_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_glib_schemas_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_graphviz_dot_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_hwdb_update_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_icon_cache_update_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_ldconfig_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_locale_archive_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_mandb_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_sysusers_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_dynamic_trust_store_trigger",
        )
        self._add_hook(
            location,
            system_context,
            "export",
            "_clr_mime_update_trigger",
        )

        with open(system_context.file_name("/usr/lib/os-release"), "r") as osr:
            for l in osr:
                l = l.strip()
                if l.startswith("BUILD_ID="):
                    build_id = l[9:]
                    verbose(f"Installed {build_id}.")
                    system_context.set_substitution("DISTRO_VERSION_ID",
                                                    build_id)
                    system_context.set_substitution("DISTRO_VERSION", build_id)

        system_context.set_substitution("DISTRO_ID_LIKE", "clearlinux")
        system_context.set_substitution(
            "ROOTFS_PARTLABEL", "root_${TIMESTAMP}-${DISTRO_VERSION_ID}")
        system_context.set_substitution(
            "VRTYFS_PARTLABEL", "vrty_${TIMESTAMP}-${DISTRO_VERSION_ID}")
        system_context.set_substitution(
            "KERNEL_FILENAME",
            "${PRETTY_SYSTEM_NAME}_${TIMESTAMP}-${DISTRO_VERSION_ID}.efi",
        )
        system_context.set_substitution(
            "CLRM_IMAGE_FILENAME",
            "${PRETTY_SYSTEM_NAME}_${TIMESTAMP}-${DISTRO_VERSION_ID}",
        )
        self._execute(location.next_line(), system_context,
                      "create_os_release")
Beispiel #24
0
    def __call__(
        self,
        location: Location,
        system_context: SystemContext,
        *args: typing.Any,
        **kwargs: typing.Any,
    ) -> None:
        """Execute command."""
        name = args[0]
        if not name.endswith(".conf"):
            name += ".conf"

        device_id = kwargs.get("device", "disk0")
        assert not " " in device_id

        contents = "[Partition]\n"

        type = kwargs.get("type", "")
        contents += f"Type={type}\n"
        label = kwargs.get("label", "")
        if label:
            contents += f"Label={label}\n"
        uuid = kwargs.get("uuid", "")
        if uuid:
            contents += f"UUID={uuid}\n"
        priority = kwargs.get("priority", 0)
        if priority != 0:
            contents += f"Priority={priority}\n"
        weight = kwargs.get("weight", 1000)
        if weight != 1000:
            contents += f"Weight={weight}\n"
        padding_weight = kwargs.get("paddingWeight", 0)
        if padding_weight != 0:
            contents += f"PaddingWeight={padding_weight}\n"
        minSize = kwargs.get("minSize", "")
        if minSize:
            contents += f"SizeMinBytes={minSize}\n"
        maxSize = kwargs.get("maxSize", "")
        if maxSize:
            contents += f"SizeMaxBytes={maxSize}\n"
        minPadding = kwargs.get("minPadding", "")
        if minPadding:
            contents += f"PaddingMinBytes={minPadding}\n"
        maxPadding = kwargs.get("maxPadding", "")
        if maxPadding:
            contents += f"PaddingMaxBytes={maxPadding}\n"

        rel_path = os.path.join(
            system_context.substitution("DISTRO_ID"), "repart.d", device_id,
        )
        path = os.path.join(system_context.boot_directory, "extra", rel_path,)
        filename = os.path.join(path, name)
        trace(f"Creating repart.d file {filename} (relative: {rel_path}).")

        os.makedirs(path, mode=0o750, exist_ok=True)
        with open(filename, "wb") as f:
            f.write(contents.encode("utf-8"))
        os.chown(filename, 0, 0, follow_symlinks=False)
        os.chmod(filename, 0o644)

        # set up substitutions:
        device_ids = system_context.substitution_expanded(
            "DEPLOY_DEVICE_IDS", ""
        ).split()
        if not device_id in device_ids:
            device_ids.append(device_id)
        system_context.set_substitution("DEPLOY_DEVICE_IDS", " ".join(device_ids))

        key = f"DEPLOY_{device_id}_REPART_D"
        repart_path = system_context.substitution_expanded(key, "")
        if not repart_path:
            trace(f"Setting {key} to {rel_path}.")
            system_context.set_substitution(key, rel_path)
        else:
            assert rel_path == repart_path

        if type == "esp" and uuid:
            system_context.set_substitution("EFI_PARTITION_PARTUUID", uuid)
Beispiel #25
0
def _root_part_label(system_context: SystemContext) -> str:
    label = system_context.substitution('ROOTFS_PARTLABEL', '')
    return label if label else '{}_{}'.format(
        system_context.substitution('DISTRO_ID', 'clrm'),
        system_context.substitution('DISTRO_VERSION_ID',
                                    system_context.timestamp))