×

You can create virtual machines (VMs) from the command line by editing or creating a VirtualMachine manifest. You can simplify VM configuration by using an instance type in your VM manifest.

You can also create VMs from instance types by using the OKD web console.

Create a VM from a manifest

You can create a virtual machine (VM) from a VirtualMachine manifest. To simplify the creation of these manifests, you can use the virtctl command-line tool.

Prerequisites
  • You have installed the virtctl CLI.

  • You have installed the OpenShift CLI (oc).

Procedure
  1. Create a VirtualMachine manifest for your VM and save it as a YAML file. For example, to create a minimal Fedora VM, run the following command:

    $ virtctl create vm --name rhel-9-minimal --volume-import type:ds,src:openshift-virtualization-os-images/rhel9
  2. Review the VirtualMachine manifest for your VM:

    This example manifest does not configure VM authentication.

    Example manifest for a Fedora VM
    apiVersion: kubevirt.io/v1
    kind: VirtualMachine
    metadata:
      name: rhel-9-minimal
    spec:
      dataVolumeTemplates:
      - metadata:
          name: imported-volume-mk4lj
        spec:
          sourceRef:
            kind: DataSource
            name: rhel9
            namespace: openshift-virtualization-os-images
          storage:
            resources: {}
      instancetype:
        inferFromVolume: imported-volume-mk4lj
        inferFromVolumeFailurePolicy: Ignore
      preference:
        inferFromVolume: imported-volume-mk4lj
        inferFromVolumeFailurePolicy: Ignore
      runStrategy: Always
      template:
        spec:
          domain:
            devices: {}
            memory:
              guest: 512Mi
            resources: {}
          terminationGracePeriodSeconds: 180
          volumes:
          - dataVolume:
              name: imported-volume-mk4lj
            name: imported-volume-mk4lj
    • name: rhel-9-minimal specifies the name of the VM.

    • name: rhel9 specifies the boot source for the guest operating system in the sourceRef section.

    • namespace: openshift-virtualization-os-images specifies the namespace for the boot source. Golden images are stored in the openshift-virtualization-os-images namespace.

    • instancetype: inferFromVolume: imported-volume-mk4lj specifies the instance type inferred from the selected DataSource object.

    • preference: inferFromVolume: imported-volume-mk4lj specifies that the preference is inferred from the selected DataSource object.

  3. Create a virtual machine by using the manifest file:

    $ oc create -f <vm_manifest_file>.yaml
  4. Optional: Start the virtual machine:

    $ virtctl start <vm_name>

Upload a VM image by using the CLI

You can upload an operating system image by using the virtctl command-line tool. You can use an existing data volume or create a new data volume for the image.

Prerequisites
  • You must have an ISO, IMG, or QCOW2 operating system image file.

  • For best performance, compress the image file by using the virt-sparsify tool or the xz or gzip utilities.

  • The client machine must be configured to trust the OKD router’s certificate.

  • You have installed the virtctl CLI.

  • You have installed the OpenShift CLI (oc).

Procedure
  1. Upload the image by running the virtctl image-upload command:

    $ virtctl image-upload dv <datavolume_name> \
      --size=<datavolume_size> \
      --image-path=</path/to/image>
    <datavolume_name>

    The name of the data volume.

    <datavolume_size>

    The size of the data volume. For example: --size=500Mi, --size=1G

    </path/to/image>

    The file path of the image.

    • If you do not want to create a new data volume, omit the --size parameter and include the --no-create flag.

    • When uploading a disk image to a PVC, the PVC size must be larger than the size of the uncompressed virtual disk.

    • To allow insecure server connections when using HTTPS, use the --insecure parameter. When you use the --insecure flag, the authenticity of the upload endpoint is not verified.

  2. Optional. To verify that a data volume was created, view all data volumes by running the following command:

    $ oc get dvs