How to clone and use KVM virtual machines in Linux

The most important and commonly used functions in the virtualization world are functions such as cloning a virtual machine, taking a snapshot of the current state of the virtual machine, and migrating the virtual machine in real time.
Recently I have been using KVM and Openstack Cloud computing platforms. Sometimes you have to clone a VM to use it for other projects, and in this little guide I will show you how to do it with the virt-clone command. virt-clone is a command line tool for cloning an existing virtual machine image using libvirt as the hypervisor management library.
Follow these steps to clone a virtual machine on KVM.

  1. Ensure that the virtual machine to be cloned is powered off. Using the following command, simply replace “asterisk-cloud” with your virtual machine name.

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%23%20virsh%20shutdown%20asterisk-cloud%0A”/]

You can use the virsh list –all command to get a list of virtual machines managed by virsh:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%23%20root%20at%20controller%20in%20~%20%5B22%3A06%3A29%5D%0A%E2%86%92%20virsh%20list%20–all%0A%C2%A0Id%C2%A0%C2%A0%C2%A0%20Name%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20State%0A—————————————————-%0A%C2%A0-%C2%A0%C2%A0%C2%A0%C2%A0%20asterisk-cloud%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20shut%20off%0A%C2%A0-%C2%A0%C2%A0%C2%A0%C2%A0%20instance-00000004%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20shut%20off%C2%A0″/]

  1. Clone the virtual machine. Here I want to clone the asterisk-cloud virtual machine to kamailio-cloud virtual machine. Below is the code entered

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%23%20root%20at%20controller%20in%20~%20%5B17%3A42%3A47%5D%0A%E2%86%92%20virt-clone%20%0A–connect%3Dqemu%3A%2F%2F%2Fsystem%20%0A–original%20asterisk-cloud%20%0A–name%20kamailio-cloud%20%0A–file%20%2Fvar%2Flib%2Flibvirt%2Fimages%2Fkamailio-cloud.qcow2″/]

The output is as follows:

Assigned “kamailio-cloud.qcow2” | 8.0 GB 00:01:21
The clone “kamailio-cloud” has been successfully created.

If you want to connect to a remote KVM / QEMU host, put the url before / system. It looks like:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%E2%86%92%20virt-clone%20%0A–connect%3Dqemu%3A%2F%2F192.168.1.30%2Fsystem%20%0A–original%20asterisk-cloud%20%0A–name%20kamailio-cloud%20%0A–file%20%2Fvar%2Flib%2Flibvirt%2Fimages%2Fkamailio-cloud.qcow2″/]

asterisk-cloud: the name of the VM cloned from kamailio-cloud: the name provided for the generated VM after cloning. kamailio-cloud.qcow2: Saves the image from which kamailio-cloud boots.
Check and confirm that the kamailio-cloud.qcow2 file is successfully stored in the / var / lib / libvirt / images folder.

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%23%20root%20at%20controller%20in%20~%20%5B17%3A45%3A59%5D%0A%E2%86%92%20ls%20%2Fvar%2Flib%2Flibvirt%2Fimages%20%0Atotal%206946188%0A-rw-r–r–%201%20root%20root%203238789120%20Mar%2020%2017%3A42%20asterisk-cloud.qcow2%0A-rwxr-xr-x%201%20qemu%20qemu%20666894336%20Mar%2016%2004%3A14%20CentOS-7-x86_64-Minimal-1503-01.iso%0A-rw-r–r–%201%20root%20root%203207331840%20Mar%2020%2017%3A44%20kamailio-cloud.qcow2″/]

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

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%23%20root%20at%20controller%20in%20~%20%5B17%3A49%3A48%5D%0A%E2%86%92%20virsh%20list%20–all%0AId%20Name%20State%0A—————————————————-%0A-%20asterisk-cloud%20shut%20off%0A-%20instance-00000004%20shut%20off%0A-%20kamailio-cloud%20shut%20off%C2%A0″/]

Before starting kamailio-cloud, I had to change the path of the channel source mode from the original. Do this by editing the file /etc/libvirt/qemu/kamailio-cloud.xml

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%E2%94%8C%E2%94%80%E2%94%80%20root%20at%20controller%20(~)%0A%E2%94%94%E2%94%80%3Evi%20%2Fetc%2Flibvirt%2Fqemu%2Fkamailio-cloud.xml”/]

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

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%3Cchannel%20type%3D’unix’%3E%0A%3Csource%20mode%3D’bind’%20path%3D’%2Fvar%2Flib%2Flibvirt%2Fqemu%2Fchannel%2Ftarget%2Fdomain-kamailio-cloud%2Forg.qemu.guest_agent.0’%2F%3E”/]

It turned out to be:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%3Cchannel%20type%3D’unix’%3E%0A%3Csource%20mode%3D’bind’%20path%3D’%2Fvar%2Flib%2Flibvirt%2Fqemu%2Fchannel%2Ftarget%2Fdomain-asterisk-cloud%2Forg.qemu.guest_agent.0’%2F%3E%0A”/]

To start the cloned virtual machine, simply type:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%C2%A0%23%20virsh%20start%20kamailio-cloud%20–console%0A”/]

After logging in, I have to change the uuid of the network interface. It is eth0 on my CentOS 7.x Guest computer. To generate a new uuid for this interface, simply type:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%5Broot%40openfire-cloud%20~%5D%23%20uuidgen%20eth0%0A29e28904-c8f6-4ac0-93f0-d5d2cedb8555″/]

My original eth0 configuration file from asterisk-cloud vm looks like this:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”TYPE%3DEthernet%0ABOOTPROTO%3Ddhcp%0ADEFROUTE%3Dyes%0AIPV4_FAILURE_FATAL%3Dno%0AIPV6INIT%3Dyes%0AIPV6_AUTOCONF%3Dyes%0AIPV6_DEFROUTE%3Dyes%0AIPV6_FAILURE_FATAL%3Dno%0ANAME%3Deth0%0AUUID%3D230e87b1-f847-4cab-86b9-df4268a0a0a3%0ADEVICE%3Deth0%0AONBOOT%3Dyes%0APEERDNS%3Dyes%0APEERROUTES%3Dyes%0AIPV6_PEERDNS%3Dyes%0AIPV6_PEERROUTES%3Dyes%0A”/]

I will change the uuid to generate the old one:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%5Broot%40openfire-cloud%20~%5D%23%20vim%20%2Fetc%2Fsysconfig%2Fnetwork-scripts%2Fifcfg-eth0%0A”/]

Modify the following configuration to suit your use case.

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”TYPE%3DEthernet%0ATYPE%3DEthernet%0ABOOTPROTO%3Ddhcp%0ADEFROUTE%3Dyes%0AIPV4_FAILURE_FATAL%3Dno%0AIPV6INIT%3Dyes%0AIPV6_AUTOCONF%3Dyes%0AIPV6_DEFROUTE%3Dyes%0AIPV6_FAILURE_FATAL%3Dno%0ANAME%3Deth0%0AUUID%3D29e28904-c8f6-4ac0-93f0-d5d2cedb8555%0ADEVICE%3Deth0%0AONBOOT%3Dyes%0APEERDNS%3Dyes%0APEERROUTES%3Dyes%0AIPV6_PEERDNS%3Dyes%0AIPV6_PEERROUTES%3Dyes%C2%A0″/]

Restart the network service:

[pastacode lang=”bash” message=”” highlight=”” provider=”manual” manual=”%5Broot%40openfire-cloud%20~%5D%23%20service%20network%20restart%20%0ARestarting%20network%20(via%20systemctl)%3A%C2%A0%20%5B%C2%A0%20OK%C2%A0%20%5D%0A%5Broot%40openfire-cloud%20~%5D%23%20systemctl%20restart%20network.service%20%0A%5Broot%40openfire-cloud%20~%5D%23%0A”/]

that’s it. You should have a running / working clone virtual machine. You can also execute this form graphical interface using Virt Manager.

Sidebar