In KVM, there is option to clone KVM Guest machine. Through which we can duplicate our work that we had completed on parent Guest machine.

In KVM libvirt had virt-clone command-line tool used to clone KVM Guest Machine from existing parent machine. It would be same duplicate copy of parent machine is terms hardware configuration and OS state. There are few items which should which uniqueness will be update to avoid clash between parent and clone machine.

Setup

We used Ubuntu 16.04.3 LTS as Host Machine and CentOS7 Guest machine.

Host Machine Used
#lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.3 LTS
Release:	16.04
Codename:	xenial

Guest srv7 using in this Post
#virsh list
 Id    Name                           State
----------------------------------------------------
 8     srv6                           running
Virt-clone used to clone
#which virt-clone 
/usr/bin/virt-clone

# dpkg -S /usr/bin/virt-clone
virtinst: /usr/bin/virt-clone

There are two methods to clone KVM Guest machine

-- Auto clone : Just need to provide parent Guest machine name, will clone automatically
-- Customize : We could provide settings like Clone Guest machine, file path, networking etc.

Auto Clone

In this type of cloning, we just need to provide parent KVM Guest machine name, this will automatically clone.

Syntax

virt-clone -o <original Guest Machine> --auto-clone

Example

In below example, we are cloning srv7 with auto-clone method

#virt-clone -o srv7 --auto-clone
Allocating 'system-clone.img'      |  20 GB  00:00:11     

Clone 'srv7-clone' created successfully.

In above command we clone srv7 to srv7-clone, Disk file is located in same directory where original file was located

#virsh domblklist srv7-clone
Target     Source
------------------------------------------------
vda        /data/vm_images/CentOS7/system-clone.img


#virsh domblklist srv7
Target     Source
------------------------------------------------
vda        /data/vm_images/CentOS7/system.img

Customize Clone

In this method we could provide user input for Clone Guest Machine settings.

Syntax

virt-clone -o <Original Guest Machine> -n <New Clone Guest Machine> -f <Clone Guest Disk File>

Example

In below example,we would see how we could create clone of existing Guest machine to new with provided name and Disk file path

We should create separate directory to save Disk file path
#mkdir /data/vm_images/centos7-clone-manual

#virt-clone -o srv7 -n srv7-clone-manual -f /data/vm_images/centos7-clone-manual/system.img
Allocating 'system.img'                             |  20 GB  00:00:17     

Clone 'srv7-clone-manual' created successfully.

We have used -n for Clone Guest machine name -f for Disk file path

This clone machine has separate directory, this will easy to relate directory per Guest images.

#virsh domblklist srv7-clone-manual
Target     Source
------------------------------------------------
vda        /data/vm_images/centos7-clone-manual/system.img

Short video for same
--------------------