Virtual Machine Clone
Virtual machine cloning refers to creating a replica of a virtual machine at a specific point in time. The cloned virtual machine contains all the configurations, operating systems, applications, and data of the original virtual machine.
Prerequisites
-
To use the virtual machine cloning functionality, its disk must utilize a storage class that supports snapshot features. Virtual machine snapshots save the current state of the virtual machine, which can be used to restore the virtual machine to that point in time in the event of an unexpected failure.
-
The jq package must be installed on the node where the script is to be executed. jq is a lightweight command-line JSON processing tool used for parsing and processing JSON data.
Steps to Operate
Note: All operations below must be performed on the master node of the cluster where the virtual machine resides.
-
Open the CLI tool.
-
Execute the following command to create and open the vm-clone.sh file.
-
Press i
and 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"
-
Press shift+:wq
to save the file.
-
Execute the following command to add execution permissions to the vm-clone.sh file.
-
Execute the following command to run the script file.{#clone}
./vm-clone.sh <NAMESPACE> <VM_NAME> <VM_CLONE_NAME>
Parameter Description:
- NAMESPACE: Specify the namespace of the virtual machine to be cloned, replacing the <NAMESPACE> part with this namespace.
- VM_NAME: Specify the name of the virtual machine to be cloned, replacing the <VM_NAME> part with this name.
- VM_CLONE_NAME: Specify the name of the cloned virtual machine, replacing the <VM_CLONE_NAME> part with this name.
-
When a message similar to the following appears, it indicates that 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
-
Go to Platform Management.
-
In the left navigation bar, click Virtualization Management > Virtual Machine Images.
-
You will see the cloned virtual machine with the specified name from the Run Script step; the default state of the cloned virtual machine is Stopped.
-
Click on the name of this virtual machine, and the page will redirect to the virtual machine detail page in the Container Platform.
-
Click Start to successfully start the virtual machine.