Creating VirtualMachines

The virtctl sub command create vm allows easy creation of VirtualMachine manifests from the command line. It leverages instance types and preferences and provides several flags to control details of the created virtual machine.

For example there are flags to specify the name or run strategy of a virtual machine or flags to add volumes to a virtual machine. Instance types and preferences can either be specified directly or it is possible to let KubeVirt infer those from the volume used to boot the virtual machine.

For a full set of flags and their description use the following command:

  1. virtctl create vm -h

Creating VirtualMachines on a cluster

The output of virtctl create vm can be piped into kubectl to directly create a VirtualMachine on a cluster, e.g.:

  1. # Create a VM with name my-vm on the cluster
  2. virtctl create vm --name my-vm | kubectl create -f -
  3. virtualmachine.kubevirt.io/my-vm created

Creating Instance Types

The virtctl subcommand create instancetype allows easy creation of an instance type manifest from the command line. The command also provides several flags that can be used to create your desired manifest.

There are two required flags that need to be specified: the number of vCPUs and the amount of memory to be requested. Additionally, there are several optional flags that can be used, such as specifying a list of GPUs for passthrough, choosing the desired IOThreadsPolicy, or simply providing the name of our instance type.

By default, the command creates the cluster-wide resource. If the user wants to create the namespaced version, they need to provide the namespaced flag. The namespace name can be specified by using the namespace flag.

For a complete list of flags and their descriptions, use the following command:

  1. virtctl create instancetype -h

Examples

Create a manifest for a VirtualMachineClusterInstancetype with the required —cpu and —memory flags

  1. virtctl create instancetype --cpu 2 --memory 256Mi

Create a manifest for a VirtualMachineInstancetype with a specified namespace

  1. virtctl create instancetype --cpu 2 --memory 256Mi --namespace my-namespace

Create a manifest for a VirtualMachineInstancetype without a specified namespace name

  1. virtctl create instancetype --cpu 2 --memory 256Mi --namespaced

Creating Preferences

The virtctl subcommand create preference allows easy creation of a preference manifest from the command line. This command serves as a starting point to create the basic structure of a manifest, as it does not allow specifying all of the options that are supported in preferences.

The current set of flags allows us, for example, to specify the preferred CPU topology, machine type or a storage class.

By default, the command creates the cluster-wide resource. If the user wants to create the namespaced version, they need to provide the namespaced flag. The namespace name can be specified by using the namespace flag.

For a complete list of flags and their descriptions, use the following command:

  1. virtctl create preference -h

Examples

Create a manifest for a VirtualMachineClusterPreference with a preferred cpu topology

  1. virtctl create preference --cpu-topology preferSockets

Create a manifest for a VirtualMachinePreference with a specified namespace

  1. virtctl create preference --namespace my-namespace

Create a manifest for a VirtualMachinePreference with the preferred storage class

  1. virtctl create preference --namespaced --volume-storage-class my-storage

Specifying or inferring instance types and preferences

Instance types and preference can be specified with the appropriate flags, e.g.:

  1. virtctl create vm --instancetype my-instancetype --preference my-preference

The type of the instance type or preference (namespaced or cluster scope) can be controlled by prefixing the instance type or preference name with the corresponding CRD name, e.g.:

  1. # Using a cluster scoped instancetype and a namespaced preference
  2. virtctl create vm \
  3. --instancetype virtualmachineclusterinstancetype/my-instancetype \
  4. --preference virtualmachinepreference/my-preference

If a prefix was not supplied the cluster scoped resources will be used by default.

To infer instance types and/or preferences from the volume used to boot the virtual machine add the following flags:

  1. virtctl create vm --infer-instancetype --infer-preference

Boot order of added volumes

Please note that volumes of different kinds currently have the following fixed boot order regardless of the order their flags were specified on the command line:

  1. ContainerDisk
  2. DataSource
  3. Cloned PVC
  4. Directly used PVC

If multiple volumes of the same kind were specified their order is determined by the order in which their flags were specified.

Specifying cloud-init user data

To pass cloud-init user data to virtctl it needs to be encoded into a base64 string. Here is an example how to do it:

  1. # Put your cloud-init user data into a file.
  2. # This will add an authorized key to the default user.
  3. # To get the default username read the documentation for the cloud image
  4. $ cat cloud-init.txt
  5. #cloud-config
  6. ssh_authorized_keys:
  7. - ssh-rsa AAAA...
  8. # Base64 encode the contents of the file without line wraps and store it in a variable
  9. $ CLOUD_INIT_USERDATA=$(base64 -w 0 cloud-init.txt)
  10. # Show the contents of the variable
  11. $ echo $CLOUD_INIT_USERDATA I2Nsb3VkLWNvbmZpZwpzc2hfYXV0aG9yaXplZF9rZXlzOgogIC0gc3NoLXJzYSBBQUFBLi4uCg==

You can now use this variable as an argument to the --cloud-init-user-data flag:

  1. virtctl create vm --cloud-init-user-data $CLOUD_INIT_USERDATA

Examples

Create a manifest for a VirtualMachine with a random name:

  1. virtctl create vm

Create a manifest for a VirtualMachine with a specified name and RunStrategy Always

  1. virtctl create vm --name=my-vm --run-strategy=Always

Create a manifest for a VirtualMachine with a specified VirtualMachineClusterInstancetype

  1. virtctl create vm --instancetype=my-instancetype

Create a manifest for a VirtualMachine with a specified VirtualMachineInstancetype (namespaced)

  1. virtctl create vm --instancetype=virtualmachineinstancetype/my-instancetype

Create a manifest for a VirtualMachine with a specified VirtualMachineClusterPreference

  1. virtctl create vm --preference=my-preference

Create a manifest for a VirtualMachine with a specified VirtualMachinePreference (namespaced)

  1. virtctl create vm --preference=virtualmachinepreference/my-preference

Create a manifest for a VirtualMachine with an ephemeral containerdisk volume

  1. virtctl create vm --volume-containerdisk=src:my.registry/my-image:my-tag

Create a manifest for a VirtualMachine with a cloned DataSource in namespace and specified size

  1. virtctl create vm --volume-datasource=src:my-ns/my-ds,size:50Gi

Create a manifest for a VirtualMachine with a cloned DataSource and inferred instancetype and preference

  1. virtctl create vm --volume-datasource=src:my-annotated-ds --infer-instancetype --infer-preference

Create a manifest for a VirtualMachine with a specified VirtualMachineCluster{Instancetype,Preference} and cloned PVC

  1. virtctl create vm --volume-clone-pvc=my-ns/my-pvc

Create a manifest for a VirtualMachine with a specified VirtualMachineCluster{Instancetype,Preference} and directly used PVC

  1. virtctl create vm --volume-pvc=my-pvc

Create a manifest for a VirtualMachine with a clone DataSource and a blank volume

  1. virtctl create vm --volume-datasource=src:my-ns/my-ds --volume-blank=size:50Gi

Create a manifest for a VirtualMachine with a specified VirtualMachineCluster{Instancetype,Preference} and cloned DataSource

  1. virtctl create vm --instancetype=my-instancetype --preference=my-preference --volume-datasource=src:my-ds

Create a manifest for a VirtualMachine with a specified VirtualMachineCluster{Instancetype,Preference} and two cloned DataSources (flag can be provided multiple times)

  1. virtctl create vm --instancetype=my-instancetype --preference=my-preference --volume-datasource=src:my-ds1 --volume-datasource=src:my-ds2

Create a manifest for a VirtualMachine with a specified VirtualMachineCluster{Instancetype,Preference} and directly used PVC

  1. virtctl create vm --instancetype=my-instancetype --preference=my-preference --volume-pvc=my-pvc