Virtualisierung – Erste Schritte

Jan Kuparinen Version F34 onwards Last review: 2020-11-28 Needs Review!

Fedora uses the libvirt family of tools as its virtualization solution.

Unterstützung für Hardware-Virtualisierung aktivieren

Dieser Abschnitt beschreibt die Einrichtung von libvirt auf Ihrem System. Nach der Einrichtung von libvirt können Sie virtualisierte Gastbetriebssysteme, auch virtuelle Maschinen genannt, erstellen.

Systemvoraussetzungen

Um Virtualisierung unter Fedora auszuführen, benötigen Sie:

  • Pro Gastsystem werden mindestens 600 MB Festplattenspeicher benötigt. Ein minimales Fedora-System mit Befehlszeile benötigt 600 MB Speicherplatz. Standardmäßige Fedora-Desktop-Gastsysteme benötigen mindestens 3 GB Speicherplatz.

  • At least 256MB of RAM per guest, plus 256MB for the base operating system. At least 756MB is recommended for each guest of a modern operating system. A good way to estimate this is to think about how much memory is required for the operating system normally, and allocate that amount to the virtualized guest.

KVM benötigt eine CPU mit Virtualisierungserweiterungen, die in den meisten Consumer-CPUs vorhanden sind. Diese Erweiterungen heißen Intel VT oder AMD-V. Um zu überprüfen, ob Ihre CPU diese unterstützt, führen Sie folgenden Befehl aus:

$ grep -E '^flags.*(vmx|svm)' /proc/cpuinfo

If this command results in nothing printed, your system does not support the relevant virtualization extensions. You can still use QEMU/KVM, but the emulator will fall back to software virtualization, which is much slower.

Virtualisierungssoftware installieren

When installing Fedora, you can install the virtualization packages by selecting Virtualization in the Base Group in the installer.

For existing Fedora installations, you can install the virtualization tools via the command line using the Virtualization Package Group. To view the packages, run:

$ dnf group info virtualization

Group: Virtualization
 Description: These packages provide a graphical virtualization environment.
 Mandatory Packages:
   virt-install
 Default Packages:
   libvirt-daemon-config-network
   libvirt-daemon-kvm
   qemu-kvm
   virt-manager
   virt-viewer
 Optional Packages:
   libguestfs-tools
   python3-libguestfs
   virt-top
  1. Run the following command to install the mandatory and default packages in the virtualization group:

    $ sudo dnf install @virtualization

    Alternativ können Sie die obligatorischen, Standard- und optionalen Pakete mit folgendem Befehl installieren:

    $ sudo dnf group install --with-optional virtualization
  2. Nach der Installation der Pakete starten Sie den libvirtd-Dienst:

    $ sudo systemctl start libvirtd

    Um den Dienst beim Systemstart zu starten, führen Sie Folgendes aus:

    $ sudo systemctl enable libvirtd
  3. To verify that the KVM kernel modules are properly loaded:

    $ lsmod | grep kvm
    kvm_amd               114688  0
    kvm                   831488  1 kvm_amd

    Wenn dieser Befehl kvm_intel oder kvm_amd ausgibt, ist KVM ordnungsgemäß konfiguriert.

Netzwerkunterstützung

By default, libvirt will create a private network for your guests on the host machine. This private network will use a 192.168.x.x subnet and not be reachable directly from the network the host machine is on. However, virtual guests can use the host machine as a gateway and can connect out via it. If you need to provide services on your guests that are reachable via other machines on your host network you can use iptables DNAT rules to forward in specific ports, or you can set up a bridged environment.

See the libvirt networking setup page for more information on how to setup a bridged network.

Virtuelle Maschinen erstellen

Die Installation von Fedora-Gastsystemen mit Anaconda wird unterstützt. Die Installation kann über die Befehlszeile mit dem Programm virt-install oder über die Benutzeroberfläche mit dem Programm virt-manager gestartet werden.

Einen Gast mit virt-install erstellen

virt-install is a command-line based tool for creating virtualized guests. Execute virt-install --help for command line help, or you can find the manual page at man 1 virt-install.

To use the virt-install command, you should first download an ISO of the Fedora version you wish to install. You can find the latest Fedora images at https://fedoraproject.org. This ISO is only needed during Fedora installation, and can be deleted to free up storage space afterwards if desired.

In this example we’ll use Fedora Workstation.

Planning VM Resources

Adjust the ram, vcpus, and disk size parameters according to the resources you have available.

  • Storage: An easy way to check your disk size from a bash shell is using the df(1)` utility from the shell:

    $ df -h
  • Memory: You can check your available memory from the shell using free(1):

    $ free -m
  • VCPU: Sie können Ihre Prozessorinformationen mit lscpu(1) überprüfen:

    $ lscpu

When allocating resources to your VM, keep in mind the minimum system requirements for the version of Fedora you are installing as well as your use case requirements. For Fedora 43, you can find this in the Release Notes.

Speicher für die VM erstellen

The libvirt default storage pool is located at `/var/lib/libvirt/images - which is the parent file path we use in this example. For individuals who are lacking enough storage in that path, you can simply mount a new disk or partition to that directory path (from the BASH shell, type man 1 mount) or select a new path. In the example virt-install command below, the disk did not exist prior to running virt-install. When the specified disk is not pre-existing, you must specify the size so virt-install can create a disk for you. If your disk already exists, you can safely remove the ,size=20 parameter from the disk argument.

You have several disk storage options for your VM. While it’s outside the scope of this article to discuss these in detail, the following are a few common options. These examples use 20G as the upper limit for disk size, but you can adjust this size to fit your needs.

Again, you do not need to manually allocate storage using the example options shown below if you specify the size parameter in the virt-install example shown below.

Raw File (Non-Sparse)

To create a fully allocated (non-sparse) raw file:

$ sudo dd if=/dev/zero of=/var/lib/libvirt/images/guest.img bs=1M count=20480

you can also use fallocate(1):

$ sudo fallocate -l 20480M /var/lib/libvirt/images/guest.img
Raw File (Sparse)

To create a dynamically allocated (sparse) raw file:

$ sudo rm -f /var/lib/libvirt/images/guest.img
$ sudo truncate --size=20480M /var/lib/libvirt/images/guest.img
QCOW2 To create a new qcow2-formatted disk separately, you can use qemu-img (the example below specifies a disk size of 20G):
# sudo qemu-img create -f qcow2 /var/lib/libvirt/images/guest.qcow2 20480

More information about libvirt storage options can be found at https://libvirt.org/storage.html.

Finally, run the virt-install command using the following format (adjusting parameters as needed):

$ sudo virt-install --name Fedora43 \
--description 'Fedora 43 Workstation' \
--ram 4096 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/Fedora-Workstation-43/Fedora-Workstation-43-20180518.0.x86_64.qcow2,size=20 \
--os-variant fedora43 \
--network bridge=virbr0 \
--graphics vnc,listen=127.0.0.1,port=5901 \
--cdrom /var/lib/libvirt/images/Fedora-Workstation-43/Fedora-Workstation-Live-x86-64-43-1.1.iso \
--noautoconsole

Note: For the graphics parameter, we’re setting the vnc listener to localhost because it’s more secure to tunnel your VNC connection through SSH so that you don’t expose VNC to everyone with access to the network.

virt-install can use kickstart files, for example, virt-install -x ks=kickstart-file-name.ks.

If graphics were enabled, a VNC window will open and present the graphical installer. If graphics were not enabled, a text installer will appear. Proceed with the Fedora installation.

Creating a guest with virt-manager

  1. Start Virtual Machine Manager by navigating to Applications  System Tools, or by running the following command:

    $ sudo virt-manager
  2. Open a connection to a hypervisor by navigating to File  Add connection.

  3. Wählen Sie qemu für KVM oder Xen für Xen.

  4. Choose local or select a method to connect to a remote hypervisor.

  5. After a connection is opened, click the new icon next to the hypervisor, or right-click on the active hypervisor and select New.

  6. Configure the virtual machine following the steps in the New VM wizard.

  7. Click Finish at the end of the wizard to provision the guest operating system. After a few moments a VNC window will appear. Proceed with the Fedora installation.

Virtuelle Maschinen verwalten

Sobald die Installation des Gastbetriebssystems abgeschlossen ist, kann es mit dem Programm virt-manager oder über die Befehlszeile mit virsh verwaltet werden.

Managing guests with virt-manager

  1. Start the Virtual Machine Manager by navigating to menu:[Applications]System Tools, or run:

    $ virt-manager

    If you are not root, you will be prompted to enter the root password.

  2. Choose the host you wish to manage and click Connect in the Open Connection dialog window.

  3. The list of virtual machines is displayed in the main window. Guests that are running will display a ">" icon. Guests that are not running will be greyed out.

  4. Um ein bestimmtes Gastsystem zu verwalten, doppelklicken Sie darauf oder klicken Sie mit der rechten Maustaste und wählen Sie Öffnen.

  5. Es öffnet sich ein neues Fenster für das Gastsystem, über das Sie dessen Konsole nutzen, Informationen über dessen virtuelle Hardware einsehen und den Prozess starten, stoppen und pausieren können.

For further information about virt-manager, see RedHat virt-manager guide.

Bugs in the virt-manager tool should be reported in Bugzilla against the virt-manager component.

Verwaltung von Gastsystemen mit virsh

The virsh command-line utility allows you to manage virtual machines on the command line. The virsh utility is built around the libvirt management API:

  • virsh has a stable set of commands whose syntax and semantics are preserved across updates to the underlying virtualization platform.

  • virsh kann als Benutzer ohne Administratorrechte für Leseoperationen verwendet werden (z.B. zum Auflisten von Domains, Auflisten von Domainstatistiken).

  • virsh can manage domains running under Xen, QEMU/KVM, ESX, or other back-ends with no perceptible difference to the user.

Eine virtuelle Maschine starten:

$ virsh create <Name der virtuellen Maschine>

To list the virtual machines currently running:

$ virsh list

Um alle virtuellen Maschinen aufzulisten, egal ob sie laufen oder nicht:

$ virsh list --all

To gracefully power off a guest:

$ virsh shutdown <virtual machine (name | id | uuid)>

To non gracefully power off a guest:

$ virsh destroy <virtual machine (name | id | uuid)>

To save a snapshot of the machine to a file:

$ virsh save <virtual machine (name | id | uuid)> <filename>

To restore a previously saved snapshot:

$ virsh restore <filename>

To export the configuration file of a virtual machine:

$ virsh dumpxml <virtual machine (name | id | uuid)

For a complete list of commands available for use with virsh:

$ virsh help

Or consult the manual page: man virsh.

Bugs in the virsh tool should be reported in Bugzilla against the libvirt component.

Remote management

The following remote management options are available:

  • If using non-root users via SSH, see the setup instructions in https://wiki.libvirt.org/page/SSHSetup

  • If using root for access via SSH, then create SSH keys for root, and use ssh-agent and ssh-add before launching virt-manager.

  • To use TLS, set up a local certificate authority and issue x509 certs to all servers and clients. For information on configuring this option, see https://wiki.libvirt.org/page/TLSSetup.

Weitere Virtualisierungsoptionen

QEMU/KVM ohne libvirt

QEMU/KVM can be invoked directly without libvirt, however you cannot to use tools such as virt-manager, virt-install, or virsh. Plain QEMU (without KVM) can also virtualize other processor architectures like ARM or PowerPC.

Xen

Fedora kann als Xen-Gastbetriebssystem und auch als Xen-Host verwendet werden (letzteres gilt ab Fedora 16; für die Verwendung einer älteren Fedora-Version als Xen-Host siehe die experimentelle Paketquelle unter https://myoung.fedorapeople.org/dom0). Eine Anleitung zur Installation und Einrichtung eines Fedora-Xen-Hosts finden Sie auf der Seite Fedora Host Installation im Xen-Projekt-Wiki.

OpenStack

OpenStack consists of a number of services for running infrastructure as a service (IaaS) clouds. They are the Object Store (Swift), Compute (Nova), and Image (Glance) services.

OpenNebula

OpenNebula is an open source toolkit for data center virtualization.

oVirt

The oVirt project is an open virtualization project providing a end-to-end, server virtualization management system with advanced capabilities for hosts and guests, including high availability, live migration, storage management, system scheduler, and more.

Fehlerbehebung und bekannte Probleme

First take a look at the well-known common issues. Replace the version number by the version you are actually using.

For troubleshooting tips, see Virtualization – How to Debug Issues