Selected VM terminal opening via keyboard bind

Hi all, Qubes noob here. I use a script on a keyboard bind to open a terminal in whatever VM is currently “in focus”. I essentially followed the instructions from “zoldkorte” comment in this reddit. This worked quite well on Qubes 4.0, but now that I finally had the time to migrate to 4.1 it no longer works. Unfortunately, I am not yet knowledgeable enough in Qubes to know where the script got broken. Do any of the veterans have any idea on how to fix it?

#!/bin/bash

run_terminal='
for t in $TERMINAL gnome-terminal xfce4-terminal urxvt rxvt terminator Eterm aterm xterm roxterm; do
    which $t > /dev/null 2>&1 && exec $t;
done
'

get_id() {
#    local id=$(xprop -root _NET_ACTIVE_WINDOW)
#    echo ${id##* } # extract id
    xprop -root _NET_ACTIVE_WINDOW | cut -d " " -f5 | cut -d"," -f1
}

get_vm() {
    local id=$(get_id)
    local vm=$(xprop -id $id | grep '_QUBES_VMNAME(STRING)')
    local vm=${vm#*\"} # extract vmname
    echo ${vm%\"*} # extract vmname
}

main() {
    local vm=$(get_vm)
    if [[ -n "$vm" ]]; then
        qvm-run "$vm" "bash -c '$run_terminal'"
    else # run terminal in dom0
        exec bash -c "$run_terminal"
    fi
}

main
1 Like

@tzwcfq Thank you for the quick answer. I’m a bit strapped for time now, but as soon as I have some spare time I’ll give it a try.

@tzwcfq I’ve finally managed to sit down and try out the way described in the link you sent. By using it I managed to fix part of the problem, i.e. get the VM in focus. Now I am missing the opening of the terminal in that VM.

Opening the terminal in dom0 (in the case of no VM in focus) is working as expected, but for some reason neither qvm-run "$vm" "bash -c '$run_terminal'" nor qvm-run "$vm" "bash -c xterm" are working.
PS.: “run_terminal” is defined in the link I put on the original post.

Do you know what could have broken that command? If not, do you know where I can find the documentation on qvm-run (I could not find it on Qubes website)?

https://dev.qubes-os.org/projects/core-admin-client/en/latest/manpages/qvm-run.html

@tzwcfq, thank you for the help! The resources you provided me with helped solve the issue. I am leaving below the version of the script that worked for me in case anyone else is interested in using the script.

#!/bin/bash

run_terminal='
for t in $TERMINAL gnome-terminal xfce4-terminal urxvt rxvt terminator Eterm aterm xterm roxterm; do
    which $t > /dev/null 2>&1 && exec $t;
done
'

id=$(xprop -root _NET_ACTIVE_WINDOW | cut -d " " -f5 | cut -d"," -f1)
vm=$(xprop -id $id | grep '_QUBES_VMNAME(STRING)')
vm=${vm#*\"}
vm=${vm%\"*}

if [[ -n "$vm" ]]; then
    #qvm-run "$vm" qubes-run-terminal
	qvm-run "$vm" terminator
else # run terminal in dom0
    exec bash -c "$run_terminal"
fi