Playing Around With Mkosi - Part I

This covers the first part of my experiences of descending down the rabbit hole of exploring the nifty tool for generating bespoke operating system images called mkosi. In this part, I will cover the process of building a custom Fedora Linux 40 operating system image with a minimal set of packages.

Playing Around With Mkosi - Part I

Intro

Back in the first week of August 2023, I got to know about a nifty tool called mkosi while moderating the talk about things that are new in systemd delivered by Zbigniew Jędrzejewski-Szmek during Flock To Fedora 2023. The tool allows users to create bespoke operating system images and generate an operating system filetree that can be booted as containers, virtual machines, or baremetal hardware. On returning from the conference, one of the first things that I did was to take the tool, which its authors call "a fancy wrapper around dnf --installroot , apt, pacman and zypper that generates customized disk images with a number of bells and whistles", for a spin and understand its capabilities.

At the time of writing this article, mkosi version 14 was available on the official repositories of Fedora Linux 38 which already had an outdated set of features so I decided to spin up a minimal installation of Fedora Linux 40, which had become the Rawhide version then on a virtual machine and installed the most recent version, that was mkosi version 15.1 then. The first distribution that I wanted to build operating system images for was obviously Fedora Linux so I went ahead with writing myself a basic operating system configuration file. This file helped me define details like the distribution name, release number, output format, image bootability, installed packages and a bunch more things.

$ sudo dnf install mkosi

How-to

Here are the steps I followed to create an operating system image.

  • STEP 1
    Create a new directory for the operating system image generation project and make it the current working directory.
$ mkdir fedogrid
$ cd fedogrid
  • STEP 2
    Create a minimal operating system configuration file named mkosi.conf using a text editor of your choice.
$ nano mkosi.conf
[Distribution]
Distribution=fedora
Release=40
Architecture=x86-64
Bootable=yes

[Output]
Format=disk
Output=fedogrid
CompressOutput=no
ImageVersion=0.1.0

[Content]
Packages=kernel
         systemd
         systemd-boot
         udev
         util-linux
         dnf
         NetworkManager
         iproute
         ncurses
         basesystem
         sudo
Bootable=yes
Locale=en_US.UTF-8
Timezone=Asia/Kolkata
Hostname=fedogrid
RootPassword=fedogrid
RootShell=/usr/bin/bash
Autologin=yes

[Validation]
Checksum=yes
  • STEP 3
    Execute the following command to start building the operating system image from the configuration provided in the file.
$ sudo mkosi build
‣ Removing output files…
‣ Building default image
‣  Mounting image…
‣   Installing Fedora
....
....
....
....
‣  Calculating SHA256SUMS…
‣  Saving manifest fedogrid_0.1.0.manifest
‣  /home/fedohide-main/projects/fedogrid/fedogrid size is 1.7G, consumes 1.4G.
  • STEP 4
    Check the contents of the current working directory and the generated content from the operating system should be present.
$ ls -lha
total 1.6G
drwxr-xr-x.  2 gridhead gridhead 4.0K Sep  2 17:51 .
drwxr-xr-x. 12 gridhead gridhead 4.0K Aug 25 09:20 ..
lrwxrwxrwx.  1 gridhead gridhead   18 Sep  2 17:51 fedogrid -> fedogrid_0.1.0.raw
-rw-r--r--.  1 gridhead gridhead 146M Sep  2 17:51 fedogrid_0.1.0.efi
-rw-r--r--.  1 gridhead gridhead  24K Sep  2 17:51 fedogrid_0.1.0.manifest
-rw-r--r--.  1 gridhead gridhead 1.8G Sep  2 17:51 fedogrid_0.1.0.raw
-rw-r--r--.  1 gridhead gridhead  259 Sep  2 17:51 fedogrid_0.1.0.SHA256SUMS
-rwxr-xr-x.  1 gridhead gridhead  14M Sep  2 17:51 fedogrid_0.1.0.vmlinuz
lrwxrwxrwx.  1 gridhead gridhead   30 Sep  2 17:51 fedogrid-initrd -> fedogrid-initrd_0.1.0.cpio.zst
-rw-r--r--.  1 gridhead gridhead  40M Sep  2 17:51 fedogrid-initrd_0.1.0.cpio.zst
-rw-r--r--.  1 gridhead gridhead  518 Sep  2 17:47 mkosi.conf

Outro

The generated operating system images can be booted either as a container using systemd-nspawn, as a virtual machine using QEMU/KVM or as a baremetal installation by writing it to a storage drive. In the subsequent posts, I will go on to explore how these images can be put to use in a variety of use cases. Additional information on the accepted variables for the operating system configuration file can be found here. Readers are encouraged to take the minimal configuration specified in this article for a spin and then begin introducing their own changes to it to better understand the effects that these variables have on the resulting image whenever they are changed from their pre-defined value.