第 3 章 Creating virtual machines
To create a virtual machine (VM) in RHEL 10, you can use the command line or the RHEL 10 web console.
3.1. Creating virtual machines by using the command line 复制链接链接已复制到粘贴板!
You can create a virtual machine (VM) on your RHEL 10 host by using the virt-install utility.
Prerequisites
- Virtualization is enabled on your host system.
- You have a sufficient amount of system resources to allocate to your VMs, such as disk space, RAM, or CPUs. The recommended values might vary significantly depending on the intended tasks and workload of the VMs.
An operating system (OS) installation source is available locally or on a network. This can be one of the following:
- An ISO image of an installation medium
A disk image of an existing VM installation
警告Installing from a host CD-ROM or DVD-ROM device is not possible in RHEL 10. If you select a CD-ROM or DVD-ROM as the installation source when using any VM installation method available in RHEL 10, the installation will fail. For more information, see the Red Hat Knowledgebase.
Also note that Red Hat provides support only for a limited set of guest operating systems.
-
To create a VM that uses the
systemconnection of libvirt, you must have root privileges or be in thelibvirtuser group on the host. For more information, see User-space connection types for virtualization. - Optional: A Kickstart file can be provided for faster and easier configuration of the installation.
Procedure
To create a VM and start its OS installation, use the
virt-installcommand, along with the following mandatory arguments:--name- the name of the new machine
--memory- the amount of allocated memory
--vcpus- the number of allocated virtual CPUs
--disk- the type and size of the allocated storage
--cdromor--location- the type and location of the OS installation source
--osinfothe OS type and version that you intend to install
注意To list all available values for the
--osinfoargument, run thevirt-install --osinfo listcommand.For more details, you can also run the
osinfo-query oscommand. However, you might need to install thelibosinfo-binpackage first.Based on the chosen installation method, the necessary options and values can vary. See the following examples for more details.
Create a VM and install an OS from a local ISO file:
The following command creates a VM named demo-guest1 that installs the Windows 10 OS from an ISO image locally stored in the /home/username/Downloads/Win10install.iso file. This VM is also allocated with 2048 MiB of RAM and 2 vCPUs, and an 80 GiB qcow2 virtual disk is automatically configured for the VM.
# virt-install \ --name demo-guest1 --memory 2048 \ --vcpus 2 --disk size=80 --osinfo win10 \ --cdrom /home/username/Downloads/Win10install.iso
Create a VM, install an OS from a live CD, and do not create a permanent disk:
The following command creates a VM named demo-guest2 that uses the /home/username/Downloads/rhel10.iso image to run a RHEL 10 OS from a live CD. No disk space is assigned to this VM, so changes made during the session will not be preserved. In addition, the VM is allocated with 4096 MiB of RAM and 4 vCPUs.
# virt-install \ --name demo-guest2 --memory 4096 --vcpus 4 \ --disk none --livecd --osinfo rhel10.0 \ --cdrom /home/username/Downloads/rhel10.iso
Create a VM and import an existing disk image:
The following command creates a RHEL 10 VM named demo-guest3 that connects to an existing disk image, /home/username/backup/disk.qcow2. This is similar to physically moving a hard drive between machines, so the OS and data available to demo-guest3 are determined by how the image was handled previously. In addition, this VM is allocated with 2048 MiB of RAM and 2 vCPUs.
# virt-install \ --name demo-guest3 --memory 2048 --vcpus 2 \ --osinfo rhel10.0 --import \ --disk /home/username/backup/disk.qcow2Note that you must use the
--osinfooption when importing a disk image. If it is not provided, the performance of the created VM will be negatively affected.
Create a VM and install an OS from a remote URL:
The following command creates a VM named demo-guest4 that installs from the
http://example.com/OS-installURL. For the installation to start successfully, the URL must contain a working OS installation tree. In addition, the OS is automatically configured by using the /home/username/ks.cfg kickstart file. This VM is also allocated with 2048 MiB of RAM, 2 vCPUs, and a 160 GiB qcow2 virtual disk.# virt-install \ --name demo-guest4 --memory 2048 --vcpus 2 --disk size=160 \ --osinfo rhel10.0 --location http://example.com/OS-install \ --initrd-inject /home/username/ks.cfg --extra-args="inst.ks=file:/ks.cfg console=tty0 console=ttyS0,115200n8"
Create a VM and install an OS in a text-only mode:
The following command creates a VM named demo-guest5 that installs from a
RHEL10.isoimage file in text-only mode, without graphics. It connects the guest console to the serial console. The VM has 16384 MiB of memory, 16 vCPUs, and 280 GiB disk. This kind of installation is useful when connecting to a host over a slow network link.# virt-install \ --name demo-guest5 --memory 16384 --vcpus 16 --disk size=280 \ --osinfo rhel10.0 --location RHEL10.iso \ --graphics none --extra-args='console=ttyS0'
Create a VM and install an OS in graphical mode:
The following command creates a VM named demo-guest-6, which has the same configuration as demo-guest5, but provides the host device
pci_0003_00_00_0for networking and configures graphics for a graphical installation.# virt-install \ --name demo-guest6 --memory 16384 --vcpus 16 --disk size=280 \ --os-info rhel10.0 --location RHEL10.iso --graphics vnc,listen=0.0.0.0,5901 \ --input keyboard,bus=virtio --input mouse,bus=virtio \ --hostdev pci_0003_00_00_0 --network noneNote that the name of the host device available for installation can be retrieved by using the
virsh nodedev-list --cap pcicommand. To use the the installation GUI, you can connect any VNC viewer to the host’s IP at VNC port 5901 when the installation starts. However, you might have to open this port in the firewall first, for example:# firewall-cmd --add-port 5901/tcp
Create a VM on a remote host:
The following command creates a VM named demo-guest7, which has the same configuration as demo-guest5, but resides on the 192.0.2.1 remote host.
# virt-install \ --connect qemu+ssh://root@192.0.2.1/system --name demo-guest7 --memory 16384 \ --vcpus 16 --disk size=280 --osinfo rhel10.0 --location RHEL10.iso \ --graphics none --extra-args='console=ttyS0'
Create a VM on a remote host and use a DASD mediated device as storage:
The following command creates a VM named demo-guest-8, which has the same configuration as demo-guest5, but for its storage, it uses a DASD mediated device
mdev_30820a6f_b1a5_4503_91ca_0c10ba12345a_0_0_29a8, and assigns it device number1111.# virt-install \ --name demo-guest8 --memory 16384 --vcpus 16 --disk size=280 \ --osinfo rhel10.0 --location RHEL10.iso --graphics none \ --disk none --hostdev mdev_30820a6f_b1a5_4503_91ca_0c10ba12345a_0_0_29a8,address.type=ccw,address.cssid=0xfe,address.ssid=0x0,address.devno=0x1111,boot-order=1 \ --extra-args 'rd.dasd=0.0.1111'Note that the name of the mediated device available for installation can be retrieved by using the
virsh nodedev-list --cap mdevcommand.
-
For additional options and examples of
virt-installcommands, see thevirt-install (1)man page on your system.
Verification
- If the VM is created successfully, a virt-viewer window opens with a graphical console of the VM and starts the guest OS installation.
Troubleshooting
If
virt-installfails with acannot find default networkerror:Ensure that the
libvirt-daemon-config-networkpackage is installed:# dnf info libvirt-daemon-config-network Installed Packages Name : libvirt-daemon-config-network [...]Verify that the
libvirtdefault network is active and configured to start automatically:# virsh net-list --all Name State Autostart Persistent -------------------------------------------- default active yes yesIf it is not, activate the default network and set it to auto-start:
# virsh net-autostart default Network default marked as autostarted # virsh net-start default Network default startedIf activating the default network fails with the following error, the
libvirt-daemon-config-networkpackage has not been installed correctly.error: failed to get network 'default' error: Network not found: no network with matching name 'default'To fix this, re-install
libvirt-daemon-config-network:# dnf reinstall libvirt-daemon-config-networkIf activating the default network fails with an error similar to the following, a conflict has occurred between the default network’s subnet and an existing interface on the host.
error: Failed to start network default error: internal error: Network is already in use by interface ens2To fix this, use the
virsh net-edit defaultcommand and change the192.0.2.*values in the configuration to a subnet not already in use on the host.