Create a VM Template from an Existing Virtual Machine

This document outlines how to create a reusable virtual machine (VM) template from an existing VM for rapid deployment of new VMs.

Prerequisites

  • A properly deployed and configured KubeVirt environment.
  • Access to the Web Console and kubectl tool.
  • A configured VM with necessary software already installed.

Procedure

Step 1: Basic Configuration on the Virtual Machine

Inside the VM, perform the following steps:

  • Install cloud-init.
  • Install the qemu-guest-agent.
  • Install any required software.

Once installations are complete, run the following commands to clean cloud-init data and shut down the VM:

cloud-init clean
shutdown -h now

Step 2: Create a VM Snapshot

Using the KubeVirt Web Console:

  1. Navigate to Virtualization > Virtual Machines.
  2. Select the VM intended to serve as a template.
  3. Click Actions, select Create Snapshot, name your snapshot, and confirm.

Step 3: Retrieve Disk Snapshot Resource Name

Obtain the complete snapshot resource name using one of these methods:

  • Via Web Console:

    • Navigate to Storage > Volume Snapshots.
    • Find and record the full snapshot resource name under "Data Source."
  • Using kubectl:

    kubectl get volumesnapshots -n <NAMESPACE>

    Record the complete snapshot resource name from the output.

Step 4: Create a DataSource Resource

Create the following DataSource resource in the kube-public namespace, ensuring you replace placeholders with the actual snapshot name and namespace:

apiVersion: cdi.kubevirt.io/v1beta1
kind: DataSource
metadata:
  annotations:
    cpaas.io/display-name: MicroOS-Clone
  labels:
    virtualization.cpaas.io/image-os-arch: amd64
    virtualization.cpaas.io/image-os-type: linux
    virtualization.cpaas.io/storage-class: cephrbd
    virtualization.cpaas.io/access-mode: ReadWriteMany
    virtualization.cpaas.io/size: 30Gi
    virtualization.cpaas.io/volume-mode: Block
  name: microos-clone
  namespace: kube-public
spec:
  source:
    snapshot:
      name: <Your Snapshot Resource Name>
      namespace: <Your Snapshot Namespace>

Label Parameters Explanation:

KeyPossible ValuesDescription
virtualization.cpaas.io/image-os-archamd64, arm64VM OS architecture
virtualization.cpaas.io/image-os-typelinux, windowsVM OS type
virtualization.cpaas.io/storage-classstorage class nameDefault storage class, adjustable during VM creation
virtualization.cpaas.io/access-modeReadWriteOnce, ReadWriteManyDisk access mode; use ReadWriteMany for VM live migration
virtualization.cpaas.io/sizeCapacity (Gi, Ti, etc.)Default disk size; specify appropriate size
virtualization.cpaas.io/volume-modeBlock, FilesystemDisk volume mode; Block mode recommended for better performance

Important:

  • Ensure the namespace is kube-public.
  • These disk-related parameters can be modified during VM creation but providing defaults simplifies the process.

Step 5: Create a New VM Using the Template

  1. Access the KubeVirt Web Console, go to Container Platform > Virtualization > Virtual Machines.
  2. Click Create Virtual Machine.
  3. Under Image, select Image Instance as the Provision Method.
  4. Select your newly created DataSource from the dropdown.
  5. Configure any additional parameters as required and complete the VM creation process.

You have now successfully created and deployed new VMs using your VM template.