Kernel Virtual Machine: A Free and Open Source Virtualization Technology for Linux
Kernel Virtual Machine: A Free and Open Source Solution for Virtualization
If you are looking for a way to run multiple operating systems on a single physical machine, you might have heard of virtualization. Virtualization is a technology that allows you to create virtual machines (VMs) that emulate the hardware and software of a real computer. You can use VMs to test different OSes, applications, configurations, or scenarios without affecting your main system.
kernel virtual machine pdf free
There are many virtualization solutions available in the market, but one of the most popular and powerful ones is kernel virtual machine (KVM). KVM is a free and open source solution that integrates with the Linux kernel and allows you to run multiple VMs with near-native performance. In this article, we will explain what KVM is, how it works, how to install and configure it on Linux, how to manage and monitor your VMs, how to access them remotely, and how to download free PDF books on KVM.
How KVM Works
KVM is a kernel module that adds virtualization capabilities to the Linux kernel. It leverages the hardware features of modern processors that support virtualization extensions, such as Intel VT-x or AMD-V. These extensions allow the CPU to switch between different modes of operation, such as host mode (running the host OS) and guest mode (running the guest OS). This way, the CPU can execute instructions from multiple VMs without interfering with each other.
KVM also relies on another component called QEMU (Quick Emulator), which is a user-space program that emulates various devices and peripherals for the VMs, such as disk drives, network cards, graphics cards, etc. QEMU also provides an interface for managing the VMs, such as creating, deleting, starting, stopping, etc.
The combination of KVM and QEMU allows you to run almost any OS on your Linux host, such as Windows, macOS, BSD, Solaris, etc. You can also run different versions or distributions of Linux on your VMs, such as Ubuntu, Fedora, Debian, etc. You can even run nested VMs, which means running a VM inside another VM.
How to Install and Configure KVM on Linux
Before you can use KVM, you need to make sure that your system meets the following requirements:
You have a 64-bit processor that supports virtualization extensions (Intel VT-x or AMD-V). You can check this by running the command grep -E '(vmxsvm)' /proc/cpuinfo. If you see vmx or svm in the output, it means your processor supports virtualization.
You have enough RAM and disk space for your VMs. The amount of RAM and disk space you need depends on the number and size of your VMs, but as a general rule, you should have at least 4 GB of RAM and 20 GB of disk space for each VM.
You have a Linux distribution that supports KVM. Most modern Linux distributions support KVM, but some may require additional packages or configuration. In this article, we will use Ubuntu 20.04 as an example, but you can adapt the instructions to your own distribution.
Once you have verified that your system meets the requirements, you can follow these steps to install and configure KVM on Linux:
Installing KVM Packages
The first step is to install the KVM packages on your Linux host. On Ubuntu 20.04, you can use the apt command to install the following packages:
sudo apt update sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager
The qemu-kvm package installs the QEMU emulator and the KVM module. The libvirt-daemon-system and libvirt-clients packages install the libvirt library and tools, which provide an API and a command-line interface for managing KVM. The bridge-utils package installs the tools for creating and managing network bridges, which allow your VMs to communicate with each other and with the outside world. The virtinst package installs the virt-install tool, which allows you to create VMs from the command line. The virt-manager package installs the virt-manager tool, which provides a graphical user interface for managing KVM.
Enabling Virtualization Support in BIOS
The next step is to enable virtualization support in your BIOS settings. This may vary depending on your motherboard model and manufacturer, but in general, you need to reboot your system and enter the BIOS setup menu by pressing a key such as F2, F10, or Del. Then, look for an option such as "Virtualization Technology", "Intel Virtualization Technology", "AMD-V", or "SVM Mode" and enable it. Save your changes and exit the BIOS setup menu.
Creating a Virtual Network
The third step is to create a virtual network for your VMs. A virtual network is a logical network that connects your VMs to each other and to the outside world. There are different types of virtual networks, such as NAT (Network Address Translation), bridged, routed, isolated, etc., but in this article, we will use a NAT network as an example.
A NAT network allows your VMs to share the IP address of your host machine and access the internet through it. It also provides DHCP (Dynamic Host Configuration Protocol) and DNS (Domain Name System) services for your VMs. To create a NAT network, you can use either the virsh tool or the virt-manager tool.
To use the virsh tool, run the following command:
sudo virsh net-define /usr/share/libvirt/networks/default.xml sudo virsh net-start default sudo virsh net-autostart default
This will create a NAT network named "default" with the following parameters:
The network address is 192.168.122.0/24
The gateway address is 192.168.122.1
The DHCP range is 192.168.122.2-192.168.122.254
The DNS server is 192.168.122.1
To use the virt-manager tool, open it from the application menu or run the following command:
sudo virt-manager
This will open a window like this:
will see a window like this:
Enter a name for your network, such as "default", and click on "Forward". Then, choose "NAT" as the network mode and click on "Forward". Then, choose "IPv4 only" as the IP family and enter the network address, netmask, gateway, and DHCP range for your network. You can use the same parameters as the virsh tool or customize them as you wish. Click on "Forward" and then on "Finish". This will create a NAT network with the parameters you specified.
Creating a Virtual Machine
The fourth step is to create a virtual machine for your desired OS. A virtual machine is a file that contains the disk image, memory, CPU, and other devices of a virtual computer. To create a virtual machine, you need to have an installation media for your OS, such as an ISO file or a CD/DVD. You also need to specify the name, memory, disk size, and other options for your VM. To create a VM, you can use either the virt-install tool or the virt-manager tool.
To use the virt-install tool, run the following command:
sudo virt-install --name ubuntu-vm --memory 2048 --disk size=20 --cdrom /path/to/ubuntu.iso --network network=default --graphics spice
This will create a VM named "ubuntu-vm" with the following parameters:
The memory size is 2048 MB
The disk size is 20 GB
The installation media is /path/to/ubuntu.iso (replace this with the actual path to your ISO file)
The network is connected to the default NAT network
The graphics mode is SPICE (Simple Protocol for Independent Computing Environments), which allows you to access your VM remotely with high performance and quality
To use the virt-manager tool, open it from the application menu or run the following command:
sudo virt-manager
This will open a window like this:
Click on the "File" menu and select "New Virtual Machine". You will see a window like this:
Choose "Local install media (ISO image or CDROM)" as the installation method and click on "Forward". Then, browse to your ISO file or insert your CD/DVD and click on "Forward". Then, choose "Linux" as the OS type and "Ubuntu" as the OS variant and click on "Forward". Then, enter a name for your VM, such as "ubuntu-vm", and adjust the memory and CPU settings as you wish. Click on "Forward". Then, choose "Select or create custom storage" and click on "Manage...". You will see a window like this:
Click on the "+" button to create a new storage pool. You will see a window like this:
Enter a name for your storage pool, such as "default", and choose "dir: Filesystem Directory" as the type. Click on "Forward". Then, enter "/var/lib/libvirt/images" as the target path and click on "Finish". This will create a storage pool in that directory. Then, select your storage pool and click on the "+" button to create a new storage volume. You will see a window like this:
Enter a name for your storage volume, such as "ubuntu-vm.qcow2", and choose "qcow2" as the format. Enter 20 GB as the capacity and click on "Finish". This will create a disk image file in that directory. Then, select your storage volume and click on "Choose Volume". This will return you to the previous window. Click on "Forward". Then, choose "Specify shared device name" and enter "default" as the network name. This will connect your VM to the default NAT network. Click on "Forward". Then, choose "Spice" as the display type and click on "Finish". This will create a VM with the parameters you specified.
How to Manage and Monitor KVM Virtual Machines
Once you have created your VMs, you can manage and monitor them using various commands and tools. You can start, stop, pause, resume, reboot, or delete your VMs. You can also list, view, resize, migrate, backup, or restore your VMs. You can use either the virsh tool or the virt-manager tool to perform these tasks.
Starting, Stopping, and Pausing VMs
To start a VM, you can use the following command:
sudo virsh start ubuntu-vm
This will start the VM named "ubuntu-vm". You can replace this with the name of your VM.
To stop a VM, you can use the following command:
sudo virsh shutdown ubuntu-vm
This will gracefully shut down the VM named "ubuntu-vm". You can replace this with the name of your VM.
To force stop a VM, you can use the following command:
sudo virsh destroy ubuntu-vm
This will forcefully power off the VM named "ubuntu-vm". You can replace this with the name of your VM. Note that this may cause data loss or corruption in your VM, so use it with caution.
To pause a VM, you can use the following command:
sudo virsh suspend ubuntu-vm
This will suspend the VM named "ubuntu-vm". You can replace this with the name of your VM. This will save the state of your VM in memory and stop its execution.
To resume a VM, you can use the following command:
sudo virsh resume ubuntu-vm
This will resume the VM named "ubuntu-vm". You can replace this with the name of your VM. This will restore the state of your VM from memory and continue its execution.
To reboot a VM, you can use the following command:
sudo virsh reboot ubuntu-vm
This will reboot the VM named "ubuntu-vm". You can replace this with the name of your VM. This will restart your VM as if you pressed the reset button on a physical machine.
To use the virt-manager tool, open it from the application menu or run the following command:
sudo virt-manager
This will open a window like this:
Select your VM from the list and click on the buttons on the toolbar to perform various actions. You can also right-click on your VM and select an option from the menu. The buttons and options are as follows:
The green triangle button or the "Run" option starts your VM.
The red square button or the "Shut Down" option gracefully shuts down your VM.
The yellow pause button or the "Pause" option suspends your VM.
The blue play button or the "Resume" option resumes your VM.
The circular arrow button or the "Reboot" option reboots your VM.
The red power button or the "Force Off" option forcefully powers off your VM.
The trash bin button or the "Delete" option deletes your VM.
Listing and Viewing VMs
To list all your VMs, you can use the following command:
sudo virsh list --all
This will show you a table like this:
Id Name State --- --- --- 1 ubuntu-vm running - windows-vm shut off This shows you the ID, name, and state of each VM. The ID is a number that identifies your VM. The name is a string that you assigned to your VM when you created it. The state is either running, paused, shut off, or crashed.
To view more details about a specific VM, you can use the following command:
sudo virsh dominfo ubuntu-vm
This will show you something like this:
Name ubuntu-vm --- --- UUID 12345678-1234-1234-1234-123456789abc --- --- OS Type hvm State running CPU(s) 2 CPU Time 12.3s Max Memory 2097152 KiB Used Memory 2097152 KiB Persistent yes Autostart disable Managed Save no Security Model apparmor Security DOI 0 This shows you more information about your VM, such as the UUID (Universally Unique Identifier), the OS type (hardware virtualized or paravirtualized), the CPU and memory usage, the persistence and autostart settings, the security model and domain of interpretation, etc.
To use the virt-manager tool, open it from the application menu or run the following command:
sudo virt-manager
This will open a window like this:
Select your VM from the list and double-click on it or click on the "Open" button. This will open a window like this:
This shows you a graphical console of your VM, where you can see and interact with your guest OS. You can also see various tabs that show you more details about your VM, such as overview, performance, hardware, snapshots, etc.
Resizing and Migrating VMs
Sometimes, you may need to resize or migrate your VMs to adjust to your changing needs or preferences. Resizing means changing the disk size or memory size of your VMs. Migrating means moving your VMs from one host to another. You can use either the virsh tool or the virt-manager tool to perform these tasks.
Resizing VMs
To resize the disk size of a VM, you need to first shut down your VM and then use the following command:
sudo virsh blockresize ubuntu-vm /var/lib/libvirt/images/ubuntu-vm.qcow2 30G
This will resize the disk image file of the VM named "ubuntu-vm" to 30 GB. You can replace this with the name and path of your VM and disk image file. You can also specify a different size as you wish.
To resize the memory size of a VM, you need to first shut down your VM and then use the following command:
sudo virsh setmaxmem ubuntu-vm 4096M --config sudo virsh setmem ubuntu-vm 4096M --config
This will resize the memory size of the VM named "ubuntu-vm" to 4096 MB. You can replace this with the name of your VM and the desired memory size. The --config option makes sure that the changes are persistent across reboots.
To use the virt-manager tool, open it from the application menu or run the following command:
sudo virt-manager
This will open a window like this:
Select your VM from the list and right-click on it and select "Shut Down". Then, right-click on it again and select "Details". This will open a window like this:
Click on the "Hardware" tab and select the disk or memory device that you want to resize. Then, click on the "Edit" button and adjust the size as you wish. Click on "Apply" to save your changes.
Migrating VMs
To migrate a VM from one host to another, you need to have both hosts connected to the same network and have KVM installed and configured on both hosts. You also need to have a shared storage pool or a shared directory for your disk image files. To migrate a VM, you can use either the virsh tool or the virt-manager tool.
Using virsh tool
To use the virsh tool, run the following command on the source host:
sudo virsh migrate --live ubuntu-vm qemu+ssh://user@destination/system
This will migrate the VM named "ubuntu-vm" to the destination host using the qemu+ssh protocol. You can replace this with the name of your VM and the user and hostname of your destination host. The --live option means that the migration will happen while the VM is running, without any downtime. You may need to enter the password for the user on the destination host.
Using virt-manager tool
To use the virt-manager tool, open it from the application menu or run the following command on the source host:
sudo virt-manager
This will open a window like this:
Select your VM from the list and right-click on it and select "Migrate". You will see a window like this:
Enter the user and hostname of your destination host and choose "QEMU+SSH" as the method. Check the "Live migration" option and click on "Migrate". You may need to enter the password for the user on the destination host.
How to Access KVM Virtual Machines Remotely
Sometimes, you may want to access your VMs remotely from another machine, such as your laptop or smartphone. There are different methods and protocols for accessing your VMs remotely, such as SSH (Secure Shell), VNC (Virtual Network Computing), or SPICE (Simple Protocol for Independent Computing Environments). You can use either the command-line tools or the graphical tools to connect to your VMs.
Using SSH
SSH is a protocol that allows you to securely log in to a remote machine and execute commands. To use SSH, you need to have an SSH server running on your VM and an SSH client on your local machine. You also need to have a pair of SSH keys for authentication. To use SSH, follow these steps:
On your VM, install the openssh-server package using apt or yum command.
On your VM, edit the /etc/ssh/sshd_config file and make sure that the following lines are uncommented and set as follows:
PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes
On your VM, restart the ssh service using systemctl or service command.
On your local machine, generate a pair of SSH keys using ssh-keygen command.
On your local machine, copy your public key