KVM in Practice
Introduction
KVM is one of the most widely used hardware virtualization technologies today. This post covers setting up a KVM environment, managing KVM virtual machines, and programming with libvirt in Java.
Operating System: CentOS 7 (1804)
CPU: 2
Memory: 2G
ISO Image: http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1804.iso
Setting Up the KVM Environment
Setting up a KVM environment can be broken down into four steps.
Step 1: Check Whether the CPU Supports Virtualization
KVM hardware virtualization is full virtualization and requires hardware support. Therefore, before setting up a KVM environment, you need to confirm that the CPU supports virtualization.
Command:grep -E "(vmx|svm)" /proc/cpuinfo
Result:

Notes:
- /proc: The proc filesystem is a pseudo-filesystem that stores a series of special files reflecting the current state of the running kernel. Users can read system hardware and process information through these files, and can even modify kernel behavior by changing certain files.
- /proc/cpuinfo: CPU-related information.
- vmx: Intel Virtualization Technology
- svm: AMD Virtualization Technology
Step 2: Install KVM-Related Components
Command:yum -y install qemu-kvm qemu-kvm-tools virt-manager virt-install libvirt
Notes:
- qemu-kvm: User-space tool for managing KVM, network cards, sound cards, PCI devices, etc.
- qemu-kvm-tools: KVM debugging and diagnostic tools.
- virt-manager: Optional. A desktop GUI tool for managing virtual machines via libvirt.
- virt-install: A tool for installing virtual machines.
- libvirt: A Linux API that implements Linux virtualization functionality, supporting various hypervisors including Xen and KVM, as well as QEMU and some virtual products for other operating systems.
Step 3: Verify KVM Is Loaded Successfully
Command:lsmod | grep kvm
Result:

Notes:
- lsmod: Displays status information about modules currently loaded into the kernel.
Step 4: Start libvirtd
Command:systemctl start libvirtdsystemctl staus libvirtdsystemctl enable libvirtd
Result:

Notes:
- systemctl: A systemd utility responsible for controlling the systemd system and service manager.
Virtual Machine Operations
Creating a Virtual Machine
Step 1: Create a Disk Image
Command:qemu-img create -f qcow2 /opt/test.qcow 10G
Result:

Notes:
- qemu-img: The disk management tool for QEMU.
- qemu-img create [-f fmt][-o options] filename [size]:
- -f specifies the disk format,
- -o additional options,
- filename the disk image filename,
- size the disk size.
Step 2: Create the Virtual Machine
Command:virt-install --name CentOS-7.5-x86_64 --virt-type kvm --ram 1024 --cdrom=/root/CentOS-7-x86_64-Minimal-1804.iso --disk path=/opt/test.qcow --network network=default --graphics vnc,listen=0.0.0.0
Result:

Notes:
- –name: Virtual machine name.
- –virt-type: Virtualization type.
- –ram: Memory.
- –cdrom: OS installation image.
- –disk: System disk.
- –network: Network configuration.
- –graphics: Configure guest graphics output.
Step 3: Check Virtual Machine Status (Optional)
Command:virsh list --all
Result:
Step 4: Remote Access to the Virtual Machine (Optional)

Notes:
Connect to the host machine’s IP and port using a VNC client to access the virtual machine.
Common Virtual Machine Commands
Common Commands:
| Command | Description |
|---|---|
| virsh list [–all] | List virtual machines and their status. Without –all, only active VMs are shown. |
| virsh start [Name] | Start the virtual machine with the given Name. |
| virsh reboot [Id/Name] | Reboot the virtual machine by its id or name. |
| virsh suspend [Id/Name] | Suspend the virtual machine by its id or name. |
| virsh resume [Id/Name] | Resume the virtual machine by its id or name, restoring it to the running state. |
| virsh shutdown [Id/Name] | Shut down the virtual machine by its id or name. |
| virsh destroy [Id/Name] | Force-stop the virtual machine. |
| virsh undefine [Id/Name] | Remove the virtual machine by its id or name. |
Example:

Cloning a Virtual Machine
Command:
virt-clone -o CentOS-7.5-x86_64 --auto-clone
Result:
