Cloning a virtual machine is really simple. In the case of KVM it is as simple as turning off the source virtual machine for data consistency and using the virt-clone command.
It can be done in two ways, following a wizard by running virt-clone with the -prompt parameter or bypassing all necessary parameters.
In the first case simply answer the questions that are emerging, which basically are:
- Name of the source virtual machine to be cloned.
- Name for the resulting virtual machine.
- Path to disk or cloned disks (destination disks).
With this example we show step by step how to clone the machine name_of_new_vm and create the machine name_of_current_vm:
1 Suspend the source virtual machine
virsh suspend name_of_current_vm
2 Run the virt-clone command
- Prompt option:
# virt-clone --prompt What is the name of the original virtual machine? name_of_current_vm What is the name for the cloned virtual machine? name_of_new_vm What would you like to use as the cloned disk (file path) for '/data2/KVM/name_of_current_vm_d1.raw'? /data2/KVM/name_of_new_vm_d1.raw Cloning name_of_new_vm_d1.raw | 36 GB 01:14 Clone 'name_of_current_vm' created successfully.
- Command line option:
# virt-clone -o name_of_current_vm -n name_of_new_vm -f /data2/KVM/name_of_new_vm_d1.raw
2.1 Multiple disks
If the original machine has multiple disks, you must use the -f ( -file ) parameter as many times as you have defined in XML of the server in the corresponding order. For example:
virt-clone -o master_server -n srvdb_master -f /dataocz/KVM/srvdb_master_d1.raw -f /dataocz/KVM/srvdb_master_d2.raw
3 Start the new virtual machine
# virsh start name_of_new_vm Domain name_of_new_vm started
4 Determine on which port you are listening to the new machine
# virsh vncdisplay name_of_new_vm :2
Booting the VNC Viewer (View link) to finish configuring the new machine (Hostname, IP Address, etc.).
5 Resume source virtual machine
# virsh resume name_of_current_vm Domain name_of_current_vm resumed