QEMU - resizing issues

First note

I am using i3 window manager in Ubuntu 24.04. This fix would do the same for XFCE, and I think it should also work in KDE and other window managers. Though, I have doubts that this will work with Wayland.

Fixing the issue

First we have to check which kernel card we are using, this is determent with ls /sys/class/drm/ – which would show something like:

ls /sys/class/drm/
card1  card1-Virtual-1  renderD128  version

With the knowledge that we need card1 in this specific case, we edit the file: sudo vi /etc/udev/rules.d/50-x-resize.rules. In this file, we provide the following line:

ACTION=="change",KERNEL=="card1", SUBSYSTEM=="drm", RUN+="/usr/local/bin/x-resize"

After creating this resize rule, let’s create the binary that it calls, which is done by editing: sudo vi /usr/local/bin/x-resize – entering the following bash code:

#!/bin/bash
RUN+="/usr/local/bin/x-resize" 
LOG_DIR=/var/log/autores;
if [ ! -d $LOG_DIR ]; then
    mkdir $LOG_DIR;
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

With the file saved, we have to give it the correct execution permissions with sudo chmod +x /usr/local/bin/x-resize and we can now try and resize our window.

Credits

https://dannyda.com/2020/10/22/how-to-fix-cant-resize-kali-linux-vm-screen-display-via-virt-viewer-running-on-proxmox-ve-pve-with-default-xfce4-desktop-environment/#4-Make-resize-fully-automatic