# KVM (Kernel-based Virtual Machine) Cheat Sheet ## Installation ### Check KVM Support ```bash # Check if CPU supports virtualization egrep -c '(vmx|svm)' /proc/cpuinfo # Check if KVM modules are loaded lsmod | grep kvm ``` ### Install KVM and Related Tools ```bash # Debian/Ubuntu sudo apt update sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager # RHEL/CentOS/Fedora sudo dnf install @virtualization ``` ## User Management ```bash # Add user to libvirt group sudo usermod -aG libvirt $USER sudo usermod -aG kvm $USER # Restart libvirtd service sudo systemctl restart libvirtd ``` ## VM Management with virsh ### Basic VM Operations ```bash # List all VMs virsh list --all # Start a VM virsh start vm_name # Gracefully shutdown a VM virsh shutdown vm_name # Force shutdown a VM virsh destroy vm_name # Reboot a VM virsh reboot vm_name # Delete a VM (and its storage) virsh undefine vm_name --remove-all-storage # Suspend/resume a VM virsh suspend vm_name virsh resume vm_name ``` ### VM Information ```bash # Get VM information virsh dominfo vm_name # View VM XML configuration virsh dumpxml vm_name # Edit VM XML configuration virsh edit vm_name # Display VM console virsh console vm_name ``` ### VM Creation ```bash # Create VM from an XML file virsh define vm_config.xml # Clone an existing VM virt-clone --original source_vm --name new_vm --auto-clone ``` ## Storage Management ### Storage Pools ```bash # List storage pools virsh pool-list --all # Define new storage pool from XML virsh pool-define pool_config.xml # Start storage pool virsh pool-start pool_name # Enable autostart for pool virsh pool-autostart pool_name # Refresh storage pool virsh pool-refresh pool_name ``` ### Storage Volumes ```bash # List volumes in a pool virsh vol-list pool_name # Create volume in a pool virsh vol-create-as pool_name vol_name 10G # Delete a volume virsh vol-delete vol_name --pool pool_name # Resize a volume virsh vol-resize vol_name new_size --pool pool_name ``` ## Network Management ### Virtual Networks ```bash # List all networks virsh net-list --all # Start a network virsh net-start network_name # Enable autostart for network virsh net-autostart network_name # View network XML virsh net-dumpxml network_name # Edit network configuration virsh net-edit network_name ``` ## Snapshots ### VM Snapshots ```bash # Create a snapshot virsh snapshot-create-as vm_name snapshot_name --description "description" # List snapshots virsh snapshot-list vm_name # View snapshot details virsh snapshot-info vm_name snapshot_name # Revert to snapshot virsh snapshot-revert vm_name snapshot_name # Delete snapshot virsh snapshot-delete vm_name snapshot_name ``` ## Performance Monitoring & Tuning ```bash # Show VM resource usage virt-top # Display VM I/O statistics virsh domstats vm_name --block # Set VM memory virsh setmem vm_name 2G --config --live # Set VM vCPUs virsh setvcpus vm_name 2 --config --live ``` ## Backup & Migration ### Backup ```bash # Export VM definition virsh dumpxml vm_name > vm_backup.xml # Create disk backup sudo dd if=/var/lib/libvirt/images/vm_disk.qcow2 of=/backup/vm_disk.qcow2 bs=1M ``` ### Migration ```bash # Live migration (shared storage) virsh migrate --live vm_name qemu+ssh://destination_host/system # Migration with storage virsh migrate --live --copy-storage-all vm_name qemu+ssh://destination_host/system ``` ## Troubleshooting ```bash # Check libvirt service status sudo systemctl status libvirtd # View VM logs virsh domblklist vm_name sudo tail -f /var/log/libvirt/qemu/vm_name.log # Check VM process status ps aux | grep vm_name # Debug connection issues virsh -c qemu:///system ``` ## Advanced Features ### CPU Pinning ```bash # Set CPU affinity virsh vcpupin vm_name vcpu_number host_cpu_list ``` ### Nested Virtualization ```bash # Enable nested virtualization (Intel) echo "options kvm-intel nested=1" | sudo tee /etc/modprobe.d/kvm-intel.conf sudo modprobe -r kvm_intel && sudo modprobe kvm_intel # Enable nested virtualization (AMD) echo "options kvm-amd nested=1" | sudo tee /etc/modprobe.d/kvm-amd.conf sudo modprobe -r kvm_amd && sudo modprobe kvm_amd ``` ### QEMU Guest Agent ```bash # Install in guest (Debian/Ubuntu) sudo apt install qemu-guest-agent sudo systemctl start qemu-guest-agent # Get guest information virsh qemu-agent-command vm_name '{"execute":"guest-info"}' ``` ## Image Management ```bash # Convert between image formats qemu-img convert -f raw -O qcow2 source.img target.qcow2 # Resize an image qemu-img resize vm_disk.qcow2 +10G # Create a new image qemu-img create -f qcow2 new_disk.qcow2 20G # Inspect image information qemu-img info vm_disk.qcow2 ```