KVM (Kernel-based Virtual Machine) is an open-source virtualization infrastructure for the Linux kernel that transforms it into a type-1 hypervisor.
- Scalability: Supports large-scale virtualization deployments
- Cost-effective: Open-source solution with no licensing fees
- Security: Integrates with Linux security features including SELinux
- Thin Provisioing: Kvm works with thin provisioing in the aspect of disks file as snap below
- Live and Offline Migration: Supports both live and offline VM migration, this feature helps when any maintenance is planned on the KVM host
- Resource
Overcommitment: Enables allocation of more virtual resources than physically available
Note:
Overcommitting in KVM (Kernel-based Virtual Machine) refers to the practice of allocating more virtualized resources to guest virtual machines than are physically available on the host system. This is possible because most VMs don't use 100% of their allocated resources at all times.
Benefits of Overcommitting:
- Improved hardware utilization
- Reduced infrastructure costs
- Flexible resource allocation
Risks of Overcommitting:
- Potential performance degradation if all VMs demand full resources simultaneously
- Potential for VM crashes or host instability if overcommitment is too aggressive
- Requires careful monitoring and management
- Not ideal for performance-sensitive workloads
- Requires specific processor extensions (Intel VT-x or AMD-V)
- Advanced networking can be complex
- Enterprise features may need additional tooling
Runs directly on the host's hardware to control the hardware and manage guest OSes.
Examples:
- XEN
- IBM LPARs
- ESXi
Runs as a software layer on top of a host operating system.
Examples:
- VMware Workstation
- VirtualBox
| Feature | Type 1 Hypervisor | Type 2 Hypervisor |
|---|---|---|
| Performance | High (direct hardware) | Moderate (host overhead) |
| Latency | Low | Higher |
| Use Cases | Servers, Cloud | Dev/Testing |
| Deployment | Enterprise infrastructure | Local machines |
| Use env | Best for production workloads | Best for development environments |
Note:
KVM is a type 1 hypervisor if you consider the OS as a hypervisor layer but in the meanwhile this OS can mange other apps, in this aspect you can consider the KVM as a type 2 hypervisor
| Term | Description |
|---|---|
| Host | Physical server that hosts guest VMs |
| VM | Guest virtual machine created on a KVM host |
| Virt-manager | Graphical tool for managing VMs |
| Virt-install | CLI tool for installing guest VMs |
| Libvirtd | Background service that manages KVM virtualization |
Note:
KVM is typically installed directly on a Linux OS, but for testing purposes, we set it up inside a virtual machine running on VMware Workstation under Windows. This creates a two-layer virtualization setup ... yes, it might sound crazy, but it provides valuable hands-on experience with KVM and deepens your understanding of nested virtualization.
- Two hard disks
- Two NICs
- Enough CPU and Memory
Check cpu info if it supports the vmx or svm for virtualization:
grep -E --color=auto 'vmx|svm|0xc0f' /proc/cpuinfoIf there is no output, check the virtualize Intel VT option in processor, VM setting
Configure repos in Centos:
sudo vi /etc/yum.repos.d/CentOS-Base.repoEdit the baseurl and hash the mirror:
baseurl=http://vault.centos.org/7.9.2009/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=7&arch=$basearch&repo=os&infra=stockAfter editing use this commands:
sudo yum clean all
sudo yum makecache
sudo yum updateInstall these packages:
yum install virt-install qemu-kvm libvirt libvirt-python libquestfs-tools virt-manager -yEnable the libvirtd daemon:
systemctl enable --now libvirtdReboot the kvm-host:
systemctl rebootEnsure the kernel modules for kvm are loaded:
modinfo kvm_intel
modinfo kvmConfigure the network in the kvm-host
Note:
Libvirtd and it's services create a virtual bridge interface virbr0 with network 192.168.122.0/24 and create a nic virbr0-nic
- edit this file: /etc/sysconfig/network-scripts/ifcfg-:
TYPE=Ethernet
BOOTPROTO=none
NAME=<interface-name>
ONBOOT=yes
BRIDGE=virbr0- create this file: /etc/sysconfig/network-scripts/ifcfg-virbr0:
TYPE=BRIDGE
DEVICE=virbr0
BOOTPROTO=none
ONBOOT=yes
IPADDR=<Natting-ip-in-your-system>
NETMASK=255.255.255.0
GATWAY=<your-gateway-in-your-system>Enable forwarding:
echo net.ipv4.ip_forward = 1 > /usr/lib/sysctl.d/60-libvirtd.conf
/sbin/sysctl -p /usr/lib/sysctl.d/60-libvirtd.confConfigure the firewalld:
firewall-cmd --permanent --direct --passthrough ipv4 -I FORWARD -i bridge0 -j ACCEPT
firewall-cmd --permanent --direct --passthrough ipv4 -I FORWARD -o bridge0 -j ACCEPT
firewall-cmd --reloadList the interfaces of the kvm-host:
virsh net-listEdit the default interface:
virsh net-dumpxml default
virsh net-edit defaultCreate storage pool for storing th VM images:
vgcreate lab-kvm-storage /dev/sdb
lvcreate -l +100%FREE -n lab-kvm-lv lab-kvm-storage
mkfs.xfs /dev/mapper/lab--kvm--storage-lab--kvm--lvAdd the following entry in /etc/fstab:
echo "/dev/mapper/lab--kvm--storage-lab--kvm--lv /var/lib/libvirt/images xfs defaults 0 0" >> /etc/fstab
mount –aCreate storage pool and start it, By default the libvirt use directory /var/lib/libvirt/images on a host as an initial file system storage pool:
virsh pool-define-as lab-kvm-storagepool --type dir --target /var/lib/libvirt/images
virsh pool-autostart lab-kvm-storagepool
virsh pool-start lab-kvm-storagepool
virsh pool-listTo see detailed info about pool:
virsh pool-list --all --detailsCheck if the guest OS is supported by kvm or not:
osinfo-query osNote:
In kvm termenology .. domain is equal to vm
Copy the iso from windows to vm in vmware workstation:
scp "E:\Linux ISO\CentOS-7-x86_64-Minimal-2009.iso" root@<ip-of-kvm-host:/Change the permissions in iso:
chmod 755 name-of-the-iso-fileUse virt-manager to create vm in GUI:
virt-managerChoose ur customize resoure but choose the network virbr0
QEMU (Quick Emulator) is a critical component in the KVM (Kernel-based Virtual Machine) virtualization stack. Here's a detailed explanation:
QEMU is an open-source machine emulator and virtualizer that works alongside KVM to provide complete virtualization solutions. In the KVM context
- Hardware Emulation: QEMU provides device emulation (CPU, memory, storage, network devices, etc.)
- User-space Component: While KVM operates in kernel space, QEMU runs in user space handling I/O and device emulation
- Management Interface: QEMU offers tools and interfaces to manage virtual machines
The typical architecture looks like this:
Guest OS → KVM (kernel module) → QEMU (user space) → Host OS Hardware
- KVM handles the CPU and memory virtualization (via kernel modules)
- QEMU handles the emulation of all other hardware components
- Emulates standard PC hardware (PIIX3/4 IDE, PS/2 mouse/keyboard, etc.)
- Can emulate various network cards, sound cards, and other peripherals
- Translates guest CPU instructions to host instructions
- When used with KVM, most instructions run natively on hardware
qemu-system-x86_64 -enable-kvm -m 2048 -hda /path/to/disk.img -cdrom /path/to/iso.isoNote:
In modern Linux distributions, you'll typically interact with QEMU through higher-level tools like libvirt and virt-manager rather than directly with QEMU commands.
- Kvm is the hypervisor
- Qemu is the command line interface for managing the vms
- Libvirt is the graphical user interface for managing the vms
- All of them are collection of software you should make sure they are installed in ur host
List all running VMs:
virsh list:List all VMs:
virsh list --allStart the VM:
virsh start <VM-id or Name>Stop the VM:
virsh shutdown <VM-id or Name>Reboot the VM:
virsh reboot <VM-id or Name>Suspend VM:
virsh suspend <VM-id or Name>Resume VM:
virsh resume <VM-id or Name>Destroy VM:
virsh shutdown <VM-id or Name>
virsh undefine <VM-id or Name>
virsh destroy <VM-id or Name>Enter guest's console:
virsh console <VM-id or Name>Exit guest's console:
Ctrl + AltTo enable autostart of the VM with the host:
virsh autostart <VM-id or Name>To disable autostart of the VM with the host:
virsh autostart --disable <VM-id or Name>To get the more info about specific VM (configuration of the vm):
virsh dominfo <VM-id or Name>To show the uuid for the VM:
virsh domuid <VM-id or Name>Note:
- Qemu stands for quick emulator
- Qcow2 stands for qemu copy-on-write
- And this file represent the disk image var/lib/libvirt/images/target_vm.qcow2, it contains the entire vm' disk (OS, apps, files)
From virt-manager click on the vm and it shutoff Or from command line:
Virsh shutdown vm
Virt-clone –original source-vm --name target-vm -f /var/lib/libvirt/images/target_vm.qcow2First you should shutdown the VM, though you can take backups when guest is running but it can lead to a unhealthy backup image:
virsh shutdown <VM-id or Name>Copy the disk file:
cp –p /var/lib/libvirt/images/guest-vm-image /path-to-backup-locationCopy the conf file:
cp –p /etc/libvirt/qemu/guest-vm.xml /path-to-backup-locationList the current snapshots:
virsh snapshot-list vm-name Create a snapshot:
virsh snapshot-create-as --domain vm-name –name "snapshot-name" --description "ur-descriptiuon"Check the details of a snapshot:
virsh snapshot-info –domain vm-name –currentTo revert to a snapshot (restore):
virsh shutdown –domain vm-name
virsh snapshot-revert --domain vm-name --snapshotname "name-of-the-snapeshot" --runningTo delete a snapshot:
virsh snapshot-delete --domain vm-name --snapshot "snapshot-name"We wil use qemu-img utility to resize the disk.
Check the details of the image:
Virsh vol-info /var/lib/libvirt/images/vm-name.qcow2Expand the disk:
Qemu-img resize /var/lib/libvirt/images/vm-name.qcow2 +2GCheck the volume again:
Virsh vol-info /var/lib/libvirt/images/vm-name.qcow2Note:
You have resized the qemu image file and it corrupt ur disk image or ur system gets rebooted a couple of times which leads to corruption of the qemu image, so the solutions is repair the image
Shutdown the vm:
virsh shutdown vm-nameInstall libguestfs-tools packages in the hosting system kvm:
yum install libguestfs-toolsPerform QEMU image fs repair using guestfish:
guestfish –a /path-to-specific-qemo-image
run
list-filesystems
fsck xfs /dev/sda1
fsck xfs /dev/centos/root
qStart the VM:
virsh start vm-nameLogs files are in /var/log/libvirt/qemu/
Dir that shared between vm on kvm and the kvm host itself
In your host kvm create a directory:
mkdir share
chmod 777 share/
touch any fileOn the VM GUI
qemu-img convert -f vdi -O qcow2 /path-to-name-of-the-image-in-vdi-format /var/lib/libvirt/images/lubunu.qcow2











