Skip to content

Instantly share code, notes, and snippets.

@manuuurino
Forked from 3lpsy/x-resize
Created September 1, 2023 14:51
Show Gist options
  • Select an option

  • Save manuuurino/ad96dfbdc5654c7095d28252901763b2 to your computer and use it in GitHub Desktop.

Select an option

Save manuuurino/ad96dfbdc5654c7095d28252901763b2 to your computer and use it in GitHub Desktop.

Revisions

  1. manuuurino revised this gist Sep 1, 2023. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions x-resize
    Original file line number Diff line number Diff line change
    @@ -38,21 +38,21 @@ function x_resize() {
    done

    for u in "${!usrs[@]}"; do
    for i in $(sudo ps e -u "$u" | sed -rn 's/.* DISPLAY=(:[0-9]*).*/\1/p');do
    for i in $(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; }')
    session_output=$(su - "$session_user" -c "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;
    su - "$session_user" -c "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
    x_resize
  2. @3lpsy 3lpsy revised this gist Feb 22, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions x-resize
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,8 @@
    # 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`
  3. @3lpsy 3lpsy revised this gist Feb 22, 2021. 1 changed file with 16 additions and 9 deletions.
    25 changes: 16 additions & 9 deletions x-resize
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,30 @@
    #!/bin/bash
    # Bash required
    # Should be run as root and saved to /usr/local/bin/x-resize
    # Requies udev rule: /etc/udev/rules.d/50-x-resize.rules
    # udev rule content:
    # 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"
    # Make sure auto-resize is enabled in virt-viewer/spicy
    # 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
    # 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
    # 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=/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=()
  4. @3lpsy 3lpsy revised this gist Feb 22, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion x-resize
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,8 @@
    # Bash required
    # Should be run as root and saved to /usr/local/bin/x-resize
    # Requies 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"
    # udev rule content:
    # ACTION=="change",KERNEL=="card0", SUBSYSTEM=="drm", RUN+="/usr/local/bin/x-resize"
    # Make sure auto-resize is enabled in virt-viewer/spicy
    # 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
  5. @3lpsy 3lpsy revised this gist Jul 18, 2020. No changes.
  6. @3lpsy 3lpsy created this gist Jul 18, 2020.
    48 changes: 48 additions & 0 deletions x-resize
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/bin/bash
    # Bash required
    # Should be run as root and saved to /usr/local/bin/x-resize
    # Requies 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"
    # Make sure auto-resize is enabled in virt-viewer/spicy
    # 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=/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