Skip to content

Instantly share code, notes, and snippets.

@promovicz
Created October 25, 2020 10:21
Show Gist options
  • Save promovicz/333bf68442d84ced432a1d3addc36e3a to your computer and use it in GitHub Desktop.
Save promovicz/333bf68442d84ced432a1d3addc36e3a to your computer and use it in GitHub Desktop.
Qubes domain restart script
#!/bin/sh
# qvm-restart <domain>
#
# Restart the given Qubes DOMAIN, reattaching
# all its network dependents as needed.
#
set -e
# get some color from terminfo
normal="$(tput sgr0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
# say [-ne] <message>...
say() {
echo "$@""${normal}"
}
# try <message> <command>...
try() {
say -n "$1"
shift 1
if $@; then
say "${green}done"
return 0
else
rc=$!
say "${red}failed"
return ${rc}
fi
}
# main <domain>
main() {
local domain="$1"
# check that we got an argument
if [ -z "${domain}" ]; then
say "Usage: $0 <domain>"
exit 1
fi
# check for extraneous arguments
shift 1
if [ -n "$*" ]; then
say "Usage: $0 <domain>"
exit 1
fi
# find out address of domain, and if it exists at all
local address="$(qvm-ls --raw-data -O NAME,IP "${domain}" | cut -d \| -f 2)"
if [ -z "${address}" ]; then
say "${red}Domain ${domain} does not exist."
exit 1
fi
# check that the domain is running
if ! (qvm-ls --raw-data -O STATE "${domain}" | grep -q Running); then
say "Domain ${domain} is not running".
exit 1
fi
# find all direct running dependents
local dependents=""
if [ "${address}" != "-" ]; then
say -n "Checking for dependents... "
dependents="$(qvm-ls --raw-data --running -O NAME,IP,GATEWAY | grep "|${address}$" | cut -d \| -f 1 | tr '\n' ' ')"
if [ -n "${dependents}" ]; then
say "${yellow}${dependents}"
else
say "${green}none"
fi
fi
# detach dependents
for d in ${dependents}; do
try "Detaching ${d}... " qvm-prefs ${d} netvm none
done
# restart the domain
try "Stopping ${domain}... " qvm-shutdown --wait "${domain}"
try "Starting ${domain}... " qvm-start "${domain}"
# reattach dependents
for d in ${dependents}; do
try "Attaching ${d}... " qvm-prefs ${d} netvm ${domain}
done
}
# call main
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment