Home / Platform management / Virtualization / Virtual Machine Image Export

Virtual Machine Image Export

This feature is used to export the system image of a virtual machine and upload it to object storage, so that files in the object storage can be added as a source to the platform’s virtual machine image.

Procedure

Note: All the operations below must be performed on the master node of the cluster where the virtual machine is located.

Stop the Virtual Machine

  1. In the left navigation bar, click Virtualization > Virtual Machines.

  2. Click the name of the virtual machine whose system image you want to export. The page will redirect to the Container Platform’s virtual machine details page.

  3. Click Stop.

Create vmexport Resource

  1. Open the CLI tool.

  2. Execute the following commands to set variables.

    NAMESPACE=<namespace> 
    VM_NAME=<vm_name>
    TTL_DURATION=2h

    Parameter description:

    • NAMESPACE: The name of the namespace where the virtual machine is located. Replace <namespace> with this name.
    • VM_NAME: The name of the virtual machine whose system image needs to be exported. Replace <vm_name> with this name.
    • TTL_DURATION: The lifetime of the export task, default is 2 hours, which can be increased as needed.
  3. Execute the following command to create a vmexport resource.

    cat <<EOF | kubectl create -f -
    apiVersion: export.kubevirt.io/v1alpha1
    kind: VirtualMachineExport
    metadata:
      name: export-$VM_NAME
      namespace: $NAMESPACE
    spec:
      ttlDuration: $TTL_DURATION
      source:
        apiGroup: "kubevirt.io"
        kind: VirtualMachine
        name: $VM_NAME
    EOF

    If you see output similar to the following, the creation was successful.

    virtualmachineexport.export.kubevirt.io/export-k1 created
  4. Execute the following command to check the status of the vmexport resource.

    kubectl -n $NAMESPACE get vmexport export-$VM_NAME -w

    Output:

    NAME        SOURCEKIND       SOURCENAME   PHASE
    export-k1   VirtualMachine   k1           Ready
  5. When the PHASE field in the above output changes to Ready, press ctrl (control) + c to stop the watch operation.

  6. Execute the following command to get the TOKEN.

    TOKEN=$(kubectl -n $NAMESPACE get secret export-token-export-$VM_NAME -o jsonpath={.data.token} | base64 -d)

Download Virtual Machine Image File

  1. Execute the following command to get the IP address of the virtual machine export Pod in the specified namespace and store it in the EXPORT_SERVER_IP environment variable.

    EXPORT_SERVER_IP=$(kubectl -n $NAMESPACE get po virt-export-export-$VM_NAME -o jsonpath='{.status.podIP}')
  2. Execute the following command to set the URL environment variable, which points to the virtual machine’s disk image file.

    URL=https://$EXPORT_SERVER_IP:8443/volumes/$VM_NAME-rootfs/disk.img.gz
  3. Execute the following command to download the image file. The downloaded file name will be disk.img.gz.

    curl -k -O -H "x-kubevirt-export-token: $TOKEN" $URL

Upload Virtual Machine Image File to Object Storage

Upload the downloaded image file to object storage. You can use any S3 tool for uploading. This document uses the mc (minio-client) tool as an example.

  1. Execute the following command to configure the mc tool and connect to the specified S3 storage service.

    mc alias set minio <ENDPOINT> <ACCESSKEY> <SECRETKEY>

    Parameter description:

    • ENDPOINT: The address of the S3 storage service. Replace with this address.
    • ACCESSKEY, SECRETKEY: The user ak, sk of the S3 storage service for authentication. For more information, refer to MinIO Object Storage .
  2. Execute the following command to create a bucket for storing virtual machine image files.

    mc mb minio/vmdisks
  3. Execute the following command to upload the exported virtual machine image file disk.img.gz to the created bucket.

    mc put disk.img.gz minio/vmdisks

Create Virtual Machine Image

  1. In the left navigation bar, click Virtualization > Images.

  2. Click Add VM Image.

  3. Fill in the image address as <ENDPOINT>/vmdisks/disk.img.gz. Replace <ENDPOINT> with the address of the S3 storage service. For other parameter descriptions, refer to Add Virtual Machine Image .

  4. Click Add.

Create a Virtual Machine Using the New Virtual Machine Image

For detailed operations, refer to the relevant documentation in Container Platform > Virtualization > Create a virtual machine.