Installing an Alpine Root Filesystem

Now we will install a minimal root filesystem in the VM, using the Alpine Linux distribution.

Creating a Virtual Disk

First create an empty virtual hard disk for the VM:

cd ~/virt-101-exercise
./qemu-8.2.0-rc2/prefix/bin/qemu-img create -f qcow2 alpine.qcow2 2G

Here we use the qemu-img tool (compiled alongside Qemu) to create in a file named alpine.qcow2 a virtual hard disk of size 2 GB. We indicate also that the format of the disk should be Qcow2.

Downloading and Extracting an Alpine Image

Next we download an installation ISO image of Alpine:

wget https://dl-cdn.alpinelinux.org/alpine/v3.19/releases/x86_64/alpine-standard-3.19.0-x86_64.iso

Installing Alpine on the Virtual Disk

Next we will launch the VM with the ISO image in the virtual CD drive, as well as the virtual hard disk plugged:

./qemu-8.2.0-rc2/prefix/bin/qemu-system-x86_64 -m 1G -nic user -boot d -cdrom alpine-standard-3.19.0-x86_64.iso -hda alpine.qcow2 -nographic

The notable parameters here are:

  • -nic user that creates a virtual network allowing the VM to access the internet (needed for Alpine installation).
  • -boot d -cdrom alpine-standard-3.19.0-x86_64.iso uses the ISO image in the virtual CD-ROM drive of the VM, and indicates to the Qemu's BIOS to boot on that CD-ROM (d).
  • -hda alpine.qcow2 plugs the previously created empty virtual hard disk in the VM.

The ISO can take a few seconds to boot silently. When the login prompt appears:

Welcome to Alpine Linux 3.19
Kernel 6.6.4-1-lts on an x86_64 (/dev/ttyS0)

localhost login:

Login as root, no password is needed. In the VM, start the installation by running the following command:

setup-alpine

Next the setup process will ask a series of questions. Answers the default choice (i.e. just press enter) for all but the following:

  • Choose a password when asked.
  • To the question Allow root ssh login? answer yes.
  • To the question Which disk(s) would you like to use? answer sda.
  • To the question How would you like to use it? answer sys.
  • To the question Erase the above disk(s) and continue? answer y

Once the installation is over, shut down the VM with the following command:

halt

Wait a few seconds then exit Qemu with ctrl+a then x.

Alpine is now installed on the virtual hard disk alpine.qcow2.

Creating a VM Boot Script

As you can see the Qemu invocation command can be pretty long and it is tedious to type or even copy paste it each time we boot the VM. We can create a shell script to automate the launch of the VM. Edit the file ~/virt-101-exercise/launch-vm.sh and write inside the following:

#!/bin/bash

./qemu-8.2.0-rc2/prefix/bin/qemu-system-x86_64 \
    -m 1G \
    -nographic \
    -nic user \
    -hda alpine.qcow2 \
    -kernel linux-6.6.4/arch/x86_64/boot/bzImage \
    -append "console=ttyS0 root=/dev/sda3"

You will notice that this VM uses the kernel we compiled in the previous step, and boots on the virtual hard disk alpine.qcow2 (/dev/sda3 from the kernel point of view)..

Give the script executable permissions:

chmod +x ~/virt-101-exercise/launch-vm.sh

KVM Acceleration. If your have a native Linux installation on your host, or if you are running a privileged container, you can tell Qemu to activate KVM acceleration. This will transform your emulated VM into a proper direct execution-based VM which concretely will make it much faster. To check if you can enable KVM acceleration, check for the presence of the file /dev/kvm. If it is present, add this option to Qemu's command line: -enable-kvm You may need to invoke Qemu as root for the VM to start if you use that option.

Starting the VM

The VM can now be started with the script:

cd ~/virt-101-exercise
./launch-vm.sh

The kernel will boot and load the root filesystem, then Alpine with initialise. Once you reach the login prompt, login as root with the password you defined during the installation. The VM is now fully installed!

Always try to shut down the VM properly with the halt command, and wait before the kernel prints reboot: System halted before killing Qemu with ctrl+a then x. If you don't there is a non-negligible risk of shutting down the filesystem in an inconsistent state, corrupting it. If that happens you will lose the VM's filesystem content and will be forced to reinstall Alpine.