qemush/bin/qemush

182 lines
4.1 KiB
Bash
Executable file

#!/bin/bash
# version=0.2.0
# Function to re-exec the script as another user via sudo (only if needed)
exec_as() {
local user
user="$1"
shift
if [ "$(whoami)" != "$user" ]; then
exec sudo -E -H -u "$user" -- "$0" "$@"
fi
}
# Exec the script as qemu
exec_as qemu "$@"
# Directories used in the program
bin="${HOME}/launchers"
images="${HOME}/disks"
sockets="${HOME}/sockets"
# Environment
PATH="${bin}:${HOME}/bin:${PATH}"
EDITOR="${EDITOR:-nvim}"
export QEMUSH_NAME
# Aliases
alias ls='ls --color=auto'
alias exec='exec '
if [ -t 1 ]; then
alias pretty_cat='source-highlight --failsafe -f esc --style-file=esc.style -i'
else
alias pretty_cat=cat
fi
shopt -s expand_aliases
# Set a restrictive umask to make sure qemu user files are private
umask 027
# Function to print a colored error
perror() {
>&2 printf '\033[1;31mKO:\033[0m \033[1m%s\033[0m\n' "$*"
}
# Function to show the usage
public_help() {
local name
name="$(basename "$0")"
exec 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} edit <VM name> - edit VM launching script
${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
${name} add <path to script> [<VM name>] - add a launching script
${name} do <command> - run shell input as user qemu
EOF
}
# Function to throw an invalid usage error (skill issue)
error_usage() {
perror "Invalid usage"
>&2 public_help
return 1
}
# Function to start a virtual machine
public_start() {
QEMUSH_NAME="$1"
shift
"$QEMUSH_NAME" "$@" || perror "error launching virtual machine \"${QEMUSH_NAME}\""
}
# Attach to a running virtual machine output, the latest opened if no
# argument is provided
public_watch() {
QEMUSH_NAME="$1"
shift
socat -,echo=0,icanon=0 "UNIX-CONNECT:$(sockpath)"
}
# List running virtual machines
public_active() {
echo "Running machines:"
exec ls -t "$sockets"
}
# List available virtual machines entrypoints
public_ls() {
echo "Available machines:"
exec ls "$bin"
}
# Create a copy-on-write disk for a virtual machine
public_diskadd() {
QEMUSH_NAME="$1"
shift
exec qemu-img create -f qcow2 "$(diskpath)" "$1"
}
# Delete a disk
public_diskrm() {
QEMUSH_NAME="$1"
shift
exec rm -vi -- "$(diskpath)"
}
# List available disks
public_diskls() {
echo "Available disks:"
exec ls "$images"
}
# Edit a virtual machine entrypoint with a text editor
public_edit() {
local file="${bin}/${1}"
"$EDITOR" "$file"
[ -f "$file" ] && exec chmod u+x "$file"
}
# Delete a virtual machine entrypoint
public_rm() {
exec rm -vi -- "${bin}/${1}"
}
# Invoke bash as qemu user in its home directory
public_shell() {
cd || return
exec bash -i
}
# Output the content of an entrypoint, with coloration if on a virtual
# terminal
public_cat() {
pretty_cat "${bin}/${1}"
}
# Copy a file in entrypoints folder
public_add() {
trap return EXIT
set -e
local name
name="${2:-$(basename "$1")}"
cp -v -i -- "$1" "${bin}/${2}"
chmod 740 "${bin}/${name}"
set +e
trap - EXIT
}
# Run shell commands as qemu
public_do() {
exec sh -c "$*"
}
# Retrieve user requested function
function="$1"
shift
# Defauts to `active` if no function is supplied; else checks for a public
# function named after the argument; else fails
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