How to use the command 'mkosi' (with examples)
- Linux
- December 17, 2024
The mkosi
command is a powerful tool designed to build modern and legacy-free Linux images. It is a part of the systemd
suite and allows the creation of various types of images for different Linux distributions. Developed for ease of use, it automates much of the drudgery associated with image and distribution creation processes.
Use case 1: Show Current Build Configuration
Code:
mkosi summary
Motivation:
Understanding what configuration is used for the image build process is crucial for any developer or system administrator. The summary
option is a go-to tool for ensuring you are building the exact image you need — it helps verify settings before committing to the potentially time-consuming build process.
Explanation:
summary
: This subcommand does not require any additional arguments and outputs a detailed list of the current build settings. This includes information like the target distribution, architecture, image format, and other relevant details.
Example Output:
Distribution: Ubuntu
Architecture: x86_64
Image Format: raw
Boot Loader: systemd-boot
...
Use case 2: Build an Image with Default Settings
Code:
mkosi build --distribution fedora
Motivation:
The ability to quickly build an image using default settings presents a convenient way to create a base operating system environment. If you’re working on specific system configurations or need to deploy quickly, using the host’s distribution or specifying another can rapidly accelerate development cycles.
Explanation:
build
: This command triggers the image building process.--distribution fedora
: The argument--distribution
specifies that the image being built should be based on Fedora. Other supported distributions include Debian, Ubuntu, Arch, Opensuse, etc.
Example Output:
Building image with distribution: fedora
Downloading Fedora base packages...
Installing packages...
Image build complete.
Use case 3: Build an Image and Open an Interactive Shell
Code:
mkosi shell
Motivation:
Deploying an image and immediately accessing it through an interactive shell can significantly enhance testing and development efficiency. With a simple shell command, developers can explore the newly built environment, allowing them to tweak and test changes instantaneously.
Explanation:
shell
: This argument directsmkosi
to build the image and then run a systemd-nspawn container with an interactive shell access. This provides an isolated and secure environment to explore your image.
Example Output:
Spawning container...
Welcome to Fedora 34
[root@container ~]#
Use case 4: Boot an Image in a Virtual Machine Using QEMU
Code:
mkosi qemu
Motivation:
Booting an image in a virtual machine allows developers to test the image in an environment that closely mirrors the target deployment environment. Using QEMU enhances this, given its capabilities to simulate different hardware and system configurations.
Explanation:
qemu
: This argument instructsmkosi
to boot the newly built or existing image using QEMU. It requires disk or CPIO images and optionally includes a kernel if needed for booting.
Example Output:
Launching QEMU...
Image is booting...
Welcome to your virtual machine!
Use case 5: Display Help
Code:
mkosi help
Motivation:
Accessing help documentation at the command line is essential for both new and experienced users who might need quick guidance on command usage, arguments, or features. It ensures users can efficiently utilize all of mkosi
’s capabilities by providing a comprehensive guide directly in the terminal.
Explanation:
help
: This universal command provides instructions and information on how to usemkosi
. It displays available commands, arguments, and options.
Example Output:
Usage: mkosi [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
build Build an image
shell Run an interactive shell
qemu Boot image in QEMU
summary Show current build configuration
...
Conclusion:
The mkosi
command offers an efficient suite of tools for building, testing, and deploying Linux images across a variety of platforms and distributions. Whether verifying build settings with summary
, quickly bootstrapping an image with build
, or interacting with a newly created environment through shell
and qemu
, mkosi
provides users the flexibility and features needed to streamline Linux image operations.