×

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.

Creating a VM from a VirtualMachine 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 must have the virtctl command-line tool installed.

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-datasource 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 (1)
    spec:
      dataVolumeTemplates:
      - metadata:
          name: imported-volume-mk4lj
        spec:
          sourceRef:
            kind: DataSource
            name: rhel9 (2)
            namespace: openshift-virtualization-os-images (3)
          storage:
            resources: {}
      instancetype:
        inferFromVolume: imported-volume-mk4lj (4)
        inferFromVolumeFailurePolicy: Ignore
      preference:
        inferFromVolume: imported-volume-mk4lj (5)
        inferFromVolumeFailurePolicy: Ignore
      runStrategy: Always
      template:
        spec:
          domain:
            devices: {}
            memory:
              guest: 512Mi
            resources: {}
          terminationGracePeriodSeconds: 180
          volumes:
          - dataVolume:
              name: imported-volume-mk4lj
            name: imported-volume-mk4lj
    1 The VM name.
    2 The boot source for the guest operating system.
    3 The namespace for the boot source. Golden images are stored in the openshift-virtualization-os-images namespace.
    4 The instance type is inferred from the selected DataSource object.
    5 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>