Migration OpenVZ to Xen (Debian)

I recently migrated my servers from Debian squeeze with OpenVZ to Debian 7 with Xen. The reason is that debian wheezy doesn’t contain any packages of OpenVZ in the official repositories.
So I decided to migrate to Xen.

The steps to do that are:

  1. Save container in image files
  2. Install operating system (Debian)
  3. Install virtualization (Xen)
  4. Copy image files into newly created virtual machines
  5. Prepare vms

Here are the detailed steps:

1 Create image files
You have to create a image file for each container you have. The following code creates a 1GB image file:
dd if=/dev/zero of=~/tmp/image.img bs=1M count=1024
Change the count number and the file will be smaller or greater. At least the image file should be as large as your container.
To get the size of your container you can do:
du -sh /vz/root/$VEID/

Next you have to format your image file:
/sbin/mkfs.ext3 /tmp/image.img

and mount it in your file system:
mount -t ext3 -o loop /tmp/image.img /media/Imagemount

This will copy all files from container to your image file (-H is for hardlinks)
rsync -aAXHv --delete /vz/root/$VEID/* /media/Imagemount

2 Install operating system (Debian)
2.1 Format disk
If you are not able to install your debian normally, cause you have no physically access to the server, the next steps will prepare the installation with a rescue system:
First you have to boot into rescue system.
Next you will erase your whole disk and create new partitions:
fdisk /dev/sda

We want to create for partitions:

  1. Boot partition (300 MB)
  2. Data partition (20 GB)
  3. Swap space (double size of your ram)
  4. LVM space for your virtual machines

To change the type of a partition to swap you have to enter „t“ and set the partition id to „82“ (partition id „8e“ for LVM).
Save your changes with „w“.

2.2 Install raid
This chapter is only neccessary if you want to install a RAID 1 (mirror).

Clone partition table to /dev/sdb
sfdisk -d /dev/sda | sfdisk --force /dev/sdb

Create raids
apt-get install mdadm
Create a raid for each partition:
mdadm -v --create /dev/md0 --level=mirror --raid-devices=2 /dev/sda1 /dev/sdb1
mdadm -v --create /dev/md1 --level=mirror --raid-devices=2 /dev/sda2 /dev/sdb2
mdadm -v --create /dev/md2 --level=mirror --raid-devices=2 /dev/sda3 /dev/sdb3
mdadm -v --create /dev/md3 --level=mirror --raid-devices=2 /dev/sda4 /dev/sdb4

Wait until synchronisation finished
watch cat /proc/mdstat

2.3 Format partitions
Create swap partition
mkswap /dev/md2

Format boot partion
mkfs.ext3 /dev/md0

Format data partition
mkfs.ext4 /dev/md1

Create lvm volume
apt-get install lvm2
pvcreate /dev/md3
vgcreate vg0 /dev/md3

Mount data partition and create main folders
mount /dev/md1/ /mnt/
cd /mnt
mkdir etc
mkdir boot
mkdir var
mkdir usr
mkdir tmp
mkdir home

Mount boot partition into filesystem
mount /dev/md0 boot/

Edit fstab file to mount partitions on boot:
vi /mnt/etc/fstab
Insert the following lines:
proc /proc proc defaults 0 0
/dev/md0 /boot ext3 defaults 0 2
/dev/md1 / ext3 defaults 0 1
/dev/md2 none swap defaults,pri=1 0 0

2.3 Installation of debian
Install debootstrap
apt-get install debootstrap
Download all sources from ftp.de.debian.org for debian wheezy and install it to /mnt/
debootstrap --arch amd64 wheezy /mnt/ http://ftp.de.debian.org/debian

Prepare to chroot into new debian
mount -t proc none /mnt/proc
mount -o bind /dev /mnt/dev
mount -o bind /sys /mnt/sys

chroot into system
chroot /mnt

Restore network settings.
vi /etc/network/interfaces

Set host name
vi /etc/hostname

Update system
apt-get update
apt-get install mdadm openssh-server lvm2 locales grub2 console-data

Set locales
dpkg-reconfigure locales

Installation of kernel
apt-get install linux-image-amd64

Installation of grub
grub-install --no-floppy /dev/md0

Change password with passwd

Reboot the system

3 Install virtualization (Xen)
3.1 Prepare OS for xen
apt-get install bridge-utils

Create bridge interface
vi /etc/network/interfaces

You have to edit the lines with your internet connection interface
auto eth0
iface eth0 inet static

To something like that:
auto eth0
iface eth0 inet manual
auto xenbr0

iface xenbr0 inet static
bridge_ports eth0

3.2 Installation of xen
apt-get install xen-linux-system

Change boot order
dpkg-divert --divert /etc/grub.d/08_linux_xen --rename /etc/grub.d/20_linux_xen

Reboot machine to run debian with debian-xen kernel

Install xen tools
apt-get install xen-tools

4 Copy image files into newly created virtual machines
You have to create your vms. The command for this is

After your vm has been created you have to mount the disk to copy your files in it
mount /dev/vg0/test-disk /mnt/test-disk

Next you can copy the files from your image file to your virtual disk
rsync -aAXHv /path/to/your/mounted/image/file/* /mnt/test-disk/

Now you have to make a few changes:
Edit fstab to mount your disk.
vi /etc/fstab
The following lines should be fine for standard configuration:
proc /proc proc defaults 0 0
none /dev/pts devpts mode=0620 0 0
/dev/xvda1 none swap sw 0 0
/dev/xvda2 / ext4 noatime,nodiratime,errors=remount-ro 0 1

Edit inittab to load a console after boot
vi /etc/inittab
Add this line to the end:
1:2345:respawn:/sbin/getty 38400 hvc0

Now you can unmount (umount) your disk.

Edit the xen configuration file of your vm to add the bridge as your network interface.
vi /etc/xen/test.cfg
Change the line
vif = [ 'mac=xx:xx...xx']
to something like that
vif = [ 'mac=xx:xx...xx,bridge=xenbr0']

That’s it 🙂

Try to boot your vm with:
xm create -c /etc/xen/test.cfg

Now you’re ready to have fun with Xen 🙂

If you want to add a internal network (for example for all your virtual machines) you have to add a new bridge:
vi /etc/network/interfaces
Add these lines:
auto eth0.100
iface eth0.100 inet manual

auto xenbr1
iface xenbr1 inet static
bridge_ports eth0.100

Edit your vm cfg
vi /etc/xen/test.cfg
Add a new interface
vif = [ 'mac=xx:xx...xx,bridge=xenbr0', 'mac=xx:xx...xx,bridge=xenbr1']

Reboot your vm and add the new interface in your /etc/network/interfaces
vi /etc/network/interfaces
auto eth1
iface eth1 inet static

Sources (English and German):

Getagged mit: , , , , , ,