-
-
Save 3lpsy/4cc344ae031bf77595991c536cbd3275 to your computer and use it in GitHub Desktop.
#!/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 |
It also works on xfce.
It works perfectly under LigthDM + MATE
It works on XFCE @ Kali Linux
It works thank you, though sudo didnt work for me, so i used su instead. https://gist.github.com/manuuurino/ad96dfbdc5654c7095d28252901763b2
@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!
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.
Super simple and useful, works like a charm in Kali 2024.3 and xfce. Many thanks!
I'm pretty sure this line:
https://gist.github.com/3lpsy/4cc344ae031bf77595991c536cbd3275#file-x-resize-L2
# SPDX-License-Identifier: MIT License
should be:
# SPDX-License-Identifier: MIT
If someone wants this to work on Wayland too, I was able to achieve this for Sway, you can check repository. It's quite fresh but I think I will stick to it.
Also, script was not working for my i3
on X11 because of XAUTHORITY
files - I was getting error:
Authorization required, but no authorization protocol specified
Can't open display :0
Rewrote some logic with xauth
and was able to make this work.
If someone wants this to work on Wayland too, I was able to achieve this for Sway, you can check repository. It's quite fresh but I think I will stick to it.
Also, script was not working for my
i3
on X11 because ofXAUTHORITY
files - I was getting error:Authorization required, but no authorization protocol specified Can't open display :0
Rewrote some logic with
xauth
and was able to make this work.
Attempting to manage various sessions from root is a bit hacky, in my opinion. The solution which was recommended to me by udev/systemd people was to use a per-session script, which is what I've done in https://gitlab.com/apteryks/x-resize. It works well for Xorg. I haven't yet looked into what would need to be added to support Wayland, so thanks for sharing your solution.
Thank you for creating this script. Now my Kali on arm64 VM window resizes properly again!
It's nice to see that this has helped people. This of course isn't the optimal way to do this and there's a bunch of things I could improve but it works and I'm typically running it on VMs I don't really care too much about. I've come back to this once or twice recently so here's some copy paste for easy installation. Not really tested or guaranteed to work of course but it's what I used so figured I'd drop it here.
echo 'ACTION=="change",KERNEL=="card0", SUBSYSTEM=="drm", RUN+="/usr/local/bin/x-resize" ' | sudo tee /etc/udev/rules.d/50-x-resize.rules
sudo mkdir /var/log/autores
sudo curl "https://gist.githubusercontent.com/3lpsy/4cc344ae031bf77595991c536cbd3275/raw/4965f04913626eb94485a5e6a67938b16c16b6e0/x-resize" -o /usr/local/bin/x-resize
sudo chmod +x /usr/local/bin/x-resize
sudo udevadm control --reload-rules
sudo systemctl enable --now spice-vdagentd
sudo apt install -y qemu-guest-agent spice-vdagent xserver-xspice xserver-xorg-video-qxl x11-xserver-utils || sudo dnf install qemu-guest-agent spice-vdagent spice-server xorg-x11-drv-qxl xrandr -y
And if people are running into Authority issues, I added this to my .xinitrc. This is specific to fedora so adjust your variables accordingly:
export DISPLAY=:0.0
export XAUTHORITY=${XAUTHORITY:-$HOME/.Xauthority}
# Optionally, set the current desktop environment variable
#export XDG_CURRENT_DESKTOP=XFCE
# Import the environment into your systemd user session
systemctl --user import-environment DISPLAY XAUTHORITY XDG_CURRENT_DESKTOP
# Now start your XFCE session
exec xfce4-session
Took a bit of sleuthing but found out why it wasn't working for me, but I just had to change line 36. For some reason there is no users
variable in declare -A
. For Kali linux.
< for i in $(users);do
> for i in $(awk -F: '{print $1}' /etc/passwd);do
@flanter21, I've fixed it like this. The w
command only lists the logged in users:|
# The `systemd` package in Kali Linux and Debian Trixie does not include
# /lib/systemd/system/systemd-update-utmp.service. As a result, the `users`
# and `who` commands lack the required output.
for i in $(w -h | awk '{print $1}');do
[[ $i = root ]] && continue # skip root
usrs[$i]=1
done
I've only tested it on mate so far but I imagine it'll also work for xfce.