DE

Create and Manage Virtuelle Machines

After the necessary preparation, we now come to the main topic of how to create, manage, and especially start virtual machines. As previously mentioned, there are both graphical as well as command-line frontends. We’ll begin with Cockpit, since with this approach the start will likely be more seamless.

The Graphical Approach

Fedora recommends Cockpit, a web-based graphical interface primarily designed for server use. Through an extension, Cockpit can also be used to manage virtual machines. It enables access to VMs from other computers on the same network, though we’ll focus on its local usage.

First, we add Cockpit to the already installed packages:

sudo dnf install cockpit cockpit-machines

Cockpit uses a socket that establishes a connection between the browser and the application via port 9090. The socket is managed as Systemd unit:

sudo systemctl start cockpit.socket

For local access, Cockpit can be opened in the browser via http://localhost:9090/. After logging in with a system user, it’s recommended to click on Administrative Access at the top to grant the logged-in user sudo permissions, since without Polkit errors may occur when creating a VM.

In the left menu you’ll find Virtual machines, under which you can create a new virtual machine with Create VM. The process is self-explanatory, though you should make sure to set a root password for the VM.

To actually see what is happening in the machine that were started, use Launch remote viewer under Console. Further down on the same page, there’s also the option to create snapshots of the current VM state and to restore it later on.

Creating Virtual Machines via the Command Line

Cockpit is so convenient because it hides the technical details when creating and registering VMs. For this reason it’s also recommended even for users who prefer to manage and start VMs with CLI tools like virsh and virt-viewer.

However, for those who want to handle VM creation via the command line, several pre-installed tools are available. First, you need a file that simulates a physical hard drive (Virtual Disk Image). This file serves as storage for the virtual machine. There are various ways to create such an image, including dd, fallocate, and truncate. They differ in how exactly the space is allocated.

For most users, it’s easiest to use a QEMU tool to create a qcow2 image. This format only uses as much storage on the host computer’s physical hard drive as is actually written by the guest machine. Qcow2 images also support snapshots.

To create a virtual system, three essential parameters must be specified: a name, the file format, and the storage size that should be available to the guest system. Here an example for Fedora Workstation 40:

sudo qemu-img create -f qcow2 \
    /var/lib/libvirt/images/Fedora-Workstation-40/ \
    Fedora-Workstation-40-202408-16.qcow2 \
    20480

Now for the actual registration of the new machine, for which we’ll use virt-install. The tool takes system specifications as its argument, including name, description, and the distribution or variant of the operating system (e.g., Fedora 40). Available options can be queried with osinfo-query os. You must also specify how much CPU and RAM should be available to the guest. Network settings ensure that the virtual machine can use the host’s internet connection.

We also reference the created disk image, which is typically stored under /var/lib/libvirt/images. Additionally, an image of the desired distribution is needed to perform the installation (in the example: Fedora Workstation 40).

The installation and registration of a virtual machine might look like this:

sudo virt-install --name Fedora40 \
--description 'Fedora 40 Workstation' \
--ram 8192 \
--vcpus 4 \
--disk path=/var/lib/libvirt/images/Fedora-Workstation-40/Fedora-Workstation-40-20240816.qcow2,size=10 \
--os-variant fedora40 \
--network bridge=virbr0 \
--graphics vnc,listen=127.0.0.1,port=5901 \
--cdrom /var/lib/libvirt/images/Fedora-Workstation-40/Fedora-Workstation-Live-x86_64-40-1.14.iso \
--noautoconsole

The Command-Line Approach

The installed packages enable complete management of virtual machines via the command line. With virsh, guest systems registered with libvirt can be easily started, stopped, or immediately terminated. These actions are performed using the commands sudo virsh start <name>, sudo virsh shutdown <name>, and sudo virsh destroy <name>.

A list of all registered systems, including their names, can be displayed with sudo virsh list --all. To unregister a system from libvirt, use sudo virsh undefine <name>. The command sudo virsh undefine <name> --remove-all-storage additionally removes the disk image of the respective VM.

It’s important to note that management tools like virsh (or Cockpit) don’t provide graphical output of the running VMs. For graphical access, you can start a VNC viewer specialized for virtual machines with sudo -E virt-viewer.

After some work with the machine, the current state can be saved as a snapshot using sudo virsh save <name>. You can return to a saved state at any time with sudo virsh restore <filename>.

Article from August 24, 2024.