Twitter icon LinkedIn Icon Github Icon Stackoverflow Icon

Cloning a KVM Virtual Machine Image

By: Roger Creasy

I often need to test on servers set up in a very precise way. I may need to test an application running on an Ubuntu server with Apache and a specific version of php. Or, I may need to test an application or site on a LEMP stack running on CentOS.

Rather than building these servers from scratch each time, I have base machines with networking, etc. already set up. I clone these base machines then customize the clone as needed. This post will explain the process I use to create these customized servers.

For reference, I am running Fedora (currently version 30) on my development laptop. These instructions are designed around that setup.

We need to see a list of virtual machines on our host, in this case, my development laptop. I limited the available vms in order to simplify this tutorial.

sudo virsh list --all

You need to either become root, or use sudo. Without sudo the commands will appear to work. But, there will be no results.

The above command should output something similar to the below.

                                                                                                                                    
[sudo] password for roger:                                                                                                                                                                                      

 Id   Name        State                                                                                                                                                                                         
---------------------------                                                                                                                                                                                     
 4    centos7.0   running

The vm you want to clone may or may not be running. If it is running, you will have to shut it down or suspend it. I have run into problems with file locks when cloning a suspended machine. So, I recommend stopping the vm.

sudo virsh shutdown centos7.0
Domain centos7.0 is being shutdown

Now, listing should show the machine as paused:

sudo virsh list --all
 Id   Name         State
-----------------------------
 5    centos7.0    shut down

We want to clone this base machine, and give it a new name.

sudo virt-clone --original centos7.0 --name newCentos7.0 --auto-clone

Allocating 'newCentos7.0.qcow2'   |  20 GB  00:00:37

Clone 'newCentos' created successfully.

Replace "centos7.0" with the name of your existing base vm. And, replace "newCentos7.0" with whatever you want your new vm to be named.

After the copy completes, list your VMs again.

sudo virsh list --all

 Id   Name         State
-----------------------------
 -    centos7.0       shut off
 -    newCentos7.0    shut off

Next, start both machines.

sudo virsh start centos7.0

sudo virsh start centos7.0
Domain centos7.0 started


sudo virsh start newCentos7.0
Domain newCentos7.0 started

You now have a base machine that you can configure and customize to fit your needs. It may take a bit of trial and error to get the base machine (the one you clone) set up with all of the common configuration that you need. But, you will end up with a base machine that you can clone as needed.

I hope you find this post helpful. If you have suggestions for improvements, or find any errors, please contact me via the methods in the footer.

Publication Date: 2019-07-10 13:50:42