Monday, November 17, 2008

KVM virt-clone

One of the things I love about Vmware is the ability to deploy a VM from a template. Linux has never had the same capacity, but there is a clone feature. Just as with Vmware, the original VM must be powered off. This is because we can't copy and image file if the VM is making live changs to the image.

Unlike Vmware, we have the advantage of being able to use Logical Volumes in Linux, rather than image files. An LVM image provides better performance that a flat file because we are "closer to the bare metal" of the drive, rather than inside a file system. We can resize an LMV image using native Linux commands, rather than using a third party tool. Unfortunately, this mean cloning requires one extra step: we have to preallocate the LogVol. That should take about 10 seconds.
[root@adama ~]# lvcreate -L 5G img -n vm06f8
  Logical volume "vm06f8" created
[root@adama ~]# virt-clone -o vcl02 -n vcl03 \
  -f /dev/img/vm06f8 --force

Cloning from /dev/img/vm05f8 to /dev/img/vm06f8
Cloning domain... |         5.0 GB     02:55
In this example, we created the image LogVol, /dev/img/vm6f8, and replicated Dom vcl02 as vcl03. Since we were using a preallocated volume, we needed the --force. (No, it is not smart enough to make the the LogVol. Yes, I tried.)

Our big problem now is that both VMs have the same hostname and IP address. Since vcl02 is down, we can bring vcl03 up, and make the changes with out an conflicts. If... You had used LVM inside vcl02, and you used a different VG name than on Dom0, then you could use kpartx to mount the root filesystem, and change the config files directly.


  1. do both lvm need to have the same size?

  2. I never tried, but I'm sure the clone could be bigger. Any unused space would appear to fdisk as unpartitioned. Making the clone smaller would be hit or miss, but could cause problems for the clones volume groups.

    At this point, I'm working more with Citrix Xenserver.

  3. If you have differents sizes they "destroy" destination image and create a new one with same size of source

  4. I clone the KVM VM using the following but it didn't give me the network interface on the destination

    virt-clone --original sl6-2 \
    --name sl6-3 \
    --file /dev/mapper/vg_kvm-kvm5 --prompt

    eth0 interface does not exists in sl6-3 I do not know how to fix the issue.

  5. question... what if I have two lvm's for every Xen machine, for ex, storage & swap. How do I clone using two different storage devices?