Cloning a KVM Virtual Machine


This is a guide on copying the base 14.04 Ubuntu image to another machine to use. Hope fully all that is needed it to create the new file system, and change the hostname.

Creating the file system

Firstly, I create the file system the machine would use, on the existing LVM partitions, make this the same size as the base 1404 (can resize it later if needs be):

$ sudo lvcreate -L 5G -n mogile1 vhosts
Logical volume "mogile1" created

Copying the base1404 image to mogile1

Make sure that the base 1404 server isn’t running (not listed in virsh list), and then copy the drive over. (Block size of 64k, seems a good bet for copying)
$ sudo dd bs=64k if=/dev/vhosts/base-1404 of=/dev/vhosts/mogile1

Creating the virtual host

Next to make the copy. I copied the /etc/libvirt/qemu/base-1404.xml to /tmp/mogile1.xml and changed the following lines:

<source dev='/dev/vhosts/mogile1'/> (within <disk type=’block’> section)
<mac address='52:54:00:62:3d:9a'/>

Make sure that the uuid is only HEX digits and letters (0-9 a-f).You can change the ram or CPUs, etc as needed. (uuidgen is a good command for making a new UUID)

Once you have created the xml you can use the following commands to create, and then start the domain:

$ virsh define mogile1.xml
Domain mogile1 defined from mogile1.xml
$ virsh dumpxml mogile1

You can then start it up with:
$ virsh start mogile1

Remember to change the hostname in /etc/hostname and /etc/hosts after reboot, and all will be good. Although you could do this on the copy of the hard drive before you boot it up. See the how to mount partitions guide for information on this



Leave a Reply

Your email address will not be published. Required fields are marked *