| tags:linux mkosi storage categories:Linux
Creating bootable images for physical machines
Instead of running through a installer everytime you setup a PC you can create a bootable image with mkosi
After my first post about mkosi where I introduced the tool, I tried to use it as often as I could
to easy the pain of spontanious image creation.
One thing I really don’t like todo is to run an OS installer to setup a PC.
Especially if it is a machine which won’t use without mouse/screen anyway.
So I’d rather create a bootable image, copy it either to a disk directly or use a live system to do that if there’s no other way.
Preparation
If you don’t have enough space on the boot medium but a lot of memory, you can bootstrap a Archlinux in a RAM disk and work from there
cd /tmp
mkdir arch
pacstrap arch base mkosi vim systemd dnf mtools dosfstools xfsprogs apt debootstrap cpio
systemd-nspawn -D arch
Installation
The following commands use mkosi
to create such an image for EFI systems:
# for Arch Linux
sudo mkosi --format disk --distribution arch --bootable --output new-server-os-01 --package linux,systemd --cache-dir /var/cache/pacman/pkg/ --root-password foobar2342
# for Debian
sudo mkosi --format disk --distribution debian --release bookworm --bootable --output new-server-os-01 --package linux-image-amd64,systemd --cache-dir /var/cache/pacman/pkg/ --root-password foobar2342
# for Rocky with some basic tools
sudo mkosi --format disk --distribution rocky --release 9 --bootable --output new-server-os-01 --package kernel,systemd,dnf,passwd,iproute,epel-release,vim,dhclient,findutils,less,bash --cache-dir /var/cache/pacman/pkg/ --root-password foobar2342
Afterwards you can test the full boot loader chain in the image with, or make changes inside the OS
# test with nspawn
sudo systemd-nspawn -i new-server-os-01.raw -b
# test with qemu
qemu-system-x86_64 -m 2048 -smp 2 -bios /usr/share/ovmf/x64/OVMF_CODE.fd -hda new-server-os-01.raw
# just get a shell inside the image
sudo systemd-nspawn -i new-server-os-01.raw
Eventually get the size of the physical disk you want to use, resize the image and copy it:
sudo fdisk -l /dev/sdX | head -n 1
sudo truncate --size 128035676160 new-server-os-01.raw
sudo parted new-server-os-01.raw resizepart 2 100%
sudo dd if=new-server-os-01.raw of=/dev/sda bs=2M oflag=direct status=progress
It might also be necessary to create a EFI boot entry, like this:
efibootmgr --create --disk /dev/sda --part 1 --loader '\EFI\BOOT\BOOTX64.EFI' --label 'Linux' --unicode
Afterwards you should be good to go :-)