KVM-based virtual machines can easily be configured on Ubuntu using the virt-install command-line tool, the virt-manager GUI tool, or the Virtual Machines module of the Cockpit web console. This chapter will use Cockpit to install an operating system as a KVM guest on an Ubuntu host. The chapter titled Creating Ubuntu 22.04 KVM Virtual Machines using Cockpit and virt-manager will cover using the virt-manager tool to create new virtual machines.
The next chapter, Creating Ubuntu 22.04 KVM Virtual Machines with virt-install and virsh will cover the command-line approach to virtual machine creation.
Installing the Cockpit Virtual Machines Module
The virtual machines module may not be included in a standard Cockpit installation by default. Assuming that Cockpit is installed and configured, the virtual machines module may be installed as follows:
# apt install cockpit-machinesCode language: plaintext (plaintext)
Once installed, the Virtual Machines option (marked A in Figure 24-1) will appear in the navigation panel the next time you log into the Cockpit interface:
Creating a Virtual Machine in Cockpit
To create a virtual machine in Cockpit, click the Create VM button marked B in Figure 24-1 to display the creation dialog.
Within the dialog, enter a name for the machine and choose whether the installation media is in the form of an ISO accessible via a URL or a local filesystem path, or select the vendor and operating system type information for the guest request and choose the Download an OS option to have the installation image downloaded automatically during the installation process.
Also, specify the size of the virtual disk drive to be used for the operating system installation and the amount of memory to be allocated to the virtual machine:
Click on the Create button to build the virtual machine. After the creation process is complete, select the new VM from the list to display the configuration details, as shown in Figure 24-3:
As described in An Overview of Ubuntu 22.04 Virtualization Techniques, KVM provides virtual machines with several options in terms of network configuration. To view and change the network settings of a virtual machine, scroll down to the Network interfaces section of the VM Overview screen and click the Edit button:
In the resulting dialog, the Network Type menu may be used to change the type of network connection, for example, from Virtual network (NAT) to direct attachment (MacVTap) or Bridge to LAN.
Starting the Installation
To start the new virtual machine and install the guest operating system from the designated installation media, click the Install button at the top of the overview page. Cockpit will start the virtual machine and scroll down to the Console view where the guest OS screen will appear:
If the installation fails, check the message to see if an error occurred when opening the installation image. This usually occurs because the QEMU emulator runs as a user named qemu, which does not have access to the directory in which the ISO installation image is located. To resolve this issue, open a terminal window (or connect with SSH if the system is remote), change directory to the location of the ISO image file, and add the libvirt-qemu user to the access control list (ACL) of the parent directory as follows:
# cd /path/to/iso/directory # setfacl -m u:libvirt-qemu:x ..Code language: plaintext (plaintext)
After making this change, check the setting as follows:
# getfacl .. # file: .. # owner: demo # group: demo user::rwx user:libvirt-qemu:--x group::r-x mask::r-x other::---Code language: plaintext (plaintext)
Once these changes have been made, click the Install button again to complete the installation.
To complete the installation, interact with the screen in the Consoles view just as you would if you were installing the operating system on physical hardware. If the console is too small to accommodate the entire guest operating system screen, click the Expand button in the top right-hand corner.
It is also possible to connect with and display the graphical console for the VM from outside the Cockpit browser session using the virt-viewer tool. To install virt-viewer on an Ubuntu system, run the following command:
# apt install virt-viewerCode language: plaintext (plaintext)
The virt-viewer tool is also available for Windows systems and can be downloaded from the following URL:
To connect with a virtual machine running on the local host, run virt-viewer and select the virtual machine to which you wish to connect from the resulting dialog:
The above command will list system-based virtual machines. To list and access session-based guests, launch virt-viewer as follows:
$ virt-viewer --connect qemu:///sessionCode language: plaintext (plaintext)
Alternatively, it is also possible to specify the virtual machine name and bypass the selection dialog entirely, for example:
# virt-viewer demo-vm-guestCode language: plaintext (plaintext)
To connect a virt-viewer instance to a virtual machine running on a remote host using SSH, the following command can be used:
$ virt-viewer --connect qemu+ssh://<user>@<host>/system <guest name>Code language: plaintext (plaintext)
$ virt-viewer --connect qemu+ssh://[email protected]/system demo_vm_guestCode language: plaintext (plaintext)
When using this technique, it is important to note that you will be prompted twice for the user password before the connection is fully established.
Once the virtual machine has been created, the Cockpit interface can monitor the machine and perform tasks such as rebooting, shutting down, or deleting the guest system. An option is also included on the Disks panel to add disks to the virtual machine configuration.
Working with Storage Volumes and Storage Pools
When a virtual machine is created, it will usually have at least one virtual disk drive. The images that represent these virtual disk drives are stored in storage pools. A storage pool can be an existing directory on a local filesystem, a filesystem partition, a physical disk device, Logical Volume Management (LVM) volume group, or even a remote network file system (NFS).
Each storage pool is divided into one or more storage volumes. Storage volumes are typically individual image files, each representing a single virtual disk drive, but they can also take the form of physical disk partitions, entire disk drives, or LVM volume groups.
When a virtual machine was created using the previous steps, a default storage pool was created to store virtual machine images. This default storage pool occupies space on the root filesystem and can be reviewed from within the Cockpit Virtual Machine interface by selecting the Storage Pools option at the top of the panel marked C in Figure 24-1 above.
When selected, the screen shown in Figure 24-7 below will appear containing a list of storage pools currently configured on the system:
In the above example, the default storage pool is located on the root filesystem and stores the virtual machine image in the /var/lib/libvirtd/images directory. To view the storage volumes contained within the pool, select the Storage Volumes tab highlighted in Figure 24-8:
In the case of the demo guest, the storage volume takes the form of an image file named demovm-guest.qcow2. In addition, the pool also includes a storage volume containing the installation ISO image. To find out which storage volume a particular virtual machine uses, return to the main Virtual Machine Cockpit screen, select the virtual machine, and scroll to the Disks panel, as shown in Figure 24-9:
Although using the default storage pool is acceptable for testing purposes and early experimentation, it is recommended that additional pools be created for general virtualization use. To create a new storage pool, display the Storage Pools screen within Cockpit and click on the Create storage pool button to display the dialog shown in Figure 24-10:
In the above example, a new storage pool is being created named MyPool using a file system partition mounted as /MyPool within the local filesystem (the topic of disk drives, partitions, and mount points is covered later in the chapter entitled Adding a New Disk Drive to an Ubuntu 22.04 System). Once created, the pool will be listed within the Cockpit storage pool screen and can contain storage volumes as new virtual machines are created.
This chapter has outlined using the Cockpit web-based interface to create and manage KVM-based virtual machines. The Cockpit interface has the advantage of not requiring access to a desktop environment running on the host system. An alternative option is using the virt-manager graphical tool outlined in the next chapter.