100 lines
1.9 KiB
Bash
Executable file
100 lines
1.9 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
if [ "$(whoami)" != root ]; then
|
|
exec sudo -u qemu "$0" "$@"
|
|
else
|
|
cd || exit
|
|
fi
|
|
|
|
bin=bin
|
|
images=images
|
|
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} active - (default behaviour) list active VMs
|
|
${name} start <VM name> - start a VM
|
|
${name} watch <VM name> - attach VM screen session
|
|
${name} ls - list available VMs
|
|
${name} add <VM name> <template> - add launch script based on template
|
|
${name} rm <VM name> - delete launch script
|
|
${name} diskls - list available disk images
|
|
${name} diskadd <disk name> <size> - create a disk image
|
|
${name} diskrm <disk name> - delete disk image
|
|
${name} shell - start a shell as user qemu
|
|
${name} help - show this help
|
|
EOF
|
|
}
|
|
|
|
error_usage() {
|
|
perror "Invalid usage"
|
|
>&2 public_help
|
|
return 1
|
|
}
|
|
|
|
public_start() {
|
|
local vm_name="$1"
|
|
shift
|
|
|
|
screen -S "$vm_name" "$vm_name" "$@"
|
|
}
|
|
|
|
public_watch() {
|
|
screen -dr "$1"
|
|
}
|
|
|
|
public_active() {
|
|
screen -ls
|
|
}
|
|
|
|
public_ls() {
|
|
echo "Available machines:"
|
|
ls --color=auto "$bin"
|
|
}
|
|
|
|
diskpath() {
|
|
printf "%s.qcow2" "${images}/${1}"
|
|
}
|
|
|
|
public_diskadd() {
|
|
qemu-img create -f qcow2 "$(diskpath "$1")" "$2"
|
|
}
|
|
|
|
public_diskrm() {
|
|
rm -vi -- "$(diskpath "$1")"
|
|
}
|
|
|
|
public_diskls() {
|
|
echo "Available disks:"
|
|
ls --color=auto "$images"
|
|
}
|
|
|
|
public_edit() {
|
|
"$EDITOR" "${bin}/${1}"
|
|
}
|
|
|
|
public_rm() {
|
|
rm -vi -- "${bin}/${1}"
|
|
}
|
|
|
|
public_shell() {
|
|
bash -i
|
|
}
|
|
|
|
function="$1"
|
|
shift
|
|
|
|
if [ -z "$function" ]; then
|
|
public_active
|
|
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
|
"public_${function}" "$@"
|
|
else
|
|
error_usage
|
|
fi
|