https://www.linux-kvm.org/page/Main_Page
Debian offers two graphical front-end management programs for use with QEMU/KVM — gnome-boxes and virt-manager — but these both pull in lots of extra dependencies and run autostarted system daemons that may not be needed at all if the qemu-system command is run directly from the command line, or from shell functions and scripts.
For this guide I will show how to run a KVM-enabled virtual machine with a shared clipboard, a shared folder, automatic resizing of the guest desktop to fit the host's window and automatic pass-through for USB ports, all directly from the command line. It is a curated and condensed version of this ArchWiki page, which can be referred to for more detail: https://wiki.archlinux.org/index.php/QEMU
Prerequisites
Host:
The hardware virtualisation options for the processor need to be enabled from the firmware ("BIOS") menus, for Intel they should be called "VT-X" extensions and for AMD look for "AMD-V". Check if they are enabled in the host by running:
- Code: Select all
LC_ALL=C lscpu | grep Virtualization
We also need the QEMU/KVM packages and a SPICE viewer to see the desktop:
- Code: Select all
# apt install qemu-kvm virt-viewer
Guest:
Once the guest is installed (see the next section for details) we need the SPICE package for clipboard sharing:
- Code: Select all
# apt install spice-vdagent
The xserver-xorg-video-qxl package is also needed but that's pulled in by the xorg metapackage and so should already be installed in most systems.
Installing the Guest
First create a directory to work in:
- Code: Select all
mkdir ~/qemu
If this is on a btrfs filesystem then be sure to disable copy-on-write for that directory to prevent image corruption:
- Code: Select all
# chattr +C ~/qemu
Now create a disk image, this example uses 30GiB with the raw format (use qcow2 for a smaller image size at the cost of some I/O):
- Code: Select all
cd ~/qemu
qemu-img create -f raw disk.img 30G
And finally run the virtual machine with the disk & ISO image attached (2GiB of RAM are assigned in this example):
- Code: Select all
kvm -m 2G -drive file=~/qemu/disk.img,format=raw,cache=none,if=virtio -cdrom /path/to/example.iso -boot order=d
That should pop up a QEMU screen running the installer, follow the usual installation steps and make sure that /dev/vda is selected as the bootloader target (it will offer /dev/fd0 as the default choice, which is the floppy drive).
Running the Guest
The guest can be run with a fairly simple qemu-system invocation (as when the system was being installed but without the ISO image):
- Code: Select all
kvm -m 2G -drive file=~/qemu/disk.img,format=raw,cache=none,if=virtio
But for a shared clipboard and automatic desktop resizing a SPICE viewer is needed, for Debian this is provided by the virt-viewer package and the remote-viewer(1) command.
These options will launch QEMU for a SPICE viewer with a shared clipboard:
- Code: Select all
kvm -m 2G \
-cpu host \
-soundhw hda \
-drive file=~/qemu/disk.img,format=raw,cache=none,if=virtio \
-vga qxl \
-device virtio-serial-pci \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent \
-spice unix,addr=/tmp/vm_spice.socket,disable-ticketing \
&
And this will connect the viewer:
- Code: Select all
remote-viewer "spice+unix:///tmp/vm_spice.socket"
Shared folder
There are several options for folder sharing under QEMU but here we are going to use Plan 9 folder sharing over Virtio because it doesn't need any servers running and it's the most l33t option by far.
Details here: https://wiki.qemu.org/Documentation/9psetup
Debian's kernel has all the required configuration so we just need to add these options (just before the ampersand line in the qemu-system command):
- Code: Select all
-fsdev local,id=qemu_dev,path=/home/$user/Public,security_model=none \
-device virtio-9p-pci,fsdev=qemu_dev,mount_tag=qemu_mount \
^ That shares ~/Public, replace $user with the actual username.
Then create a shared folder on the guest:
- Code: Select all
# mkdir /shared
Correct the ownership so your normal user can access it (replace "$user" with the actual username):
- Code: Select all
# chown -R $user:$user /shared
And add this line to /etc/fstab in the guest to mount it automatically on demand:
- Code: Select all
qemu_mount /shared 9p rw,sync,dirsync,access=client,trans=virtio,x-systemd.automount 0 0
USB device passthrough
Details here: https://www.spice-space.org/usbredir.html
So for USB 3.0 support (two ports) we add these options:
- Code: Select all
-device nec-usb-xhci,id=usb \
-chardev spicevmc,name=usbredir,id=usbredirchardev1 \
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1 \
-chardev spicevmc,name=usbredir,id=usbredirchardev2 \
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2 \
Launching the Guest
So now we can put all these options together in a launch script, for this we will use a file at /usr/bin/virt-launch with this content (again, replace $user with the actual username):
- Code: Select all
#!/bin/sh
qemu-system-x86_64 -enable-kvm \
-m 2G \
-cpu host \
-smp cores=4 \
-drive file=/home/$user/qemu/disk.img,format=raw,cache=none,if=virtio \
-soundhw hda \
-vga qxl \
-device virtio-serial-pci \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent \
-spice unix,addr=/tmp/vm_spice.socket,disable-ticketing \
-device nec-usb-xhci,id=usb \
-chardev spicevmc,name=usbredir,id=usbredirchardev1 \
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1 \
-chardev spicevmc,name=usbredir,id=usbredirchardev2 \
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2 \
-fsdev local,id=qemu_dev,path=/home/$user/Public,security_model=none \
-device virtio-9p-pci,fsdev=qemu_dev,mount_tag=qemu_mount \
&
remote-viewer "spice+unix:///tmp/vm_spice.socket"
Now make the file exectable:
- Code: Select all
# chmod +x /usr/local/bin/virt-launch
Once that's done the VM & viewer can be started with the virt-launch command.
For an XDG-compliant menu entry use a file at /usr/local/share/applications/virt-launch.desktop with this content:
- Code: Select all
[Desktop Entry]
Type=Application
Encoding=UTF-8
Name=Virtual Machine
Comment=Start QEMU/KVM with ~/qemu/disk.img
Exec=/usr/local/bin/virt-launcher
Categories=System;
Happy virtualisation!
