Usually the purpose of cloning a KVM guest OS is to have it use the same binaries and configuration as the parent, but allow it to run as an independent entity.
When first cloned, the whole guest OS is cloned including host name, network settings, etc. and for those reasons you cannot run the parent and the clone at the same time.
In this article, I will describe the minimal steps required to give the clone its own identity and able to run as a host independent of the original parent.
Prerequisites
Create an Ubuntu guest OS virtualized using KVM, here is an article describing how to create a guest OS named ukvm1404.
Identify parent VM and its disk files
If you followed my previous article, you will be using a VM named “ukvm1404”. We will continue its use in these instructions.
$ virsh list Id Name State --------------------------- 97 ukvm1404 running
Identify the disks associated with the guest OS.
$ virsh dumpxml ukvm1404 | grep "source file" <source file='/data/kvm/pool/ukvm1404.img'/>
Then gracefully stop the VM before we start cloning it.
$ virsh shutdown ukvm1404
Clone the VM and disks
Now we clone the VM using the “–auto-clone” switch.
$ virt-clone --original=ukvm1404 --name=uclone1 --auto-clone
If you want to manually set the path and name of the newly cloned disks, then instead of using “–auto-clone”, instead you would supply as many “–file” arguments as there are disks (e.g. “–file /path/disk1.img –file /path/disk2.img”).
Checking the results of the cloning, we can see the new VM has a new disk image.
$ virsh dumpxml uclone1 | grep "source file" <source file='/data/kvm/pool/uclone1.img'/>
Start new VM
Doing another full listing, we can see our original “ukvm1404” is in the paused state, and “uclone1” is still powered off.
$ virsh list --all Id Name State ---------------------------------------------------- 97 ukvm1404 paused - uclone1 shut off $ virsh start uclone1
At this point, if you did a ‘virsh edit uclone1’ and ‘virsh edit ukvm1404’, you would see that they both have unique uuid and MAC address on their network interface. The hardware is taken care of by the cloning, but you still have the OS level fixing.
Fix OS settings of clone
Now login to the console of the cloned host, and change anything that tied it to the old hostname. I cannot tell you how to reconfigure your applications, but at the very least change the hostname, hosts file, any static networking settings, and create a fresh set of keys for ssh – then restart the host.
$ sudo vi /etc/hostname $ sudo vi /etc/hosts # on older versions of Ubuntu and RHEL variants $ rm /etc/udev/rules.d/70-persistent-net.rules $ sudo vi /etc/network/interfaces # on newer versions of Ubuntu with systemd networking # remove cloned ids used as DHCP identifier sudo rm /etc/machine-id /var/lib/dbus/machine-id # recreate ids unique to guest sudo dbus-uuidgen --ensure && sudo cp /var/lib/dbus/machine-id /etc/. $ sudo rm /etc/ssh/ssh_host_* $ sudo dpkg-reconfigure openssh-server $ sudo reboot
Restart parent host
Now that the cloned host has its own identity, it should be safe to bring the parent back up from a paused state.
$ virsh start ukvm1404
Validate independence
Now that both the parent and clone VM are running, check the independence of both by running “hostname” and checking the settings in “ifconfig”.
A VM with a single DHCP ethernet interface is going to clone easily, you will have to work a little harder on fixup when you have multiple interfaces, static IPs, etc..
REFERENCES
https://ubuntuforums.org/showthread.php?t=2376922
http://www.havetheknowhow.com/Configure-the-server/KVM-clone-a-vm.html
https://computingforgeeks.com/how-to-clone-and-use-kvm-virtual-machine-in-linux/
https://www.cyberciti.biz/faq/how-to-clone-existing-kvm-virtual-machine-images-on-linux/ (reconfigures ssh)
http://koo.fi/blog/2010/11/28/cloning-ubuntu-1004-server-kvm-guests-efficiently/ (ssh keys)
https://blog.digitalocean.com/avoid-duplicate-ssh-host-keys/ (ssh key regen)
http://koo.fi/blog/2010/11/27/serial-console-for-ubuntu-server-1004-kvm-guests/
https://superuser.com/questions/958739/create-linked-clone-or-layered-shared-disks-with-qemu (linked clones with qemu qcow2 format, copy-on-write)
https://forums.unraid.net/topic/48837-creating-linked-clones/ (example ‘qemu-img create command…’)
http://www.greenhills.co.uk/2013/03/24/cloning-vms-with-kvm.html (write up of thin provisioning with qcow2)
70-presistent-net.rules on Ubuntu
changing machine-id to ensure unique DHCP lease on Ubuntu with systemd
systemd uses different method to generate DUID, which is why we must use machine-id
NOTES
Instead of recreating ‘machine-id’, can also configure /etc/netplan/01-netcfg.yaml to use MAC with ‘dhcp-identifier: mac’
network: version: 2 renderer: neworkd ethernets: ens3: dhcp4: yet dhcp-identifier: mac