7.2.2. Create a VM from a web-hosted image
You can create a virtual machine (VM) from an image on a web page by using the command line.
When the VM is created, the data volume with the image is imported into persistent storage.
Prerequisites
- You must have access credentials for the web page that contains the image.
-
You have installed the
virtctlCLI. -
You have installed the OpenShift CLI (
oc).
Procedure
Create a
VirtualMachinemanifest for your VM and save it as a YAML file. For example, to create a minimal Red Hat Enterprise Linux (RHEL) VM from an image on a web page, run the following command:$ virtctl create vm --name vm-rhel-9 --instancetype u1.small --preference rhel.9 --volume-import type:http,url:https://example.com/rhel9.qcow2,size:10GiReview the
VirtualMachinemanifest for your VM:apiVersion: kubevirt.io/v1 kind: VirtualMachine metadata: name: vm-rhel-9 spec: dataVolumeTemplates: - metadata: name: imported-volume-6dcpf spec: source: http: url: https://example.com/rhel9.qcow2 storage: resources: requests: storage: 10Gi instancetype: name: u1.small preference: name: rhel.9 runStrategy: Always template: spec: domain: devices: {} resources: {} terminationGracePeriodSeconds: 180 volumes: - dataVolume: name: imported-volume-6dcpf name: imported-volume-6dcpf-
metadata.namedefines the VM name. -
spec.dataVolumeTemplates.metadata.namedefines the data volume name. -
spec.dataVolumeTemplates.spec.source.http.urldefines the URL of the image. -
spec.dataVolumeTemplates.spec.storage.resources.requests.storagedefines the size of the storage requested for the data volume. -
spec.instancetype.namedefines the instance type to use to control resource sizing of the VM. -
spec.preference.namedefines the preference to use.
-
Create the VM by running the following command:
$ oc create -f <vm_manifest_file>.yamlThe
oc createcommand creates the data volume and the VM. The CDI controller creates an underlying PVC with the correct annotation and the import process begins. When the import is complete, the data volume status changes toSucceeded. You can start the VM.Data volume provisioning happens in the background, so there is no need to monitor the process.
Verification
The importer pod downloads the image from the specified URL and stores it on the provisioned persistent volume. View the status of the importer pod:
$ oc get podsMonitor the status of the data volume:
$ oc get dv <data_volume_name>If the provisioning is successful, the data volume phase is
Succeeded.Example output:
NAME PHASE PROGRESS RESTARTS AGE imported-volume-6dcpf Succeeded 100.0% 18sVerify that provisioning is complete and that the VM has started by accessing its serial console:
$ virtctl console <vm_name>If the VM is running and the serial console is accessible, the output looks as follows:
Successfully connected to vm-rhel-9 console. The escape sequence is ^]