Skip to content

Instantly share code, notes, and snippets.

@3lpsy
Last active April 10, 2024 17:26
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save 3lpsy/4cc344ae031bf77595991c536cbd3275 to your computer and use it in GitHub Desktop.
Save 3lpsy/4cc344ae031bf77595991c536cbd3275 to your computer and use it in GitHub Desktop.
Manual Implementation of Auto Resizing For Non-Gnome Environments (like XFCE) running under Spice/Libvirt
#!/bin/bash
# SPDX-License-Identifier: MIT License
# Steps:
# 1) Make sure bash is available
# 2) Create udev rule
# - path to new udev rule: /etc/udev/rules.d/50-x-resize.rules
# - udev rule content:
# ACTION=="change",KERNEL=="card0", SUBSYSTEM=="drm", RUN+="/usr/local/bin/x-resize"
# 3) Create /var/log/autores directory
# 4) Create script /usr/local/bin/x-resize (this file) and make executable
# 5) Reload udev rules with `sudo udevadm control --reload-rules`
# 6) Make sure auto-resize is enabled in virt-viewer/spicy
# 7) Make sure qemu-guest-agent spice-vdagent xserver-xspice xserver-xorg-video-qxl are installed
# 8) Make sure spice-vdagentd is loaded and running fine
# Debugging:
# - Watch udev events on resize with `udevadm monitor`
# - Watch dmesg (may not be super useful) with `dmesg -w`
# - Watch autores logs with `tail -f /var/log/autores/autores.log`
# Credits:
# - Credit for Finding Sessions as Root: https://unix.stackexchange.com/questions/117083/how-to-get-the-list-of-all-active-x-sessions-and-owners-of-them
# - Credit for Resizing via udev: https://superuser.com/questions/1183834/no-auto-resize-with-spice-and-virt-manager
## Ensure Log Directory Exists
LOG_DIR=${AUTORES_LOG_DIR:-/var/log/autores};
if [ ! -d $LOG_DIR ]; then
mkdir $LOG_DIR; # this may fail due to perm issues, just create it before hand to be safe with correct perms
fi
LOG_FILE=${LOG_DIR}/autores.log
## Function to find User Sessions & Resize their display
function x_resize() {
declare -A disps usrs
usrs=()
disps=()
for i in $(users);do
[[ $i = root ]] && continue # skip root
usrs[$i]=1
done
for u in "${!usrs[@]}"; do
for i in $(sudo ps e -u "$u" | sed -rn 's/.* DISPLAY=(:[0-9]*).*/\1/p');do
disps[$i]=$u
done
done
for d in "${!disps[@]}";do
session_user="${disps[$d]}"
session_display="$d"
session_output=$(sudo -u "$session_user" PATH=/usr/bin DISPLAY="$session_display" xrandr | awk '/ connected/{print $1; exit; }')
echo "Session User: $session_user" | tee -a $LOG_FILE;
echo "Session Display: $session_display" | tee -a $LOG_FILE;
echo "Session Output: $session_output" | tee -a $LOG_FILE;
sudo -u "$session_user" PATH=/usr/bin DISPLAY="$session_display" xrandr --output "$session_output" --auto | tee -a $LOG_FILE;
done
}
echo "Resize Event: $(date)" | tee -a $LOG_FILE
x_resize
@3lpsy
Copy link
Author

3lpsy commented Jul 18, 2020

I've only tested it on mate so far but I imagine it'll also work for xfce.

@3lpsy
Copy link
Author

3lpsy commented Feb 22, 2021

It also works on xfce.

@giacchetta
Copy link

It works perfectly under LigthDM + MATE

@brlin-tw
Copy link

It works on XFCE @ Kali Linux

@manuuurino
Copy link

manuuurino commented Sep 1, 2023

It works thank you, though sudo didnt work for me, so i used su instead. https://gist.github.com/manuuurino/ad96dfbdc5654c7095d28252901763b2

@Apteryks
Copy link

@3lpsy Hi! For packaging in free software distributions, would you be so kind as to add a free software license SPDX header line to this file? Something like: # SPDX-License-Identifier: GPL-3.0-or-later would do. Thank you!

@Apteryks
Copy link

For those who'd like to distribute such a solution, I've come up with an alternative (Guile written) script licensed under the GPLv3 or later: https://gitlab.com/Apteryks/x-resize

It's successfully being used in the Guix demo VM to have dynamic resizing with SPICE using the Xfce desktop.

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