KVM: Tutorial & Best Practices
A virtualization system
KVM (Kernel-based Virtual Machine) is a virtualization solution for Linux that allows you to run multiple virtual machines (VMs) on a single physical machine. Each VM is isolated from the others, and each one can run its own operating system and applications as if it were a physical machine.
KVM is built into the Linux kernel, which makes it fast and efficient. It also supports a wide range of hardware, including processors with hardware virtualization support, making it suitable for use in production environments.
To use KVM, you will need to install the KVM software on your Linux system and create one or more virtual machines. You can then start, stop, and manage the VMs using the KVM tools or through a graphical interface such as virt-manager.
KVM is a powerful and flexible tool for managing virtualization on Linux, and it is widely used in organizations and businesses to consolidate hardware resources, improve resource utilization, and facilitate the deployment and management of multiple VMs.
To install and configure KVM under Linux, you will need to follow these steps:
Install KVM: To install KVM on a Debian-based system (such as Ubuntu), you can use the following command:
sudo apt-get update && sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients
To install KVM on a Red Hat-based system (such as CentOS), you can use the following command:
sudo yum install qemu-kvm libvirt virt-install
Check hardware support: Before you can use KVM, you will need to make sure that your hardware supports virtualization. You can use the following command to check if your CPU has virtualization support:
grep -E --color 'vmx|svm' /proc/cpuinfo
If the command returns any output, it means that your CPU supports virtualization. If the command does not return any output, it means that your CPU does not support virtualization and you will not be able to use KVM.
Create a virtual machine: To create a virtual machine with KVM, you can use the virt-install command. This command allows you to specify the operating system, memory, disk space, and other options for the VM.
For example, to create a VM with 1 GB of RAM and a 20 GB hard drive, you could use the following command:
sudo virt-install --name myvm --ram 1024 --disk size=20
This will start the VM creation process, and it will prompt you to select an operating system ISO file and configure other options such as the network configuration and the installation type.
Start the virtual machine: Once you have created a virtual machine, you can start it using the virsh command. For example, to start the VM that you created in step 3, you can use the following command:
sudo virsh start myvm
To connect to the VM, you will need to use a VNC client such as TightVNC or RealVNC. You can find the VNC connection information for the VM by running the following command:
sudo virsh vncdisplay myvm
This will output the IP address and port number that you can use to connect to the VM using a VNC client.
Manage the virtual machine: To manage the virtual machine, you can use the virsh command to perform various actions such as shutting down, rebooting, or suspending the VM. For example, to shut down the VM that you created in step 3, you can use the following command:
sudo virsh shutdown myvm
You can also use the virt-manager graphical interface to manage VMs. To start virt-manager, use the following command:
This will open the virt-manager window, which allows you to view and manage your VMs.
That's it! You should now have a working KVM installation on your Linux machine. You can create and manage multiple VMs using the tools and commands described above.
Install KVM packages
apt install qemu-kvm libvirt-clients libvirt-daemon-system virtinst libguestfs-tools
Configure NAT for Network Access
The network interface is already there, but it is not started. Configure autostart like this:
virsh net-autostart default virsh net-start default
Prepare Automatic Installation for Debian
Create a script
preseed.cfg which is used to automate Debian installation:
Create a new Virtual Machine
virt-install --virt-type kvm --name vm2 --vcpus 2 --ram 1024 --disk size=8 --network bridge=virbr0 --graphics none --console pty,target_type=serial --location http://ftp.de.debian.org/debian/dists/stable/main/installer-amd64/ --os-variant debian9 --extra-args "auto=true language=en country=DE locale=en_US.UTF-8 hostname=vm2 domain=example.com console=ttyS0,115200n8 serial" --initrd-inject=preseed.cfg
Management of Virtual Machines
virsh list virsh list --all virsh start vm1 --console virsh shutdown vm1 virsh destroy vm1 virsh undefine vm1
Accessing Data from VM
You can mount the file system of a VM like this:
guestmount -d vm1 -m /dev/sda1 /mnt
To see the console of a virtual machine, it has to be forwarded. To do that, you need to modify the boot parameters inside the VM and add the console in /boot/grub/grub.cfg:
linux /boot/vmlinuz-4.9.0-4-amd64 root=/dev/vda1 ro quiet console=ttyS0
Change memory settings:
virsh setmaxmem vm1 1G --config virsh setmem vm1 1G --config