QEMU is an open source virtual machine (similar to VMware)

QEMU is an open source virtual machine (similar to VMware). For kernel development projects, it has two nice feature:

 

1. you can attach GDB to it and debug the lowest levels of the kernel

2. it can directly load a kernel image, making it easier to replace the kernel

Using QEMU:

1. Download the binary distribution of QEMU from here:

http://fabrice.bellard.free.fr/qemu/download.html

2. Download a Linux ISO. I recommend Centos 4 sever. You want this file:

CentOS-4.4.ServerCD-i386.iso

from a mirror on this web page:

http://isoredirect.centos.org/centos/4/isos/i386/

3. Install QEMU under /scratch/{your username}

tar xzf qemu-0.8.2-i386.tar.gz

setenv path = ( $path /scratch/{your username}/usr/local/bin # for csh / tcsh

4. Create a virtual disk (4 GB should be enough)

qemu-img create -f qcow disk.img 4G

5. Boot up the virtual machine from the image:

qemu -hda disk.img -net nic -net user -boot d -cdrom CentOS-4.4-ServerCD-i386.iso -L usr/local/share/qemu/

the "-boot d" means boot from hard disk, the "-hda disk.img" says what virtual disk to use, and the "-cdrom" says what ISO to use as a cdrom

6. Install the operating system. Use DHCP for networking

7. On the next boot, use this command line to boot from your virtual disk:

qemu -hda disk.img -net nic -net user -boot c -redir tcp:5555::22 -L usr/local/share/qemu/

 

 

This boots your virtual machine and redirects the SSH port (22) on the virtual machine to port 5555 on the host. You can now ssh and scp to your virtual machine doing this:

scp -P 5555 root@localhost:/usr/... ...

ssh -p 5555 root@localhost

Note the difference in captialization.

8. If you want to attach a debugger to your virtual machine, append either "-s" or "-s -S" to the command line:

qemu -hda disk.img -net nic -net user -boot c -redir tcp:5555::22 -s –S -L usr/local/share/qemu/

"-s" means listen for gdb on port 1234

"-s -S" means listen for gdb on port 1234 before booting

To connect from gdb, start gdb on the vmlinux file and use the target command:

gdb vmlinux

> target remote localhost:1234

 

9. If you want to boot directly from your local machine (and avoid the grub prompt), you need a ramdisk and an image.

You need to boot your virtual machine and either build a ramdisk:

mkinitrd -f /boot/initrd-mine 2.6.17-xxx

where 2.6.17-xxx is the version number of your kernel.

Or, you can copy down an existing ramdisk. Either way, you need to get the ramdisk off your VM onto your host computer.

You can then boot directly with this command:

qemu -hda disk.img -net nic -net user -boot c -redir tcp:5555::22 -kernel bZimage -initrd inird-mine -L usr/local/share/qemu/

 

10. Download the kernel source from CentOS:

 

http://dev.centos.org/centos/4.4beta/os/SRPMS/kernel-2.6.9-42.EL.src.rpm

Build a directory for unpacking the rpm:

mkdir usr
mkdir usr/src
mkdir usr/src/redhat
mkdir usr/src/redhat

Edit ~/.rpmmacros and add a line with the directory where you want to put the souce:

%_topdir /scratch/.../...

Unpack the RPM

rpm -ivh kernel-2.6.9.42.EL.src.rpm
cd /scratch/.../... # into your rpm topdir
mkdir BUILD
rpmbuild -bp SPECS/kernel-2.6.spec --target=i686

 

11. The kernel source is now in the BUILD/kernel-2.6.9/linux-2.6.9 directory.

You can build your kernel by running:

make oldconfig
make bzImage