Building Disk Images with systemd-repart

By default, KIWI NG creates a disk image on a loop device: it partitions the device, creates the filesystems on the partitions, mounts them and copies the root tree into them. This requires loop devices and mount privileges on the build host, and the kernel writes data like timestamps into the filesystems that changes with every build.

systemd-repart creates the partition table and the filesystems including their content directly in the image file. The mkfs tools are called in a mode that populates the new filesystem from a directory, for example mkfs.ext4 -d. No loop devices and no mounts are used.

Enabling systemd-repart

Set the disk_builder attribute of the oem type:

<type image="oem" filesystem="ext4" firmware="uefi"
      bootloader="systemd_boot" disk_builder="systemd-repart"/>

The build host needs systemd-repart with support for the --offline option, which is available since systemd version 254, and the mkfs tools of the used filesystems (mkfs.vfat for the EFI partition). The image needs the systemd-boot EFI binaries, which are part of the systemd-boot package on most distributions, or the grub2 EFI binaries if bootloader="grub2" is used, see EFI Boot with grub2.

Supported Image Descriptions

The systemd-repart disk builder supports a subset of the oem type features. KIWI NG stops with an error that lists all unsupported features of an image description before it starts to build. Supported are:

  • EFI firmware (firmware="efi" or firmware="uefi") with a GPT partition table. Legacy BIOS boot is not supported, an EFI CSM partition is not created.

  • The systemd_boot bootloader, with loader entries or with an UKI if <dracut uefi="true"/> is set.

  • The grub2 bootloader for EFI only, with a menu entry for each kernel flavor or with an UKI if <dracut uefi="true"/> is set. Legacy BIOS boot is not supported, therefore eficsm="false" must be set on x86, see EFI Boot with grub2.

  • The dracut initrd system.

  • An ext2, ext3, ext4, xfs or btrfs root filesystem.

  • Btrfs volumes, defined in the <systemdisk> section with filesystem="btrfs", including the root subvolume, the snapper snapshot layout and the read-only root snapshot, see Btrfs Volumes.

  • Btrfs compression, requested with btrfs_compression, see Btrfs Compression.

  • Filesystem create options for the root filesystem, set by fscreateoptions, see Filesystem Create Options.

  • A swap partition, requested with <oem-swap> and <oem-swapsize> in the <oemconfig> section.

  • The disk.sh, pre_disk_sync.sh and editbootconfig scripts.

  • Disk formats like qcow2, which are converted from the raw disk image.

Not supported are LVM volume management, Btrfs quota groups (btrfs_quota_groups and the quota volume attribute), volumes with copy_on_write="false", software RAID, LUKS encryption, dm-integrity, dm-verity, overlayroot, custom and spare partitions, an extra boot partition, partition clones, an unpartitioned area and the editbootinstall script.

How the Disk Is Created

The disk image contains the following partitions, in this order:

  1. The EFI system partition, label EFI, mounted at /boot/efi

  2. The swap partition, label SWAP, if requested

  3. The root partition, label as set by rootfs_label or ROOT. It uses the remaining space of the disk. With apply_dps_codes="true" the partition gets the root partition type of the Discoverable Partitions Specification, otherwise the generic Linux type.

The GPT partition names are the same as the labels.

The steps that run in the mounted disk image with the default disk builder run in a staging directory instead:

  1. KIWI NG writes the metadata and the initrd into the root tree as with the default disk builder.

  2. KIWI NG copies the root tree into a staging directory below the target directory. The staging directory needs as much space as the root tree.

  3. disk.sh runs chrooted in the staging directory, then the SELinux labels are set. Unlike with the default disk builder, the staging directory is not bind mounted onto itself.

  4. KIWI NG installs systemd-boot, the loader configuration and the loader entries into boot/efi of the staging directory. bootctl is not used, because it requires a mounted EFI system partition. KIWI NG copies the systemd-boot EFI binary to EFI/systemd/ and EFI/BOOT/ and writes the entry token, like bootctl install does. With bootloader="grub2" KIWI NG sets up grub2 instead, see EFI Boot with grub2.

  5. systemd-repart creates the disk image: the EFI system partition from boot/efi of the staging directory, the root partition from the rest of the staging directory.

The root tree itself is not changed by these steps. The initrd is still created with dracut in a chroot of the root tree, which bind mounts /dev and /proc as for every image type.

Filesystem Create Options

systemd-repart has no partition setting for custom mkfs options. Instead it appends the content of the SYSTEMD_REPART_MKFS_OPTIONS_ environment variable, with <FS> being the filesystem name in upper case, for example SYSTEMD_REPART_MKFS_OPTIONS_EXT4, to the mkfs call of each filesystem of that type. KIWI NG passes the options set by fscreateoptions for the root filesystem through this variable. Options already set in the variable are kept and come first:

<type image="oem" filesystem="ext4" firmware="uefi"
      fscreateoptions="-O ^has_journal -E stride=8"
      disk_builder="systemd-repart">
    <bootloader name="systemd_boot"/>
</type>

The options are passed without further validation and follow the options systemd-repart sets itself. An option given in fscreateoptions therefore overrides the same option of systemd-repart. Options that control the label, the UUID, the sector size or the source directory the filesystem is populated from must not be set, KIWI NG and systemd-repart rely on them.

mke2fs only uses the last -E option given. systemd-repart passes its own extended options, nodiscard,lazy_itable_init=1, with -E, and so does KIWI NG for a reproducible build, see Reproducible Builds. Therefore KIWI NG takes the values of the -E options in fscreateoptions for an ext2, ext3 or ext4 root filesystem and passes them together with these extended options in one -E option. In the example above mke2fs is called with -O ^has_journal -E nodiscard,lazy_itable_init=1,stride=8.

Filesystem UUIDs

systemd-repart derives the filesystem UUIDs from the partition UUIDs when it creates the filesystems. KIWI NG writes /etc/fstab and the root= kernel parameter before that, thus it references the partitions by their partition UUID, PARTUUID=..., instead of the filesystem UUID. With devicepersistency="by-label" the filesystem labels are used.

The grub2 setup needs the filesystem UUIDs in advance. systemd-repart calculates the UUID of a filesystem as HMAC-SHA256 of the string file-system-uuid, keyed by the partition UUID. The first half of the result is used as version 4 UUID, for vfat the first 32 bits are the volume ID. KIWI NG calculates the filesystem UUIDs the same way. After systemd-repart has created the disk, KIWI NG reads the filesystem UUIDs with blkid. If they don’t match the calculated ones, KIWI NG stops with an error instead of producing an image that doesn’t boot.

Btrfs Volumes

With the btrfs volume manager, systemd-repart creates the volumes as subvolumes of the root filesystem while it creates the filesystem. It uses the Subvolumes= setting, which systemd-repart implements by the --subvol option of mkfs.btrfs:

<type image="oem" filesystem="btrfs" firmware="uefi"
      btrfs_root_is_subvolume="true"
      btrfs_root_is_snapper_snapshot="true"
      disk_builder="systemd-repart">
    <bootloader name="grub2"/>
    <systemdisk>
        <volume name="home"/>
        <volume name="var"/>
    </systemdisk>
</type>

The build host needs systemd-repart with support for Subvolumes=, which is available since systemd version 257, and a mkfs.btrfs that supports the --subvol option with subvolume types like default-ro:, as for example btrfs-progs version 6.14 does.

systemd-repart passes the flags of a subvolume as suffix to mkfs.btrfs, for example --subvol path:ro,default, while mkfs.btrfs expects the subvolume type as prefix, for example --subvol default-ro:path. Therefore KIWI NG does not use the :ro flag of Subvolumes= and the DefaultSubvolume= setting. Instead it passes the read-only and the default subvolume to mkfs.btrfs through the SYSTEMD_REPART_MKFS_OPTIONS_BTRFS environment variable. Options already set in this variable are kept.

The subvolume layout is the same as the one the default disk builder creates:

  • With btrfs_root_is_subvolume="true" the root tree is stored in the root subvolume, /@ by default, or the name set by <volume name="@root=name"/>. Otherwise the root tree is stored in the toplevel of the filesystem.

  • With btrfs_root_is_snapper_snapshot="true" the root tree is stored in the first snapper snapshot /@/.snapshots/1/snapshot, and /@/.snapshots is mounted at /.snapshots. With btrfs_root_is_readonly_snapshot="true" the snapshot is created read-only and the root filesystem is mounted read-only.

  • Each volume is a subvolume below its parent, which is the root subvolume if the parent attribute is not set. The data of a volume is copied into its subvolume. If the subvolume is not stored below the location of the root tree, for example with a snapshot or with parent="/", its data is excluded from the copy of the root tree and only the empty mount point directory is kept there.

  • The root subvolume is the default subvolume unless btrfs_set_default_volume="false" is set. In that case /etc/fstab and the kernel command line mount the root subvolume by the subvol= and rootflags=subvol= options. A snapshot is always the default subvolume.

  • /etc/fstab mounts each volume by the subvol= option, using the same device reference as the root filesystem. The filesystem_check volume attribute enables the filesystem check of the volume.

snapper requires a mounted btrfs filesystem and can’t be used to setup the first snapshot. Instead KIWI NG writes the /.snapshots/1/info.xml file of the first snapshot, with the date taken from SOURCE_DATE_EPOCH if set, and creates the /etc/snapper/configs/root configuration from the snapper configuration template if snapper is installed in the image.

grub2 looks up its files from the toplevel of a btrfs filesystem, unless the btrfs_relative_path lookup is used, which is the case with btrfs_set_default_volume="true" or a snapper snapshot. Without it KIWI NG writes the paths of the grub2 configuration, the kernel and the initrd as they are located in the filesystem, for example /@/boot/grub2/grub.cfg.

Btrfs Compression

With btrfs_compression, the data systemd-repart copies into the btrfs root filesystem is compressed. KIWI NG writes the algorithm and the optional level, specified as algorithm[:level], to the Compression= and CompressionLevel= settings of the root partition, which systemd-repart implements by the --compress option of mkfs.btrfs:

<type image="oem" filesystem="btrfs" firmware="uefi"
      btrfs_compression="zstd:3"
      disk_builder="systemd-repart">
    <bootloader name="systemd_boot"/>
</type>

The build host needs systemd-repart with support for Compression= and CompressionLevel=, which is available since systemd version 257, and a mkfs.btrfs that supports the --compress option. Older versions of systemd-repart ignore these settings, therefore KIWI NG refuses to create the disk with them.

The compression settings only apply while the filesystem is created. As with the default disk builder, /etc/fstab mounts the root filesystem and each volume with the compress= option, for example compress=zstd:3, such that data written by the system booted from the image is compressed as well.

EFI Boot with grub2

With bootloader="grub2" the systemd-repart disk builder sets up grub2 for EFI only:

<type image="oem" filesystem="ext4" firmware="uefi" eficsm="false"
      disk_builder="systemd-repart">
    <bootloader name="grub2"/>
</type>

The kernel, the initrd and the grub2 configuration stay on the root filesystem, as with the default disk builder. The EFI system partition contains the EFI binaries and an early boot configuration. The setup in the staging directory works as follows:

  1. With firmware="efi" KIWI NG uses the unsigned grub2 EFI image of the distribution if there is one. Otherwise KIWI NG creates it with grub2-mkimage, chrooted in the staging directory. With firmware="uefi" KIWI NG copies the shim, the signed grub2 EFI image and the MOK manager of the distribution into EFI/BOOT/. shim-install and grub2-install are not used, because they require access to the disk device.

  2. The early boot configuration in the EFI system partition searches the root filesystem by its UUID, see above, and loads /boot/grub2/grub.cfg from it. With <dracut uefi="true"/> it chainloads the UKI from EFI/Linux/ of the EFI system partition instead.

  3. grub2-mkconfig can’t be used, because it looks up the root device from the mounted root filesystem. KIWI NG writes /boot/grub2/grub.cfg from a template. It contains a menu entry for the kernel of each kernel flavor, and a failsafe entry if requested. The timeout, the console, the graphics mode and the theme are set as configured in the <bootloader> section. The first kernel flavor is the default entry.

  4. KIWI NG writes /etc/default/grub as with the default disk builder. A grub2-mkconfig call on the booted system, for example at a kernel update, creates the grub2 configuration from it.

With SOURCE_DATE_EPOCH set, the partition UUIDs and the seed for all other identifiers systemd-repart creates are derived from SOURCE_DATE_EPOCH, see Reproducible Builds. The modification times of the files in the staging directory are clamped to SOURCE_DATE_EPOCH. systemd-repart passes SOURCE_DATE_EPOCH to the mkfs tools, but not all of them use it for every time based or random value, therefore KIWI NG sets the following additional options and environment variables:

  • mkfs.vfat before version 4.3 writes the current time into the volume label. KIWI NG passes --invariant together with the volume ID systemd-repart assigns through the SYSTEMD_REPART_MKFS_OPTIONS_VFAT environment variable.

  • mke2fs creates a random directory hash seed. KIWI NG passes the filesystem UUID as hash_seed through the SYSTEMD_REPART_MKFS_OPTIONS_EXT2, _EXT3 or _EXT4 environment variable.

  • mkfs.btrfs creates random device, chunk tree and subvolume UUIDs and mkfs.xfs creates random inode generation numbers. KIWI NG sets the DETERMINISTIC_SEED=1 environment variable, which makes both tools derive these values from the filesystem UUID.

Not all versions of the mkfs tools use SOURCE_DATE_EPOCH and DETERMINISTIC_SEED. Older versions write the current time into the superblock and into the inodes, which includes the inode change time of every file copied into the filesystem, and create random values. This also applies to the directories systemd-repart creates or modifies itself, like the filesystem root directory or the parent directories of CopyFiles= and MakeDirectories= targets that do not exist in the source. They carry the time of the build, and only the mkfs tool clamps it to SOURCE_DATE_EPOCH. The root filesystem is therefore only bit identical if the build host provides at least the following versions:

Filesystem

Package

Minimum version

ext2, ext3, ext4

e2fsprogs

1.47.1

btrfs

btrfs-progs

7.3

XFS

xfsprogs

6.18

KIWI NG issues a warning if an older version is found. The disk image is still created, but it is not bit identical to an image created from the same root tree in another run.