kiwi-ng system boxbuild

Builds an image in a box: a virtual machine started with QEMU/KVM, or a container, which contains KIWI NG and all tools to build images. Inside of the box, kiwi-ng system build runs with the arguments after the kiwi subcommand. Only the description and the target directory are shared with the box. This allows to build images independent of the build host, as a normal user, and for other distributions than the one of the host. The boxes are provided in the Open Build Service and are downloaded and updated by the command. For an introduction, see Building in a Self-Contained Environment.

Note

Migration: KIWI NG no longer looks up a box configuration file or a keyring in $PWD, $HOME/.config/kiwi or /etc, neither with the former names kiwi_boxed_plugin.yml and kiwi_boxed_plugin.gpg nor with the current names kiwi_boxconfig.yml and kiwi_boxconfig.gpg. It also no longer reads the KIWI_BOXED_PLUGIN_CFG and KIWI_BOXED_PLUGIN_KEYRING environment variables. Files found there are ignored without a warning, and the box configuration and keyring that come with KIWI NG are used instead. Anyone using a custom box configuration file must now pass it with --box-config, and put the keyring next to it with the matching name, for example my_boxes.gpg next to my_boxes.yml, or set signing_key on each box.

Usage

kiwi-ng [global options] system boxbuild \
    --box <name> \
    [options] \
    kiwi <build options>
kiwi-ng system boxbuild --list-boxes

<build options> are the options of kiwi-ng system build, with --description and --target-dir required. Of the global options, --debug, --type, --profile and --kiwi-file are passed on to the build in the box. -- is accepted instead of kiwi, as in the former kiwi-boxed-plugin.

Options

Either --box or --list-boxes must be given, otherwise the command does nothing.

Option

Description

--box <name>

The box to build in.

--list-boxes

List the available boxes.

--box-config <file>

The box configuration file. Its keyring is the file of the same name with the gpg suffix, see Box Verification. Default: the box configuration and keyring that come with KIWI NG.

--container

Build in a container instead of a virtual machine. The virtual machine options have no effect.

--x86_64, --aarch64

The architecture of the box and the image. Default: the host architecture. --x86_64 takes precedence.

--kiwi-version <version>

Install this KIWI NG version from PyPI in the box. Mutually exclusive with --kiwi-git.

--kiwi-git <git_link>

Build and install KIWI NG from the git link in the box, for example https://github.com/OSInside/kiwi/tree/BRANCH_NAME, by default from the default branch.

--shared-path <path>

An additional host path to share with the box, at the same path.

--no-update-check

Do not check for a new version of the box.

--no-snapshot

Write into the disk of the box, which keeps for example the package cache for later builds. Changes of --kiwi-version or --kiwi-git stay in the box. A box update discards them.

Virtual Machine

Option

Description

--box-memory <memory>

The main memory, passed to the QEMU option -m, for example 8G. Default: the memory of the box, 8096M for the KIWI NG boxes.

--box-smp-cpus <number>

The number of CPUs. Default: 4.

--box-console <ttyname>

The kernel console. Default: the console of the box, hvc0 for the KIWI NG boxes.

--box-debug

Keep the virtual machine running after the build and show the boot messages of the virtual machine and a trace of the build script in the box. Without this option the console shows the output of the build and kernel error messages only.

--machine <qemu_machine>

The QEMU machine type, required for cross architecture builds and for architectures without a default, such as Arm. virt is a good choice.

--cpu <qemu_cpu>

The QEMU CPU type. Default: the host CPU, which only works for the host architecture.

--no-accel

Run without KVM acceleration.

--9p-sharing, --virtiofs-sharing, --sshfs-sharing

The backend to share directories with the box. Default: 9p. virtiofs takes precedence over sshfs.

--ssh-key <name>

The ssh key to authorize. Default: id_rsa.

--ssh-port <port>

The host port for the ssh port of the box. Default: 10022.

Box Verification

The files of a virtual machine box are verified with signed checksum files. For each architecture of a box, the box configuration file lists the box files in boxfiles and the checksum files in boxchecksums:

boxfiles:
  - TumbleWeed-Box.x86_64-Kernel.tar.xz
  - TumbleWeed-Box.x86_64-System.qcow2
boxchecksums:
  - TumbleWeed-Box.x86_64-Kernel.tar.xz.sha256
  - TumbleWeed-Box.x86_64-System.qcow2.sha256

A checksum file contains the sha256 checksum and the name of one or more box files, as written by sha256sum. Its signature is the detached signature in the file with the name of the checksum file and the asc extension appended, for example TumbleWeed-Box.x86_64-System.qcow2.sha256.asc. The checksum files and their signatures are downloaded along with the box files. KIWI NG verifies the signatures with sqv, the signature verification tool of the Sequoia OpenPGP project, and the keys of the box keyring, or the signing_key of the box, and each box file with the checksum of its name. If a signature or a checksum does not match, the command fails.

Downloaded box files are verified before they replace the box files in $KIWI_BOXED_CACHE_DIR. Box files from the cache are verified unless --no-update-check is set. With --no-snapshot, the system image of the box changes when it is used and is only verified when it is downloaded.

The Open Build Service creates the checksum files and signs them with the key of the project. The keyring that comes with KIWI NG contains the key of the Virtualization:Appliances project, which builds the boxes of the default box configuration. The keyring of a box configuration file given by --box-config is the file of the same name with the gpg suffix, for example my_boxes.gpg for my_boxes.yml. For a box built in another way, sign its checksum files and provide the keyring with the public key, for example:

$ sha256sum My-Box.x86_64-Kernel.tar.xz > My-Box.x86_64-Kernel.tar.xz.sha256
$ gpg --armor --detach-sign My-Box.x86_64-Kernel.tar.xz.sha256
$ gpg --export KEY_ID > my_boxes.gpg

The keyring applies to all boxes of the box configuration file. To verify the checksum files of a box with its own key instead, set signing_key next to boxchecksums for the architecture of the box:

boxchecksums:
  - My-Box.x86_64-Kernel.tar.xz.sha256
  - My-Box.x86_64-System.qcow2.sha256
signing_key: /etc/kiwi/my_box.gpg

The signing_key file holds the public key, for example as exported with gpg --export KEY_ID, and replaces the keyring for this box and architecture. A relative path is relative to the current directory and a leading is expanded to the home directory. If the keyring or the signing_key file does not exist, the command fails.

Building Your Own Boxes

The boxes are KIWI NG images themselves. The descriptions of the boxes of the default box configuration are in the boxes directory of the KIWI NG source repository, one per distribution, for example boxes/tumbleweed, boxes/leap, boxes/fedora and boxes/ubuntu. A box of your own is best derived from one of them.

Profiles

A box description provides the parts of a box as profiles:

Kernel

A pxe type with initrd_system="dracut". It creates a .tar.xz archive with the kernel and the initrd. QEMU boots the kernel and the initrd from it directly. The archive must contain a file matching *.kernel, and a file matching *.initrd if the box sets use_initrd.

System

An oem type with format="qcow2" and <oem-resize> set to false. It creates the disk of the virtual machine, which is booted with the kernel of the Kernel profile. The firmware and bootloader settings determine the partition layout, and the root partition must match the root= option of the cmdline of the box, for example /dev/vda3 for x86_64 with UEFI firmware, /dev/vda2 for aarch64 and /dev/vda1 for s390x without a boot partition.

Container

A docker type with /usr/lib/systemd/systemd as entry point, used by --container. This profile is optional.

What a Box Provides

A box starts the build itself when it boots, takes the build options from the kernel command line and returns the result to the host. The reference descriptions implement this with the following parts, which a box of your own needs as well:

images.sh

Installs the build runner /usr/local/bin/run_kiwi, which is the part of the file after its first lines. The build runner reads the options of kiwi-ng system build from the kernel command line, or from /container.cmdline in a container. It waits for the network interface lan0 and mounts the shared directories by their tags kiwidescription, kiwibundle and custompath, with the 9p, virtiofs or sshfs sharing backend. It runs the build, bundles the result into /bundle, writes result.log and result.code there and powers off the virtual machine unless --box-debug is set. It also installs KIWI NG for --kiwi-version and --kiwi-git. images.sh is shared by all boxes and is located in the boxes directory.

root/usr/lib/systemd/system/kiwi.service

The systemd unit which runs the build runner once the network is online. config.sh enables it, as well as systemd-networkd, systemd-resolved and sshd for the Kernel and System profiles.

root/etc/udev/rules.d/70-persistent-net.rules and root/etc/systemd/network

Name the network interface lan0 and configure it by DHCP.

root/etc/systemd/system/*getty*.service.d/override.conf

Provide a root shell on the console if the kernel command line contains kiwi-no-halt, which is set by --box-debug.

box-key-unsafe.tgz

An archive with the ssh key pair of the root user of the box, used to mount the shared directories from the host with the sshfs sharing backend. Its private key is published, therefore the key must not be trusted for anything else. The archive is shared by all boxes and is located in the boxes directory.

The packages include KIWI NG and the tools it needs for the builds, such as python3-kiwi and kiwi-systemdeps on openSUSE, as well as a kernel, openssh, sshfs and netcat-openbsd for the sshfs sharing backend, python3-pip for --kiwi-version and git and the Python build tools for --kiwi-git. See the package list of the reference description of your distribution.

Building the Box Files

The reference descriptions are built in the Open Build Service. They use the repositories of the build service project by obsrepositories:/, and images.sh and box-key-unsafe.tgz are added to each package. For a build with KIWI NG, copy a description along with these files, change the name of the image in appliance.kiwi, and set the repositories on the command line. Each profile is built into its own target directory, as the results of both profiles have the same base name:

$ git clone https://github.com/OSInside/kiwi
$ cp -a kiwi/boxes/tumbleweed my-box
$ cp kiwi/boxes/images.sh kiwi/boxes/box-key-unsafe.tgz my-box/
$ sed -i 's/name="TumbleWeed-Box"/name="My-Box"/' my-box/appliance.kiwi
$ for profile in Kernel System; do
      sudo kiwi-ng --profile ${profile} system build \
          --description my-box --target-dir /tmp/my-box-${profile} \
          --set-repo https://download.opensuse.org/tumbleweed/repo/oss
  done

The Kernel profile creates My-Box.x86_64-version.tar.xz, the System profile My-Box.x86_64-version.qcow2, where {version} is the version of appliance.kiwi. Use --add-repo to add more repositories, for example one providing a newer KIWI NG.

Publishing the Box Files

The box configuration refers to the box files by name. With names that do not contain the version, the box configuration stays the same for new versions of the box. Copy the box files with such names, create and sign their checksum files as described in Box Verification, and export the public key to the keyring of your box configuration file:

$ mkdir publish
$ cp /tmp/my-box-Kernel/My-Box.x86_64-*.tar.xz publish/My-Box.x86_64-Kernel.tar.xz
$ cp /tmp/my-box-System/My-Box.x86_64-*.qcow2 publish/My-Box.x86_64-System.qcow2
$ cp /tmp/my-box-System/My-Box.x86_64-*.packages publish/My-Box.x86_64-System.packages
$ cd publish
$ for file in My-Box.x86_64-Kernel.tar.xz My-Box.x86_64-System.qcow2; do
      sha256sum ${file} > ${file}.sha256
      gpg --armor --detach-sign ${file}.sha256
  done
$ gpg --export KEY_ID > ../my_boxes.gpg

Upload the files of publish to a web server. The box files are downloaded by HTTP or HTTPS, a local directory is not supported as source of a box. Then add the box to your box configuration file my_boxes.yml, next to its keyring my_boxes.gpg:

box:
  -
    name: mybox
    mem_mb: 8096M
    processors: 4
    console: hvc0
    arch:
      -
        name: x86_64
        cmdline:
          - root=/dev/vda3
          - rd.plymouth=0
        source: https://example.com/boxes
        packages_file: My-Box.x86_64-System.packages
        boxfiles:
          - My-Box.x86_64-Kernel.tar.xz
          - My-Box.x86_64-System.qcow2
        boxchecksums:
          - My-Box.x86_64-Kernel.tar.xz.sha256
          - My-Box.x86_64-System.qcow2.sha256
        container: ''
        use_initrd: true

source is the location of the box files, boxfiles lists the .tar.xz archive of the Kernel profile and the .qcow2 disk of the System profile. packages_file is downloaded on each update check and compared to the one of the former download. The box files are only downloaded again if it has changed, therefore publish the .packages file of each new version of the box along with it. container is the container image of the Container profile, pushed to a container registry, or empty for a box without one. The box is then used with --box-config:

$ kiwi-ng system boxbuild --box-config my_boxes.yml --list-boxes
$ kiwi-ng system boxbuild --box-config my_boxes.yml --box mybox kiwi \
    --description kiwi-descriptions/suse/x86_64/suse-leap-16.0 \
    --target-dir /tmp/myimage

Environment

Variable

Description

KIWI_BOXED_CACHE_DIR

The directory for the boxes. Default: $HOME/.kiwi_boxes.

Example

$ git clone https://github.com/OSInside/kiwi-descriptions
$ kiwi-ng --profile Virtual system boxbuild --box leap kiwi \
    --description kiwi-descriptions/suse/x86_64/suse-leap-16.0 \
    --target-dir /tmp/myimage