Linux KVM Reference

This superpost amalgamates an array of concepts around the installation and usage of Linux KVM on RedHat-style distributions including AlmaLinux, CentOS, RedHat Enterprise Linux, and Rocky Linux; and includes detail on adjacent concepts such as ZFS storage pools. All commands are executed as root.

Install and Configure KVM

Install Required Packages

dnf -y install libvirt libvirt-client virt-install

Install KVM GUI Tools Packages

dnf -y install virt-manager virt-viewer

Enable Standard Users to Use the System URI

Edit libvirtd.conf

Edit /etc/libvirt/libvirtd.conf and uncomment all lines beginning with unix_sock.

unix_sock_group = "libvirt"
unix_sock_ro_perms = "0777"
unix_sock_rw_perms = "0770"
unix_sock_admin_perms = "0700"
unix_sock_dir = "/run/libvirt"

Create libvirt.sh

Create an /etc/profile.d/libvirt.sh script to configure the default URI:

echo 'export LIBVIRT_DEFAULT_URI="qemu:///system"' > /etc/profile.d/libvirt.sh
chmod 644 /etc/profile.d/libvirt.sh

Manage User Group Membership

Add the standard users to the libvirt, kvm, and qemu groups.

Enable and Start Services

systemctl enable --now libvirtd

Networking

Create a Network Bridge

Display the network connections and note the name of the ethernet device which is referenced as [ETHDEV] in the following commands.

nmcli connection show
nmcli connection delete [ETHDEV]
nmcli connection add type bridge autoconnect yes con-name virbr0 ifname virbr0
nmcli connection modify virbr0 ipv4.method disabled
nmcli connection add type bridge-slave autoconnect yes con-name [ETHDEV] ifname [ETHDEV] master virbr0
nmcli connection up virbr0

Create a Private Network

Create a virsh-net-private.xml file:

<network>
    <name>private</name>
    <bridge name="virbr1"/>
    <ip address="10.0.1.1" netmask="255.255.255.0"/>
</network>

Define and start the private network:

virsh net-define ./virsh-net-private.xml
virsh net-autostart private
virsh net-start private

Storage Pools

Create an LVM Storage Pool

virsh pool-define-as --name kvm-pool --type logical --target /dev/kvm-pool
virsh pool-autostart kvm-pool
virsh pool-start kvm-pool

Create a Directory-based Storage Pool

mkdir -p /srv/kvm-pool
chmod 770 /srv/kvm-pool
chown qemu:libvirt /srv/kvm-pool
chmod g+ws /srv/kvm-pool
virsh pool-define-as --name kvm-pool --type dir --target /srv/kvm-pool
virsh pool-autostart kvm-pool
virsh pool-start kvm-pool

Installing Guest Operating Systems

AlmaLinux 10.0 Minimal Guest with Secure Boot, UEFI, and a TPM

virt-install \
--name almalinux \
--vcpus 2 \
--ram 2048 \
--boot uefi,loader_secure=yes \
--tpm backend.type=emulator,backend.version=2.0,model=tpm-tis \
--os-variant rhel10.0 \
--network network=virbr0,model=virtio \
--console pty,target_type=serial \
--disk pool=kvm-pool,size=60,bus=virtio \
--graphics none \
--location http://repo.almalinux.org/almalinux/10.0/BaseOS/x86_64/os/ \
--extra-args "console=ttyS0,115200n8 inst.ks=http://repositories.onezeroone.dev/ks/almalinux-10.0-minimal-ks.cfg"

Windows 11 Enterprise Evaluation Edition with Secure Boot, UEFI, and a TPM

virt-install \
--name Windows-11-Enterprise-Evaluation \
--vcpus 4 \
--memory 4096 \
--boot uefi \
--boot loader_secure=yes,menu=on \
--tpm backend.type=emulator,backend.version=2.0,model=tpm-tis \
--os-variant win11 \
--network network=virbr0,model=virtio \
--disk pool=kvm-pool,size=120,bus=virtio \
--disk device=cdrom,path=/srv/kvm/iso/microsoft-windows-11-enterprise-evaluation.iso,bus=sata \
--disk device=cdrom,path=/srv/kvm/iso/virtio-win.iso,bus=sata \
--graphics vnc \
--sound ich6 \
--features hyperv_relaxed=on,hyperv_vapic=on,hyperv_spinlocks=on,hyperv_spinlocks_retries=8191 \
--clock hypervclock_present=yes

Guest Management Command Reference

CommandDescription
virsh listShow running guests
virsh list --allShow all defined guests
virsh start [GUESTNAME]Start a guest
virsh shutdown [GUESTNAME]Stop a guest (ACPI)
virsh destroy [GUESTNAME]Stop a guest (force)
virsh autostart [GUESTNAME]Mark a guest for auto-start on hypervisor start
virsh autostart --disable [GUESTNAME]Un-mark a guest for auto-start
virsh vncdisplay [GUESTNAME]Show the VNC console number for a graphical guest

Relevant Links