Home / Best Practices / Custom virtual machine image solution

Custom virtual machine image solution

The platform page does not support creating images based on existing virtual machines. If you have such a requirement, please refer to this document for creating images based on existing virtual machine instances.

Cautions

To ensure data integrity, the virtual machine needs to be suspended during the image creation process. The suspended virtual machine is close to a shutdown state, and all business operations cannot be accessed.

Download the virtctl tool

Suspend virtual machines

Execute the following command on the cluster master node:

wget https://github.com/kubevirt/kubevirt/releases/download/v0.50.0/virtctl-v0.50.0-linux-amd64 -O /usr/local/bin/virtctl && chmod a+x /usr/local/bin/virtctl

Download the virtctl tool

Execute the following command on the cluster’s Master node:

kubectl get vm -A | grep {vm-name}           # Get accurate vm Name and vm namespace
virtctl -n {vm-namespace} pause vm {vm-name} # Suspend virtual machine

Get the PV name and node of the virtual machine system disk

Create an image of the file system type of the system disk

Execute the following command on the Master node of the cluster:

kubectl -n {vm-namespace} get pvc {vmname}-rootfs -o jsonpath='{.spec.volumeName}'
kubectl -n {vm-namespace} get vmi {vmname} -o jsonpath='{.status.nodeName}'

Creating an Image of a Block Device Type System Disk

Note: The following commands must be executed on the physical machine where the virtual machine is located.

  1. Obtain the mount path.

    mount | grep "{vm-pv-name}/mount" | awk '{print $3}'
    find /var/lib -name "*{vm-pv-name}*"
    
    # If mount Unavailable can be used find, Example:
    # find /var/lib -name "*pvc-0dc285db-7893-460e-a7d4-3372f6364a76*"
  2. Install the qemu-img tool.

    yum install qemu-img -y
  3. Image format conversion.

    qemu-img convert -f raw -O qcow2 {pv-mount-path}/disk.img /path/to/disk.qcow2
    
    # Example:qemu-img convert -f raw -O qcow2 /var/lib/kubelet/pods/7d7437ec-251c-44f7-9117-386b6fe19687/volumes/kubernetes.io~csi/pvc-048936ad-c763-4302-9ec5-21cd3ad0aaa2/mount/disk.img centos7.qcow2 

Wake up a hung virtual machine

Caution: The following commands need to be executed on the physical machine where the virtual machine is located.

  1. Obtain the mount path.

    mount | grep "{vm-pv-name}/dev" | awk '{print $3}'
    
    # Example:mount | grep "pvc-0577145f-708e-44b0-8c66-f500acf76be9/dev" | awk '{print $3}' 
    
    find /var/lib -name "*{vm-pv-name}*"
    # If mount Unavailable can be used find, Example:
    # find /var/lib -name "*pvc-0dc285db-7893-460e-a7d4-3372f6364a76*"
  2. Install the qemu-img tool.

    yum install qemu-img -y
  3. Image format conversion.

    qemu-img convert -f raw -O qcow2 {pv-mount-path} /path/to/disk.qcow2
    
    # Example:qemu-img convert -f raw -O qcow2 /var/lib/kubelet/plugins/kubernetes.io/csi/volumeDevices/pvc-5595c02f-89ac-45ea-9112-788c527e45b5/dev/478e9258-6aa8-4fde-b295-a9bdd9e27fc1 disk.qcow2 

Downloading

Execute the following command on the Master node of the cluster:

virtctl -n {vm-namespace} unpause vm {vm-name}
# Example: virtctl -n virt unpause vm vm-test

Global dock keyclock

After creating the qcow2 file, you need to upload it to an S3 or HTTP server and obtain the download address. Then, you can add the virtual machine image through the custom image function. Here are several common storage methods that can be used to import virtual machine images to the platform: