Building the Linux Kernel

Now we will build the operating system kernel we will use for the guest: Linux.

Downloading and Extracting Linux

Similarly to what we did with Qemu, we first download the sources of Linux v6.6.4:

cd ~/virt-101-exercise
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.4.tar.xz

Extract the archive:

tar xf linux-6.6.4.tar.xz

Compiling a Minimal Linux Kernel for Our Virtual Machine

Next we'll compile a minimal version of Linux that can run in a VM created by Qemu. To that aim we'll use the default configuration for a basic x86-64 machine. That configuration can be generated as follows:

cd linux-6.6.4
make x86_64_defconfig

Once done launch the compilation of the kernel with the following command:

make -j4

This will take a while, but don't worry you will have to compile the entirety of the kernel only once. The subsequent build for that exercise (e.g. after you have implemented the driver) will be incremental, i.e. much faster (a few seconds). Once finished, the compiled kernel's binary is arch/x86/boot/bzImage.

Trying Out the Guest Kernel

We can already try to boot the kernel we just built with Qemu as follows:

cd ~/virt-101-exercise
./qemu-8.2.0-rc2/prefix/bin/qemu-system-x86_64 -m 1G -kernel linux-6.6.4/arch/x86_64/boot/bzImage -nographic -append "console=ttyS0"

Here we tell Qemu to create a machine with 1G of RAM and to use the kernel we compiled. The option -append indicates that the option "console=ttyS0" should be passed to the kernel, telling it to output its boot log on the VM's serial console.

You should see Linux starting to boot. The boot process should end with the following error:

Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

Once again you can exit Qemu with ctrl+a then x. This is normal: the kernel is not sufficient to fully run a VM, and we are missing a root filesystem. The root filesystem consists in all the user space basic system utilities such as a shell, etc. We will install a minimal Alpine Linux filesystem in the next step.