Menu

How clone a KVM virtual machine on Ubuntu Server

NOTE: This guide has been tested on Lucid Lynx 10.04 & Karmic Koala 9.10 although it may work equally well on earlier or later version of Ubuntu.

Following on from the guide showing you how to install KVM, one of the things that's very useful when using virtual machines is the ability to clone them.

Before you can clone a virtual machine (vm) you first need to install the relevant package:

sudo apt-get install python-virtinst

Once the package is installed you then need to ensure the machine you are about to clone is shutdown before you attempt to clone it. You can check the state of the machine by issuing the following command:

virsh --connect qemu:///system list --all

This should produce output similar to the following:

$ virsh --connect qemu:///system list --all
    Connecting to uri: qemu:///system
    Id Name State
    ----------------------------------
    2 MediaServer    running
    3 WindowsXP1    running
    4 UbuntuServer   shut off
    .....
    .....

If the machine you wish to clone is currently running then issue the following command to shut it down:

virsh --connect qemu:///system shutdown TheVMYouWishToStop

where TheVMYouWishToStop is the name of the VM you wish to stop. As mentioned in the previous guide, for Ubuntu machines you need to have installed acpid before you can close the machine down gracefully. If acpid is not installed then you can still halt the machine by issuing destroy instead of shutdown. The destroy argument is equivalent to pulling the power cord on the machine so it's quite brutal and can result in data loss and/or corruption. So you really should install acpid!

Once the machine has been shut down you issue the following command to clone it:

virt-clone --connect=qemu:///system -o CurrentVM -n NewVM -f /home/htkh/VMs/NewVM.gcow2

where CurrentVM is the name of the VM you wish to clone, NewVM is the name for the new VM and NewVM.gcow2 is the name of the physical file the VM will be stored in. Be sure to include the full path.

The new cloned machine should work without issue but I did come across a few gotcha's when cloning Ubuntu VMs that you should be aware of:

My cloned Ubuntu VM is not visible on my network

Although the original VM was able to see the network and was visible to other clients on the network the cloned VM was not. The clone would not get an IP address from my router. Even assigning a static IP address did not help. There is a workaround tho: You firstly need to gain console access to your cloned VM. This is simple if you've installed Virtual Machine Manager on your host. After gaining console access simply issue the following command:

sudo rm /etc/udev/rules.d/70-persistent-net.rules

Then reboot the VM (reminder: sudo reboot -h now) and you should now have full network access to and from your cloned VM.

My cloned Ubuntu VM is conflicting with the original VM on my network

When you clone a VM you need to update the hostname for the cloned machine. If you don't then your network may act strangely. So change the hostname by updating the following two files on your cloned VM to give your new VM a unique hostname:

sudo vim /etc/hosts

sudo vim /etc/hostname

My cloned Ubuntu VM keeps giving me a mountall:cancelled error

If you clone an Ubuntu VM that is running NFS then you will receive the following error each time you try and edit a file on the cloned machine:

mountall:cancelled General error mounting filesystems. A maintenance shell will now be started. CONTROL-D will terminate this shell and re-try

Unfortunately I've not found a way to work round this problem AFTER you've cloned your VM. However, if you un-mount the NFS exports on the donor machine BEFORE cloning it then you will not get this error. Once you've cloned the machine simply re-mount the exports on both the original VM and the cloned VM. You can un-mount the NFS export by editing the /etc/fstab file and masking out the relevant exports/mounts:

sudo vim /etc/fstab

Once you've cloned the machine then simply unmask the exports/mounts on both machines.

Still stuck? Not what you were looking for? Then head over to the Discussion Forum