Xen - Creating images and templates with LVM volumes

No votes yet

When using XEN virtualization it's good practice to use LVM volumes as raw disk devices for the vm guests. The main advantage is that there is no file system for the Xen host to manage and the guest has direct access to the physical volume = better performance!
Another advantage is that you can leverage LVM snapshots adding a similar function to your Xen setup as known in VMWare.

One drawback when using LVM for your virtual guests is that the vm's disk is less portable. There are tools that can handle LVM imaging, but dd is the OSS tool giving you a 1:1 copy of your disk. It's free & it's proven.
dd's known drawback is that the dd dump files get big and time to backup/restore a LVM volume can be lengthy.

Here's how to speed things up and also save on space needed for your LVM images & backups by combining dd with gzip. On modern hardware you can get speeds up to ~90Mb/sec - meaning you can restore a template or image of 15 GB in 3 to 4 minutes.

NOTE: With big data partitions containing a small percentage of data (25% or less of the disk total), it's better to use traditional backup methods. Here dd will lose you precious time backing up parts that contain no real data.

To create an image from the LVM volume:

NOTE: Always make sure your guest is shutdown to ensure data integrity.

dd if=/dev/[lvmsystem]/[lvmpartition] bs=64k | gzip -c > /[path for your image file]/[machine_disk name].img.gz

This command tells dd to use your LVM volume as input using a block size of 64k, the pipe then hands this input stream over to gzip and that zips the stream to the given img.gz file.

* If your source LVM volume contains errors due to disk failure or other, you can tell dd to copy all parts of the disk it can acces. To do this add options conv=sync,noerror before the bs=64k statement.

Restore and Image / Template to an LVM volume:

*If necessary first create a LVM volume where you want the copy written back. It must be at lease the same size (or bigger) than the original LVM volume where the image was taken from.

gunzip -c /[path containing your image file]/[machine_disk name].img.gz | dd of=/dev/[lvmsystem]/[lvmpartition] bs=64k

Any way to mount the image?

Is there anyway to mount the image that was create?

re: Any way to mount the image?

Seeing the zipped file contains a disk dump, there is no way I know of to mount the image directly. Could be there are tools out there for the task.

The way to do it in this case:
If you want to edit the image contents, you have the option to create a new (temporary) LVM volume and write back the dd image to the LVM volume. After having dumped the image back to the LVM volume, you can mount it and edit the contents (or boot it up as VM - of course be careful with duplicate ip assignments).

If you want to save the modified LVM contents as the new state, just dump back the LVM volume to a new image file.

It may not be as instant as a tool like Ghost Explorer or such, but just as powerful as you also have the added option to maintain your templates by adding the latest updates and so forth.

After having done your maintenance you can simply remove the temporary LVM volume you created.