### mkosi Build Script Example Source: https://github.com/systemd/mkosi/blob/main/blog/content/a-reintroduction-to-mkosi.md This script is executed during the mkosi build process. Files placed in $DESTDIR are installed into the image. Build dependencies can be specified using BuildPackages=. ```sh #!/bin/sh meson setup "$BUILDDIR" "$SRCDIR" ninja -C "$BUILDDIR" if ((WITH_TESTS)); then meson test -C "$BUILDDIR" fi meson install -C "$BUILDDIR" ``` -------------------------------- ### Example of BuildSources Matcher Source: https://github.com/systemd/mkosi/blob/main/mkosi.md This example demonstrates how the BuildSources matcher in a drop-in configuration can include settings based on a specific build source target path defined in the main mkosi.conf. ```ini [Build] BuildSources=../abc/qed:kernel ``` ```ini [Match] BuildSources=kernel ``` -------------------------------- ### Example mkosi.conf BuildSources Source: https://github.com/systemd/mkosi/blob/main/mkosi/resources/man/mkosi.1.md Demonstrates how to specify a build source target path in mkosi.conf. ```ini [Build] BuildSources=../abc/qed:kernel ``` -------------------------------- ### Example Match BuildSources Source: https://github.com/systemd/mkosi/blob/main/mkosi/resources/man/mkosi.1.md Shows how a drop-in configuration can match a specific build source target. ```ini [Match] BuildSources=kernel ``` -------------------------------- ### Install and Copy RPMs with mkosi Source: https://github.com/systemd/mkosi/blob/main/docs/building-rpms-from-source.md Use this script to install mkosi, then copy generated RPMs to the output directory. Ensure the script is executable and has the necessary permissions. ```shell #!/bin/sh set -e mkosi-install mkosi cp "$PACKAGEDIR"/*mkosi*.rpm "$OUTPUTDIR" ``` -------------------------------- ### Run Installation Tests Source: https://github.com/systemd/mkosi/blob/main/CLAUDE.md Execute installation tests, which involve venv, pip, and zipapp installations, using mkosi box and pytest. These tests are skipped by default due to network installations. ```bash bin/mkosi box -- pytest -m install ``` -------------------------------- ### Install mkosi Companion Tools Source: https://github.com/systemd/mkosi/blob/main/README.md Symlink companion tools like mkosi-addon, mkosi-initrd, and mkosi-sandbox to your PATH for easy access. ```shell ln -s $PWD/mkosi/bin/mkosi-addon ~/.local/bin/mkosi-addon ln -s $PWD/mkosi/bin/mkosi-initrd ~/.local/bin/mkosi-initrd ln -s $PWD/mkosi/bin/mkosi-sandbox ~/.local/bin/mkosi-sandbox ``` -------------------------------- ### Build and boot Fedora image with qemu Source: https://github.com/systemd/mkosi/blob/main/mkosi.md Builds a Fedora image with necessary boot components and then boots it using qemu for a virtual machine environment. The example shows automatic login for the root user. ```console $ mkosi -d fedora -p systemd-udev,systemd-boot,kernel-core build $ mkosi -d fedora vm ... fedora login: root (automatic login) [root@fedora ~]# ``` -------------------------------- ### Prepare Script for RPM Build Dependencies Source: https://github.com/systemd/mkosi/blob/main/docs/building-rpms-from-source.md This script installs non-dynamic and dynamic build dependencies for RPMs. It uses `rpmspec` to identify initial dependencies and `rpmbuild -bd` in a loop to resolve dynamic dependencies, installing them with `mkosi-install`. ```shell #!/bin/sh set -e if [ "$1" = "final" ]; then exit 0 fi mkosi-chroot \ env --chdir=mkosi \ rpmspec \ --query \ --buildrequires \ --define "_topdir /var/tmp" \ --define "_sourcedir $PWD/mkosi/rpm" \ rpm/mkosi.spec | \ sort --unique | \ tee /tmp/buildrequires | \ xargs --delimiter '\n' mkosi-install until mkosi-chroot \ env --chdir=mkosi \ rpmbuild \ -bd \ --noprep \ --build-in-place \ --define "_topdir /var/tmp" \ --define "_sourcedir $PWD/mkosi/rpm" \ --define "_build_name_fmt %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm" \ rpm/mkosi.spec do EXIT_STATUS=$? if [ $EXIT_STATUS -ne 11 ]; then exit $EXIT_STATUS fi mkosi-chroot \ rpm \ --query \ --package \ --requires \ /var/tmp/SRPMS/mkosi-*.buildreqs.nosrc.rpm | \ grep --invert-match '^rpmlib(' | \ sort --unique >/tmp/dynamic-buildrequires sort /tmp/buildrequires /tmp/dynamic-buildrequires | \ uniq --unique | \ tee --append /tmp/buildrequires | \ xargs --delimiter '\n' mkosi-install done ``` -------------------------------- ### Setup integration tests for mkosi Source: https://github.com/systemd/mkosi/blob/main/README.md Prepare the environment for integration tests by building a tools tree and an image. This script sets up local configurations for a given image and tools tree distribution. ```sh tools/integration-test-setup.sh $DISTRIBUTION $TOOLS_TREE_DISTRIBUTION ``` -------------------------------- ### Configure BuildSources with Source:Target Pairs Source: https://github.com/systemd/mkosi/blob/main/mkosi/resources/man/mkosi.news.7.md Example of how to use BuildSources to specify source directories and their mount points relative to the top-level source directory when running scripts. ```mkosi BuildSources=../my-project:my-project ``` -------------------------------- ### Install mkosi using venv and pip Source: https://github.com/systemd/mkosi/blob/main/README.md Manually create a Python virtual environment and install mkosi using pip. This method is equivalent to pipx installation but requires manual environment management. ```shell python3 -m venv mkosivenv mkosivenv/bin/pip install git+https://github.com/systemd/mkosi.git mkosivenv/bin/mkosi --version ``` -------------------------------- ### Install mkosi using pipx Source: https://github.com/systemd/mkosi/blob/main/README.md Install mkosi directly from its git repository using pipx for a managed Python environment. ```shell pipx install git+https://github.com/systemd/mkosi.git mkosi --version ``` -------------------------------- ### Create GPT image with systemd and boot Source: https://github.com/systemd/mkosi/blob/main/mkosi.md Creates a raw GPT image with systemd and prepares it for booting. ```console # mkosi -p systemd -i boot ``` -------------------------------- ### Start Bash in a Network Namespace Sandbox Source: https://github.com/systemd/mkosi/blob/main/mkosi/resources/man/mkosi-sandbox.1.md Starts a bash shell in the current working directory within its own network namespace. It binds the current directory and uses the same directory for the sandbox. ```sh mkosi-sandbox --bind / / --same-dir --unshare-net ``` -------------------------------- ### Install Distribution Packages with mkosi Source: https://github.com/systemd/mkosi/blob/main/mkosi.md Use this option to install specified distribution packages into the image. It supports various package specifications including names, versions, architectures, globs, virtual provides, and local file paths. ```ini Packages=meson libfdisk-devel.i686 git-* /usr/bin/ld @development-tools python3dist(mypy) ``` -------------------------------- ### Boot Fedora image with graphical console Source: https://github.com/systemd/mkosi/blob/main/mkosi.md Boots a Fedora image in a qemu virtual machine with a graphical user interface enabled using the --console=gui option. ```console $ mkosi -d fedora --console=gui qemu ``` -------------------------------- ### Boot a kernel directly with mkosi vm Source: https://github.com/systemd/mkosi/blob/main/mkosi/resources/man/mkosi.1.md This snippet demonstrates booting a kernel directly using mkosi vm, bypassing the bootloader. It's useful for experimenting with different kernels and command lines. ```console $ mkosi vm -kernel ... -initrd ... -append '...' ``` -------------------------------- ### Build and Boot Arch Linux Disk Image with QEMU Source: https://github.com/systemd/mkosi/blob/main/blog/content/a-reintroduction-to-mkosi.md Builds an Arch Linux GPT disk image and boots it using QEMU. The resulting image is named 'image.raw' in the current directory. ```sh $ mkosi -d arch -p systemd -p udev -p linux -t disk qemu ``` -------------------------------- ### Base Image Configuration Source: https://github.com/systemd/mkosi/blob/main/docs/sysext.md Configures the base image output format as a directory and specifies packages to install. ```ini [Output] Format=directory [Content] CleanPackageMetadata=no Packages=systemd udev ``` -------------------------------- ### Build and Boot Arch Linux Disk Image with systemd-nspawn Source: https://github.com/systemd/mkosi/blob/main/blog/content/a-reintroduction-to-mkosi.md Builds an Arch Linux GPT disk image and boots it using systemd-nspawn. The resulting image is named 'image.raw' in the current directory. ```sh $ mkosi -d arch -p systemd -p udev -p linux -t disk boot ``` -------------------------------- ### Integrate Initrd into Distribution Image Build Source: https://github.com/systemd/mkosi/blob/main/docs/initrd.md Pass the path to your built initrd image using the --initrd option or the Initrd= setting in your mkosi configuration. ```bash mkosi --initrd= ... ``` -------------------------------- ### Run mkosi as a Python module Source: https://github.com/systemd/mkosi/blob/main/README.md Execute mkosi directly using the Python interpreter without needing a zipapp or global installation. ```shell python3 -m mkosi ``` -------------------------------- ### Create bootable GPT image with Fedora and systemd-boot Source: https://github.com/systemd/mkosi/blob/main/mkosi.md Builds a bootable GPT image named foobar.raw using Fedora, kernel-core, systemd, systemd-boot, and udev. It then proceeds to boot the image. ```console $ mkosi -d fedora -p kernel-core -p systemd -p systemd-boot -p udev -o foobar.raw # mkosi --output foobar.raw boot $ mkosi --output foobar.raw vm ```