Posted by Steve on Thu 8 Mar 2007 at 16:18
One of the nice things about using Xen is that it doesn't require much setup to create new guests - just a loopback file or two, or an LVM partition. If you use NFS to store your remote systems you don't even need that. Here we'll give a quick example of booting Xen guests which will mount their root file-systems via NFS.
To boot Xen domains over the network you'll need several things working:
Unfortunately the Debian Xen packages do not contain kernels which have support for NFS mounting of their root filesystem, so if you want to use NFS-mounted domU upon Debian you must compile your own kernel.
Building your own kernel doesn't take too long, and you're still able to use the other Debian-supplied Xen packages (such as the hypervisor, and the tools).
Since I wanted NFS root support, and a recent kernel, I chose to use the xen-unstable repository, which is the development release of Xen.
To pull this down you'll need the mercurial revision control package:
apt-get install mercurial
Once you have it you can fetch the most current version of the source by running:
hg clone http://xenbits.xensource.com/xen-unstable.hg
Change into the directory and now you're ready to build the kernel. Since we have the Debian packages we don't want to build anything else, not the hypervisor, nor the Python tools.
First of all configure the source, then build and install it:
# make linux-2.6-xen-config CONFIGMODE=menuconfig # make linux-2.6-xen-build # make linux-2.6-xen-install
When configuring the source you want to select the following options :
Filesystems Network Filesystems [*] NFS file system support [*] Root file system on NFS
This should ensure that CONFIG_ROOT_NFS is set to "y" in your configuration file.
Any other choices which suit your environment are fine, but these are the reason for rebuilding in the first place so don't forget them!
All being well you'll find a new kernel and modules installed upon your dom0 system, and we can proceed to setup the NFS server with our new system.
With the rebuild, providing you compile NFS support into the kernel you'll not need an initrd image.
I'm going to setup a machine called "yours.my.flat" (192.168.1.40) to serve the root filesystem for my Xen guest - since I have only one guest I will use a simple scheme which doesn't involve significant customisation of the exported filesystem.
If you were using this kind of system in production you would probably want to have either one NFS share per-client, or do some other fancy organisation.
To start with we'll need an NFS server installed, if we don't have one already:
apt-get install nfs-user-server portmap
Now we'll create a directory to export:
mkdir -p /mnt/etch
Add this to the /etc/exports file, so that it can be served via NFS:
/mnt/etch 192.168.1.0/255.255.255.0 (rw,sync,no_root_squash)
Note: here we've exported this filesystem such that any host within the range 192.168.1.0/24 has full read & write access to it. This is overly permissive.
Now we want to populate the filesystem with something the client can boot. We've previously shown how to use debootstrap to create new Debian installations, so we'll gloss over the details:
debootstrap etch /mnt/etch/
This gives us a basic system, but it won't be very usable since it is missing some files. We'll simply copy some files from the host to make it more complete:
cp /etc/resolv.conf /mnt/etch/etc cp /etc/hosts /mnt/etch/etc echo "nfsclient.my.flat" >> /mnt/etch/etc/hostname
Now we'll add an SSH server package, and a couple more:
chroot /mnt/etch/ /usr/bin/apt-get install openssh-server nfs-common portmap
Finally we need to make sure that the /etc/fstab file upon the guest is correct - so we'll add the following to it:
192.168.1.40:/home/etch / nfs rw 0 0 proc /proc proc rw,nodev,nosuid,noexec 0 0 sys /sys sysfs rw,nodev,nosuid,noexec 0 0
The last step is to copy the built kernel modules from /lib/modules/2.6.18-xen on the Xen host to the NFS-exported filesystem, so that module loading works correctly when our client boots.
Note: 192.168.1.40 is the IP address of the NFS server.
Now we should be almost complete - we have:
Whats missing is the Xen configuration file.
Each xen domain (domU) typically has a configuration file beneath the directory /etc/xen, so we'll create a new one for this NFS-booting client, /etc/xen/nfsclient.my.flat.cfg:
# Common things. kernel = '/boot/vmlinuz-2.6.18-xen' memory = '64' # Name name = 'nfsclient.my.flat' hostname = 'nfsclient.my.flat' # Networking basics vif = [ 'ip=192.168.1.112' ] netmask = '255.255.255.0' gateway = '192.168.1.1' ip = '192.168.1.112' broadcast = '192.168.1.255' # NFS option nfs_server = '192.168.1.40' nfs_root = '/mnt/etch' root = '/dev/nfs'
Here the kernel is the kernel we've previously built and I've setup the client to have a static address. If you wanted you could use DHCP too - I just like static IPs for Xen guests.
Now that we have everything we can try starting this Xen guest domain:
# xm create nfsclient.my.flat.cfg -c
All being well you should see your machine boot up with the static address we've allocated in the configuration file, mount its root filesystem via NFS, and boot as normal.
This article can be found online at the Debian Administration website at the following bookmarkable URL:
This article is copyright 2007 Steve - please ask for permission to republish or translate.