Virtual Machine Cloning
Virtual machine cloning refers to creating a copy of a virtual machine at its current point in time. The cloned virtual machine contains all configurations, operating systems, applications, and data of the original virtual machine.
Prerequisites
-
To use the virtual machine cloning feature, its disk must use a storage class that supports snapshot functionality. Virtual machine snapshots save the current state of the virtual machine and can be used to restore the virtual machine to this point in time in case of unexpected failures.
-
Install the jq package on the node where the script needs to be executed. jq is a lightweight command-line JSON processing tool used for parsing and processing JSON data.
Procedure
Note: All the operations below must be performed on the master node of the cluster where the virtual machine is located.
-
Open the CLI tool.
-
Execute the following command to create and open the vm-clone.sh file.
vi vm-clone.sh -
Type
iand copy the following content into the vm-clone.sh file.#!/bin/bash vm_clone() { NAMESPACE=$1 VM_NAME=$2 VM_CLONE_NAME=$3 cat <<EOF | kubectl create -f - kind: VirtualMachineClone apiVersion: clone.kubevirt.io/v1alpha1 metadata: name: clone-$VM_NAME namespace: $NAMESPACE spec: source: apiGroup: kubevirt.io kind: VirtualMachine name: $VM_NAME target: apiGroup: kubevirt.io kind: VirtualMachine name: $VM_CLONE_NAME labelFilters: - "*" - "!ovn.kubernetes.io/*" annotationFilters: - "*" - "!ovn.kubernetes.io/*" template: labelFilters: - "*" - "!ovn.kubernetes.io/*" annotationFilters: - "*" - "!ovn.kubernetes.io/*" EOF if [ $? -eq 0 ]; then echo "Create vmclone resource Succeeded" else echo "Create vmclone resource failed" exit 1 fi echo "Waiting for vm clone completion" while true; do phase=$(kubectl -n $NAMESPACE get vmclone clone-$VM_NAME -o jsonpath='{.status.phase}') if [ "$phase" == "Succeeded" ]; then break elif [ "$phase" == "Failed" ]; then echo "VirtualMachineClone resource phase is Failed" exit 1 fi sleep 5 done echo "vm clone completion" dvList=$(kubectl -n $NAMESPACE get vm $VM_CLONE_NAME -o jsonpath='{.spec.template.spec.volumes}' | jq . | grep restore- | grep name | awk '{print $2}') for dv in $dvList; do kubectl -n $NAMESPACE label --overwrite dv $(echo $dv | sed 's/\"//g') vm.cpaas.io/used-by=$VM_CLONE_NAME if [ $? -ne 0 ]; then echo "update DV label failed" exit 1 fi done pvcList=$(kubectl -n $NAMESPACE get vm $VM_CLONE_NAME -o jsonpath='{.spec.template.spec.volumes}' | jq . | grep restore- | grep claimName | awk '{print $2}') for pvc in $pvcList; do kubectl -n $NAMESPACE label --overwrite pvc $(echo $pvc | sed 's/\"//g') vm.cpaas.io/used-by=$VM_CLONE_NAME if [ $? -ne 0 ]; then echo "update PVC label failed" exit 1 fi done } if [ $# -ne 3 ]; then echo "error: parameters error" echo "Usage: ./vm-clone.sh NAMESPACE VM_NAME VM_CLONE_NAME" exit 1 fi # exec vm clone vm_clone "$1" "$2" "$3" -
Type
shift+:wqto save the file. -
Execute the following command to add execution permissions to the vm-clone.sh file.
chmod +x vm-clone.sh -
Execute the following command to run the script file.
./vm-clone.sh <NAMESPACE> <VM_NAME> <VM_CLONE_NAME>Parameter description:
- NAMESPACE: Enter the namespace where the virtual machine to be cloned is located. Replace <NAMESPACE> with this namespace.
- VM_NAME: Enter the name of the virtual machine to be cloned. Replace <VM_NAME> with this name.
- VM_CLONE_NAME: Enter the name of the cloned virtual machine. Replace <VM_CLONE_NAME> with this name.
-
When you see output similar to the following, the cloning is complete.
virtualmachineclone.clone.kubevirt.io/clone-k1 created Create vmclone resource Succeeded Waiting for vm clone completion vm clone completion datavolume.cdi.kubevirt.io/restore-e8ff0e7b-dc7e-4140-aec7-8556cfcf4533-rootfs labeled datavolume.cdi.kubevirt.io/restore-e8ff0e7b-dc7e-4140-aec7-8556cfcf4533-1 labeled
Related Operations
View and Start the Cloned Virtual Machine
-
In the left navigation bar, click Virtualization > Images.
-
You can see the cloned virtual machine with the name specified in the Run Script step. The cloned virtual machine is in the Stopped state by default.
-
Click the name of this virtual machine, and the page will redirect to the Container Platform’s virtual machine details page.
-
Click Start to successfully start the virtual machine.