Creating virtual machines from the command line

You can create virtual machines (VMs) from the command line by editing or creating a VirtualMachine manifest.

Creating a VM from a VirtualMachine manifest

You can create a virtual machine (VM) from a VirtualMachine manifest.

Procedure

  1. Edit the VirtualMachine manifest for your VM. The following example configures a Fedora VM:

    Example manifest for a Fedora VM

    1. apiVersion: kubevirt.io/v1
    2. kind: VirtualMachine
    3. metadata:
    4. labels:
    5. app: <vm_name> (1)
    6. name: <vm_name>
    7. spec:
    8. dataVolumeTemplates:
    9. - apiVersion: cdi.kubevirt.io/v1beta1
    10. kind: DataVolume
    11. metadata:
    12. name: <vm_name>
    13. spec:
    14. sourceRef:
    15. kind: DataSource
    16. name: rhel9
    17. namespace: openshift-virtualization-os-images
    18. storage:
    19. resources:
    20. requests:
    21. storage: 30Gi
    22. running: false
    23. template:
    24. metadata:
    25. labels:
    26. kubevirt.io/domain: <vm_name>
    27. spec:
    28. domain:
    29. cpu:
    30. cores: 1
    31. sockets: 2
    32. threads: 1
    33. devices:
    34. disks:
    35. - disk:
    36. bus: virtio
    37. name: rootdisk
    38. - disk:
    39. bus: virtio
    40. name: cloudinitdisk
    41. interfaces:
    42. - masquerade: {}
    43. name: default
    44. rng: {}
    45. features:
    46. smm:
    47. enabled: true
    48. firmware:
    49. bootloader:
    50. efi: {}
    51. resources:
    52. requests:
    53. memory: 8Gi
    54. evictionStrategy: LiveMigrate
    55. networks:
    56. - name: default
    57. pod: {}
    58. volumes:
    59. - dataVolume:
    60. name: <vm_name>
    61. name: rootdisk
    62. - cloudInitNoCloud:
    63. userData: |-
    64. #cloud-config
    65. user: cloud-user
    66. password: '<password>' (2)
    67. chpasswd: { expire: False }
    68. name: cloudinitdisk
    1Specify the name of the virtual machine.
    2Specify the password for cloud-user.
  2. Create a virtual machine by using the manifest file:

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

    1. $ virtctl start <vm_name>