Home/Blog/KVM Installation Guide | Linux Virtualization Setup
Virtualization

KVM Installation Guide | Linux Virtualization Setup

Master KVM virtualization with this comprehensive step-by-step installation and configuration guide for enterprise environments.

KVM Installation Guide | Linux Virtualization Setup

This comprehensive guide will walk you through setting up a production-ready two-node KVM farm using OpenFiler for storage and Fedora Core 14 as your KVM hosts. By the end of this tutorial, you’ll master KVM installation, virtual machine creation, and live migration between hosts—all using the essential KVM toolset.

Install Essential Components

Begin by installing the core virtualization components. First, identify and install the appropriate NFS client for your system architecture:

yum list | grep nfs

Install the NFS client for your architecture (example for i686):

yum install nfs-utils.i686

Next, install the essential virtualization components with a single command:

yum install virt-manager virt-viewer libvirt qemu-kvm
  • virt-manager: Virtual machine management utility
  • virt-viewer: Console access for virtual machines
  • libvirt: Core hypervisor infrastructure
  • qemu-kvm: KVM enablement package

Best Practice: Restart your system after installation to ensure all components load properly and hardware virtualization features are activated.

Configure DNS and Network Settings

Proper DNS configuration is critical for KVM cluster functionality. If you lack a dedicated DNS server, configure host file entries manually:

gedit /etc/hosts

Add entries for each KVM host using this format:

192.168.1.10    kvm-host1.local
192.168.1.11    kvm-host2.local

Setup NFS Shared Storage

Create a dedicated mount point for shared VM storage and configure the NFS share:

mkdir /var/lib/libvirt/images/kvmshared
mount -t nfs 10.0.0.28:/mnt/volgroup1/volume1/Share1/ /var/lib/libvirt/images/kvmshared

Make the mount persistent by adding it to /etc/fstab:

echo "10.0.0.28:/mnt/volgroup1/volume1/share1/ /var/lib/libvirt/images/kvmshared nfs defaults 0 0" >> /etc/fstab

Set proper permissions and configure SELinux for NFS usage:

chmod 755 -R /var/lib/libvirt/images
setsebool -P virt_use_nfs on

Configure Firewall for Live Migration

Open essential ports for remote management and live migration functionality:

  • Port 22: SSH access for remote management
  • Ports 49152-49261: Live migration traffic

Configure firewall rules using the system firewall utility:

/usr/bin/system-config-firewall

Add TCP port range 49152-49261 for migration traffic and apply the configuration.

Configure the Hypervisor

Start the libvirt daemon and initialize the virtual machine manager:

service libvirtd start
virt-manager

Update the default storage pool to use your NFS shared storage:

gedit /etc/libvirt/storage/default.xml

Change the path from /var/lib/libvirt/images to /var/lib/libvirt/images/kvmshared and restart the hypervisor:

service libvirtd restart

Creating Virtual Machines

Follow these steps to create your first virtual machine using virt-manager:

  1. Launch virt-manager and click the “New” button
  2. Enter a descriptive name for your virtual machine
  3. Select “Local Media (ISO Image or CDROM)” as the installation source
  4. Browse and select your ISO file from the shared storage location
  5. Allocate appropriate memory and CPU resources
  6. Configure virtual disk size (consider unchecking “Allocate Entire Disk Now” for space efficiency)
  7. Review settings and click “Finish” to create the VM

Pro Tip: To release mouse and keyboard control from a VM, press Ctrl+Alt on the left side of your keyboard.

Live Migration Setup

Enable seamless VM migration between hosts by registering remote systems in virt-manager:

Register Remote Hosts

  1. Open virt-manager and go to File → Add Connection
  2. Check “Connect to remote Host”
  3. Select “QEMU/KVM” from the hypervisor dropdown
  4. Choose “SSH” as the connection method
  5. Set username to “root”
  6. Enter the remote hostname
  7. Accept SSH certificates and provide credentials

Perform Migration

To migrate a running VM:

  1. Right-click the virtual machine
  2. Select “Migrate”
  3. Choose the destination host from the dropdown
  4. Click “Migrate” to begin the process

Important: After migration, delete the VM entry from the original host (but DO NOT delete storage files) to prevent conflicts with future migrations.

Troubleshooting and Useful Commands

Keep these essential commands and troubleshooting tips handy:

Fix Migration Issues

iptables -I INPUT -j ACCEPT

List Running VMs

virsh -c qemu:///system list

Direct VM Boot

qemu-kvm -m 1024 -cdrom /path/to/image.iso -boot d

Configuration files are stored in /etc/libvirt and logs can be found in /var/log/libvirt.

Frequently Asked Questions

Find answers to common questions

To optimize KVM performance on Fedora, particularly concerning CPU and memory management, several key configurations and best practices must be implemented. First, ensure that your host system supports hardware virtualization (Intel VT-x or AMD-V) and that it is enabled in the BIOS. This can significantly enhance KVM performance by allowing the hypervisor to run virtual machines more efficiently.

Next, consider configuring CPU pinning, which binds virtual CPUs (vCPUs) of your virtual machines to specific physical CPUs on the host. This reduces context switching and can enhance performance, especially for CPU-intensive workloads. You can achieve this by editing the XML configuration of your VM using virsh edit <vm-name> and setting the <vcpu> section to specify the CPU cores allocated to the VM.

Memory management is also crucial. It’s advisable to use huge pages, which can improve memory performance by reducing the overhead associated with managing large amounts of memory. You can enable huge pages in Fedora by adding the following line to your /etc/sysctl.conf:

mmap_min_addr = 65536

Then, allocate huge pages by adding the following to your /etc/default/grub:

GRUB_CMDLINE_LINUX="... hugepages=4G"

After updating the grub configuration, run grub2-mkconfig -o /boot/grub2/grub.cfg and reboot your system.

Additionally, consider configuring the balloon driver (virtio-balloon) for dynamic memory management in your VMs. This allows the hypervisor to adjust the memory allocated to a VM based on demand, which can prevent resource starvation and improve overall system performance.

Finally, regularly monitor your KVM environment using tools like virt-top or htop to track resource usage and identify any bottlenecks in CPU or memory allocation. By applying these optimizations, you can significantly enhance the performance of your KVM host and the virtual machines running on it.

When setting up NFS for KVM on Fedora, SELinux (Security-Enhanced Linux) configurations play a crucial role in ensuring secure and efficient operation. By default, SELinux is set to 'enforcing' mode, which can restrict access to NFS mounts unless properly configured. This can lead to issues where KVM cannot access the shared storage, resulting in failures when creating or managing virtual machines.

To configure SELinux for NFS, you need to set the appropriate context for the NFS share. First, ensure that the NFS server exports the shared directory with the correct SELinux context. You can set the context using the semanage command. For example:

semanage fcontext -a -t nfs_t '/path/to/nfs/share(/.*)?'
restorecon -R /path/to/nfs/share

This command ensures that the NFS share is labeled for NFS access. Additionally, you need to allow NFS-related SELinux booleans. Use the following commands to set the necessary booleans:

setsebool -P nfs_export_all_rw 1
setsebool -P allow_nfsd_anon_write 1

These commands permit NFS to handle read and write requests and allow anonymous users to write to the exported directories. After making these changes, restart the NFS service to apply the new settings:

systemctl restart nfs-server

Troubleshooting SELinux-related issues can be done using audit2why or audit2allow, which help interpret SELinux denial messages. For example, check SELinux logs in /var/log/audit/audit.log for any AVC (Access Vector Cache) denials related to NFS. You can run:

audit2why < /var/log/audit/audit.log

This command will provide a human-readable explanation of the denial. If necessary, you can create custom policies to allow specific access. However, it's essential to ensure that any changes do not compromise security. Regular audits of SELinux settings and access logs will help maintain a secure KVM environment while ensuring proper access to NFS shares.

Configuring firewall settings for KVM is crucial to ensure secure remote management and seamless migration of virtual machines. Fedora uses firewalld as the default firewall management tool, which provides a dynamic way to manage firewall rules.

To begin, you need to open the necessary ports for KVM management and VM migration. The essential ports include:

  • TCP port 16509 for libvirt management (used by virt-manager)
  • TCP port range 49152-49261 for live migration

You can enable these ports with the following commands:

f firewall-cmd --zone=public --add-port=16509/tcp --permanent
firewall-cmd --zone=public --add-port=49152-49261/tcp --permanent

After adding these ports, reload the firewall to apply the changes:

f firewall-cmd --reload

In addition to opening ports, it is also advisable to create a dedicated firewall rule for virt-manager that allows only trusted IP addresses to connect to the management interface. This can be done by creating a specific rule:

f firewall-cmd --zone=public --add-rich-rule='rule family=

Optimize Your Virtual Infrastructure

VMware, Hyper-V, or cloud VMs — our team helps you get the most from your virtualization investment.