Building Xen-3.4 testing and linux-2.6.31.10 domain0 kernel on Debian-testing

This is a howto on compiling and installing Xen 3.4 testing from the repository and linux kernel 2.6.31.10 for a Debian squeeze domain0.
Note: Try and do everything as root. If you do a make and then make install with different users, it will completely recompile over again. Now even though linux-2.6.18-xen is the proper kernel to be using for a Xen domain0, the new version of udev in all the usable distros do not support the older kernels (anything before 2.6.25 I believe). So I will be using the 2.6.31.10 kernel along with the gentoo Xen patches. Lets see how this goes.

Getting the source:

Install Mercurial
> aptitude install mercurial
Get the Xen Source
> hg clone http://xenbits.xensource.com/xen-3.4-testing.hg
Get the Xen domain0 Linux kernel source and the patches from here and here respectively:
> wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.31.10.tar.bz2
> wget http://gentoo-xen-kernel.googlecode.com/files/xen-patches-2.6.31-10.tar.bz2
Now unpack them with
> tar -jxvf linux-2.6.31.10.tar.bz2
> mkdir xen-patches
> cd xen-patches
> tar -jxvf ../xen-patches-2.6.31-10.tar.bz2
> cd ~/

Apply the patches

>cd linux-2.6.31.10
>cat ../xen-patches/* | patch -p1
Now you probably want to rename your kernel to indicate is has been patched. You can also modify the "Extra version" variable in make menuconfig later on if you want the kernel to have a different name to show up with 'uname'.
> cd ~/
> mv linux-2.6.31.10 linux-2.6.31.10-xen

Get Dependencies:

There are quite a lot of dependencies. Too many to list individually, but here's how to install them all:
> aptitude install gawk bzip2 mercurial transfig bzip2 screen ssh debootstrap iproute bridge-utils python-twisted binutils zlib1g-dev python-dev python python-central libssl-dev install libcurl3-dev libncurses5-dev x-dev build-essential gettext libx11-dev make bin86 bcc gcc ncurses-dev patch xorg-dev transfig libsdl1.2-dev libvncserver-dev udev pciutils-dev graphviz texlive-latex-base git-core texinfo texlive-latex-extra texlive-font-utils texlive-fonts-extra texlive-fonts-recommended
Grab a coffee...

Compiling:

Enter the Xen source directory
> cd xen-3.4-testing.hg
Cross your fingers and start compiling
> make
Or if you have a fast machine with lots of cores, use the -j switch. Usually you use the number of cores you have plus 2.
> make -j 4
This will take a long time...grab another coffee, If the build fails anywhere I may have missed a dependency. Its usually pretty obvious what the dependency is by the build error message. Just go install it and try again. Xen is hopefully compiled so we can take care of the kernel. Onward with our linux-2.6.31.10-xen kernel.
cd linux-2.6.31.10-xen
> make menuconfig
You need to enable Xen Support (under processor type and features) as well as Priviledged Domain (under device drivers->Xen) and I also set Xen compatibility to 3.3.0 and above (under device drivers->Xen->Xen Version compatibility). Once you've enabled everything you think you'll need, start compiling.
> make
Once its done without errors, go ahead and install it, but do not use the -j option for the install.
> make install
Also you need to install the modules
> make modules_install
Once that is done, look in the /boot directory
> cd /boot && ls
You should see some files: xen-3.4.3-rc3-pre.gz and vmlinuz-2.6.31.10. If not, you need to go back and deal with the problem because nothing will work past this point. Also check the /lib/modules directory for a directory called 2.6.31.10

Generate an initrd image:

So you've got Xen and the linux kernel compiled, now you need an initrd image to go along with the kernel. Enter the /boot directory:
> cd /boot
Setup the dependencies for the initrd image:
> depmod -a 2.6.31.10
Now generate an initrd image using the mkinitramfs command
> mkinitramfs -o initrd.img-xen 2.6.31.10
There should now be a file called initrd.img-xen in /boot

Grub2:

This can be a bit of a pain because of grub2 being rather....Beta, but we cant let that stop us. (If you have grub legacy, look here) Make sure you have a livecd available in case you make a mistake with the config file formatting and grub fails. Now open the grub config file using your favorite editor (which is probably vim because otherwise your insane) like this
> vim /boot/grub/grub.cfg
Scoll down to where you see the menuentries. Add your own entry with something like this.
menuentry "Xen, with Linux 2.6.31.10" {
        echo    Loading Linux 2.6.18.8-xen ...
        multiboot       /boot/xen-3.4.3-rc3-pre.gz
        module  /boot/vmlinuz-2.6.31.10 dummy=dummy root=/dev/sda1 ro
        module  /boot/initrd.img-xen
}
You may notice a few things different. You need to use multiboot and modules to pass Xen, the kernel and your initrd img into grub2. Also you may notice the parameter dummy=dummy going into the kernel, this is because at the time of writing this, there is a bug in grub2 which causes the first parameter not to be passed into the kernel. So we can create a useless one as the first parameter. That way when they fix the bug nothing horrible will happen to our bootup. Also you may not want to put your new entry at the top until you are sure everything works because grub defaults to the top entry.

Booting:

Cross your fingers and reboot! Hold shift to get grub to show you a list if it doesnt by default. If it boots congratulations.

Testing a guest domain:

Now you probably want to setup a Virtual Machine to test your setup. http://stacklet.com/ has some pre-build appliance images you can grab and test with. Check out this post for more info on starting a virtual machine.