KVM/libvirt Virtualization
Check Hardware Virtualization Support
Section titled “Check Hardware Virtualization Support”Before installing KVM, you must confirm that the CPU supports hardware virtualization (Intel VT-x or AMD-V).
# Check if the CPU supports virtualization extensionsgrep -Ec '(vmx|svm)' /proc/cpuinfoIf the return value is greater than 0, the CPU supports virtualization.
# Check if KVM kernel modules are loadedlsmod | grep kvmYou should see kvm and either kvm_intel (Intel CPU) or kvm_amd (AMD CPU).
Install KVM and Related Tools
Section titled “Install KVM and Related Tools”sudo dnf install -y qemu-kvm libvirt virt-install virt-viewer \ libvirt-client bridge-utilssudo dnf install -y @virt qemu-kvm libvirt virt-install \ virt-viewer bridge-utilssudo yum install -y qemu-kvm libvirt virt-install virt-viewer \ bridge-utils libvirt-clientIf you need a graphical management tool:
sudo dnf install -y virt-managerStart the libvirtd Service
Section titled “Start the libvirtd Service”sudo systemctl enable --now libvirtdVerify:
sudo systemctl status libvirtdvirsh versionAdd Your User to the libvirt Group
Section titled “Add Your User to the libvirt Group”sudo usermod -aG libvirt $(whoami)Log out and back in for the change to take effect. After that, you can use virsh as a regular user to connect to system-level VMs.
virsh Basic Commands
Section titled “virsh Basic Commands”virsh is the command-line management tool for libvirt, used to manage the full lifecycle of virtual machines.
VM Lifecycle Management
Section titled “VM Lifecycle Management”# List running VMsvirsh list
# List all VMs (including powered-off ones)virsh list --all
# Start a VMvirsh start myvm
# Graceful shutdownvirsh shutdown myvm
# Force power off (equivalent to pulling the power plug)virsh destroy myvm
# Reboot a VMvirsh reboot myvm
# Suspend/resume a VMvirsh suspend myvmvirsh resume myvmVM Information Queries
Section titled “VM Information Queries”# View VM detailsvirsh dominfo myvm
# View VM XML configurationvirsh dumpxml myvm
# View VM VNC display addressvirsh vncdisplay myvm
# View disks used by a VMvirsh domblklist myvm
# View VM network interfacesvirsh domiflist myvmVM Configuration Modifications
Section titled “VM Configuration Modifications”# Edit VM XML configurationvirsh edit myvm
# Set VM to auto-start on bootvirsh autostart myvm
# Disable auto-startvirsh autostart --disable myvm
# Modify VM memory (requires powered-off state)virsh setmaxmem myvm 4G --configvirsh setmem myvm 4G --config
# Modify vCPU count (requires powered-off state)virsh setvcpus myvm 4 --config --maximumvirsh setvcpus myvm 4 --configCreate VMs with virt-install
Section titled “Create VMs with virt-install”Install from ISO
Section titled “Install from ISO”sudo virt-install \ --name almalinux9 \ --ram 2048 \ --vcpus 2 \ --disk path=/var/lib/libvirt/images/almalinux9.qcow2,size=20,format=qcow2 \ --os-variant almalinux9 \ --network network=default \ --graphics vnc,listen=0.0.0.0 \ --cdrom /data/iso/AlmaLinux-9-latest-x86_64-minimal.iso \ --boot uefiAutomated Installation with Kickstart
Section titled “Automated Installation with Kickstart”sudo virt-install \ --name almalinux9-auto \ --ram 2048 \ --vcpus 2 \ --disk path=/var/lib/libvirt/images/alma9-auto.qcow2,size=20,format=qcow2 \ --os-variant almalinux9 \ --network network=default \ --graphics none \ --location /data/iso/AlmaLinux-9-latest-x86_64-minimal.iso \ --initrd-inject /data/ks/alma9-ks.cfg \ --extra-args "inst.ks=file:/alma9-ks.cfg console=ttyS0"Quick Creation from Cloud Images
Section titled “Quick Creation from Cloud Images”# Download the cloud imagewget https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2
# Copy and resize the diskcp AlmaLinux-9-GenericCloud-latest.x86_64.qcow2 /var/lib/libvirt/images/myvm.qcow2qemu-img resize /var/lib/libvirt/images/myvm.qcow2 20G
# Create a VM using cloud-init configurationsudo virt-install \ --name myvm \ --ram 2048 \ --vcpus 2 \ --disk /var/lib/libvirt/images/myvm.qcow2 \ --os-variant almalinux9 \ --network network=default \ --import \ --cloud-init root-password-generate=on,disable=on \ --graphics noneManaging VMs
Section titled “Managing VMs”Snapshot Management
Section titled “Snapshot Management”# Create a snapshotvirsh snapshot-create-as myvm --name "before-update" --description "Before system update"
# List snapshotsvirsh snapshot-list myvm
# Revert to a snapshotvirsh snapshot-revert myvm --snapshotname "before-update"
# Delete a snapshotvirsh snapshot-delete myvm --snapshotname "before-update"Disk Management
Section titled “Disk Management”# Create a new diskqemu-img create -f qcow2 /var/lib/libvirt/images/data-disk.qcow2 50G
# Hot-add a disk to a VMvirsh attach-disk myvm /var/lib/libvirt/images/data-disk.qcow2 vdb \ --driver qemu --subdriver qcow2 --persistent
# View disk informationqemu-img info /var/lib/libvirt/images/myvm.qcow2
# Detach a diskvirsh detach-disk myvm vdb --persistentClone a VM
Section titled “Clone a VM”# Power off the source VM firstvirsh shutdown myvm
# Clonevirt-clone --original myvm --name myvm-clone \ --file /var/lib/libvirt/images/myvm-clone.qcow2Delete a VM
Section titled “Delete a VM”# Power off the VMvirsh destroy myvm
# Remove the VM definitionvirsh undefine myvm
# Remove along with all storagevirsh undefine myvm --remove-all-storagevirt-manager Graphical Management
Section titled “virt-manager Graphical Management”virt-manager provides a graphical interface for VM management.
Local Usage
Section titled “Local Usage”Launch directly in a desktop environment:
virt-managerRemote Management via SSH
Section titled “Remote Management via SSH”Connect to a remote server’s libvirt from your local workstation:
Or in the virt-manager interface, select “File > Add Connection”, choose QEMU/KVM, and check “Connect to remote host over SSH”.
Network Configuration
Section titled “Network Configuration”Default NAT Network
Section titled “Default NAT Network”libvirt creates a default NAT network named default (typically 192.168.122.0/24).
# List networksvirsh net-list --all
# View network detailsvirsh net-info default
# Start the default networkvirsh net-start default
# Set the default network to auto-startvirsh net-autostart defaultConfigure a Bridged Network
Section titled “Configure a Bridged Network”A bridged network allows VMs to connect directly to the physical network, obtaining IP addresses in the same subnet as the host.
-
Create a bridge using nmcli:
Terminal window # Create bridge br0sudo nmcli connection add type bridge con-name br0 ifname br0# Configure bridge IP (modify according to your network environment)sudo nmcli connection modify br0 \ipv4.addresses 192.168.1.100/24 \ipv4.gateway 192.168.1.1 \ipv4.dns "223.5.5.5,8.8.8.8" \ipv4.method manual# Add the physical NIC to the bridgesudo nmcli connection add type bridge-slave con-name br0-ens192 \ifname ens192 master br0# Activate the bridge (note: remote operation may disconnect the session)sudo nmcli connection up br0sudo nmcli connection down ens192 -
Create a libvirt bridge network definition file
/tmp/bridge-net.xml:<network><name>host-bridge</name><forward mode="bridge"/><bridge name="br0"/></network> -
Register the bridge network in libvirt:
Terminal window virsh net-define /tmp/bridge-net.xmlvirsh net-start host-bridgevirsh net-autostart host-bridge -
Use the bridged network when creating a VM:
Terminal window sudo virt-install \--name bridged-vm \--ram 2048 \--vcpus 2 \--disk path=/var/lib/libvirt/images/bridged-vm.qcow2,size=20 \--os-variant almalinux9 \--network network=host-bridge \--cdrom /data/iso/AlmaLinux-9-latest-x86_64-minimal.iso
Switch Networks for an Existing VM
Section titled “Switch Networks for an Existing VM”# Power off the VMvirsh shutdown myvm
# Edit the VM configurationvirsh edit myvmIn the XML, find the <interface> section and change it to:
<interface type='network'> <source network='host-bridge'/> <model type='virtio'/></interface>Start the VM:
virsh start myvmPerformance Optimization Tips
Section titled “Performance Optimization Tips”# Enable KSM (Kernel Same-page Merging) to save memorysudo systemctl enable --now ksmsudo systemctl enable --now ksmtuned
# Check KSM statuscat /sys/kernel/mm/ksm/pages_sharedVM configuration recommendations:
- Use
virtiodrivers for the best disk and network performance - Prefer the
qcow2disk format (supports snapshots and thin provisioning) - Consider using
rawformat disks for I/O-intensive workloads - Allocate Hugepages for VMs to improve memory performance