How to Install and Use KVM on CentOS: A Comprehensive Guide
Kernel-based Virtual Machine (KVM) is a powerful virtualization technology built into the Linux kernel. It allows you to create and manage virtual machines (VMs) directly on your CentOS system, offering a robust and performant platform for running multiple operating systems simultaneously. This comprehensive guide walks you through the entire process of installing, configuring, and utilizing KVM on CentOS, from initial setup to advanced management techniques.
I. Checking Hardware Compatibility and Prerequisites:
Before diving into the installation, it’s crucial to ensure your hardware supports virtualization. Most modern CPUs include virtualization extensions (Intel VT-x or AMD-V).
- Verify CPU Virtualization Support:
bash
grep -E 'vmx|svm' /proc/cpuinfo
If the command returns any output (e.g., vmx
or svm
), your CPU supports virtualization. If not, you’ll need to enable it in your BIOS settings.
- Check for KVM Modules:
bash
lsmod | grep kvm
If you see kvm_intel
(for Intel) or kvm_amd
(for AMD), KVM modules are already loaded.
- Required Packages:
Ensure you have a stable internet connection and update your system:
bash
sudo dnf update -y
II. Installing KVM and Related Packages:
- Install the KVM Packages:
bash
sudo dnf install qemu-kvm qemu-img libvirt libvirt-daemon-system libvirt-clients virt-install bridge-utils -y
This command installs the core KVM packages:
qemu-kvm
: The KVM hypervisor and emulator.qemu-img
: A utility for managing disk images.libvirt
: A library and tools for managing virtual machines.libvirt-daemon-system
: System daemon for libvirt.libvirt-clients
: Client tools for interacting with libvirt.virt-install
: A command-line tool for creating VMs.-
bridge-utils
: Utilities for configuring network bridges. -
Start and Enable the libvirtd Service:
bash
sudo systemctl start libvirtd
sudo systemctl enable libvirtd
- Add Your User to the libvirt Group:
bash
sudo usermod -aG libvirt $USER
Log out and back in (or restart your system) for the group changes to take effect. This allows you to manage VMs without sudo
.
- Verify KVM Installation:
bash
virsh -c qemu:///system list --all
This command should list any existing VMs (likely none at this point) and connect to the system hypervisor. Seeing the prompt without errors confirms a successful installation.
III. Creating a Virtual Machine:
There are several ways to create a VM with KVM:
A. Using virt-install (Command-Line):
virt-install
offers a flexible way to create VMs from the command line:
bash
sudo virt-install \
--name=myvm \ # VM name
--ram=2048 \ # Memory in MB
--vcpus=2 \ # Number of virtual CPUs
--disk path=/var/lib/libvirt/images/myvm.qcow2,size=20 \ # Disk image path and size in GB
--os-variant=centos8.0 \ # OS type for optimal configuration
--network bridge=br0 \ # Network bridge (explained later)
--graphics vnc \ # Graphical console via VNC
--console pty,target_type=serial \ # Serial console
--location=/path/to/iso/CentOS-8-x86_64-1905-dvd1.iso \ # Path to the installation ISO
--extra-args='console=ttyS0,115200n8 serial' # Extra arguments for serial console
B. Using virt-manager (Graphical Interface):
If you prefer a graphical interface, install virt-manager
:
bash
sudo dnf install virt-manager -y
Launch virt-manager
from the application menu or by typing virt-manager
in the terminal. The GUI guides you through the VM creation process, offering a user-friendly experience.
IV. Network Configuration:
Proper network configuration is crucial for VM connectivity. The recommended approach is using a bridge interface:
- Create a Bridge Interface:
bash
sudo nmcli connection add type bridge con-name br0 ifname br0
sudo nmcli connection modify br0 ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.method manual
sudo nmcli connection modify br0 ipv4.dns 8.8.8.8 8.8.4.4 # Configure DNS servers
sudo nmcli connection up br0
Replace 192.168.1.100/24
and 192.168.1.1
with appropriate IP addresses and gateway for your network.
- Assign the Bridge to the VM:
During VM creation (using virt-install
or virt-manager
), select the br0
bridge for the network interface.
V. Managing Virtual Machines:
Once your VMs are created, you can manage them using various tools:
-
virsh (Command-Line):
-
virsh list
: List running VMs. virsh list --all
: List all VMs (running and stopped).virsh start <vm_name>
: Start a VM.virsh shutdown <vm_name>
: Gracefully shut down a VM.virsh destroy <vm_name>
: Forcefully stop a VM.virsh console <vm_name>
: Access the VM’s console.virsh edit <vm_name>
: Edit the VM’s XML configuration file.virsh snapshot-create-as <vm_name> <snapshot_name>
: Create a snapshot.-
virsh snapshot-revert <vm_name> <snapshot_name>
: Revert to a snapshot. -
virt-manager (Graphical Interface):
virt-manager
provides a user-friendly interface for managing VMs, including starting, stopping, configuring, and monitoring them.
VI. Advanced Topics:
-
Disk Management:
-
qemu-img create -f qcow2 <disk_image_path> <size>G
: Create a new disk image. qemu-img convert -f qcow2 -O qcow2 <source_image> <destination_image>
: Convert disk image formats.-
qemu-img resize <disk_image_path> +<size>G
: Resize a disk image. -
Performance Tuning:
-
Hugepages: Using hugepages can improve VM performance. Consult the CentOS documentation for configuring hugepages.
- CPU Pinning: Pinning vCPUs to physical cores can reduce context switching and improve performance.
-
SR-IOV: Single Root I/O Virtualization allows direct assignment of PCI devices to VMs.
-
Live Migration:
Live migration allows you to move a running VM from one physical host to another without interruption. This requires shared storage and network connectivity between the hosts.
- Virtual Networks:
Libvirt allows you to create and manage virtual networks for isolating VMs.
-
Security Considerations:
-
SELinux: Ensure SELinux is configured correctly for KVM.
- Firewall: Configure your firewall to allow traffic to and from VMs.
- Secure access to the hypervisor: Restrict access to
virsh
andvirt-manager
.
This comprehensive guide provides a solid foundation for installing, configuring, and using KVM on CentOS. Remember to consult the official documentation for further details and advanced features. As you explore the world of KVM, you’ll discover its flexibility and power for creating and managing virtualized environments. Remember to adapt the provided commands and configurations to your specific needs and environment. By following the steps outlined here and utilizing the available resources, you can successfully leverage KVM to create a robust and efficient virtualization platform on your CentOS system.