mkosi

From ArchWiki

mkosi stands for Make Operating System Image, and is a tool for generating an OS tree or image that can be booted.

Installation

Install mkosi or mkosi-gitAUR. Depending on what distribution you want to use in your OS tree/image, install the following packages:

Distribution Package
Arch arch-install-scripts
Debian debootstrap, debian-archive-keyring, apt
Ubuntu debootstrap, ubuntu-keyring, apt
Fedora dnf
OpenSUSE zypper-gitAUR
CentOS dnf-legacy-utilsAUR

Basic usage

You can create an image by just running it without any arguments:

# mkosi

You can specify option as arguments or by editing files in the current folder.

Create and boot a Debian image

The following example will create a bootable image with the latest Debian version and packages openssh-client and vim installed. This command requires mkosi's optional dependencies mtools and systemd-ukify. Depending on the image, can also be built by an unprivileged (non-root) user:

$ mkosi --distribution debian --release bookworm --format disk --bootable --checksum --root-password password --package linux-image-generic,systemd,systemd-boot,systemd-sysv,udev,dbus --package openssh-client,vim --output image.raw

systemd-nspawn can boot the resulting image:

# systemd-nspawn --boot --image image.raw

It can also be virtualized with QEMU/KVM or with systemd-vmspawn(1):

$ mkosi --output image.raw --qemu-smp 2 qemu

You can also write this image to a USB drive and use it to boot your computer.

Using configuration files

The same Debian image can be created using a configuration file, mkosi.conf, and then run mkosi without any arguments:

mkosi.conf
[Distribution]
Distribution=debian
Release=bookworm

[Output]
Format=disk
Output=image.raw

[Content]
Bootable=yes
Packages=linux-image-generic,systemd,systemd-boot,systemd-sysv,udev,dbus

         openssh-client
         vim
RootPassword=password

[Validation]
Checksum=yes

See bootable[dead link 2024-10-12 ⓘ] for more distro booting examples.

Downloaded package files and temporary build files are stored in the home cache directory or in /var/cache/mkosi and /var/tmp.

Configuration settings

Settings can be specified as command-line arguments or, for example, in a file called mkosi.conf in the current directory. The most important settings are:

Command line Configuration file Description
--distribution
[Distribution]
Distribution=
Name of the distribution to install. Supported are: fedora, debian, ubuntu, arch, opensuse, mageia, centos, openmandriva, rocky, alma
--release
[Distribution]
Release=
Version of the distribution: a numeric string or a distribution version name (bookworm, …)
--format
[Output]
Format=
Format of the image to create. For example:
  • directory: plain directory
  • disk: image file with GPT partition table
  • tar: tarball of a plain directory
--output
[Output]
Output=
Name of the image file or directory
--output-directory
[Output]
OutputDirectory=
Location of the image file or directory (and other generated artifacts)
--bootable
[Content]
Bootable=
Enable or disable generation of a bootable image: yes, no
--package
[Content]
Packages=
List of packages to be installed into the image: (multi) line and/or comma separated list
--root-password
[Content]
RootPassword=
Initial root password

Usage as initramfs generator

Mkosi provides a kernel-install plugin to build an initramfs, similarly to mkinitcpio or dracut.

In order to use it, install cpio and edit the kernel-install configuration file:

/etc/kernel/install.conf
initrd_generator=mkosi-initrd
Note: The generated initrds are much larger than the ones generated by mkinitcpio.

See also