Master KVM: Deploy and Manage Virtual Machines on Linux with Libvirt
This guide walks through KVM’s background, features, architecture, and step‑by‑step deployment—including hardware checks, module loading, qemu‑kvm options, bridge configuration, script creation, VM installation, and Libvirt‑based management with virt‑install and virt‑manager—providing a comprehensive tutorial for Linux virtualization.
Introduction
KVM (Kernel Virtual Machine) is a virtualization solution originally developed by Qumranet, merged into the Linux kernel since version 2.6.20, and supported by Red Hat after acquiring Qumranet in 2008. This article explains how to deploy and manage KVM.
KVM Overview
Features
KVM runs on x86 CPUs with Intel VT or AMD‑V, uses the kvm.ko kernel module (or kvm‑intel.ko / kvm‑amd.ko) and a modified QEMU ( qemu‑kvm) as the user‑space controller. Memory management relies on the Linux kernel, and KVM can use any storage supported by Linux. Performance reaches about 95 % of native speed, and it scales to many CPUs and large memory.
Architecture
KVM consists of a kernel module and QEMU that emulates hardware; each VM is a regular Linux process managed by the kernel.
Platform Deployment
Hardware detection
Check CPU flags for virtualization support: egrep --color '(vmx|svm)' /proc/cpuinfo If the output contains vmx (Intel) or svm (AMD), the hardware supports KVM.
Load modules:
modprobe kvmmodprobe kvm_intel # or kvm_amdlsmod | grep kvmInstall management tools
yum install qemu-kvm qemu-kvm-tools -yln -s /usr/libexec/qemu-kvm /usr/sbin/qemu‑kvm command options
Common options include:
-name name – set VM name
-M machine – select machine type
-m megs – RAM size
-cpu model – CPU model
-smp … – SMP configuration
-drive … – define disk devices
-net … – network configuration
Create disk image
qemu-img create -f qcow2 -o size=50G,preallocation=metadata /kvm/images/centos6.qcow2Configure bridge
vim /etc/sysconfig/network-scripts/ifcfg-br0Set DEVICE=br0, TYPE=Bridge, ONBOOT=yes, BOOTPROTO=none, IPADDR=172.16.10.124, PREFIX=16, GATEWAY=172.16.0.1, DNS1=172.16.0.1 vim /etc/sysconfig/network-scripts/ifcfg-eth0 Set DEVICE=eth0, TYPE=Ethernet, ONBOOT=yes, BRIDGE=br0
service network restartBridge scripts
#!/bin/bash
switch=br0
if [ -n "$1" ]; then
ip link set $1 up
sleep 1
brctl addif $switch $1
exit 0
else
echo "Error: No Interface."
exit 1
fi#!/bin/bash
switch=br0
if [ -n "$1" ]; then
brctl delif $switch $1
ip link set $1 down
exit 0
else
echo "Error: No Interface."
exit 1
fichmod -R +x /kvm/script/Install a VM with qemu‑kvm
yum install tigervnc -yqemu-kvm -name "centos6.6" \
-m 512 -smp 2 \
-drive file=/kvm/images/centos6.qcow2,media=disk,format=qcow2 \
-drive file=CentOS-6.6-x86_64-bin-DVD1.iso,media=cdrom \
-net nic -net tap,ifname=vnet0,script=/kvm/script/qemu-ifup,downscript=/kvm/script/qemu-ifdown \
-boot order=dc,once=dVNC server runs on ::1:5900; connect with vncviewer :5900.
Libvirt‑based management
Libvirt provides an API and CLI tools to simplify VM lifecycle management. Install components:
yum install libvirt virt-manager virt-viewer python-virtinst -yStart the daemon: service libvirtd start Create a bridge with libvirt:
virsh iface-bridge eth0 br0virt‑install
virt‑install creates VMs via libvirt. Common options:
-n NAME – VM name
-r MEMORY – RAM in MB
--vcpus … – CPU count
--cpu … – CPU model
-c CDROM – installation media
--network … – network configuration
--disk … – storage definition
--boot … – boot order
--autostart – start VM on host boot
Example virt‑install command
virt-install -n "centos6.6" \
--vcpus=2 -r 512 -c CentOS-6.6-x86_64-bin-DVD1.iso \
--disk path=/kvm/images/centos6.6.qcow2,bus=virtio,size=50,sparse \
--network bridge=br0,model=virtio --forcevirt‑manager
virt‑manager is a Python‑based graphical front‑end that uses libvirt to manage VMs. After mounting the installation ISO and configuring an HTTP repository, start the GUI with virt-manager & and create a new VM, optionally using network installation.
Conclusion
KVM can be managed directly with qemu‑kvm commands or more conveniently through libvirt tools such as virt‑install and virt‑manager; the choice depends on personal preference.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
