Duplicating and Backing Up LVM Backed Xen DomU from a Remote Server
LVM’s snapshot feature enables us to duplicate an LVM backed Xen DomU in seconds inside of the same volume group. When we want to duplicate LVM backed Xen DomU from a remote server, we need to make use of LVM’s snapshot function and tools like scp and dd. Backing up the DomU is only part of the process of duplicating one DomU. We can finish the backing up work after copying DomU’s image to remote server. How to duplicate and back up LVM backed Xen DomU from remote server is introduced in this post.
Assumption and our goal:
There are Xen DomU virtual machines in LVM logical volume /dev/vg_xen/vm-10.0.0.123/ on server 10.0.0.10 (remote server).
Now we want to duplicate the virtual machine vm-10.0.0.123 to vm-10.0.0.124 which we want to stored in logical volume vm-10.0.0.124 in volume group vgxen on server 10.0.0.11 (local server). vm-10.0.0.124‘s IP will be 10.0.0.124. There are 6 steps to duplicate this virtual machine. If we just need to back up it, we can just use step 1 to setp 3.
1. Duplicate the LVM logical volume on remote server
We can duplicate the LVM volume group on remote server by making a snapshot of it. The snapshot is a clone of the logical volume. We can make a new logical volume vm-10.0.0.123-snapshot as the snapshot of logical volume vm-10.0.0.123:
# lvcreate -L20480 -s -n 'vm-10.0.0.123-snapshot' /dev/vgxen/vm-10.0.0.123
Here the size of the new logical volume is 20G as the same of the original one.
Here we should make sure that vm-10.0.0.123 is power off to avoid the situation there are “write” that is still in DomU’s cache.
2. Save the snapshot to image file using dd tool on remote server
We can use dd command to save a image of the new created snapshot logical volume of the DomU:
# dd if=/dev/vgxen/vm-10.0.0.123-snapshot of=/lhome/xen-image/vm-10.0.0.123-lv.img bs=1k
This process may take several minutes or more depending on the size of the logical volume and the hard disk’s speed.
3. Copy the DomU’s image to the local server from the remote one
We can transfer the image from remote server after finishing the dd command on remote server.
# scp -c arcfour firstname.lastname@example.org:/lhome/xen-image/vm-10.0.0.123-lv.img /lhome/xen-image/
We set “-c arcfour” to get higher transfer speed of scp.
4. Create logical volume for the new DomU on local server
We can do this simultaneously with step 3. We create a new logical volume vm-10.0.0.124 under volume group vgxen on local server:
# lvcreate -L20480 -n 'vm-10.0.0.124' vgxen
The size of the logical volume is the same as the one of the DomU in remote server.
5. Duplicate the data in the logical volume from the image
In this step, we use the dd command to copy data from the image file to the newly created logical volume:
# dd if=/lhome/xen-image/vm-10.0.0.123-lv.img of=/dev/vgxen/vm-10.0.0.124 bs=1k
This may also take some time to finish.
6. Change the profile for new DomU on local server
After making the clone of the VBD. We can create a profile for the new DomU. This is the content of /lhome/xen/vm-10.0.0.124/vm.run:
name="10.0.0.124" cpus=2 memory=2048 disk=['phy:vgxen/vm-10.0.0.124,xvda,w' ] vif=[ 'bridge=eth0' ] bootloader = "/usr/bin/pygrub" onreboot = 'restart' on_crash = 'restart'
The name and disk entry are changed.
7. Start the new virtual machine and configure the new virtual machine
We can start the new DomU and enter it’s console:
# xm create /lhome/xen/vm-10.0.0.124/vm.run # xm console 10.0.0.213
# xm create -c /lhome/xen/vm-10.0.0.124/vm.run
After logging in vm-10.0.0.124, we can edit the network configuration file:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Change the IPADDR to 10.0.0.124 from 10.0.0.123. Then restart eth0:
# ifdown eth0 # ifup eth0
Make sure this interface doesn’t have HWADDR by commenting out the line that specifyHWADDR if we use Xen bridge network.
Log out of vm-10.0.0.124 and then use “Ctrl + ]” to exit the xm console.
The new virtual machine vm-10.0.0.124 which is the clone of vm-10.0.0.123 except the IP and the Dom0 on top of which it is running is ready to use now. This process can be written into one script.