Part 1: Introduction to KVM (Kernel-based Virtual Machine)
What is KVM?
Kernel-based Virtual Machine (KVM) is an open-source virtualization technology built into the Linux kernel. It allows the kernel to function as a hypervisor, enabling multiple operating systems to share a single hardware host. KVM turns Linux into a type-1 (bare-metal) hypervisor. Every virtual machine is a regular Linux process, scheduled and managed by the kernel.
KVM is suitable for running any workload on Linux, ranging from a small development script to a large enterprise application.
How Does KVM Work?
KVM adds virtualization capabilities to the Linux kernel with loadable kernel modules – kvm.ko (the core virtualization infrastructure) and processor-specific modules, kvm-intel.ko or kvm-amd.ko. A user must have hardware with virtualization extensions (Intel VT or AMD-V) to use KVM.
Each virtual machine (also known as a guest VM) has private virtualized hardware, including a network card, disk, graphics adapter, etc. The Kernel handles the scheduling and memory management for guest VMs just like it does for regular processes.
Benefits of Using KVM
- Performance: KVM enables the direct sharing of physical device drivers, which results in better I/O performance.
- Scalability: KVM can support thousands of VMs, depending on the resources available (CPU cores, memory, disk space, and network).
- Security: The same security tools and practices applied to Linux (SELinux, firewalls, etc.) can be used to secure your VMs.
- Open-source: Being open-source, it allows developers to understand the system deeply and customize it according to their needs.
In the following sections, we will look into the detailed process of implementing KVM.
Part 2: Prerequisites for Implementing KVM
Hardware Requirements
- An x86 machine running a recent Linux kernel (2.6.20 or newer) with hardware virtualization extensions (Intel VT or AMD-V).
- Adequate memory for running multiple guest VMs. The actual amount of required memory will depend on the needs of your VMs.
Software Requirements
- Linux operating system with a recent kernel version (2.6.20 or newer).
- QEMU, which is a generic and open-source machine emulator and virtualizer. It works with KVM to run virtual machines at near-native speed by executing the guest code directly on the host CPU.
- libvirt, which is an open-source API, daemon, and management tool for managing platform virtualization. It is used to interact with the virtualization capabilities of recent versions of Linux (and other OSes).
Once you have ensured that your system meets all the hardware and software requirements, you can move on to the actual process of implementing KVM.
Part 3: Implementing KVM
Step 1: Installing Required Packages
On a Debian-based system like Ubuntu, you can install the required packages with the following commands:
sudo apt-get update
sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
The `qemu-kvm` package includes the QEMU system emulator, which can use KVM for full virtualization. `libvirt-daemon-system` and `libvirt-clients` are used for managing platform virtualization. `bridge-utils` is used to create a network bridge, which allows the VMs to have network access.
Step 2: Verifying Installation
After the installation, you can check if the kvm module is loaded with the following command:
lsmod | grep kvm
If KVM is properly installed, you should see `kvm_intel` or `kvm_amd`, depending on your CPU's manufacturer.
You can also check if libvirt is running with the following command:
sudo systemctl is-active libvirtd
Step 3: Creating a VM
With the command-line tool 'virt-install', you can create a new VM:
sudo virt-install --name exampleVM --ram 2048 --disk path=/var/lib/libvirt/images/exampleVM.img,size=10 --vcpus 1 --os-type linux --os-variant ubuntu20.04 --network bridge:virbr0 --graphics none --console pty,target_type=serial --location 'http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/' --extra-args 'console=ttyS0,115200n8 serial'
The above command creates a new VM named `exampleVM` with 2048 MB of RAM, 1 CPU, and 10 GB of disk space.
Please note that creating a VM may involve additional or different steps depending on the specific needs of your use case.
---
Part 4: Managing and Monitoring VMs with KVM
Once your virtual machines are up and running, you can use a variety of commands to manage and monitor them:
1. Listing all running VMs
You can list all running VMs with the following command:
sudo virsh list --all
2. Starting and stopping a VM
To start a VM, use the following command:
sudo virsh start exampleVM
To stop a VM, use the following command:
sudo virsh shutdown exampleVM
3. Deleting a VM
To undefine a VM (which does not delete the associated disk images), use the following command:
sudo virsh undefine exampleVM
These are just the basics. There are many more commands and utilities available for managing VMs with KVM. You can also use graphical tools like Virt-Manager if you prefer a GUI over the command line.
This concludes the overview of implementing KVM on a Linux machine. There are many other facets to explore, like networking setup, storage management, snapshot management, etc. Remember to consult the man pages and other documentation for the tools discussed here for further information.
Part 5: Networking in KVM
By default, libvirt creates a virtual network for your KVM virtual machines to connect to, and this network is NATed behind the host machine's primary network connection. However, sometimes, it is necessary to configure networking beyond the default setup. Here, we will explore how to create a bridged network, which will allow our virtual machines to act as if they were physical devices on the host's network.
Step 1: Installing bridge-utils
Bridge-utils is a collection of command-line utilities for creating Ethernet bridges on Linux. To install it on a Debian-based system, run the following command:
sudo apt-get install bridge-utils
Step 2: Creating the bridge
Next, you need to configure a bridge between your physical network interface and a virtual one that your virtual machines will use. This process varies depending on whether your host uses netplan or the older /etc/network/interfaces file to configure networking.
For netplan:
Open the /etc/netplan/01-netcfg.yaml file in a text editor:
sudo nano /etc/netplan/01-netcfg.yaml
And add the following lines:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
bridges:
br0:
dhcp4: yes
interfaces:
- enp0s3
Note: Replace `enp0s3` with the name of your network interface.
Save the file and apply the changes with:
sudo netplan apply
For /etc/network/interfaces:
Open the /etc/network/interfaces file in a text editor:
sudo nano /etc/network/interfaces
And add the following lines:
auto br0
iface br0 inet dhcp
bridge_ports enp0s3
bridge_stp off
bridge_fd 0
bridge_maxwait 0
Again, replace `enp0s3` with the name of your network interface.
Save the file and restart the network service:
sudo systemctl restart networking
Step 3: Creating a new VM with bridged networking
Now, when creating a new VM, you can specify `bridge:br0` instead of `network:default` to use the new bridge:
sudo virt-install --name exampleVM2 --ram 2048 --disk path=/var/lib/libvirt/images/exampleVM2.img,size=10 --vcpus 1 --os-type linux --os-variant ubuntu20.04 --network bridge:br0 --graphics none --console pty,target_type=serial --location 'http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/' --extra-args 'console=ttyS0,115200n8 serial'
---
Part 6: Managing Storage in KVM
KVM uses storage pools to store the disk images for its virtual machines. Storage pools are divided into storage volumes, and each volume is an individual disk image. By default, a directory-based storage pool is created at /var/lib/libvirt/images.
You can manage storage through the `virsh` command-line interface or graphical tools like Virt-Manager.
**Creating a new storage pool**
Here's how to create a new directory-based storage pool:
sudo virsh pool-define-as --name newpool --type dir --target /path/to/newpool
sudo virsh pool-build newpool
sudo virsh pool-start newpool
sudo virsh pool-autostart newpool
In the above commands, replace `/path/to/newpool` with the path where you want to create the new pool.
Creating a new storage volume
To create a new storage volume in a pool, use the `vol-create-as` command:
sudo virsh vol-create-as newpool newvol 10G
This command creates a 10 GB volume named `newvol` in the `newpool` pool.
Adding a disk to a VM
To add a disk to a VM, you first need to create a new storage volume and then attach it to the VM:
sudo virsh vol-create-as newpool newvol2 10G
sudo virsh attach-disk exampleVM --source /path/to/newpool/newvol2 --target vdb --persistent
---
This concludes the exploration of implementing KVM on a Linux machine. By now, you should have a solid understanding of KVM's core concepts and how to install and manage it. However, KVM is a powerful tool with many more features to explore. Be sure to consult the official documentation and other resources to learn more.