How To Make A Kernel Sandbox using QEMU

Last post, I described a few reasons why a kernel sandbox might boost your ability to tinker with the kernel. Now I’m going to describe how you can do this!

I use a debian virtual disk image, along with QEMU for the virtualization. Why QEMU? Its open source (always a plus), free, and has been used pseudo-extensively by a lot of kernel developers. Furthermore, its controlled by the command line, which makes it easier for the type of development we’re doing.

First, we have to make the virtual machine’s system disk image by allocating a hunk of disk space in a big amorphous file. You can do this easily using the tool, “dd”. Here I am making a garbage file that is 1 GB (1M * 1M) in size. Make sure you have the space available! We will later fill this with useful data.
dd if=/dev/zero of=vm_disk.img bs=1M count=1024
Now that we have a big garbage file to work with, we have to format it with a filesystem. Set up the ext3 filesystem on the image by doing:
mkfs.ext3 vm_disk.img
(if prompted, hit ‘y’)

Mount the image in your filesystem, so you can poke around in your file:
mkdir /media/vm
mount -o loop vm_disk.img /media/vm

Now we have a file on your computer that is, for all intents and purposes, an empty virtual hard drive. We have to fill it with a minimal system now. Debian has a pretty easy tool that does just this. By running:
debootstrap sid mounted/ http://ftp.us.debian.org/debian/
Your computer will put a basic stable debian installation on your virtual disk image. Your Virtual Disk is now ready for use! Let’s unmount it…
umount /media/vm

Now, its up to you to make the latest awesome change to the kernel source, configure, and compile it. Doing this is beyond the scope of this article, but if you’re reading this article, there’s a pretty good chance you’re already schooled in how to do all this. :). As you know, when you’re done with this step, you have a kernel disk image as a result.

Alright, now we’re ready to finally run the kernel you built on the virtual disk you made! Assuming you’ve installed QEMU using your system’s package manager, just run:
qemu -hda vm_disk.img -kernel kernel_source/arch/x86/boot/bzImage -append "root=/dev/sda"

You’ll see the system booting up [hopefully], and you’ll be able to see how great the changes you made to the kernel are (or aren’t 🙂 ).

A few warnings: Make sure you have ext3 support compiled /into/ the kernel. You can use these techniques to do a few more advanced features, like rolling a swap file, and using that in qemu. Read qemu’s manpages to think up things to do. I just listed a few of the warnings that came from the top of my head, if you think of any more, be sure to leave them in the comments!

I hope that this suggestion helps you in your hacking!

This entry was posted in Coding, Open Source, Ubuntu. Bookmark the permalink.

2 Responses to How To Make A Kernel Sandbox using QEMU

  1. Pingback: Links 11/1/2010: Amarok 2.2.2 is Out | Boycott Novell

  2. guangyi says:

    Hi, Kevin. Your article is very useful and easy to follow. Now I have booted qemu-kvm with linux 2.6.36 kernel but I cannot log in with my user name and password. Do you know what is wrong with it? Thanks!!!

Leave a Reply

Your email address will not be published. Required fields are marked *