b4921b8686
les machines virtuelles
62 lines
1 KiB
Bash
Executable file
62 lines
1 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
exec_as() {
|
|
local user="$1"
|
|
shift
|
|
|
|
if [ "$(whoami)" != "$user" ]; then
|
|
exec sudo -u "$user" "$0" "$@"
|
|
else
|
|
cd
|
|
fi
|
|
}
|
|
|
|
exec_as qemu "$@" || exit
|
|
PATH="./bin:${PATH}"
|
|
|
|
perror() {
|
|
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
|
|
}
|
|
|
|
public_help() {
|
|
local name
|
|
name="$(basename "$0")"
|
|
cat << EOF
|
|
${name}: usage:
|
|
${name} start <VM name> - start a VM
|
|
${name} watch <VM name> - attach VM screen session
|
|
${name} active - (default behaviour) list active VMs
|
|
${name} list - list available VMs
|
|
${name} help - show this help
|
|
EOF
|
|
}
|
|
|
|
error_usage() {
|
|
perror "Invalid usage"
|
|
>&2 public_help
|
|
return 1
|
|
}
|
|
|
|
public_start() {
|
|
local vm_name="$1"
|
|
shift
|
|
|
|
screen -d -m -S "$vm_name" "$vm_name" "$@"
|
|
}
|
|
|
|
public_watch() {
|
|
screen -dr "$1"
|
|
}
|
|
|
|
public_list() {
|
|
screen -ls
|
|
}
|
|
|
|
function="$1"
|
|
shift
|
|
|
|
if declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
|
"public_${function}" "$@"
|
|
else
|
|
error_usage
|
|
fi
|