Quantcast
Channel: Debian User Forums
Viewing all articles
Browse latest Browse all 3395

[HowTo] Getting Started with Virt-Manager & QEMU-KVM

$
0
0
There are several options for running virtual machines in Linux. Early on, I went with VirtualBox because I was still mostly a Windows user and VBox works in both OSs. Later, when I looked into QEMU-KVM, I was discouraged by how difficult it seemed, as all the tutorials were either superficial or brilliant-but-complicated. Recently, no longer much of a Windows user, I decided to give VirtMan another shot. Was pleasantly surprised to discover QEMU isn't difficult after all. Indeed, all things considered, it's easier than VBox. What's difficult is finding a tutorial in the Goldilocks spot between superficial and inscrutable. This is my attempt at such a tutorial.

Note: Realistically, a tutorial like this only makes sense if you're looking at the dialogue boxes while reading it. So, open VirtMan in one window and the tutorial in another (or on your phone). I think you'll find everything comes together pretty easily. The folks who designed VirtMan did a good job with the GUI. If you need help, feel free to open a thread on the Forum. Please post in the support section, though, not here.

Overview. In a nutshell, we're going to:
  • Install VirtMan.
  • Configure VirtMan.
  • Create a VM.
  • Configure the VM.
Terminology. VM = virtual machine. KVM = a function of the Linux kernel which give VMs direct access to RAM and the CPU (a type-one hypervisor). QEMU = an app which emulates other hardware functions, e.g., a file which serves as VHD (virtual hard drive); QEMU is a type-two hypervisor. VirtMan = Virt-Manager = a GUI front-end for QEMU. A command line (CLI) front-end (virsh) is brought in by the same packages as install VirtMan, but I'm not going to discuss that here. libvirt = the main QEMU project; also, the label used for most folders holding QEMU files, e.g., /etc/libvirt. VM-Page = each VM's sub-page when opened from VirtMan's main page.

Installation. Sources differ somewhat on which packages they recommend for installing VirtMan. I've been happy with this command string (recommended by CwF):

Code:

sudo apt install virt-manager qemu-system-x86 qemu-utils libvirt-daemon-system
This brings in nearly a hundred dependencies, is a 60 MB download, and installs to a little more than 200 MB of space on the system partition. FYI, virsh comes in with libvirt-client, a dependency of virt-manager.

Configuration. To open VirtMan, select from Menu (add to panel, if you like). Click on QEMU/KVM (a connection, in VirtMan lingo). Now you can access settings through the menu at top, e.g., File > New Virtual Machine.

By default, VirtMan stores VHDs in /var/lib/libvirt/images. VHDs are very large. As in, a 30 GB VHD = a file that size. This isn't usually a problem if you're using a single large system partition, but if you have split off home and/or data to a separate partition, your system partition probably doesn't have room for VHD files. The solution is to open Edit > Connection Details > Storage and add a new location ("pool") for VHDs. If feasible, a separate partition makes backup of regular data files simpler, but a mere folder on an already-present home or data partition also works.

Authorization. By default, VirtMan runs as user-root. Generally, though, you want to run it as a regular user. There are a couple ways to do that. The simplest (imho) is to add yourself to the libvirt group, sudo adduser $USER libvirt. Other users, even those without admin privileges, can be given VirtMan access by adding them to the same group. Be aware, although members of the libvirt group may use VirtMan without password authorization, the VM files (including VHDs) will be owned by user-root.

Autostart Network. VirtMan uses a virtual network adapter to give VMs internet access (piggy-backing on the host's access). By default, the virtual adapter is disabled (kinda silly). Enable with Edit > Connection Details > Virtual Networks > tick box to Autostart On Boot; click Apply. Won't take effect, though, until reboot host. Enable for this boot with sudo virsh net-start default.

For virtualization to work, it must be enabled in the firmware (UEFI/BIOS). Most computers ship with virtualization enabled by default, but a few do not. If unsure, open firmware settings and find the relevant entry. On some computers, an admin password must be set before settings can be modified (see computer manual for how-to).

Creating a VM. For your first VM, create a test box you won't mind deleting if it doesn't work out. From the main page, File > New Virtual Machine. Opens a wizard with five steps:

1. How to install. Usually local install media (for a new VM), but also can re-use an existing VHD (fresh start for VM settings).

2. Choose ISO (for new VM). Click Browse, which will take you to the Storage page mentioned above. First time, add a pool for location where generally save ISO files. The same pool can be used for future installations. Select ISO from list; click Choose Volume. Will return to Step 2 box. Choose OS to install, meaning type of OS. AFAICT, automatically detect (the default) never works. Instead, untick and use search box. For Bookworm, I use debiantesting, which apparently Deb12 was when VirtMan was last updated. Click Forward button.

3. Choose memory and CPU settings. As a rule of thumb, assign half the available resources to the VM, reserving the other half for host. If that's not enough for either side, you shouldn't be using virtualization. Consider dual boot or full install to USB drive instead. Conversely, a super-duper gaming rig might be able to split RAM and CPU three or four ways.

4. Storage (VHD). Tick Select or create custom storage, then click Manage. Will take you (again) to the Storage page. Now is when you will select the pool created during the VirtMan configuration stage. Click + button to create a new VHD file; enter name, replacing debiantesting (for example) with a useful identifier, e.g., Deb12-Gnome. Leave format as qcow2 (unless you have very good reason for choosing raw). Don't bother with a backing store. Select capacity; my preference for most current Linux systems is 30 GB; can get away with 20 GB (the default) if don't plan to install much. I leave Allocate Now unticked (same as quick format, afaict, not dynamic disk sizing as in VBox). Click Choose Volume; will return to Step 4 box; click Forward.

5. Ready to begin. Enter VM name, usually the same one as used for the VHD, again replacing OS type (which is the default). Notice box to Customize configuration. This where you can select boot mode (UEFI instead of BIOS, the default). You also can set up a shared folder (see below), if planning to use one. Click Begin Installation (upper-left).

IMHO, there's not much reason for ordinary folks to use UEFI mode. It's mainly intended for developers, so they can confirm apps, drivers, etc. are secure boot compatible (or test registering them with MOK). FYI, there's a UEFI mode without secure boot (last option in a dropdown on the Configuration box), but it's incompatible with internal snapshot (see below).

Installer will go through the same steps as a regular installation. VHD will be assigned a device ID of vda (as distinguished from sda). Can be partitioned the same a regular drive (GPT or MBR), including encryption. It's even possible to have more than one VHD, or share a VHD between two VMs. For now, just run the installer. Sometimes the installer session hangs at shutdown (rather than rebooting); in that case, use Force Off (menu or icon).

Configure VM. Most configurations come in with the operating system, same as installing to bare metal, but a few things are different for VMs.

First, a quick spin around the block. Open VM-Page; select run, either from menu (first row) or by clicking icon (second row). Usually VirtMan will switch automatically to Console view (showing the VM's desktop), but sometimes have to switch manually. Indeed, can switch manually at any time among Console view, Details (hardware and system info) and Snapshots while the VM is running. Also can switch to VirtMan itself or any other app running on the host. Will switch automatically if mouse-click somewhere outside Console (e.g., an icon on host's panel). Or tap Left-Ctrl+Left-Alt simultaneously, then Alt-Tab to cycle through open apps on host.

Display mode. By default, the VM desktop will display about 2/3's normal size, readable but smaller than the available space. There is a full screen mode, but it's relatively low resolution. (If enter accidentally, escape is with a pop-up at upper-center of screen, only visible when moused over.) The better solution is to install two packages in the VM: sudo apt install qemu-guest-agent spice-vdagent. Debian's standard installer will have installed the first one automatically, so only need to add the second. Reboot. From menu, select View > Scale Display > Auto resize. [This works with Gnome, Cinnamon and Mate, but not KDE and XFCE; there are workarounds for the latter, but they're a bit complicated.] By the way, the agent packages also enable copy to/from the host (using clipboard).

Shared Folders. A neat trick for accessing files on host. From the VM-Page, open Hardware Settings. Click Memory; tick box to enable sharing; click Apply. Click Add Hardware (lower-left); select Filesystem (two-thirds of the way down list); select source path (from Storage); give a label, e.g. data-files (misleadingly, called target path, but it's really just a label); click Finish. Boot VM. Create mount point. Can mount the share manually or add to fstab. For example, I have a data partition which mounts to /data/files on host. Storage label for the share in VirtMan is data-files (hella creative, eh?). Mount point for the share is a folder in Home called Data. Mount with:

Code:

sudo mount -t virtiofs data-files $HOME/Data  #  to test, before adding to fstabecho "data-files   /home/pbear/Data   virtiofs   noatime,nofail   0   0" | sudo tee -a /etc/fstab
File permissions pass through with the share. This works for me because I'm the primary user (uid=1000) in both host and guest. To state the obvious, do not access the same files at the same time in both host and guest. To mount read-only, add ro, (no space) before noatime. Also, be advised the virtiofs method only works if both host and guest are using at least the 5.4 kernel. If that's not true in your case, look into 9p (which is the older solution).

Other tips. Not trying to cover everything, but a few pointers.

USB devices. Can be attached to a VM with Virtual Machine > Redirect USB device. Works with drives, printers, and other peripherals. Works in a live session as well as an installed system. Device isn't available to host while attached to a VM. Storage media can be shared simultaneously, though, by adding host's /media folder (or other mount location) as a shared folder (I don't bother).

Snapshots. A handy feature of VMs is the ability to try something - installing an app, say - then unwind the trial with the click of a button. If you're used to snapshot in VBox, be aware VirtMan does it a little differently. Rather than a separate file with changes, VirtMan snapshots are internal, using a copy-on-write mechanism similar to btrfs. (QEMU can do external snapshots, but they're not supported by VirtMan.) To revert, right-click snapshot and select Start; or tap Run button at lower-left. To keep changes, simply delete the snapshot.

Windows. Everything above assumes both host and guest are running Linux. VirtMan can accommodate a Windows guest, but the steps are a bit different. Also, unless using GPU passthrough, performance reportedly isn't as good as VBox, so I'm continuing to use that for my Win10 VM. If you want to pursue Windows-in-VirtMan, there are many articles on the internet.

References: KVM, QEMU, libvirt, virsh, Debian, Arch, Gentoo, Manjaro, GPU passthrough.

Statistics: Posted by pbear — 2024-04-22 05:27 — Replies 7 — Views 138



Viewing all articles
Browse latest Browse all 3395

Trending Articles