Clone Virtual Machines on KubeVirt
This document provides step-by-step guidance on cloning virtual machines (VMs) using KubeVirt's VirtualMachineClone API.
TOC
Ensure PrerequisitesStart QuicklyUnderstand the VirtualMachineClone ObjectView a Complete VirtualMachineClone ExampleUnderstand Each FieldCheck Clone Operation PhasesEnsure Prerequisites
Before initiating a VM clone operation, make sure the following requirements are satisfied:
- Snapshot‑Capable Storage: The Clone API relies on Snapshot & Restore functionalities. The virtual machine's storage class must support volume snapshots, and snapshot functionality must be explicitly enabled for that storage backend.
Start Quickly
Follow these quick steps to clone a VM:
-
Prepare the Clone Manifest:
Create a file named
clone.yamlwith the following structure: -
Execute the Clone Operation:
Apply the manifest:
-
Monitor the Clone Status:
Wait until the cloning is completed successfully:
-
Verify the Cloned VM:
Inspect the cloned VM configuration:
-
Fix the DataVolume Label (UI metadata):
The platform UI links VMs to their disks through the label
vm.cpaas.io/used-by=<vm-name>that is automatically added to every DataVolume. After a clone operation the new DataVolume inherits the label from the source VM, so the UI still thinks it belongs to the old VM. Update the label on the newly created DV so the relationship displays correctly (functionality is not affected).
Understand the VirtualMachineClone Object
View a Complete VirtualMachineClone Example
Here's a complete example of a VirtualMachineClone resource with detailed inline comments:
Understand Each Field
-
Source and Target:
- Define the original VM (
source) and the cloned VM (target). - Auto-generated if the
targetname is omitted. - Both VMs must reside within the same namespace.
- Define the original VM (
-
Label and Annotation Filters:
- Control copying or excluding labels/annotations from the source VM using wildcards (
*) and negations (!).
- Control copying or excluding labels/annotations from the source VM using wildcards (
-
Template Label and Annotation Filters:
- Useful for managing network-related annotations, especially with CNIs like Kube-OVN.
-
newMacAddresses:
- Optionally specify new MAC addresses for network interfaces.
- Automatically regenerated if omitted.
-
newSMBiosSerial:
- Optionally specify a new SMBios serial.
- Auto-generated based on VM name if not provided.
-
JSON Patches:
- Advanced customizations directly applied to VM specs.
Check Clone Operation Phases
The .status.phase of a VirtualMachineClone object changes according to the cloning process progress. The table below explains each phase: