KVM (Kernel-based Virtual Machine) is the built-in hypervisor in Linux. It turns the Linux kernel into a Type-1 hypervisor, allowing you to run multiple virtual machines on a single physical server.
Virtualization Profile
Packages: qemu-kvm, libvirt, virt-manager, virt-install
Daemon: libvirtd
Config: /etc/libvirt/
VM storage: /var/lib/libvirt/images/
CLI tool: virsh
GUI tool: virt-manager
Check Hardware Virtualization Support
# Check CPU supports virtualization:
# grep -E "vmx|svm" /proc/cpuinfo # vmx=Intel, svm=AMD
# egrep "(vmx|svm)" /proc/cpuinfo
# Load KVM module:
# modprobe kvm # generic
# modprobe kvm_intel # Intel CPUs
# modprobe kvm_amd # AMD CPUs
# Verify module loaded:
# lsmod | grep kvm
Install KVM
# Install packages:
# yum install qemu-kvm libvirt virt-install virt-manager -y
# Start and enable libvirtd:
# systemctl start libvirtd
# systemctl enable libvirtd
# Verify installation:
# virsh list --all # should return empty list
Create a Virtual Machine with virt-install
# Create VM from ISO:
# virt-install \
--name=vm01 \
--vcpus=2 \
--ram=2048 \
--disk path=/var/lib/libvirt/images/vm01.img,size=20 \
--os-type=linux \
--os-variant=rhel7 \
--location=/iso/rhel7.iso \
--network network=default \
--graphics vnc \
--console pty,target_type=serial \
--extra-args "console=tty0 console=ttyS0,115200n8"
Managing VMs with virsh
# List VMs:
# virsh list --all # all VMs
# virsh list # only running VMs
# Start/stop/restart:
# virsh start vm01
# virsh shutdown vm01 # graceful shutdown
# virsh destroy vm01 # force off (like power cut)
# virsh reboot vm01
# Suspend/resume:
# virsh suspend vm01
# virsh resume vm01
# Auto-start on boot:
# virsh autostart vm01
# virsh autostart --disable vm01
# Connect to VM console:
# virsh console vm01 # Ctrl+] to exit
# Delete VM:
# virsh undefine vm01 # removes config (keeps disk)
# virsh undefine vm01 --remove-all-storage # removes disk too
# VM info:
# virsh dominfo vm01
# virsh dumpxml vm01 # full XML config
Storage Pools
# List pools:
# virsh pool-list --all
# Create directory-based storage pool:
# virsh pool-define-as mypool dir - - - - /vm/storage
# virsh pool-build mypool
# virsh pool-start mypool
# virsh pool-autostart mypool
# Create volume in pool:
# virsh vol-create-as mypool vm02.img 20G
Virtual Networks
# List networks:
# virsh net-list --all
# Default NAT network (created automatically):
# virsh net-start default
# virsh net-autostart default
# Create bridged network:
# vim /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
IPADDR=192.168.1.10
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BRIDGE=br0
Snapshots
# Create snapshot:
# virsh snapshot-create-as vm01 snap1 "Before upgrade"
# List snapshots:
# virsh snapshot-list vm01
# Revert to snapshot:
# virsh snapshot-revert vm01 snap1
# Delete snapshot:
# virsh snapshot-delete vm01 snap1