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
Code: Select all
# apt install qemu-kvm virt-viewer
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
Installing the Guest
First create a directory to work in:
Code: Select all
mkdir ~/qemu
Code: Select all
# chattr +C ~/qemu
Code: Select all
cd ~/qemu
qemu-img create -f raw disk.img 30G
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
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
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 \
&
Code: Select all
remote-viewer "spice+unix:///tmp/vm_spice.socket"
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 \
Then create a shared folder on the guest:
Code: Select all
# mkdir /shared
Code: Select all
# chown -R $user:$user /shared
Code: Select all
qemu_mount /shared 9p rw,sync,dirsync,access=client,trans=virtio,x-systemd.automount 0 0
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 \
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"
Code: Select all
# chmod +x /usr/local/bin/virt-launch
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;