Chapter 8. Cloning virtual machines
To quickly create a new virtual machine (VM) with a specific set of properties, you can clone an existing VM.
Cloning creates a new VM that uses its own disk image for storage, but most of the clone’s configuration and stored data is identical to the source VM. This makes it possible to prepare a number of VMs optimized for a certain task without the need to optimize each VM individually.
8.1. How cloning virtual machines works
Cloning a virtual machine (VM) copies the XML configuration of the source VM and its disk images, and makes adjustments to the configurations to ensure the uniqueness of the new VM. This includes changing the name of the VM and ensuring it uses the disk image clones. Nevertheless, the data stored on the clone’s virtual disks is identical to the source VM.
This process is faster than creating a new VM and installing it with a guest operating system, and can be used to rapidly generate VMs with a specific configuration and content.
If you are planning to create multiple clones of a VM, first create a VM template that does not contain:
- Unique settings, such as persistent network MAC configuration, which can prevent the clones from working correctly.
- Sensitive data, such as SSH keys and password files.
For instructions, see Section 8.2, “Creating a virtual machine template”.
To clone a VM, you can use the RHEL 8 CLI. For details, see Section 8.3, “Cloning a virtual machine using the command-line interface”.
8.2. Creating a virtual machine template
To ensure that the virtual machine (VM) clones run correctly, remove information and configurations that are unique to the source VM, such as SSH keys or persistent network MAC configuration, before cloning the source VM.
This creates a VM template, which can be used to easily and safely create VM clones.
virt-syspreputility is installed on your host:
# yum install /usr/bin/virt-sysprep
- The VM intended as a template is shut down.
You must know where the disk image for the source VM is located, and be the owner of the VM’s disk image file.
Note that disk images for VMs created in the system connection of libvirt are by default located in the
/var/lib/libvirt/imagesdirectory and owned by the root user:
# ls -la /var/lib/libvirt/images -rw-------. 1 root root 9665380352 Jul 23 14:50 a-really-important-vm.qcow2 -rw-------. 1 root root 8591507456 Jul 26 2017 an-actual-vm-that-i-use.qcow2 -rw-------. 1 root root 8591507456 Jul 26 2017 totally-not-a-fake-vm.qcow2 -rw-------. 1 root root 10739318784 Sep 20 17:57 another-vm-example.qcow2
- Optional: Any important data on the VM’s disk has been backed up. If you want to preserve the source VM intact, clone it first and edit the clone to create a template.
Ensure you are logged in as the owner of the VM’s disk image:
# whoami root
Optional: Copy the disk image of the VM.
# cp /var/lib/libvirt/images/a-really-important-vm.qcow2 /var/lib/libvirt/images/a-really-important-vm-original.qcow2
This is used later to verify the VM was successfully turned into a template.
Use the following command, and replace /var/lib/libvirt/images/a-really-important-vm.qcow2 with the path to the disk image of the source VM.
# virt-sysprep -a /var/lib/libvirt/images/a-really-important-vm.qcow2 [ 0.0] Examining the guest ... [ 7.3] Performing "abrt-data" ... [ 7.3] Performing "backup-files" ... [ 9.6] Performing "bash-history" ... [ 9.6] Performing "blkid-tab" ... [...]
To confirm that the process was successful, compare the modified disk image to the original one. The following example shows a successful creation of a template:
# virt-diff -a /var/lib/libvirt/images/a-really-important-vm-orig.qcow2 -A /var/lib/libvirt/images/a-really-important-vm.qcow2 - - 0644 1001 /etc/group- - - 0000 797 /etc/gshadow- = - 0444 33 /etc/machine-id [...] - - 0600 409 /home/username/.bash_history - d 0700 6 /home/username/.ssh - - 0600 868 /root/.bash_history [...]
virt-sysprepcommand as shown above performs the standard VM template preparation. For more information, see the
OPERATIONSsection in the
To customize which specific operations you want
virt-sysprepto perform, use the
--operationsoption, and specify the intended operations as a comma-separated list.
- For instructions on cloning a VM template, see Section 8.3, “Cloning a virtual machine using the command-line interface”.
8.3. Cloning a virtual machine using the command-line interface
To quickly create a new virtual machine (VM) with a specific set of properties, for example for testing purposes, you can clone an existing VM. To do so using the CLI, follow the instructions below.
- The source VM is shut down.
- Ensure that there is sufficient disk space to store the cloned disk images.
- Optional: When creating multiple VM clones, remove unique data and settings from the source VM to ensure the cloned VMs work properly. For instructions, see Section 8.2, “Creating a virtual machine template”.
virt-cloneutility with options that are appropriate for your environment and use case.
Sample use cases
The following command clones a local VM named doppelganger and creates the doppelganger-clone VM. It also creates the doppelganger-clone.qcow2 disk image in the same location as the disk image of the original VM, and with the same data:
# virt-clone --original doppelganger --auto-clone Allocating 'doppelganger-clone.qcow2' | 50.0 GB 00:05:37 Clone 'doppelganger-clone' created successfully.
The following command clones a VM named geminus1, and creates a local VM named geminus2, which uses only two of geminus1's multiple disks.
# virt-clone --original geminus1 --name geminus2 --file /var/lib/libvirt/images/disk1.qcow2 --file /var/lib/libvirt/images/disk2.qcow2 Allocating 'disk1-clone.qcow2' | 78.0 GB 00:05:37 Allocating 'disk2-clone.qcow2' | 80.0 GB 00:05:37 Clone 'geminus2' created successfully.
To clone your VM to a different host, migrate the VM without undefining it on the local host. For example, the following commands clone the previously created geminus2 VM to the 10.0.0.1 remote system, including its local disks. Note that using these commands also requires root privileges for 10.0.0.1.
# virsh migrate --offline --persistent geminus2 qemu+ssh://firstname.lastname@example.org/system email@example.com's password: # scp /var/lib/libvirt/images/disk1-clone.qcow2 firstname.lastname@example.org/user@remote_host.com://var/lib/libvirt/images/ # scp /var/lib/libvirt/images/disk2-clone.qcow2 email@example.com/user@remote_host.com://var/lib/libvirt/images/
To verify the VM has been successfully cloned and is working correctly:
Confirm the clone has been added to the list of VMs on your host.
# virsh list --all Id Name State --------------------------------------- - doppelganger shut off - doppelganger-clone shut off
Start the clone and observe if it boots up.
# virsh start doppelganger-clone Domain doppelganger-clone started
For additional options for cloning VMs, see the
- For details on moving the VM clones to a different host, including troubleshooting information, see Chapter 9, Migrating virtual machines.