Pages

Tuesday, March 25, 2014

Migration of Xen Virtual Machines



This is a guide that will help you to transfer Xen PV and Xen HVM virtual servers between nodes
SAFELY. This guide explains the best methods to create backups of Xen VPS, which are also used
in the process of cloning a VPS's filesystem or migrating a VPS to another server.
Please use extreme care when backing up or restoring a customers system, since loss of data is
possible.

1. Backup/Restore Xen PV

First we will disscss about transfering Xen PV Virtual servers.
1.2. A quick glace on Xen PV
Paravirtualization (PV) is an efficient and lightweight virtualization technique introduced by Xen.
PV does not require virtualization extensions from the host CPU and thus enables virtualization on
hardware architectures that do not support Hardware-assisted virtualization. However, PV guests
and control domains require kernel support and drivers that in the past required special kernel
builds, but are now part of the Linux kernel as well as other operating systems.
1.3. General/Primary steps of Migration
1. Make a note of the VM's (Virtual Machine) name and resource details like
RAM/SWAP/DISK-SPACE (We can find this from SolusVM console.

2. Make the below folders for the Xen VPS on the server you are transfering to (i.e Destination
server):
Lets consider we are transfering vm166 ( Vm166: Xen Virtual server ID)
#mkdir /home/xen/vm166
#mkdir /home/xen/vm166/mnt

3. Copy the Xen configuration file from the source server to the destinaton server:

#scp /home/xen/vm166/vm166.cfg root@xx.xx.xx.xx:/home/solusvm/xen/vm166/

4. Check the size of the logical volumes in the source server.

Note: If it's Xen PV, ther will be an _img and a _swap Logical Volume. If it's Xen HVM there will
only be an _img logical volume.
#lvs | grep vm166
We get a similar output like on below:
#lvs | grep vm166
vm166_img Xen
vm166_swap Xen
-wi-ao---- 30.00g
-wi-ao---- 512.00m5. Create logical volumes with the same size on the destination server:
#lvcreate -L 30G -n vm166_img Xen
#lvcreate -L 512M -n vm166_swap Xen

If it's Xen PV, you should format the swap LV now:
#mkswap /dev/Xen/vm166_swap

1.4. Creating backup of XenPV VM
1. Stop the specific VM which you want to take backup. (here vm166)
You can use the below command to stop the VM.
xm destroy vm166
2. Mount the VM's logical volume on the host server:
#mkdir -p /mnt/tmp
#mount /dev/Xen/vm166_img /mnt/tmp
3. Enter into the directory where you mounted the Logical Volume vm166_img and create the
backup:
#cd /mnt/tmp
#tar -czf /home/solusvm/xen/vm101.tar.gz .
Copy this tar file to the destination server.
4. Once the backup is completed, change to /root and unmount the Logical Volume. You can also
start the VPS again at the source server if required.
#cd /root
#umount /mnt/tmp
#xm create /home/xen/vm101/vm101.cfg
1.5. Restoring XenPV VM
1. Ensure the VPS is stoped in the destination server.
2. Format the Logical Volume you are restoring to with the ext3/ext4 filesystem as per the to source
server:
#mkfs.ext3 /dev/Xen/vm166_img
3. Mount the Logical Volume on the destination server:
#mkdir -p /mnt/tmp
#mount /dev/Xen/vm166_img /mnt/tmp4. Locate the backup which is copied from the source server, and run the following to restore the
VM:
#tar xzf /home/solusvm/xen/vm166.tar.gz -C /mnt/tmp

5. Once the backup completes, change to /root and unmount the Logical Volume.

Now you can start the VM in destination server after stoping the VM in the source server.
#cd /root
#umount /mnt/tmp
#xm create /home/xen/vm166/vm166.cfg
Note: Sometimes the name of the Volume Group on the new server may be different to the old
server. If it is different, you need to edit the VPS configuration file
disk
= ['phy:/dev/Xen/vm166_img,hda,w', ',hdc:cdrom,r']

6. After restorion has been completed make sure the VM is online without any problem. After that
you need to update the SolusVM database so the VPS appears on the correct server.
- Login to the solusvm master SSH
- Run /script/vm-migrate
NOTE: Here you must enter the vserverid and the Node ID for the new server.
eg: /scripts/vm-migrate 150 4
vserverid = The number which appears on the left side of the VPS in the solusvm list. It is NOT the
same as the VM name.
nodeid = The number which appears on the left side of the Node list (Under Nodes > List) in
solusvm.
Once that is done, you have migrated the VPS successfully, and can remove the backup files.2.


Backup/Restore Xen HVM
=====================
Here we will discuss about the Migration of Xen HVM VPS.

2.1. A quick glace on Xen HVM

Fully virtualized aka HVM (Hardware Virtual Machine) guests require CPU virtualization
extensions from the host CPU (Intel VT, AMD-V). Xen uses modified version of Qemu to emulate
full PC hardware, including BIOS, IDE disk controller, VGA graphic adapter, USB controller,
network adapter etc for HVM guests. CPU virtualization extensions are used to boost performance
of the emulation. Fully virtualized guests don't require special kernel, so for example Windows
operating systems can be used as Xen HVM guest. Fully virtualized guests are usually slower than
paravirtualized guests, because of the required emulation.

2.2. General/Primary steps of Migration

1. Make a note of the VM's (Virtual Machine) name and resource details like
RAM/SWAP/DISK-SPACE (We can find this from SolusVM console.
2. Make the below folders for the Xen VPS on the server you are transfering to (i.e Destination
server):
Lets consider we are transfering vm166 ( Vm166: Xen Virtual server ID)
#mkdir /home/xen/vm166
#mkdir /home/xen/vm166/mnt
3. Copy the Xen configuration file from the source server to the destinaton server:
#scp /home/xen/vm166/vm166.cfg root@xx.xx.xx.xx:/home/solusvm/xen/vm166/
4. Check the size of the logical volumes in the source server.
Note: In Xen HVM there will only be an _img logical volume.
#lvs | grep vm166
We get a similar output like on below:
#lvs | grep vm166
vm166_img Xen
-wi-ao---- 30.00g
5. Create logical volumes with the same size on the destination server:
#lvcreate -L 30G -n vm166_img Xen2.3. Creating backup of Xen HVM
1. Ensure the VPS is stopped in the source server:
#xm destroy vm166

2. Create a compressed backup with dd & gzip

#dd if=/dev/Xen/vm166_img | gzip > /home/solusvm/xen/vm166.img.gz
Now you have a full back up of VM.

2.4. Restoring XenHVM

1. Ensure the VPS is stopped in the destination server:
2. Restore a compressed backup with gzip. Run the below command:
#zcat /home/solusvm/xen/vm166.img.gz | dd of=/dev/Xen/vm166_img

3. Now you can start the VM in destination server after stoping the VM in the source server.
#cd /root
#umount /mnt/tmp
#xm create /home/xen/vm166/vm166.cfg
Note: Sometimes the name of the Volume Group on the new server may be different to the old
server. If it is different, you need to edit the VPS configuration file
disk
= ['phy:/dev/Xen/vm166_img,hda,w', ',hdc:cdrom,r']

4. After restorion has been completed make sure the VM is online without any problem. After that
you need to update the SolusVM database so the VPS appears on the correct server using.
#/script/vm-migrate

Reference:
http://docs.solusvm.com/xen_migrations
http://wiki.xen.org/wiki/Xen_Overview

1 comments:

  1. What about creating an overview on VM backup with a detailed guide? I think it would be cool

    ReplyDelete