Auto resizing Xfce and MATE resolution in Spice (VM guide)

Auto resizing Xfce and MATE resolution in Spice (VM guide)

Unlike Plasma, Cinnamon, Gnome, LXDE and LXQt DEs… our beloved Xfce and MATE don’t auto-resize with the qemu-guest-agent library.

Not for long…

Open the VM window and make sure View > Scale Display > Auto resize VM with window is checked.

image

Start the VM and install qemu-guest-agent

sudo dnf install qemu-guest-agent # Fedora/CentOS/RHEL
sudo apt install qemu-guest-agent # Debian/Ubuntu

Manual resize:

Run the following command in terminal to resize once:

# Terminal:
xrandr --output "$(xrandr | awk '/ connected/{print $1; exit; }')" --auto

Command spread out:

# Get the name of the first connected display from xrandr using awk
DISPLAY_NAME=$(xrandr | awk '/ connected/{print $1; exit; }')

# Tell xrandr to refresh the display so it uses the new resolution applied by qemu-guest-agent
xrandr --output "$DISPLAY_NAME" --auto

To map this to a key you’ll want to use the following command and you’ll sometimes need to hit it 2 or 3 times in a row due to update lag.

# Shortcut command:
/bin/sh -c "xrandr --output $(xrandr | awk '/ connected/{print $1; exit; }') --auto"

Automatic resize:

There might be a way to watch a particular file so it only triggers on qemu-guest-agent resize but a loop gets the job done in the meantime.

sudo touch /usr/bin/auto_resize
sudo chmod +x /usr/bin/auto_resize
sudo nano /usr/bin/auto_resize
# Paste the following:

#!/bin/bash
while true
do
	DISPLAY_RES=$(xrandr | grep +)
	if [ "$DISPLAY_RES" != "$DISPLAY_RES_LAST" ]; then
		DISPLAY_NAME=$(xrandr | awk '/ connected/{print $1; exit; }')
		for i in {1..4}; do
			xrandr --output "$DISPLAY_NAME" --auto
			sleep 0.3
		done
	fi 
	DISPLAY_RES_LAST=$DISPLAY_RES
	sleep 1
done

# Test the script is working:
auto_resize
# Hit Ctrl+C to quit

# Paste the following to your preferred startup location:
nohup auto_resize > /dev/null 2>&1 &

# Xfce GUI example:
# App Menu > Session & Startup > "Application Autostart" tab
# Click "Add"
# "Name" field, paste:
auto_resize
# "Command" field, paste:
nohup auto_resize > /dev/null 2>&1 &
# Click "OK"

# Log out and back in

1 Like