Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Test Kernel changes on QEMU
Carlo Lobrano
Carlo Lobrano

Posted on

     

Test Kernel changes on QEMU

This is all about testing Linux kernel changes on a QEMU virtual machine in cycles ofbuild, install and run shortest than 5 minutes.

The Internet is full of tutorials about building the Linux Kernel, yet I found less content aboutapplying the changes to a virtualized environment, so that will be the goal of this post.

Running QEMU virtual image

Also this content is quite common, so I will be short

# Create the disk: a qcow2 format image of 2G$qemu-img create-f qcow2 alpine.img 2G# Download a live CD ISO image (e.g. Ubuntu, Alpine, whatever), then run the live image and install# it into the disk created above.$qemu-system-x86_64-cdrom alpine-downloaded.iso alpine.img-m 512M-enable-kvm# Run the newly installed image from the disk$qemu-system-x86_64-enable-kvm-m 512M-smp 4-cpu host-drivefile=alpine.img
Enter fullscreen modeExit fullscreen mode

Running QEMU with a custom kernel

You downloaded and built the Linux Kernel under~/workspace/linux, that is you know have a/linux/arch/x86_64/boot/bzImage, the command to run it in QEMU is

$qemu-system-x86_64\-enable-kvm-m 512M-smp 4-cpu host\-kernel ~/workspace/linux/arch/x86_64/boot/bzImage\-append"root=/dev/sda3 console=ttyS0 rw"\-drivefile=alpine.img
Enter fullscreen modeExit fullscreen mode

Make a change and test it

Consider now you made a change to the kernel. Any change, maybe even so silly like the one below

diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.cindex e7755d9cf..534e8051e 100644--- a/drivers/usb/serial/option.c+++ b/drivers/usb/serial/option.c@@ -2138,7 +2138,7 @@ static struct usb_serial_driver option_1port_device = {        .owner =    THIS_MODULE,        .name =     "option1",    },-   .description       = "GSM modem (1-port)",+   .description       = "it's me, Mario!",    .id_table          = option_ids,    .num_ports         = 1,    .probe             = option_probe,
Enter fullscreen modeExit fullscreen mode

How do you test it on your QEMU VM?

First, mount the VM image on the host

$mkdir /tmp/alpine$guestmount--add alpine.img--root /dev/sda3 /tmp/alpine$ls /tmp/alpinebin   dev  home  lost+found  mnt  proc  run   srv   sys  usrboot  etc  lib   media       opt  root  sbin  swap  tmp  var
Enter fullscreen modeExit fullscreen mode

Second and last, during the build, tell the kernel to install the modules inside the mounted image

$make-j$(nproc)...$make-j$(nproc) modules$ INSTALL_MOD_PATH=/tmp/alpine make modules_installINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/drivers/thermal/intel/x86_pkg_temp_thermal.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/drivers/usb/serial/option.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/drivers/usb/serial/usb-serial-simple.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/drivers/usb/serial/usb_wwan.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/drivers/usb/serial/usbserial.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/fs/efivarfs/efivarfs.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/net/ipv4/netfilter/iptable_nat.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/net/netfilter/nf_log_syslog.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/net/netfilter/xt_LOG.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/net/netfilter/xt_MASQUERADE.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/net/netfilter/xt_addrtype.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/net/netfilter/xt_mark.koINSTALL /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty/kernel/net/netfilter/xt_nat.koDEPMOD  /tmp/alpine/lib/modules/5.18.0-rc2-custom-ga1901b464e7e-dirty
Enter fullscreen modeExit fullscreen mode

don't forget to UNMOUNT the image

$umount /tmp/alpine
Enter fullscreen modeExit fullscreen mode

Run again your kernel, the change (at least the one above) will be visible loading "option" module.

Final words

This process is so fast that one can test changes to the kernel in few seconds, moreover you are going to have a wide range of debug features provided by QEMU and, not less important, your machine is preserved.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer & OpenSource contributor.Love Linux, Python and Golang, addicted to (Neo)VIM
  • Location
    Italy
  • Work
    Software Engineer in Italy
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp