Open terminal as root for any vm

A GUI script using zenity to open your favorite terminal as root it’s for minimal template users but should work for “full” template users i don’t like to type in dom0 “qvm-run -u root” then the name of the vm it’s annoying you can misstype the name and typing again is annoying.

!! You have to put the script in dom0 !!

  1. The script will ask you which vm you want to open as root
  2. You don’t even have to type the full name of the vm let’s say ur vm as for name “xxxxxxxxxxxxxxxxx” the script will automatically find the vm and log in as root for you.
  3. In case there is multiples match the script will ask you which one you want to pick. Example you have multiple disp open you just type “disp” then he will show you every match he find for “disp”
  4. You can click on the vm you want or click on “ok”
  5. I included popular terminal in the script but if your terminal is missing you can add it yourself (or i could edit the post and add more) i don’t know which one is better

#!/bin/bash

# Function to find matching VM
find_vm() {
    local pattern="$1"
    local matching_vms

    # Find VMs matching the pattern
    matching_vms=$(qvm-ls --no-spinner | grep -E "^${pattern}" | awk '{print $1}')
    
    # Count number of matching VMs
    local match_count=$(echo "$matching_vms" | wc -l)

    # Handle matching logic
    if [ -z "$matching_vms" ]; then
        zenity --error --text="No VM found matching '$pattern'" --width=300
        exit 1
    elif [ "$match_count" -gt 1 ]; then
        # If multiple matches, let user choose
        local chosen_vm=$(echo "$matching_vms" | zenity --list \
            --title="Multiple VMs Match" \
            --text="Select a VM:" \
            --column="VM Name" \
            --width=300 \
            --height=300)
        
        # If user cancels selection
        if [ -z "$chosen_vm" ]; then
            exit 1
        fi
        
        echo "$chosen_vm"
    else
        # If exactly one match
        echo "$matching_vms"
    fi
}

# Function to detect default terminal in the VM
get_vm_default_terminal() {
    local vm_name="$1"
    
    # List of terminal commands to try in order
    local terminal_commands=(
        "alacritty"
        "konsole"
        "gnome-terminal"
        "xfce4-terminal"
        "tilix" 
        "mate-terminal"
        "xterm"
    )

    # Try each terminal command
    for terminal in "${terminal_commands[@]}"; do
        # Use qvm-run to check if the terminal exists
        if qvm-run -u root "$vm_name" "command -v $terminal" > /dev/null 2>&1; then
            echo "$terminal"
            return 0
        fi
    done

    # If no terminal found
    zenity --error --text="No terminal found in VM $vm_name" --width=300
    exit 1
}

# Prompt user to enter VM name pattern
vm_pattern=$(zenity --entry \
    --title="Open Root Terminal" \
    --text="Which VM should be opened as root? (Use * for partial match)" \
    --width=300)

# Check if user canceled
if [ -z "$vm_pattern" ]; then
    zenity --error --text="No VM name entered. Exiting." --width=300
    exit 1
fi

# Replace * with .* for regex matching
vm_pattern=$(echo "$vm_pattern" | sed 's/\*/.*/')

# Find matching VM
vm_name=$(find_vm "$vm_pattern")

# Get default terminal in the VM
vm_terminal=$(get_vm_default_terminal "$vm_name")

# Run the detected terminal as root in the specified VM
qvm-run -u root "$vm_name" "$vm_terminal"

I have a similar script on a shortcut so I can open a root terminal in a
running qube.
Qubes already provides qubes-run-terminal to run an installed
terminal. That would simplify things somewhat.

I never presume to speak for the Qubes team.
When I comment in the Forum I speak for myself.

2 Likes

Can you share your script please ? I’d likee to test it to see if it’s better

I think I already have: it uses this template

#!/usr/bin/bash
ID=$(xdotool getwindowfocus)
QUBE=$(xprop _QUBES_VMNAME -id $ID|cut -f2 -d\" )
if [[ "$QUBE" == "_QUBES_VMNAME:  not found." ]]; then
  exit
else
  DO SOMETHING IN THE QUBE
fi
<small>
I never presume to speak for the Qubes team.
When I comment in the Forum I speak for myself.
</small>
1 Like

Thanks you i will test it