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
Edit the
VirtualMachine
manifest for your VM. The following example configures a Fedora VM:Example manifest for a Fedora VM
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
labels:
app: <vm_name> (1)
name: <vm_name>
spec:
dataVolumeTemplates:
- apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: <vm_name>
spec:
sourceRef:
kind: DataSource
name: rhel9
namespace: openshift-virtualization-os-images
storage:
resources:
requests:
storage: 30Gi
running: false
template:
metadata:
labels:
kubevirt.io/domain: <vm_name>
spec:
domain:
cpu:
cores: 1
sockets: 2
threads: 1
devices:
disks:
- disk:
bus: virtio
name: rootdisk
- disk:
bus: virtio
name: cloudinitdisk
interfaces:
- masquerade: {}
name: default
rng: {}
features:
smm:
enabled: true
firmware:
bootloader:
efi: {}
resources:
requests:
memory: 8Gi
evictionStrategy: LiveMigrate
networks:
- name: default
pod: {}
volumes:
- dataVolume:
name: <vm_name>
name: rootdisk
- cloudInitNoCloud:
userData: |-
#cloud-config
user: cloud-user
password: '<password>' (2)
chpasswd: { expire: False }
name: cloudinitdisk
1 Specify the name of the virtual machine. 2 Specify the password for cloud-user. Create a virtual machine by using the manifest file:
$ oc create -f <vm_manifest_file>.yaml
Optional: Start the virtual machine:
$ virtctl start <vm_name>