Compare commits
4 commits
c115dbd144
...
e1f4d4350b
Author | SHA1 | Date | |
---|---|---|---|
|
e1f4d4350b | ||
|
a0c2e2cf43 | ||
|
720d37b5a1 | ||
|
3497bd31e9 |
1 changed files with 43 additions and 31 deletions
74
bin/qemush
74
bin/qemush
|
@ -1,87 +1,99 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
exec_as() {
|
exec_as() {
|
||||||
local user="$1"
|
local user="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
if [ "$(whoami)" != "$user" ]; then
|
if [ "$(whoami)" != "$user" ]; then
|
||||||
exec sudo -u "$user" "$0" "$@"
|
exec sudo -u "$user" "$0" "$@"
|
||||||
else
|
else
|
||||||
cd
|
cd
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
exec_as qemu "$@" || exit
|
exec_as qemu "$@" || exit
|
||||||
PATH="./bin:${PATH}"
|
PATH="./bin:${PATH}"
|
||||||
|
|
||||||
perror() {
|
perror() {
|
||||||
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
|
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
|
||||||
}
|
}
|
||||||
|
|
||||||
public_help() {
|
public_help() {
|
||||||
local name
|
local name
|
||||||
name="$(basename "$0")"
|
name="$(basename "$0")"
|
||||||
cat << EOF
|
cat << EOF
|
||||||
${name}: usage:
|
${name}: usage:
|
||||||
|
${name} active - (default behaviour) list active VMs
|
||||||
${name} start <VM name> - start a VM
|
${name} start <VM name> - start a VM
|
||||||
${name} watch <VM name> - attach VM screen session
|
${name} watch <VM name> - attach VM screen session
|
||||||
${name} active - (default behaviour) list active VMs
|
|
||||||
${name} list - list available VMs
|
${name} list - list available VMs
|
||||||
${name} help - show this help
|
${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} diskls - list available disk images
|
||||||
${name} diskadd <disk name> <size> - create a disk image
|
${name} diskadd <disk name> <size> - create a disk image
|
||||||
${name} diskrm <disk name> - delete disk image
|
${name} diskrm <disk name> - delete disk image
|
||||||
|
${name} help - show this help
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
error_usage() {
|
error_usage() {
|
||||||
perror "Invalid usage"
|
perror "Invalid usage"
|
||||||
>&2 public_help
|
>&2 public_help
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
public_start() {
|
public_start() {
|
||||||
local vm_name="$1"
|
local vm_name="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
screen -d -m -S "$vm_name" "$vm_name" "$@"
|
screen -d -m -S "$vm_name" "$vm_name" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
public_watch() {
|
public_watch() {
|
||||||
screen -dr "$1"
|
screen -dr "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
public_active() {
|
public_active() {
|
||||||
screen -ls
|
screen -ls
|
||||||
}
|
}
|
||||||
|
|
||||||
public_list() {
|
public_list() {
|
||||||
echo "Available machines:"
|
echo "Available machines:"
|
||||||
ls --color=auto bin
|
ls --color=auto bin
|
||||||
}
|
}
|
||||||
|
|
||||||
diskpath() {
|
diskpath() {
|
||||||
printf 'images/%s.qcow2' "$1"
|
printf 'images/%s.qcow2' "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
public_diskadd() {
|
public_diskadd() {
|
||||||
qemu-img create -f qcow2 "$(diskpath "$1")" "$2"
|
qemu-img create -f qcow2 "$(diskpath "$1")" "$2"
|
||||||
}
|
}
|
||||||
|
|
||||||
public_diskrm() {
|
public_diskrm() {
|
||||||
rm -vi -- "$(diskpath "$1")"
|
rm -vi -- "$(diskpath "$1")"
|
||||||
}
|
}
|
||||||
|
|
||||||
public_diskls() {
|
public_diskls() {
|
||||||
echo "Available disks:"
|
echo "Available disks:"
|
||||||
ls --color=auto images
|
ls --color=auto images
|
||||||
|
}
|
||||||
|
|
||||||
|
public_add() {
|
||||||
|
ln -vs "$2" "bin/${1}"
|
||||||
|
}
|
||||||
|
|
||||||
|
public_rm() {
|
||||||
|
unlink "bin/${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function="$1"
|
function="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
if declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
if [ -z "$function" ]; then
|
||||||
"public_${function}" "$@"
|
public_active
|
||||||
|
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
||||||
|
"public_${function}" "$@"
|
||||||
else
|
else
|
||||||
error_usage
|
error_usage
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue