How to clone existing KVM virtual machine images on Linux

I wanted to clone my Debian or Ubuntu Linux KVM VM for testing purpose. How do I clone existing virtual machine images under KVM?

Introduction: You can use a simple command named virt-clone. It is a command line utility for cloning existing virtual machine images using the “libvirt” hypervisor management library. It will copy the disk images of any existing virtual machine, and define a new guest with an identical virtual hardware configuration. Elements which require uniqueness will be updated to avoid a clash between old and new guests.

How to clone existing KVM virtual machine images on Linux

To clone your VM and spawn new instances in KVML
# virt-clone --original {Domain-Vm-Name-Here} --auto-clone
OR
# virt-clone --original {Domain-Vm-Name-Here} --name {New-Domain-Vm-Name-Here} --auto-clone
OR
# virt-clone --original {Domain-Vm-Name-Here} \
--name {New-Domain-Vm-Name-Here} --file {/var/lib/libvirt/images/File.Name.here}

Examples: Cloning Guests with virt-clone

First VM/domain with devices to clone must be paused or shutoff. To gracefully shutdown a domain named ubuntu-box1, run:
$ sudo virsh shutdown ubuntu-box1
OR you can paused it as follows:
$ sudo virsh suspend ubuntu-box1
$ virsh list

Sample outputs:

Domain ubuntu-box1 suspended

 Id    Name                           State
----------------------------------------------------
 1     freebsd                        running
 5     ubuntu-box1                    paused

Let us generate a new guest name, and paths for new storage automatically for a vm called ubuntu-box1
$ sudo virt-clone --original ubuntu-box1 --auto-clone
Sample outputs:

WARNING  Setting the graphics device port to autoport, in order to avoid conflicting.
Allocating 'ubuntu-box-1-clone.qcow2'                                                              |  40 GB  00:00:04     

Clone 'ubuntu-box1-clone' created successfully.

The above command cloned the guest called “demo” on the default connection, auto generating a new name called ubuntu-box1-clone and disk clone path. You can start or resume original domain:
$ sudo virsh start ubuntu-box1
OR
$ sudo virsh resume ubuntu-box1
Next, start ubuntu-box1-clone, enter:
$ sudo virsh start ubuntu-box1-clone
Verify it:
$ virsh list
My dhcpd server gave 192.168.2.147 IP address to ubuntu-box1-clone VM, run:
$ ping -c2 192.168.2.147
Finally, ssh into the box:
$ ssh vivek@192.168.2.147
Sample sessions:

Fig.01 virt-clone command cloned existing virtual machine images

Please note, virt-clone does not change anything inside the guest OS, it only duplicates disks and does host side changes. So things like changing passwords, changing static IP address, ssh-keys, hostnames etc are outside the scope of this tool. Once login using to cloned VM using ssh, you can change those:
$ ssh vivek@192.168.2.147
$ sudo -s
# echo 'ubuntu-box1-clone' > /etc/hostname
# sed -i 's/ubuntu-box1/ubuntu-box1-clone/g' /etc/hosts
# reboot
# /bin/rm -v /etc/ssh/ssh_host_*
# dpkg-reconfigure openssh-server
# passwd vivek

Conclusion

You just learned how to use the virt-clone command which provides a number of options that can be passed on the command line to clone a KVM VM. You can use the virt-sysprep if you need to clone the VM and make/reset anything inside the guest OS:
# virsh suspend ncbz01
# virt-clone --original ncbz01 --name ncbz02 --file /var/lib/libvirt/images/ncbz02-disk01.qcow2
# virsh resume ncbz01
# virt-sysprep -d ncbz02 --hostname ncbz02 --enable user-account,ssh-hostkeys,net-hostname,net-hwaddr,machine-id --keep-user-accounts vivek --keep-user-accounts root --run 'sed -i "s/192.168.122.16/192.168.122.17/" /etc/network/interfaces'

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • miVinayMore Feb 20, 2017 @ 14:52

    Very much interesting article
    Just wanted to clarify does this copy happens bit by bit copy considering data integrity
    Also does the clone data hash value remains same?

    • 🐧 Vivek Gite Feb 20, 2017 @ 15:14

      100% replica or clone as your VM was paused or shutdown. You can recover from bad install or mistakes such as rm -rf /bin/ easily.

  • Paul Mar 27, 2017 @ 20:02

    Very helpful!

    One thing… How did you get the VM IP address?

    • 🐧 Vivek Gite Mar 28, 2017 @ 14:22

      There is a log entry for dhcpd in /var/log/ directory. Another option is arp command.

  • Bobby May 11, 2017 @ 20:28

    Getting error UUID mismatch between and , any idea why? I check libvirt.xml and the uuid’s match

    • Bobby May 12, 2017 @ 13:24

      UUID Mismatch between uuid and sysinfo

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.