Citrix Xen: How to "migrate" VHD lv from one XEN to another without export/import but with dd command

root's picture

Citrix Xen has a way to migrate the machines from one host to another via xe vm-export and xe vm-import commands but there are situations when this won't work.
For example, if the source disk is in bad shape and there are bad (unreadable) blocks on your vm's disks, the xe vm-export command will be unsuccessful with the following error:

[root@XenGz ~]# xe vm-export vm=gzlinux filename=./gzlinux.xva
The server failed to handle your request, due to an internal error.  The given message may give details useful for debugging the problem.
message: Unix.Unix_error(13, "read", "")

Well, in this situation, if you do not have any backup, you must do something and one of the options is to use dd which is able to run past the bad blocks with the right options.
Be advised, if you had important information located on the bad blocks, that information is lost from the start. You have this opportunity to save as much as you can and to save the disk as a whole. Also, if you have bad blocks on important system or boot files, your system may not properly run nor even boot.

Before using the dd command, you need a destination LV. You can create it as you want, even manually, but if you want to use it directly as xen disk in a fresh new VM, follow 1.0 first.
If you already have the target lv in good size, just skip to point 1.1.

1.0. How to create VM from OS template via command line:


Note: by default, XEN will create for Debian 8G disk. You can resize it before installation.
[root@XenGz ~]# xe vm-install template="Debian Wheezy 7.0 (64-bit)" new-name-label="gzvirt64"
dfabebc3-7e4a-5f8d-2866-681d6c646909
[root@XenGz ~]# xe vm-disk-list vm="gzvirt64"
Disk 0 VBD:
uuid ( RO)             : 2a4d5c07-3fbe-1ca2-6e8c-0681c155e5db
    vm-name-label ( RO): gzvirt64
       userdevice ( RW): 0

Disk 0 VDI:
uuid ( RO)             : 3950c41a-39ef-439f-bd02-a6e346a60b99
       name-label ( RW): 0
    sr-name-label ( RO): Local storage
     virtual-size ( RO): 8589934592

[root@XenGz ~]# lvscan |grep 3950c41a-39ef-439f-bd02-a6e346a60b99
  inactive          '/dev/VG_XenStorage-2f40bcc0-ced1-c408-4395-b735957cb19e/VHD-3950c41a-39ef-439f-bd02-a6e346a60b99' [8.02 GB] inherit
[root@XenGz ~]# xe vdi-resize uuid=3950c41a-39ef-439f-bd02-a6e346a60b99 disk-size=50GiB
[root@XenGz ~]# lvscan |grep 3950c41a-39ef-439f-bd02-a6e346a60b99
  inactive          '/dev/VG_XenStorage-2f40bcc0-ced1-c408-4395-b735957cb19e/VHD-3950c41a-39ef-439f-bd02-a6e346a60b99' [50.11 GB] inherit

1.1. How to copy with dd the vhd from another XEN installation (hdd mounted as secondary):


List the source and destination:

[root@XenGz ~]# lvscan | egrep "VHD-bef72576-33c0-4fec-9d01-bcb835814301|VHD-3950c41a-39ef-439f-bd02-a6e346a60b99"
  inactive          '/dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-bef72576-33c0-4fec-9d01-bcb835814301' [50.11 GB] inherit
  inactive          '/dev/VG_XenStorage-2f40bcc0-ced1-c408-4395-b735957cb19e/VHD-3950c41a-39ef-439f-bd02-a6e346a60b99' [50.11 GB] inherit

Activate both logical volumes (if the machines are offline, the lvs will be inactive too):

[root@XenGz ~]# lvchange -ay /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-bef72576-33c0-4fec-9d01-bcb835814301
[root@XenGz ~]# lvchange -ay /dev/VG_XenStorage-2f40bcc0-ced1-c408-4395-b735957cb19e/VHD-3950c41a-39ef-439f-bd02-a6e346a60b99

Make sure they are activated:

[root@XenGz ~]# lvscan | egrep "VHD-bef72576-33c0-4fec-9d01-bcb835814301|VHD-9f2898b0-26a2-4a84-aa94-e629824fd9db"
  ACTIVE            '/dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-bef72576-33c0-4fec-9d01-bcb835814301' [50.11 GB] inherit
  ACTIVE            '/dev/VG_XenStorage-2f40bcc0-ced1-c408-4395-b735957cb19e/VHD-9f2898b0-26a2-4a84-aa94-e629824fd9db' [50.11 GB] inherit

Run dd command as follows (please be careful of source and destination - do not mix them up because this command is destructive and without prompt):

[root@XenGz ~]# dd if=/dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-bef72576-33c0-4fec-9d01-bcb835814301 of=/dev/VG_XenStorage-2f40bcc0-ced1-c408-4395-b735957cb19e/VHD-3950c41a-39ef-439f-bd02-a6e346a60b99 conv=sync,noerror
105078784+0 records in
105078784+0 records out
53800337408 bytes (54 GB) copied, 4705.5 seconds, 11.4 MB/s

Deactivate both VHD logical volumes:

[root@XenGz ~]# lvchange -an /dev/VG_XenStorage-2f40bcc0-ced1-c408-4395-b735957cb19e/VHD-9f2898b0-26a2-4a84-aa94-e629824fd9db
[root@XenGz ~]# lvchange -an /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-bef72576-33c0-4fec-9d01-bcb835814301

Now you should have an exact replica of the VHD partition which you can attach it and put it to use on any VM.

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself!
Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.