1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-05 12:46:18 +01:00

CNV-39983: Added "virtctl create vm" to the instructions

This commit is contained in:
Jaromir Hradilek
2025-03-05 14:36:43 +01:00
committed by openshift-cherrypick-robot
parent 2cbf25d56e
commit 838daa827b
8 changed files with 304 additions and 203 deletions

View File

@@ -1,16 +1,27 @@
// Module included in the following assemblies:
//
// * virt/virtual_machines/creating_vms_rh/virt-creating-vms-from-cli.adoc
// * virt/creating_vms_advanced/creating_vms_cli/virt-creating-vms-from-cli.adoc
:_mod-docs-content-type: PROCEDURE
[id="virt-creating-vm-cli_{context}"]
= Creating a VM from a VirtualMachine manifest
You can create a virtual machine (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
. Edit the `VirtualMachine` manifest for your VM. The following example configures a {op-system-base-full} VM:
. Create a `VirtualMachine` manifest for your VM and save it as a YAML file. For example, to create a minimal {op-system-base-full} VM, run the following command:
+
[source,terminal]
----
$ virtctl create vm --name rhel-9-minimal --volume-import type:ds,src:openshift-virtualization-os-images/rhel9
----
. Review the `VirtualMachine` manifest for your VM:
+
[NOTE]
====
@@ -20,38 +31,46 @@ This example manifest does not configure VM authentication.
.Example manifest for a {op-system-base} VM
[source,yaml]
----
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: rhel-9-minimal
spec:
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: rhel-9-minimal # <1>
spec:
dataVolumeTemplates:
- metadata:
name: rhel-9-minimal-volume
spec:
sourceRef:
kind: DataSource
name: rhel9 <1>
namespace: openshift-virtualization-os-images <2>
storage: {}
- metadata:
name: imported-volume-mk4lj
spec:
sourceRef:
kind: DataSource
name: rhel9 # <2>
namespace: openshift-virtualization-os-images # <3>
storage:
resources: {}
instancetype:
name: u1.medium <3>
inferFromVolume: imported-volume-mk4lj # <4>
inferFromVolumeFailurePolicy: Ignore
preference:
name: rhel.9 <4>
inferFromVolume: imported-volume-mk4lj # <5>
inferFromVolumeFailurePolicy: Ignore
runStrategy: Always
template:
spec:
domain:
devices: {}
memory:
guest: 512Mi
resources: {}
terminationGracePeriodSeconds: 180
volumes:
- dataVolume:
name: rhel-9-minimal-volume
name: rootdisk
- dataVolume:
name: imported-volume-mk4lj
name: imported-volume-mk4lj
----
<1> The `rhel9` golden image is used to install {op-system-base} 9 as the guest operating system.
<2> Golden images are stored in the `openshift-virtualization-os-images` namespace.
<3> The `u1.medium` instance type requests 1 vCPU and 4Gi memory for the VM. These resource values cannot be overridden within the VM.
<4> The `rhel.9` preference specifies additional attributes that support the {op-system-base} 9 guest operating system.
<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.
. Create a virtual machine by using the manifest file:
+
@@ -64,5 +83,5 @@ $ oc create -f <vm_manifest_file>.yaml
+
[source,terminal]
----
$ virtctl start <vm_name> -n <namespace>
$ virtctl start <vm_name>
----

View File

@@ -1,73 +1,75 @@
// Module included in the following assemblies:
//
// * virt/virtual_machines/creating_vms_custom/virt-creating-vms-by-cloning-pvcs.adoc
// * virt/creating_vms_advanced/creating_vms_cli/virt-creating-vms-by-cloning-pvcs.adoc
:_mod-docs-content-type: PROCEDURE
[id="virt-creating-vm-cloning-pvc-data-volume-template_{context}"]
= Creating a VM from a cloned PVC by using a data volume template
You can create a virtual machine (VM) that clones the persistent volume claim (PVC) of an existing VM by using a data volume template.
This method creates a data volume whose lifecycle is dependent on the original VM. Deleting the original VM deletes the cloned data volume and its associated PVC.
You can create a virtual machine (VM) that clones the persistent volume claim (PVC) of an existing VM by using a data volume template. This method creates a data volume whose lifecycle is independent on the original VM.
.Prerequisites
* The VM with the source PVC must be powered down.
* You must have the `virtctl` command-line tool installed.
.Procedure
. Create a `VirtualMachine` manifest as shown in the following example:
. Create a `VirtualMachine` manifest for your VM and save it as a YAML file, for example:
+
[source,terminal]
----
$ virtctl create vm --name rhel-9-clone --volume-import type:pvc,src:my-project/imported-volume-q5pr9
----
. Review the `VirtualMachine` manifest for your VM:
+
[source,yaml]
----
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
labels:
kubevirt.io/vm: vm-dv-clone
name: vm-dv-clone <1>
name: rhel-9-clone # <1>
spec:
runStrategy: Halted
template:
metadata:
labels:
kubevirt.io/vm: vm-dv-clone
spec:
domain:
devices:
disks:
- disk:
bus: virtio
name: root-disk
resources:
requests:
memory: 64M
volumes:
- dataVolume:
name: favorite-clone
name: root-disk
dataVolumeTemplates:
- metadata:
name: favorite-clone
name: imported-volume-h4qn8
spec:
storage:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
source:
pvc:
namespace: <source_namespace> <2>
name: "<source_pvc>" <3>
name: imported-volume-q5pr9 # <2>
namespace: my-project # <3>
storage:
resources: {}
instancetype:
inferFromVolume: imported-volume-h4qn8 # <4>
inferFromVolumeFailurePolicy: Ignore
preference:
inferFromVolume: imported-volume-h4qn8 # <5>
inferFromVolumeFailurePolicy: Ignore
runStrategy: Always
template:
spec:
domain:
devices: {}
memory:
guest: 512Mi
resources: {}
terminationGracePeriodSeconds: 180
volumes:
- dataVolume:
name: imported-volume-h4qn8
name: imported-volume-h4qn8
----
<1> Specify the name of the VM.
<2> Specify the namespace of the source PVC.
<3> Specify the name of the source PVC.
<1> The VM name.
<2> The name of the source PVC.
<3> The namespace of the source PVC.
<4> If the PVC source has appropriate labels, the instance type is inferred from the selected `DataSource` object.
<5> If the PVC source has appropriate labels, the preference is inferred from the selected `DataSource` object.
. Create the virtual machine with the PVC-cloned data volume:
+
[source,terminal]
----
$ oc create -f <vm-clone-datavolumetemplate>.yaml
$ oc create -f <vm_manifest_file>.yaml
----

View File

@@ -0,0 +1,95 @@
// Module included in the following assemblies:
//
// * virt/creating_vms_advanced/creating_vms_cli/virt-creating-vms-from-container-disks.adoc
:_mod-docs-content-type: PROCEDURE
[id="virt-creating-vm-import-cli_{context}"]
= Creating a VM from a container disk by using the command line
You can create a virtual machine (VM) from a container disk by using the command line.
.Prerequisites
* You must have access credentials for the container registry that contains the container disk.
* You must have the `virtctl` command-line tool installed.
.Procedure
. Create a `VirtualMachine` manifest for your VM and save it as a YAML file. For example, to create a minimal {op-system-base-full} VM from a container disk, run the following command:
+
[source,terminal]
----
$ virtctl create vm --name vm-rhel-9 --instancetype u1.small --preference rhel.9 --volume-containerdisk src:registry.redhat.io/rhel9/rhel-guest-image:9.5
----
. Review the `VirtualMachine` manifest for your VM:
+
[source,yaml]
----
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: vm-rhel-9 # <1>
spec:
instancetype:
name: u1.small # <2>
preference:
name: rhel.9 # <3>
runStrategy: Always
template:
metadata:
creationTimestamp: null
spec:
domain:
devices: {}
resources: {}
terminationGracePeriodSeconds: 180
volumes:
- containerDisk:
image: registry.redhat.io/rhel9/rhel-guest-image:9.5 # <4>
name: vm-rhel-9-containerdisk-0
----
<1> The VM name.
<2> The instance type to use to control resource sizing of the VM.
<3> The preference to use.
<4> The URL of the container disk.
. Create the VM by running the following command:
+
[source,terminal]
----
$ oc create -f <vm_manifest_file>.yaml
----
.Verification
. Monitor the status of the VM:
+
[source,terminal]
----
$ oc get vm <vm_name>
----
+
If the provisioning is successful, the VM status is `Running`:
+
.Example output
[source,terminal]
----
NAME AGE STATUS READY
vm-rhel-9 18s Running True
----
. Verify that provisioning is complete and that the VM has started by accessing its serial console:
+
[source,terminal]
----
$ virtctl console <vm_name>
----
+
If the VM is running and the serial console is accessible, the output looks as follows:
+
.Example output
[source,terminal]
----
Successfully connected to vm-rhel-9 console. The escape sequence is ^]
----

View File

@@ -1,129 +0,0 @@
// Module included in the following assemblies:
//
// * virt/virtual_machines/creating_vms_custom/virt-creating-vms-from-container-disks.adoc
// * virt/virtual_machines/creating_vms_custom/virt-creating-vms-from-web-images.adoc
ifeval::["{context}" == "virt-creating-vms-from-web-images"]
:url:
:title-frag: from an image on a web page
:a-object: an image
:object: image
:data-source: web page
endif::[]
ifeval::["{context}" == "virt-creating-vms-from-container-disks"]
:container-disks:
:title-frag: from a container disk
:a-object: a container disk
:object: container disk
:data-source: container registry
endif::[]
:_mod-docs-content-type: PROCEDURE
[id="virt-creating-vm-import-cli_{context}"]
= Creating a VM {title-frag} by using the command line
You can create a virtual machine (VM) {title-frag} by using the command line.
When the virtual machine (VM) is created, the data volume with the {object} is imported into persistent storage.
.Prerequisites
* You must have access credentials for the {data-source} that contains the {object}.
.Procedure
. Edit the `VirtualMachine` manifest and save it as a `vm-rhel-datavolume.yaml` file:
+
[%collapsible]
====
[source,yaml]
----
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
creationTimestamp: null
name: vm-rhel-datavolume <1>
labels:
kubevirt.io/vm: vm-rhel-datavolume
spec:
dataVolumeTemplates:
- metadata:
creationTimestamp: null
name: rhel-dv <2>
spec:
sourceRef:
kind: DataSource
name: rhel9
namespace: openshift-virtualization-os-images
storage:
resources:
requests:
storage: 10Gi <3>
instancetype:
name: u1.small <4>
preference:
inferFromVolume: datavolumedisk1
runStrategy: Always
template:
metadata:
creationTimestamp: null
labels:
kubevirt.io/vm: vm-rhel-datavolume
spec:
domain:
devices: {}
resources: {}
terminationGracePeriodSeconds: 180
volumes:
- dataVolume:
name: rhel-dv
name: datavolumedisk1
status: {}
----
<1> Specify the name of the VM.
<2> Specify the name of the data volume.
<3> Specify the size of the storage requested for the data volume.
<4> Optional: Specify the instance type to use to control resource sizing of the VM.
====
. Create the VM by running the following command:
+
[source,terminal]
----
$ oc create -f vm-rhel-datavolume.yaml
----
+
The `oc create` command 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 to `Succeeded`. 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 {object} from the specified URL and stores it on the provisioned persistent volume. View the status of the importer pod by running the following command:
+
[source,terminal]
----
$ oc get pods
----
. Monitor the data volume until its status is `Succeeded` by running the following command:
+
[source,terminal]
----
$ oc describe dv rhel-dv <1>
----
<1> Specify the data volume name that you defined in the `VirtualMachine` manifest.
. Verify that provisioning is complete and that the VM has started by accessing its serial console:
+
[source,terminal]
----
$ virtctl console vm-rhel-datavolume
----
ifeval::["{context}" == "creating-vms-from-web-images"]
:!url:
endif::[]
ifeval::["{context}" == "creating-vms-from-container-disks"]
:!container-disks:
endif::[]

View File

@@ -0,0 +1,119 @@
// Module included in the following assemblies:
//
// * virt/creating_vms_advanced/creating_vms_advanced_web/virt-creating-vms-from-web-images.adoc
:_mod-docs-content-type: PROCEDURE
[id="virt-creating-vm-import-cli_{context}"]
= Creating a VM from an image on a web page by using the command line
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 must have the `virtctl` command-line tool installed.
.Procedure
. Create a `VirtualMachine` manifest for your VM and save it as a YAML file. For example, to create a minimal {op-system-base-full} VM from an image on a web page, run the following command:
+
[source,terminal]
----
$ virtctl create vm --name vm-rhel-9 --instancetype u1.small --preference rhel.9 --volume-import type:http,url:https://example.com/rhel9.qcow2,size:10Gi
----
. Review the `VirtualMachine` manifest for your VM:
+
[source,yaml]
----
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
name: vm-rhel-9 # <1>
spec:
dataVolumeTemplates:
- metadata:
name: imported-volume-6dcpf # <2>
spec:
source:
http:
url: https://example.com/rhel9.qcow2 # <3>
storage:
resources:
requests:
storage: 10Gi # <4>
instancetype:
name: u1.small # <5>
preference:
name: rhel.9 # <6>
runStrategy: Always
template:
spec:
domain:
devices: {}
resources: {}
terminationGracePeriodSeconds: 180
volumes:
- dataVolume:
name: imported-volume-6dcpf
name: imported-volume-6dcpf
----
<1> The VM name.
<2> The data volume name.
<3> The URL of the image.
<4> The size of the storage requested for the data volume.
<5> The instance type to use to control resource sizing of the VM.
<6> The preference to use.
. Create the VM by running the following command:
+
[source,terminal]
----
$ oc create -f <vm_manifest_file>.yaml
----
+
The `oc create` command 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 to `Succeeded`. 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:
+
[source,terminal]
----
$ oc get pods
----
. Monitor the status of the data volume:
+
[source,terminal]
----
$ oc get dv <data_volume_name>
----
+
If the provisioning is successful, the data volume phase is `Succeeded`:
+
.Example output
[source,terminal]
----
NAME PHASE PROGRESS RESTARTS AGE
imported-volume-6dcpf Succeeded 100.0% 18s
----
. Verify that provisioning is complete and that the VM has started by accessing its serial console:
+
[source,terminal]
----
$ virtctl console <vm_name>
----
+
If the VM is running and the serial console is accessible, the output looks as follows:
+
.Example output
[source,terminal]
----
Successfully connected to vm-rhel-9 console. The escape sequence is ^]
----

View File

@@ -15,4 +15,4 @@ You must install the xref:../../../virt/managing_vms/virt-installing-qemu-guest-
include::modules/virt-creating-vm-custom-image-web.adoc[leveloffset=+1]
include::modules/virt-creating-vm-import-cli.adoc[leveloffset=+1]
include::modules/virt-creating-vm-web-page-cli.adoc[leveloffset=+1]

View File

@@ -13,13 +13,8 @@ You can create virtual machines (VMs) from the command line by editing or creati
You can also xref:../../../virt/creating_vm/virt-creating-vms-from-instance-types.adoc#virt-creating-vms-from-instance-types[create VMs from instance types by using the web console].
====
[id="creating-manifests-virtctl_{context}"]
== Creating manifests by using the virtctl tool
You can use the `virtctl` CLI utility to simplify creating manifests for VMs, VM instance types, and VM preferences. For more information, see xref:../../../virt/getting_started/virt-using-the-cli-tools.adoc#vm-manifest-creation-commands_virt-using-the-cli-tools[VM manifest creation commands].
include::modules/virt-creating-vm-cli.adoc[leveloffset=+1]
.Next steps
* xref:../../../virt/managing_vms/virt-accessing-vm-ssh.adoc#virt-accessing-vm-ssh[Configuring SSH access to virtual machines]
* xref:../../../virt/managing_vms/virt-accessing-vm-ssh.adoc#virt-accessing-vm-ssh[Configuring SSH access to virtual machines]

View File

@@ -44,4 +44,4 @@ include::modules/virt-disabling-tls-for-registry.adoc[leveloffset=+1]
include::modules/virt-creating-vm-custom-image-web.adoc[leveloffset=+1]
include::modules/virt-creating-vm-import-cli.adoc[leveloffset=+1]
include::modules/virt-creating-vm-container-disk-cli.adoc[leveloffset=+1]