Home / Business / How To Clone and Use KVM Virtual Machine in Linux

How To Clone and Use KVM Virtual Machine in Linux

The most important and most used feature in world of Virtualization is the ability to clone a VM,take snapshot of the current state of a VM, Live migrate a VM, e.t.c.
Lately i have been playing with KVM and Openstack Cloud computing platform.At times you have to clone a VM to use it for a different project and in this small guide, i will show you how to do it using virt-clone command.
virt-clone is a command line tool used for cloning existing virtual machine images  that uses libvirt as hypervisor management library.
Follow steps below to clone a vm on KVM.

  1. Make sure the VM you wish to clone is powered off.Use command below, just replace “asterisk-cloud” with your vm name.
# virsh shutdown asterisk-cloud

You can get a list of Virtual Machines being managed by virsh using virsh list –all command:

# root at controller in ~ [22:06:29]
→ virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     asterisk-cloud                 shut off
 -     instance-00000004              shut off 

  1. Clone the Virtual Machine. Here i want to clone asterisk-cloud virtual Machine to kamailio-cloud virtual machine.Below is the code to enter
# root at controller in ~ [17:42:47]
→ virt-clone 
--connect=qemu:///system 
--original asterisk-cloud 
--name kamailio-cloud 
--file /var/lib/libvirt/images/kamailio-cloud.qcow2

Output is like below:

Allocating ‘kamailio-cloud.qcow2’ | 8.0 GB 00:01:21
Clone ‘kamailio-cloud’ created successfully.


If you’re connection to remote KVM/QEMU Host machine, put url before  /system.It will look something like:

→ virt-clone 
--connect=qemu://192.168.1.30/system 
--original asterisk-cloud 
--name kamailio-cloud 
--file /var/lib/libvirt/images/kamailio-cloud.qcow2

asterisk-cloud: Name of VM cloning from
kamailio-cloud: Name given to resulting VM after cloning
kamailio-cloud.qcow2: Image saved that kamailio-cloud boots from.
Check to confirm that kamailio-cloud.qcow2 file is successfully stored in /var/lib/libvirt/images folder.

# root at controller in ~ [17:45:59]
→ ls /var/lib/libvirt/images 
total 6946188
-rw-r--r-- 1 root root 3238789120 Mar 20 17:42 asterisk-cloud.qcow2
-rwxr-xr-x 1 qemu qemu 666894336 Mar 16 04:14 CentOS-7-x86_64-Minimal-1503-01.iso
-rw-r--r-- 1 root root 3207331840 Mar 20 17:44 kamailio-cloud.qcow2

If you now run the command virsh list –all, you should see cloned vm present in the list.

# root at controller in ~ [17:49:48]
→ virsh list --all
Id Name State
----------------------------------------------------
- asterisk-cloud shut off
- instance-00000004 shut off
- kamailio-cloud shut off 

Before starting the kamailio-cloud, i have to change channel source mode path from original. DO this by editing the file /etc/libvirt/qemu/kamailio-cloud.xml

┌── root at controller (~)
└─>vi /etc/libvirt/qemu/kamailio-cloud.xml

Look for the line below and change original domain name to point to new clone.

<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-kamailio-cloud/org.qemu.guest_agent.0'/>

Original was:

<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-asterisk-cloud/org.qemu.guest_agent.0'/>

To start cloned virtual Machine, just type:

 # virsh start kamailio-cloud --console

After logging in, i’ll have to change uuid for network interface. The one on My CentOS 7.x Guest machine is eth0. To generate new uuid for this interface, just type:

My original eth0 configuration file from asterisk-cloud vm looked like below:

TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=230e87b1-f847-4cab-86b9-df4268a0a0a3
DEVICE=eth0
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

I will change uuid replacing old with generated one:

Modify below configurations to fit your use cases.

TYPE=Ethernet
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=29e28904-c8f6-4ac0-93f0-d5d2cedb8555
DEVICE=eth0
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes 

The restart your network service:

That’s all. You should have a running/working cloned Virtual Machine. You can also do this form graphical interface using “Virt Manager”.

About freetutorial

Check Also

How To Start a Profitable Consulting Business

FacebookTwitter How would you like to start your very own profitable Consulting Business and land your first …

Leave a Reply

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