📜 ⬆️ ⬇️

Setting up an environment to test changes in the Linux kernel

image

Sometimes (rarely, but still) there is a need to add something or redo our favorite Linux in the core. And then the question arises: How to launch and check all these changes quickly and without smoke breaks?

It's one thing if we can organize our new functionality as a module, then we can rather easily test it without rebooting the kernel itself, simply turning it on and off via insmod. But what if the concept of modularity does not apply? For example, as in my case, when it was necessary to add a new control group subsystem (cgroups) for Jet9 and it was necessary to restart the kernel each time to check the changes?

The qemu (or qemu-kvm) virtualization system will help us in this, since it can take as parameters not only the partition with the system, but also the file itself with the kernel ( bzImage ) and initramfs . Using this functionality, we can quickly set up and use a test environment. Setup consists of only two steps:

')
Next, I briefly describe these two steps, but for all add. Questions will be answered in the comments.

Create initramfs

Create a directory structure:

mkdir -p initramfs/{bin,sbin,etc,proc,sys,cgroup,usr/bin,usr/sbin} 


Add all the necessary unix-utilities. The easiest way to do this is to use busybox. The binaries can be downloaded from here and put in / bin / busybox . You must add a symlink for sh , so that init starts:

 cd initramfs ln -s bin/busybox bin/sh 


Add the following script to ./init (it should be in the root of our image):

 #!/bin/sh #Create unix-utils symlinks to /bin/busybox /bin/busybox --install -s #Mount pseudo-fs mount -t proc proc /proc mount -t sysfs sysfs /sys mount -t cgroup cgroup /cgroup #Create null device mknod /dev/null c 1 3 exec sh 

When it starts, it will create symlinks for all basic unix-utilities, mount pseudo-fs that are convenient for us, create the / dev / null device (so that it can be used later in our scripts and suppress unnecessary output) and launch the shell.

Of course, if you need something beyond this, you can easily modify this script to your liking. I, for example, added test runs to it before launching the shell, so that they would not be manually launched each time. You can do the same with additional utilities - just add them to the image along the paths you need. Similarly with modules - if you use them when testing, just add them to / lib / modules / <kernel-ver>

Next, we will pack everything into one file:

 find . | cpio -H newc -o | gzip -c > ../initramfs.gz 


That's all, initramfs is ready.

Start kvm

It's all quite simple, everything is started with the command:

 kvm -m 128 -kernel ./arch/x86/boot/bzImage -initrd initramfs.igz -serial stdio -append "console=ttyAMA0 console=ttyS0" -display none 

options:
-m - how much RAM we allocate to the machine
-kernel - newly compiled kernel
-initrd - the initramfs created above
-append - parameters to the kernel

the most interesting is the -append parameter, in it we redirect the I / O to the serial port (remember this?;), and with the -serial parameter we redirect the serial port to stdin / stdout. This will allow us to get the output of the kernel directly to the console, bypassing the black window 80x25, in which absolutely nothing pleases. By the way, the -display none parameter allows you to remove it as well, so that it doesn’t shine (we are interested in the core, not gui).

Profit! Now the kernel boots and is ready to be tested in less than 3 seconds.

Source: https://habr.com/ru/post/265263/


All Articles