---
title: Creating virtual machines by using the CLI
---

# Creating virtual machines by using the CLI {#virt-creating-vms-from-cli}

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.

> [!NOTE]
> You can also create VMs from instance types by using the OpenShift Container Platform web console.

## Creating a VM from a VirtualMachine manifest {#virt-creating-vm-cli_virt-creating-vms-cli}

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 Red Hat Enterprise Linux (RHEL) VM, run the following command:

   ```terminal
   $ 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:

   > [!NOTE]
   > This example manifest does not configure VM authentication.

   ```yaml {title="Example manifest for a RHEL 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:
             video:
               type: virtio
           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.
   - `type: virtio` specifies the use of a custom video device (a VirtIO device in this example) to enable hardware graphics acceleration. Enabling a custom video device is in Technology Preview for OpenShift Virtualization 4.21.
3. Create a virtual machine by using the manifest file:

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

   ```terminal
   $ virtctl start <vm_name>
   ```

## Uploading a virtual machine image by using the CLI {#virt-uploading-image-virtctl_virt-creating-vms-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](https://libguestfs.org/virt-sparsify.1.html) tool or the `xz` or `gzip` utilities.
- The client machine must be configured to trust the OpenShift Container Platform 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:

   ```terminal
   $ 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.

   > [!NOTE]
   > - 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:

   ```terminal
   $ oc get dvs
   ```

### Supported custom video device types {#virt-supported-custom-video-devices_virt-creating-vms-cli}

When creating a virtual machine (VM), you can configure a custom video device type to override the default video configuration.

Configuring a custom video device allows you to specify different video devices, based on your guest operating system requirements and performance needs.

> [!IMPORTANT]
> Custom video device support is a Technology Preview feature only. Technology Preview features are not supported with Red Hat production service level agreements (SLAs) and might not be functionally complete. Red Hat does not recommend using them in production. These features provide early access to upcoming product features, enabling customers to test functionality and provide feedback during the development process.
>
> For more information about the support scope of Red Hat Technology Preview features, see [Technology Preview Features Support Scope](https://access.redhat.com/support/offerings/techpreview/).

Using a custom video device  provides several advantages:

Performance
:   Certain video devices provide better performance than the default configuration. For example, VirtIO is a more efficient video device on AMD/x86_64 architecture than legacy VGA.

Resolution flexibility
:   With some video device types, you can set custom display resolutions.

Memory efficiency
:   Some video types are more memory efficient for headless or console-only operations.

You can configure the following video device types:

- VirtIO: provides improved performance, and hardware-accelerated video decoding and encoding by offloading tasks to the host. Recommended for modern guest operating systems with available `VirtIO` drivers.
- VGA: the standard for analog video display (default on AMD/x86_64 with BIOS).
- Bochs: an emulated graphics adapter that provides a simple interface for guest operating systems to manage display settings (default on AMD/x86_64 with EFI).
- Cirrus: a legacy video device that provides stable video output.
- ramfb:  a simple, unaccelerated virtual display device primarily used in the QEMU emulator, and useful for ARM architecture.

**Video device support by architecture**

| Architecture | Boot mode | Default type | Supported types |
| --- | --- | --- | --- |
| AMD/x86_64 | BIOS | `vga` | `virtio`, `vga`, `bochs`, `cirrus`, \`ramfb\`\` |
| AMD/x86_64 | EFI | `bochs` | `virtio`, `vga`, `bochs`, `cirrus`, \`ramfb\`\` |
| ARM64 | BIOS/EFI | `virtio` | `virtio`, `ramfb` |
| s390x | BIOS/EFI | `virtio` | `virtio` |

**Additional resources**
{._additional-resources}

- [SSH access for virtual machines](/openshift-docs-markdown/virt/managing_vms/ssh/virt-accessing-vm-ssh#virt-accessing-vm-ssh)
- [Creating virtual machines from instance types](/openshift-docs-markdown/virt/creating_vm/virt-creating-vms-from-instance-types#virt-creating-vms-from-instance-types)
