ClickCease Streamlining KVM Operations: A Comprehensive Cheat Sheet

Content Table

Join Our Popular Newsletter

Join 4,500+ Linux & Open Source Professionals!

2x a month. No spam.

Streamlining KVM Operations: A Comprehensive Cheat Sheet

Rohan Timalsina

March 7, 2024 - TuxCare expert team

  • KVM offers several methods to manage virtual machines, including command-line tools and graphical user interfaces (GUIs)
  • All logs related to KVM virtual machines are stored in the /var/log/libvirt directory
  • QEMUCare is used for automated vulnerability patching KVM-based virtualization systems without rebooting or migration

The Kernel-based Virtual Machine (KVM) is a built-in open-source virtualization technology in the Linux kernel that allows running multiple virtual machines (VMs) on a single physical host machine. KVM is widely adopted in both enterprise and cloud computing environments due to its performance, flexibility, and cost-effectiveness. However, when it comes to managing KVM virtual machines, things can get tricky. In this article, we’ll explore a comprehensive list of essential commands, tips, and tricks for optimizing KVM operations and troubleshooting common issues.

 

Installing KVM on Linux

 

If you have not installed KVM on your Linux machine, use the following commands to begin the installation. 

 

On Ubuntu/Debian:

 

$ sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-install virt-manager

 

On RHEL/AlmaLinux/RockyLinux:

 

$ sudo dnf install qemu-kvm libvirt virt-install virt-manager

 

qemu-kvm: This package provides QEMU with KVM support, the core component of KVM virtualization.

libvirt-daemon-system: The main system daemon (libvirtd) for the libvirt virtualization toolkit.

libvirt-clients: Command-line tools for interacting with the libvirt daemon, useful for managing virtual machines.

bridge-utils: Utilities for configuring network bridge interfaces, often used for connecting virtual machines to the network.

virt-install: Provides utilities for creating virtual machines.

virt-manager: A graphical user interface (GUI) application for managing virtual machines. It provides a more user-friendly interface compared to command-line tools like virt-install.

 

Common KVM Operations and Commands in Linux

Creating a Virtual Machine

 

You can use the virt-install to create a new virtual machine.

$ sudo virt-install --name=ubuntu-vm \

--os-variant=ubuntu22.04 \

--vcpu=2 \

--ram=2048 \

--disk=size=20

--graphics none \

--location=/home/rocky/Downloads/ubuntu-22.04.3-desktop-amd64 \

--network bridge:virbr0

 

In the above command,

 

–name=ubuntu-vm specifies the name of the virtual machine to be created. In this case, the VM will be named “ubuntu-vm”.

 

–os-variant=ubuntu22.04 specifies the OS variant or version. In this case, it indicates that the guest OS will be Ubuntu 22.04.

 

–vcpu=2 sets the number of virtual CPUs (vCPUs) allocated to the virtual machine. This VM will have 2 vCPUs.

 

–ram=2048 sets the amount of memory (RAM) allocated to the virtual machine. This VM will have 2048 MB (2 GB) of RAM.

 

–disk size=15 defines the size of the disk image. This VM will have a disk size of 20 GB.

 

–graphics none configures the graphics settings for the virtual machine

 

–location specifies the location of the installation media (ISO file) to install the guest operating system. In this case, it points to the Ubuntu 22.04.3 ISO file located at “/home/rocky/Downloads/ubuntu-22.04.3-desktop-amd64”.

 

–network bridge:virbr0 defines the network configuration for the virtual machine.

“bridge:vibr0” specifies that the VM will use a bridged network interface, connecting to the “virbr0” bridge.

 

Starting Virtual Machines

 

To start the virtual machine “ubuntu-vm,” you can use the command:

$ sudo virsh start ubuntu-vm

 

Stopping Virtual Machines

 

To stop or turn off the virtual machine, use the command:

 

$ sudo virsh shutdown ubuntu-vm

 

Listing Virtual Machines 

 

The following command displays all virtual machines.

 

$ sudo virsh list --all

 

To list only running virtual machines, you can use:

$ sudo virsh list

 

Snapshotting Virtual Machine

 

You can create a snapshot of your virtual machine using the following syntax:

 

virsh snapshot-create VM_NAME SNAPSHOT_NAME

 

For example, to create a snapshot of “ubuntu-vm,” you can use:

 

$ sudo virsh snapshot-create ubuntu-vm ubuntu-snapshot

 

Cloning a Virtual Machine

 

To clone a virtual machine, use the below syntax:

 

$ sudo virt-clone --original=SOURCE_VM --name=NEW_VM_NAME --auto-clone

 

Monitoring Resources Usage of VM

 

The virt-top command displays stats of all virtualization systems such as CPU usage, memory usage, running status, and more.

 

$ sudo virt-top

 

Tips and Tricks to Streamline KVM Operations

 

Performance Optimization: Allocate resources (CPU, memory) according to the requirements of the workload.

Networking: Use bridged networking for VMs to directly access the network.

Security: Update and patch the host system on a regular basis. Utilize firewalls and SELinux/AppArmor for extra protection. 

Backup and Restore: Do regular backups of VMs using tools like virsh snapshot-create. Store backups in a secure location.

Resource Monitoring: Monitor VM resource usage regularly to identify bottlenecks and optimize performance.

Live Migration: Use the live migration feature to move virtual machines (VMs) between hosts without any downtime.

Troubleshooting: Check VM and KVM-related logs for errors in the /var/log/libvirt directory. If you are unable to solve any issues, seek assistance from KVM forums and communities.

 

Live Patching KVM-based Virtualization Systems

 

Conventionally, patching the KVM hypervisors has been a disruptive and intricate process, often requiring a reboot or migration of the virtual machine, leading to downtime and service disruptions.

However, TuxCare’s QEMUCare live patching solution changes the game by getting rid of all the moving around and disruptions. It allows you to apply security patches on your QEMU/KVM hypervisors without having to restart your virtual machines or migrate them to another running hypervisor.

Additionally, QEMUCare automates the patching process, ensuring all security updates are deployed automatically without any impact on the virtual machines. This also makes it easier to meet compliance requirements with timely patch management.

 

  

Final Thoughts

 

While setting up and managing KVM environments can be straightforward, mastering the various commands, tips, and tricks can significantly help in managing virtualized environments and troubleshooting issues effectively. 

Users can streamline KVM operations, optimize performance, and ensure the smooth functioning of their KVM infrastructure by implementing the essential commands and best practices outlined in this article. With continued practice and exploration, users can unlock the full potential of KVM for their virtualization needs.

Discover more about QEMUCare and learn how live patching ensures security and compliance with zero downtime.

Get in touch with a TuxCare Linux security expert to eliminate patch-related disruption with QEMUCare. 

Summary
Streamlining KVM Operations: A Comprehensive Cheat Sheet
Article Name
Streamlining KVM Operations: A Comprehensive Cheat Sheet
Description
Learn how to optimize KVM operations with essential commands, tips, and tricks. Streamline management and troubleshoot efficiently.
Author
Publisher Name
TuxCare
Publisher Logo

Looking to automate vulnerability patching without kernel reboots, system downtime, or scheduled maintenance windows?

Learn About Live Patching with TuxCare

Become a TuxCare Guest Writer

Get started

Mail

Join

4,500

Linux & Open Source
Professionals!

Subscribe to
our newsletter