Setting Up Virtual Machines with QEMU, KVM, and Virt-Manager on Debian/Ubuntu

Virtualization technology has become an indispensable tool in software development, testing, and deployment. It allows you to run multiple virtual machines (VMs) on a single physical machine, each with its own isolated operating system and resources. This tutorial focuses on setting up a virtualization environment on Debian or Ubuntu Linux using QEMU, KVM (Kernel-based Virtual Machine), and Virt-Manager.

In this tutorial you will learn:

  • How to update your Debian or Ubuntu system.
  • The process of installing QEMU, KVM, and Virt-Manager.
  • How to add your user to the necessary groups to manage VMs without root privileges.
  • Steps to verify the installation and manage virtual machines with Virt-Manager.
Setting Up Virtual Machines with QEMU, KVM, and Virt-Manager on Debian/Ubuntu
Setting Up Virtual Machines with QEMU, KVM, and Virt-Manager on Debian/Ubuntu
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Debian or Ubuntu Linux system
Software QEMU, KVM, Virt-Manager
Other Internet connection for software installation
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Step-by-Step Guide to Virtualization with QEMU, KVM, and Virt-Manager

Before diving into the steps, it’s important to understand the roles of QEMU, KVM, and Virt-Manager. QEMU is an open-source machine emulator and virtualizer that allows you to run operating systems and software designed for a different architecture. KVM is a virtualization module in the Linux kernel that allows the kernel to function as a hypervisor. Lastly, Virt-Manager is a graphical interface for managing virtual machines through libvirt.

  1. Update Your System: Ensure that your Debian or Ubuntu system is up-to-date to avoid any compatibility issues during the installation of virtualization tools. Run the following commands in a terminal:
    $ sudo apt update
    $ sudo apt upgrade




    This step updates the list of available packages and their versions and then installs the newest versions of the packages currently installed on your system.
  2. Install QEMU and Virt-Manager: Install QEMU, KVM, and Virt-Manager to set up your virtualization environment. These tools will allow you to create and manage virtual machines with ease. Execute the following command:
    $ sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager

    This command installs all necessary packages, including QEMU for emulation, KVM for hardware acceleration, libvirt daemon for managing VMs, libvirt clients for command-line interaction, bridge-utils for network bridging, and Virt-Manager for graphical management.

  3. Add Your User to Necessary Groups: To manage virtual machines without root privileges, add your user to the ‘libvirt’ and ‘kvm’ groups by running:
    $ sudo adduser $USER libvirt
    $ sudo adduser $USER kvm

    Log out and back in for the group changes to take effect. This step is crucial for enabling non-privileged access to VM management functionalities.

  4. Verify Installation: Check that the libvirt service is running with:
    $ sudo systemctl status libvirtd

    If it’s not running, start and enable it at boot with:

    $ sudo systemctl start libvirtd
    $ sudo systemctl enable libvirtd
    Check that the libvirt service is running with
    Check that the libvirt service is running with

    This step ensures that the libvirt daemon is active and set to start automatically on boot, which is necessary for managing virtual machines.

  5. Launch Virt-Manager: Open Virt-Manager either from your applications menu or by executing the following command in a terminal:
    $ virt-manager

    Virt-Manager provides a user-friendly graphical interface for creating, configuring, and managing virtual machines. It simplifies the process of VM management, making it accessible even to those new to virtualization.

  6. Create a New Virtual Machine: In Virt-Manager, click on the “Create a new virtual machine” button. Follow the wizard to select the installation method (ISO image, network installation, or importing existing disk), allocate resources (CPU, memory, disk space), and complete the setup by installing the operating system.
  7. Using Your Virtual Machine: Once the operating system installation is complete, you can start, stop, pause, and configure your virtual machine settings through Virt-Manager. Additionally, you can connect to the VM’s console to interact with it directly.

    Using Your Virtual Machine
    Using Your Virtual Machine

Advanced Tips and Tricks for Virtualization with QEMU, KVM, and Virt-Manager

After setting up your virtualization environment with QEMU, KVM, and Virt-Manager, there are several advanced features and best practices you can employ to enhance your virtual machine management and performance. Here are some tips and tricks to help you get the most out of your virtualized setup.

Utilizing QEMU Guest Agent

The QEMU Guest Agent facilitates improved communication between the host and the guest VMs, enabling functionalities such as file transfers, graceful shutdowns, and system information queries. To take advantage of these features, install the guest agent in your VMs:

$ sudo apt install qemu-guest-agent

Once installed, make sure the guest agent service is enabled and running within your VMs for enhanced performance and management capabilities.

Mastering Snapshots

Snapshots are a powerful feature that allows you to save the state of a VM at any given point in time. This is incredibly useful for testing software or updates without risking your primary system state. To create a snapshot in Virt-Manager, select your VM, click on the “Snapshots” section, and then “Take Snapshot”. Always ensure you have enough disk space available before taking snapshots to avoid performance degradation.

Keyboard Shortcuts and Sending Keys

Virt-Manager provides keyboard shortcuts for common actions such as sending the ‘Ctrl+Alt+Delete’ command to a VM. This can be done through the VM’s menu in Virt-Manager. Familiarizing yourself with these shortcuts can significantly streamline your workflow.

Attaching USB Devices

To attach USB devices directly to a VM, go to the VM’s hardware details in Virt-Manager, add a USB host device, and select the device you wish to attach. This is particularly useful for software testing on different operating systems or accessing data stored on external drives directly within your VM.

Command-Line Administration

While Virt-Manager provides a user-friendly GUI for managing VMs, knowing how to perform tasks from the command line can be invaluable, especially for remote administration or scripting. Here are some basic commands:

  • List all running VMs: sudo virsh list --all
  • Start a VM: sudo virsh start vm_name
  • Shutdown a VM: sudo virsh shutdown vm_name
  • Get VM information: sudo virsh dominfo vm_name
Listing all virtual machines from a command line
Listing all virtual machines from a command line

These commands offer a glimpse into the powerful administration capabilities available directly from the command line.



Networking Tips

Networking in virtualized environments can be complex. Using Virt-Manager, you can easily set up different types of network connections for your VMs, such as NAT (Network Address Translation) for isolated networks or bridged connections for full network integration. Experiment with different setups to find what best suits your needs.

Performance Optimization

Lastly, to ensure optimal performance of your VMs, consider allocating resources (CPU and memory) based on the workload. Overprovisioning can lead to system instability and underperformance. Additionally, using Virtio drivers for disk and network interfaces can significantly improve the performance of Linux guests.

By leveraging these advanced features and tips, you can maximize the efficiency and productivity of your virtualization environment on Debian or Ubuntu systems, making it a powerful asset for development, testing, or any other use case.

Conclusion

By following these steps, you have successfully set up a virtualization environment on your Debian or Ubuntu system using QEMU, KVM, and Virt-Manager. Virtualization offers a flexible and efficient solution for running multiple operating systems and applications on a single hardware platform, enhancing your development, testing, and deployment workflows. With Virt-Manager, managing your virtual machines becomes a straightforward process, allowing you to focus on your projects rather than the complexities of virtualization technology.

Frequently Asked Questions (FAQs) on Virtualization with QEMU, KVM, and Virt-Manager

1. Can I run Windows VMs on Linux using QEMU and KVM?
Yes, you can run Windows as a virtual machine on Linux using QEMU and KVM, with Virt-Manager facilitating the setup and management process.
2. How do I enable hardware acceleration for VMs?
Hardware acceleration is automatically enabled when you use KVM as your hypervisor. Ensure your CPU supports virtualization extensions (VT-x for Intel and AMD-V for AMD processors) and that they are enabled in the BIOS.
3. Can I migrate a VM from one host to another with Virt-Manager?
Yes, Virt-Manager supports migrating VMs from one host to another, provided both hosts are configured with compatible virtualization technologies and network settings.
4. How do I backup a virtual machine?
You can backup a VM by creating a snapshot or exporting the VM to an image file using Virt-Manager or the virsh command-line tool.
5. Is it possible to automate VM creation with scripts?
Yes, you can automate VM creation using virsh commands in scripts or by using libvirt’s API for more complex automation tasks.
6. How can I improve the graphical performance of VMs?
To improve graphical performance, use Virtio drivers for the video and display settings and consider allocating more video memory to the VM.
7. Can I use Virt-Manager on a headless server?
Yes, you can use Virt-Manager on a headless server by connecting to it remotely via SSH with X11 forwarding or by using a web-based solution like Kimchi.
8. How do I resize a VM’s disk space?
You can resize a VM’s disk space by first enlarging the disk image file with qemu-img and then resizing the filesystem within the VM.
9. Can I restrict network access for a VM?
Yes, you can configure network filters with libvirt to restrict network access for a VM, or use network isolation techniques such as creating a VLAN.
10. How do I clone a VM?
You can clone a VM using Virt-Manager’s clone feature or by manually copying the disk image and creating a new VM with the same configuration.
11. What is the best way to share files between host and VM?
The best way to share files between host and VM is through shared folders using Virt-Manager or by setting up a network share.
12. Can I run a 64-bit VM on a 32-bit host?
No, running a 64-bit VM requires a 64-bit processor and a 64-bit host operating system.
13. How can I access a VM’s console remotely?
You can access a VM’s console remotely using Virt-Manager connected via SSH or by using remote desktop protocols like VNC or SPICE.
14. Is it possible to limit a VM’s CPU usage?
Yes, you can limit a VM’s CPU usage by setting CPU limits in the VM’s configuration settings in Virt-Manager or via the virsh command-line tool.
15. How do I install drivers for Windows VMs?
For Windows VMs, you can install drivers from the Virtio driver ISO provided by the Fedora Project to improve performance for disk and network operations.
16. Can I convert a physical machine to a virtual machine?
Yes, you can convert a physical machine to a VM using tools like virt-p2v, which creates an image of the physical machine that can be run as a VM.


Comments and Discussions
Linux Forum