qvm-console-dispvm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/bash --
  2. set -eu
  3. print_usage() {
  4. cat >&2 << USAGE
  5. Usage: $0 [--autostart] [--] vmname
  6. Connects to VM console throught DispVM using the qubes.ShowInTerminal RPC service.
  7. With --autostart, start the VM first.
  8. USAGE
  9. }
  10. do_start=false
  11. if [[ $# -ge 2 ]] && [[ "$1" = '--autostart' ]]; then
  12. do_start=:
  13. shift
  14. fi
  15. if [[ $# -eq 2 ]] && [[ "$1" = '--' ]]; then
  16. shift
  17. elif [ $# -ne 1 ]; then
  18. print_usage
  19. exit 1
  20. fi
  21. QREXEC_REQUESTED_TARGET="$1"
  22. [[ "$QREXEC_REQUESTED_TARGET" =~ ^[A-Za-z][A-Za-z0-9_-]*$ ]] || { printf 'Invalid qube name %q\n' "$QREXEC_REQUESTED_TARGET">&2; exit 1; }
  23. if "$do_start"; then
  24. msg='cannot be started'
  25. qvm-start --skip-if-running -- "$QREXEC_REQUESTED_TARGET"
  26. else
  27. msg='is not running'
  28. qvm-check --quiet --running -- "$QREXEC_REQUESTED_TARGET"
  29. fi > /dev/null 2>&1 || { echo "Error: domain '$QREXEC_REQUESTED_TARGET' does not exist or $msg">&2; exit 1; }
  30. DISPVM="$(qvm-prefs -- "$QREXEC_REQUESTED_TARGET" management_dispvm)"
  31. [[ "x$DISPVM" == "x" ]] && { echo "Error: cannot determine management DispVM of domain '$QREXEC_REQUESTED_TARGET'">&2; exit 1; }
  32. sudo qvm-run -p --localcmd="QREXEC_REQUESTED_TARGET=$QREXEC_REQUESTED_TARGET QREXEC_REMOTE_DOMAIN=dom0 /etc/qubes-rpc/admin.vm.Console" --service --dispvm="$DISPVM" -- qubes.ShowInTerminal