Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active July 31, 2022 08:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan/9e1bf24c1b6ae4f1fe9587b55d3f9544 to your computer and use it in GitHub Desktop.
Save ivan/9e1bf24c1b6ae4f1fe9587b55d3f9544 to your computer and use it in GitHub Desktop.
fix-qemu-guest-resolution
#!/bin/bash
# SPICE clipboard sharing is not safe to use:
# https://bugzilla.redhat.com/show_bug.cgi?id=1320263
# but guests that have <clipboard copypaste='no'/> can't fix their screen
# resolution with `xrandr --output Virtual-0 --auto`, so use this program
# to fix the screen resolution for a qemu guest over ssh. This should be
# run on the host machine, not the guest.
set -eu -o pipefail
set-qemu-guest-resolution() {
width=$1
height=$2
# 2> /dev/null to ignore cryptic complaint about mode already existing
xrandr --newmode ${width}x${height} $(gtf ${width} ${height} 60 | grep -o 'Modeline .*' | cut -f 3- -d ' ') 2> /dev/null
xrandr --addmode Virtual-0 ${width}x${height}
xrandr --output Virtual-0 --mode ${width}x${height}
}
get-spice-window-resolution() {
host=$1
window=$(wmctrl -l | grep -- "$host on QEMU/KVM$" | cut -f 1 -d ' ')
width=$(xwininfo -id "$window" | grep -P -o 'Width: \d+$' | cut -f 2 -d ' ')
height=$(xwininfo -id "$window" | grep -P -o 'Height: \d+$' | cut -f 2 -d ' ')
echo "$width $height"
}
# Adjust these numbers for your machine
spice_ui_height=91
spice_ui_width=2
host=$1
res=($(get-spice-window-resolution "$host"))
guest_width=$((${res[0]} - $spice_ui_width))
guest_height=$((${res[1]} - $spice_ui_height))
typeset -f set-qemu-guest-resolution | ssh -- "$host" "$(cat); DISPLAY=:0.0 set-qemu-guest-resolution $guest_width $guest_height"
@ivan
Copy link
Author

ivan commented Dec 23, 2017

Usage:

sudo apt-get install --no-install-recommends wmctrl x11-utils
fix-qemu-guest-resolution HOSTNAME

HOSTNAME should be both the name of the virtual machine and the hostname by which ssh can reach it.

You may need to adjust the DISPLAY= and spice_ui_ variables.

If you have a 4K display, you will also need to increase the amount of VRAM allocated to the machine.

@ivan
Copy link
Author

ivan commented Dec 27, 2017

If you get tired of running fix-qemu-guest-resolution and mostly work with maximized SPICE windows anyway, copy set-display-resolution to the guest and create a ~/.config/autostart/ task with e.g.:

[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=fix-resolution
Comment=
Exec=set-display-resolution Virtual-0 3840 1966 60
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment