Why Xen and ISCSI?
One word, migration.
Xen has this capability of being able to move a virtual image from one machine to another. Usually this is so quick that users of the virtual image do not even notice that anything has changed.
There are a few caveats:
- The two machines must be on the same LAN segment.
- The two host machines must be the same architecture.
I am not sure exactly how far this extends, but my experience is that transitions from intel based hardware to AMD hardware doesn’t work. As well as the more obvious transition between 32 and 64 architectures. This is a shame because many other virtualisation systems can do this. And, being a home user, I have a mixture of ages, types and machine architectures. See here too. - The two host machines must both be running xend.
- The target machine must have enough physical resources (RAM) to run the image.
Another other pain is that the XEN dom0 patch is incompatible with the nvidia hardware 3d acceleration driver. This is an issue because my newest and fastest machines both run the nvidia driver. They also have more memory than my servers. It would be nice, if I needed to run a long job on a virtual machine, to pop it onto my fastest machine, and let it burble away there for a while. But this is not possible if you are using the nvidia driver.
In order for XEN to migrate, the domU has to be able to access it’s disk drives from any of the servers it might be migrated to. This can be achieved with fancy SAN based infrastructure, such as fiber channel, or similar. Or by using the network. There are three ways the network can be used:
- Root on nfs
- network block devices
- iscsi
Because root on NFS has drawbacks in performance and speed, and nfs mounted filesystems are unsuitable for certain tasks, my choice was between NBD and iscsi. NBD have been around for ages, and although mature, they are specific to linux, and have pretty much been superseeded by iscsi.
Iscsi is fairly new, but the latest debian has support for both initiators (the equivalent of a SCSI card) and targets (the equivalent of a SCSI disk) ISCSI brings the kind of flexibility of fiber channel systems to any system with a network card, though not the performance.
These instructions are based on the debian lenny distribution, with extensive references to here and here. They assume that the XEN server will also be the ISCSI target server. If this is not the case, and extra step will need to be inserted to copy the raw disk image onto the ISCSI target. The Lenny installer does not currently support installation directly onto an ISCSI target. However, I am sure this will come soon. The stock kernel supports ISCSI fully, it would just need some tweaks in the installer.
On the dom0 server, create an LVM object big enough to represent the whole disk space needed by your virtual machine. This will be the virtual disk for the domU.
lvcreate -L10240 -n iscsitest system
Download the xm-debian.cfg into /etc/xen. Copy it to a new name, say xm-iscistestinstall.cfg and edit it for your needs, particularly:
memory=128 name="iscistest" vif=['mac=00:16:3e:1a:2a:2a']
(If you’re using dhcp to provide IP addresses to your hosts, fixing the MAC addresses means that it is more likely that your virtual server will receive the same IP address each time it boots. The first half of the MAC address: 00:16:3e identifies the address as belonging to a Xen virtual machine. You can put whatever you like in here, so long is it doesn’t clash with any other MAC addresses on your LAN. This example assumes you are running dhcp)
disk = ['phy:/dev/mapper/system-iscsitest,xvda,w']
Identify your logical volume as the main disk for your new machine. We will ultimately remove this line, but for the install, it is needed.
Now run xm create with the installer to create a bootable xen virtual server:
xm create -c xm-iscsitestinstall.cfg install=true install-mirror=http://ftp.uk.debian.org/debian install-extra="clocksource=jiffies priority=low" install-suite=lenny
See the debian install guide for how to install lenny.
Some notes to bear in mind when installing:
- When you get to partitioning the disks, I would recommend using a single LVM partition. This is because we will change the underlying physical device name of the disk. LVM will identify the volume group from either the iscsi target, or the xvda drive, meaning that we don’t have to modify /etc/fstab each time we flip between the two devices. The same behaviour could be achieved using volume labels, if you are not so inclined to use LVM.
- Make sure you select a kernel image with “xen” in the name. And choose the “generic” option for initramfs building.
- When given the tasksel option, you can choose any configuration you want. If you select the minimum (ie no groups selected) then it will work, but you will need to add some specific packages to get everything to work. I will identify any special packages you need during the process.
- There is no benefit in installing a boot loader (grub or lilo). So choose the “do without a bootloader” option.
- You will need to add some additional packages by hand, before you finish the installation:
Important: Before you “Finish the installation”, choose Execute a shell:
chroot /target aptitude install openssh-client rsync open-iscsi libc6-xen cd /boot
Copy the kernel and ram disk onto the xen dom0 server. Otherwise it is not possible to boot the virtual machine.
rsync vmlinuz-2.6.26-2-xen-686 server:/etc/xen
(replace “vmlinuz-….” with the kernel name and “server” with the name of your xen server)
rsync initrd.img-2.6.26-2-xen-686 server:/etc/xen/
Now exit the shell
exit exit
And choose “Finish the installation”
Now, copy the xm-iscsitestinstall.cfg file to a new name, say xm-iscsitest.cfg and make the following edits:
Remove all the setup sections, pretty much everything before “memory = 128″ and everything including and after “# Debian Installer specific variables” is redundant.
Add the following lines:
kernel="/etc/xen/vmlinuz-2.6.26-2-xen-686" ramdisk="/etc/xen/initrd.img-2.6.26-2-xen-686" extra="root=/dev/mapper/itsystem-root ro console=hvc0 clocksource=jiffies"
The “clocksource=jiffies” is very important. I will explain it later. Leave everything else as is.
The following should modify your virtual machine so that it will run with iscsi as the filesystem.
On the xen dom0/iscsi server, install the iscsi target modules and management binary packages:
aptitiude install iscsitarget-modules-2.6.686 iscsitarget
(Remembering to replace the iscsitarget-modules with one relevant for your kernel)
Edit the file /etc/ietd.conf, add the following lines:
Target iqn.2001-04.com.example:storage.lun1 Lun 0 Path=/dev/mapper/system-iscsitest,Type=fileio Alias LUN1
And restart the iscsi target daemon:
/etc/init.d/iscsitarget restart
Run up the virtual machine:
xm create -c xm-iscsitest.cfg
Now your virtual machine is running, but it is still using the /dev/xvda device for its drive.
iscsiadm -m discovery -t st -p <ip address of server>
And connect to the device:
iscsiadm -m node --targetname "iqn.2001-04.com.example:storage.lun1" --portal "<ip address of server>:3260" --login
ls /dev
Should show a /dev/sda and /dev/sda1 … devices. Don’t try to access them, however, as you already have them open via the xvda device, and changing them in any way could cause data corruption.
Copy the file:
cp /usr/share/initramfs-tools/scripts/local-top/lvm2 /etc/initramfs-tools/scripts/local-top/lvm2
(The /etc version of this fill will not be overwritten by updates to lvm, whereas the /usr version will)
Edit /etc/initramfs-tools/scripts/local-top/lvm2
Add a line, towards the end of the file, between the lines as shown:
... modprobe -q dm-mirror sleep 5 # added by taskme lvm needs to settle before mounting volumes on iscsi activate_vg "$ROOT" ...
Modify/etc/init.d/open-iscsi
The line in the stop() function stoptargets, comment it out:
...
stop() {
# stoptargets # commented out by taskme see bug #501580
log_daemon_msg "Stopping iSCSI initiator service"
...
This is referred to on debian bug #501580
Now, create a file /etc/iscsi/iscsi.initramfs
With contents:
ISCSI_TARGET_NAME=iqn.2001-04.com.example:storage.lun1 ISCSI_TARGET_IP=<server ip address>
These lines tell the kernel to include the iscsi target code for a root file system into the initial ram disk.
Now, with this setting added, recreate the initial ram disk. This will also add our modification to /etc/initramfs-tools/scripts/local-top/lvm2
dpkg-reconfigure linux-image-2.6.26-2-xen-686
Copy the new initial ram disk onto the server
rsync /boot/initrd.img-2.6.26-2-xen-686 server:/etc/xen/
Finally, the virtual machine now configures the interface from the kernel. Any settings in /etc/network/interfaces seem to muck up the network conectivity of the machine, so comment out all lines in this file that refer to eth0:
# The primary network interface #allow-hotplug eth0 #iface eth0 inet dhcp
Shutdown the virtual machine:
shutdown -h now
If your dom0 server and iscsi target server are different machines, now is the time to copy the /dev/mapper/system-iscsitest onto the iscsi target, ensuring it is the correct size etc. In theory it would be possible to have the iscsi target made available to the virtual machine using an iscsi initiator on the dom0 machine. An exercise for another day, perhaps.
On the dom0 server, modify /etc/xen/xm-iscsitest.cfg, comment out the disk line:
#disk = ['phy:/dev/mapper/system-iscsitest,xvda,w']
And restart the virtual machine:
xm create -c xm-iscsitest.cfg
And hopefully, your server will be running. Look in /dev to ensure that xvda does not exist.
To allow your server to migrate, it is necessary to modify /etc/xen/xend-config.sxp on the dom0 server. Uncomment the following line:
(xend-relocation-server yes)
On both the main dom0 server and the dom0 server on which you wish to migrate the machine to. But be careful, as this may potentially open up a security hole on your servers.
Restart xend on both machines.
/etc/init.d/xend restart
Now, you can migrate your virtual machine. The second server has to be on the same LAN segment as the master server.
xm migrate iscsitest <ip address or hostname of second server>
Users of the virtual machine should notice a very brief pause as the server migrates (basically the time it takes to copy the virtual RAM of the server across your network). The -l options should eliminate any noticable pause, though I found this didn’t work as reliably.
The reason behind clocksource=jiffies
This setting is vitally important on debian systems running with root filesystems on iscsi. This is because the TCP sequence numbers are determined by the system clock. The bug in the lenny xen implementation means that the domU clocks can get out of sync with the dom0 clock and go backwards. This means that your TCP sequence numbers are invalid, and the TCP connection fails. You therefore loose your root filesystem. This setting de-couples the virtual machine system clock from the dom0 sufficiently that the clock does not run backwards, and your TCP connections keep working.




