Skip to content

Category: Virtualization

Live P2V migration, on the cheap

Last week I performed a live physical to virtual migration for a customer. Ghetto style.
I was actually surprised by its simplicity and how well it turned out in the end.

Basically what I did was boot the target server to a live environment in order to be able to have raw access to the disk. In this case I’ve used FAI’s Sysinfo mode, but booting any Linux Live CD would have worked equally well. After it’s booted I’ve created the the necessary partitions, created filesytems and mounted them. When those steps were done it was time to sync data and make the virtual system bootable.

Usually I would advise against this kind of migration but in this case there was only static data involved.
It worked out well for me, but your mileage may vary.

Alright here’s what I did step by step…

1. Backup partition layout and MBR

source:~ # sfdisk -d /dev/sda > /tmp/source-partitions
source:~ # dd if=/dev/sda of=/tmp/source-mbr.img bs=512 count=1
source:~ # scp /tmp/source-* target:/tmp/

2. Restore partition layout and MBR

target:~ # dd if=/tmp/source-mbr.img of=/dev/sda bs=446 count=1
target:~ # sfdisk /dev/xvda < /tmp/source-partitions

3. Create a filesystem on each partition

target:~ # for partition in xvda1 xvda6 xvda7 xvda8 xvda9 xvda10; do mkfs.ext4 /dev/$partition; done

4. Create a swap space

mkswap /dev/xvda2
swapon /dev/xvda2

5. Mount the newly created partitions

target:~ # mount /dev/xvda1 /mnt/tmp/
target:~ # mount /dev/xvda6 /mnt/tmp/var/
target:~ # mount /dev/xvda7 /mnt/tmp/tmp/
target:~ # mount /dev/xvda8 /mnt/tmp/usr
target:~ # mount /dev/xvda9 /mnt/tmp/home/
target:~ # mount /dev/xvda10 /mnt/tmp/data/

6. Synchronise data from source to target

target:~ # for i in $(seq 1 10);
rsync -av --delete --progress \
--exclude=/dev \
--exclude=/sys \
--exclude=/proc \
--exclude=/mnt/ \
--exclude=/media/ \
source:/* /mnt/tmp/;
target:~ #

Repeat this step until you’re confident nothing has changed anymore.

7. Enter the synchronized environment

target:~ # mount -t proc none /mnt/tmp/proc
target:~ # mount --rbind /dev /mnt/tmp/dev

target:~ # chroot /mnt/tmp /bin/bash
chroot:~ # source /etc/profile

8. Fix fstab entries

(only for Xen vm’s)

chroot:~ # sed -i 's+/dev/sda+/dev/xvda+g' /etc/fstab
chroot:~ # vim /etc/fstab

9. Fix network settings

chroot:~ # vim /etc/network/interfaces

10. Make the system bootable

chroot:~ # grep -v rootfs /proc/mounts > /etc/mtab
chroot:~ # grub-install --no-floppy /dev/xvda
chroot:~ # update-grub

Once you’ve run the above commands it’s time to exit the chroot, unmount all partitions and reboot
into your migrated environment. Enjoy!

Install Xen and libvirt on Debian Lenny

This should be an easy to follow guide about how to install Xen on Debian 5. You should be able to copy/paste most parts of on your shell. Please run this only on a clean and up-to-date Debian system.

Alright let’s get to it quick ‘n dirty.

First of all make sure your Debian install actually is up-to-date:

# apt-get update; apt-get upgrade

Let’s see which Xen kernel images are available and pick the most recent one to install:

# apt-cache search xen | grep image | awk '{print $1}'
# apt-get install `apt-cache search xen-linux-system \
| sort | tail -1 | awk '{print $1}'`

Once this is done reboot your system, login again and run:

# uname -a
Linux elysium 2.6.26-2-xen-amd64 #1 SMP Thu Feb 11 02:57:18 UTC 2010 x86_64 GNU/Linux

As you can see, the system is running kernel 2.6.26-2 with the xen-amd64 patch set.
As of now we should have Dom-0 available:

# xm list
Name                   ID   Mem VCPUs      State   Time(s)
Domain-0               0  3885     2     r-----      8.5


Let’s move on to the network. By default there is no bridge available from the virtual machines towards the external network. It’s fairly easy to accomplish though:

# vim /etc/xen/xend-config.sxp

Look for the following line and uncomment it:

(network-script network-bridge)

And while we’re editing the xend-config.sxp file, change the following line:

(xend-unix-server no)


(xend-unix-server yes)

Be sure to reload the new settings:

# /etc/init.d/xend restart

If you don’t edit this line or if you don’t reload, you obviously won’t be able to install or manage your virtual machines. You’d get to see errors like this:

ERROR    internal error failed to connect to xend
Traceback (most recent call last):
  File "/usr/bin/virt-install", line 693, in
  File "/usr/bin/virt-install", line 508, in main
    conn = cli.getConnection(options.connect)
  File "/var/lib/python-support/python2.5/virtinst/", line 123, in getConnection
  File "/usr/lib/python2.5/site-packages/", line 140, in open
    if ret is None:raise libvirtError('virConnectOpen() failed')
libvirtError: internal error failed to connect to xend

There are more options available to connect to xend.: xend-http-server, xend-tcp-xmlrpc-server, xend-unix-xmlrpc-server, xend-relocation-server.

The reason why I chose xend-unix-server over anything else is pretty straight forward. It only listens on the Unix socket layer which doesn’t need any other networking protocol to operate. You could compare it to connecting to localhost, but without the need for a networking device (e.g. lo0).

If you want to be able to manage this Xen server from a central node, be sure to change the appropriate management protocol. It’s beyond the scope of this howto, but I might post an howto on this subject later on this blog.

On to installing a virtual machine!

I for one am a big fan of abstraction layers. It makes life for a SysAdmin or DevOp so much easier if you’re able to run the same commands on different operating systems, platforms or architectures in general. That’s why I use libvirt. It’s a collection of libraries and tools that can be used to deploy virtual machines on different types of virtualization systems. Including but not limited to Xen, KVM and Qemu.

It’s also available as a Debian package, so installing it is very straight forward:

# apt-get install libvirt-bin virtinst

Once this is done we can install our first virtual machine:

# virt-install \
--name=test-debian-install \
--ram=1024 \
--file-size=10 \
--nographics \
--paravirt \
--file=/var/lib/xen/images/test-debian-install.img \

It’s probably a good idea to store this as a shell script on your Xen host for future reference.
You should see a familiar installer within seconds after invoking the command.

Once the install has completed you should be greeted with your new virtual machine’s login prompt:

Debian GNU/Linux 5.0 test-debian-install hvc0

test-debian-install login:

To exit your virtual machine’s console, simply press Ctrl-]