How To Clone and Use KVM Virtual Machine in Linux

ComputingPost
3 min readSep 30, 2022

--

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.
$ sudo virsh shutdown asterisk-cloud

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

$ sudo 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
$ sudo 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.

See below screenshot:

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

$ sudo 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.

$ 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.

$ sudo 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

$ sudo vi /etc/libvirt/qemu/kamailio-cloud.xml

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

Original was:

To start cloned virtual Machine, just type:

$ sudo 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:

$ uuidgen eth0

29e28904-c8f6-4ac0-93f0-d5d2cedb8555

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:

$ sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0

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:

$ sudo service network restart 

$ sudo systemctl restart 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”.

https://www.computingpost.com/how-to-clone-and-use-kvm-virtual-machine-in-linux/?feed_id=5691&_unique_id=6337447404cc0

--

--

ComputingPost
ComputingPost

Written by ComputingPost

ComputingPost — Linux Howtos, Tutorials, Guides, News, Tips and Tricks.

No responses yet