2023-10-15 11:32:26 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-11-29 14:48:24 +01:00
|
|
|
exec_as() {
|
|
|
|
local user
|
|
|
|
user="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
if [ "$(whoami)" != "$user" ]; then
|
|
|
|
exec sudo -u "$user" "$0" "$@"
|
|
|
|
else
|
|
|
|
cd || exit
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
exec_as qemu "$@"
|
2023-11-29 14:33:20 +01:00
|
|
|
|
|
|
|
bin=bin
|
2023-11-29 14:39:09 +01:00
|
|
|
images=images
|
2023-11-29 14:57:46 +01:00
|
|
|
if [ -t 1 ]; then
|
|
|
|
cat=src-hilite-lesspipe.sh
|
|
|
|
else
|
|
|
|
cat=cat
|
|
|
|
fi
|
2023-11-29 14:42:47 +01:00
|
|
|
|
2023-11-29 14:34:25 +01:00
|
|
|
PATH="./${bin}:${PATH}"
|
2023-11-29 14:52:06 +01:00
|
|
|
EDITOR="${EDITOR:-nvim}"
|
2023-10-15 11:32:26 +02:00
|
|
|
|
2023-11-29 14:42:47 +01:00
|
|
|
alias ls='ls --color=auto'
|
|
|
|
shopt -s expand_aliases
|
|
|
|
|
2023-10-15 11:57:01 +02:00
|
|
|
perror() {
|
2023-10-15 13:51:53 +02:00
|
|
|
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
|
2023-10-15 11:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public_help() {
|
2023-10-15 13:51:53 +02:00
|
|
|
local name
|
|
|
|
name="$(basename "$0")"
|
|
|
|
cat << EOF
|
2023-10-15 11:57:01 +02:00
|
|
|
${name}: usage:
|
2023-10-15 13:50:52 +02:00
|
|
|
${name} active - (default behaviour) list active VMs
|
2023-10-15 12:08:45 +02:00
|
|
|
${name} start <VM name> - start a VM
|
|
|
|
${name} watch <VM name> - attach VM screen session
|
2023-11-29 14:30:12 +01:00
|
|
|
${name} ls - list available VMs
|
2023-12-01 13:32:09 +01:00
|
|
|
${name} edit <VM name> - edit VM launching script
|
2023-10-15 13:50:52 +02:00
|
|
|
${name} rm <VM name> - delete launch script
|
2023-10-15 12:08:45 +02:00
|
|
|
${name} diskls - list available disk images
|
|
|
|
${name} diskadd <disk name> <size> - create a disk image
|
|
|
|
${name} diskrm <disk name> - delete disk image
|
2023-11-28 11:10:08 +01:00
|
|
|
${name} shell - start a shell as user qemu
|
2023-10-15 13:50:52 +02:00
|
|
|
${name} help - show this help
|
2023-10-15 11:57:01 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
error_usage() {
|
2023-10-15 13:51:53 +02:00
|
|
|
perror "Invalid usage"
|
|
|
|
>&2 public_help
|
|
|
|
return 1
|
2023-10-15 11:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public_start() {
|
2023-10-15 13:51:53 +02:00
|
|
|
local vm_name="$1"
|
|
|
|
shift
|
2023-10-15 11:57:01 +02:00
|
|
|
|
2023-11-11 22:13:29 +01:00
|
|
|
screen -S "$vm_name" "$vm_name" "$@"
|
2023-10-15 11:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public_watch() {
|
2023-10-15 13:51:53 +02:00
|
|
|
screen -dr "$1"
|
2023-10-15 11:57:01 +02:00
|
|
|
}
|
|
|
|
|
2023-10-15 11:59:15 +02:00
|
|
|
public_active() {
|
2023-10-15 13:51:53 +02:00
|
|
|
screen -ls
|
2023-10-15 11:57:01 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 14:30:12 +01:00
|
|
|
public_ls() {
|
2023-10-15 13:51:53 +02:00
|
|
|
echo "Available machines:"
|
2023-11-29 14:42:47 +01:00
|
|
|
ls "$bin"
|
2023-10-15 11:59:15 +02:00
|
|
|
}
|
|
|
|
|
2023-10-15 12:08:45 +02:00
|
|
|
diskpath() {
|
2023-11-29 14:39:09 +01:00
|
|
|
printf "%s.qcow2" "${images}/${1}"
|
2023-10-15 12:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public_diskadd() {
|
2023-10-15 13:51:53 +02:00
|
|
|
qemu-img create -f qcow2 "$(diskpath "$1")" "$2"
|
2023-10-15 12:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public_diskrm() {
|
2023-10-15 13:51:53 +02:00
|
|
|
rm -vi -- "$(diskpath "$1")"
|
2023-10-15 12:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public_diskls() {
|
2023-10-15 13:51:53 +02:00
|
|
|
echo "Available disks:"
|
2023-11-29 14:42:47 +01:00
|
|
|
ls "$images"
|
2023-10-15 12:08:45 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 14:33:20 +01:00
|
|
|
public_edit() {
|
|
|
|
"$EDITOR" "${bin}/${1}"
|
2023-10-15 13:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public_rm() {
|
2023-11-29 14:36:57 +01:00
|
|
|
rm -vi -- "${bin}/${1}"
|
2023-10-15 13:50:03 +02:00
|
|
|
}
|
|
|
|
|
2023-11-28 11:10:08 +01:00
|
|
|
public_shell() {
|
|
|
|
bash -i
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:57:46 +01:00
|
|
|
public_cat() {
|
|
|
|
"$cat" "${bin}/${1}"
|
|
|
|
}
|
|
|
|
|
2023-10-15 11:57:01 +02:00
|
|
|
function="$1"
|
2023-10-15 11:32:26 +02:00
|
|
|
shift
|
|
|
|
|
2023-10-15 13:53:11 +02:00
|
|
|
if [ -z "$function" ]; then
|
|
|
|
public_active
|
|
|
|
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
2023-10-15 13:51:53 +02:00
|
|
|
"public_${function}" "$@"
|
2023-10-15 11:57:01 +02:00
|
|
|
else
|
2023-10-15 13:51:53 +02:00
|
|
|
error_usage
|
2023-10-15 11:57:01 +02:00
|
|
|
fi
|