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/prefix/bin/qemu-img create -f qcow2 alpine.qcow2 2G
Here we use the qemu-img tool (compiled alongside QEMU) to create a virtual hard disk of size 2 GB in a file named alpine.qcow2.
We also indicate 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, and with the virtual hard disk plugged in:
./qemu-8.2.0/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 userthat 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.isouses the ISO image in the virtual CD-ROM drive of the VM, and instructs the QEMU’s BIOS to boot from that CD-ROM (d).-hda alpine.qcow2plugs 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:
Log in 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. Answer with the default choice (i.e. just press Enter) for all questions except the following:
- Choose a password when asked.
- To the question
Allow root ssh login?answeryes. - To the question
Which disk(s) would you like to use?answersda. - To the question
How would you like to use it?answersys. - To the question
Erase the above disk(s) and continue?answery.
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 it, or even copy and 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 the following inside it:
#!/bin/bash
./qemu-8.2.0/prefix/bin/qemu-system-x86_64 \
-m 1G \
-nographic \
-nic user \
-hda alpine.qcow2 \
-kernel linux-6.6/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 from 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 you 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 will make it much faster in practice. 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-kvmYou 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 will initialise.
Once you reach the login prompt, log in 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
haltcommand, and wait until the kernel printsreboot: System haltedbefore killing QEMU withctrl+a, thenx. 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.