KVM Virtualization Deployment and Management Guide
Environment Preparation and Installation
Prior to deploying KVM, verify the system environment. The following steps assume a CentOS 7 operating system. It is critical to disable the firewall and SELinux to prevent network connectivity issues during the initial setup. Additionally, ensure the system hostname is resolvable by editing the /etc/hosts file.
Verify hardware virtualization support by checking the CPU flags. The command must return output indicating either vmx (Intel) or svm (AMD).
egrep -o '(vmx|svm)' /proc/cpuinfo
Install the KVM core packages and management tools. The qemu-kvm package provides the emulator, while libvirt acts as the management interface. virt-install is used for command-line provisioning.
yum install -y qemu-kvm qemu-kvm-tools libvirt virt-install virt-manager openssh-askpass
Load the KVM kernel module and enable the libvirt service to start at boot.
lsmod | grep kvm
systemctl enable libvirtd
systemctl start libvirtd
Network Bridge Configuration
By default, KVM utilizes NAT networking. For production environments, a bridged network is recommended to allow virtual machines to appear as physical hosts on the network. Create a bridge interface named br0 and bind the physical interface (e.g., eth0) to it.
brctl addbr br0
brctl addif br0 eth0
ip addr del dev eth0 192.168.1.100/24
ifconfig br0 192.168.1.100/24 up
route add default gw 192.168.1.1
Virtual Machine Provisioning
Create a dedicated storage directory and prepare a disk image. The qcow2 format is preferred over raw because it supports snapshots and dynamic allocation.
mkdir -p /data/images
qemu-img create -f qcow2 /data/images/vm-webserver.qcow2 20G
Provision a new virtual machine using virt-install. This command specifies the resources, ISO media, and network configuration.
virt-install \
--name vm-webserver \
--virt-type kvm \
--ram 2048 \
--vcpus 2 \
--cdrom=/data/iso/CentOS-7.iso \
--disk path=/data/images/vm-webserver.qcow2 \
--network bridge=br0 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole
Connect to the virtual machine via a VNC client on port 5900 to complete the OS installation. After installation, the VM can be managed using the virsh command-line tool.
virsh list --all
virsh start vm-webserver
virsh shutdown vm-webserver
Dynamic Resource Allocation
CPU Hot-Add
To dynamically adjust the number of CPUs, edit the XML configuration to define a maximum limit. Hot removal of CPUs is not supported.
virsh edit vm-webserver
# Update the vcpu tag:
# <vcpu placement='auto' current='2'>4</vcpu>
After modifying the configuration, apply the change live:
virsh setvcpus vm-webserver 4 --live
Memory Ballooning
Memory can be adjusted dynamically using the balloon driver. First, configure the maximum memory and current memory in the XML definition.
<memory unit='KiB'>4096000</memory>
<currentMemory unit='KiB'>2048000</currentMemory>
Use the qemu-monitor-command to adjust the memory allocation.
virsh qemu-monitor-command vm-webserver --hmp --cmd info balloon
virsh qemu-monitor-command vm-webserver --hmp --cmd balloon 1024
Disk Expansion
To expand storage, create a new disk image and attach it to the running VM.
qemu-img create -f qcow2 /data/images/vm-webserver-disk2.qcow2 10G
virsh attach-disk vm-webserver /data/images/vm-webserver-disk2.qcow2 vdb --cache=none --subdriver=qcow2
Inside the guest OS, use LVM to extend the logical volume.
pvcreate /dev/vdb
vgextend centos /dev/vdb
lvextend -l +100%FREE /dev/centos/root
xfs_growfs /dev/centos/root
Snapshot Management
Snapshots require the qcow2 disk format. They allow saving the state of a VM at a specific point in time.
Create a new snapshot:
virsh snapshot-create-as vm-webserver snap1 "Initial install"
List available snapshots:
virsh snapshot-list vm-webserver
Revert to a previous state:
virsh snapshot-revert vm-webserver snap1
Delete a snapshot:
virsh snapshot-delete vm-webserver snap1