2023-12-04 12:00:53 +01:00
|
|
|
#!/bin/bash
|
2024-01-21 00:30:01 +01:00
|
|
|
# version=0.2.0
|
2023-12-04 12:00:53 +01:00
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Function to re-exec the script as another user via sudo (only if needed)
|
2023-12-04 12:00:53 +01:00
|
|
|
exec_as() {
|
|
|
|
local user
|
|
|
|
user="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
if [ "$(whoami)" != "$user" ]; then
|
2024-01-21 12:55:32 +01:00
|
|
|
exec sudo -E -H -u "$user" -- "$0" "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Exec the script as qemu
|
2023-12-04 12:00:53 +01:00
|
|
|
exec_as qemu "$@"
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Environment
|
2024-01-22 23:42:11 +01:00
|
|
|
PATH="${HOME}/launchers:${HOME}/bin:${PATH}"
|
2023-12-04 12:00:53 +01:00
|
|
|
EDITOR="${EDITOR:-nvim}"
|
2024-01-21 00:20:51 +01:00
|
|
|
export QEMUSH_NAME
|
2024-01-21 00:27:08 +01:00
|
|
|
|
|
|
|
# Aliases
|
2024-01-23 01:04:24 +01:00
|
|
|
ls='ls --color=auto'
|
2024-01-21 00:27:08 +01:00
|
|
|
|
|
|
|
# Set a restrictive umask to make sure qemu user files are private
|
2023-12-04 12:00:53 +01:00
|
|
|
umask 027
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Function to print a colored error
|
2023-12-04 12:00:53 +01:00
|
|
|
perror() {
|
2024-01-21 13:53:33 +01:00
|
|
|
>&2 printf '\033[1;31mKO:\033[0m \033[1m%s\033[0m\n' "$*"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Function to show the usage
|
2023-12-04 12:00:53 +01:00
|
|
|
public_help() {
|
|
|
|
local name
|
2024-01-22 23:43:41 +01:00
|
|
|
name=$(basename "$0")
|
2024-01-21 00:27:08 +01:00
|
|
|
|
2023-12-04 12:00:53 +01:00
|
|
|
exec cat << EOF
|
|
|
|
${name}: usage:
|
2023-12-06 11:25:12 +01:00
|
|
|
${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
|
2023-12-10 12:12:52 +01:00
|
|
|
${name} do <command> - run shell input as user qemu
|
2023-12-04 12:00:53 +01:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Function to throw an invalid usage error (skill issue)
|
2023-12-04 12:00:53 +01:00
|
|
|
error_usage() {
|
|
|
|
perror "Invalid usage"
|
|
|
|
>&2 public_help
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-01-21 01:51:07 +01:00
|
|
|
# Function to start a virtual machine
|
2023-12-04 12:00:53 +01:00
|
|
|
public_start() {
|
2024-01-21 00:20:51 +01:00
|
|
|
QEMUSH_NAME="$1"
|
2023-12-04 12:00:53 +01:00
|
|
|
|
2024-01-23 10:30:54 +01:00
|
|
|
set -- "$@" \
|
|
|
|
-monitor "unix:$(sockpath),server,nowait" \
|
|
|
|
-daemonize
|
2024-01-22 23:45:45 +01:00
|
|
|
if ! "$@"; then
|
|
|
|
perror "error launching virtual machine \"${QEMUSH_NAME}\""
|
|
|
|
return 2
|
|
|
|
fi
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Attach to a running virtual machine output, the latest opened if no
|
|
|
|
# argument is provided
|
2023-12-04 12:00:53 +01:00
|
|
|
public_watch() {
|
2024-01-21 01:51:07 +01:00
|
|
|
QEMUSH_NAME="$1"
|
|
|
|
shift
|
2024-01-21 00:23:33 +01:00
|
|
|
|
2024-01-23 10:31:41 +01:00
|
|
|
exec socat -,echo=0,icanon=0 "UNIX-CONNECT:$(sockpath)"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# List running virtual machines
|
2023-12-04 12:00:53 +01:00
|
|
|
public_active() {
|
2024-01-22 23:45:05 +01:00
|
|
|
cd || return
|
2024-01-21 01:51:07 +01:00
|
|
|
echo "Running machines:"
|
2024-01-23 01:04:24 +01:00
|
|
|
set -- $ls -t sockets "$@"
|
|
|
|
exec "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# List available virtual machines entrypoints
|
2023-12-04 12:00:53 +01:00
|
|
|
public_ls() {
|
2024-01-22 23:45:05 +01:00
|
|
|
cd || return
|
2023-12-04 12:00:53 +01:00
|
|
|
echo "Available machines:"
|
2024-01-23 01:04:24 +01:00
|
|
|
set -- $ls launchers "$@"
|
|
|
|
exec "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Create a copy-on-write disk for a virtual machine
|
2023-12-04 12:00:53 +01:00
|
|
|
public_diskadd() {
|
2024-01-21 00:20:51 +01:00
|
|
|
QEMUSH_NAME="$1"
|
|
|
|
shift
|
2024-01-21 01:40:45 +01:00
|
|
|
exec qemu-img create -f qcow2 "$(diskpath)" "$1"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Delete a disk
|
2023-12-04 12:00:53 +01:00
|
|
|
public_diskrm() {
|
2024-01-23 01:13:56 +01:00
|
|
|
for disk in "$@"; do
|
|
|
|
QEMUSH_NAME="$disk"
|
|
|
|
rm -vi -- "$(diskpath)"
|
|
|
|
done
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# List available disks
|
2023-12-04 12:00:53 +01:00
|
|
|
public_diskls() {
|
2024-01-22 23:45:05 +01:00
|
|
|
cd || return
|
2023-12-04 12:00:53 +01:00
|
|
|
echo "Available disks:"
|
2024-01-23 01:04:24 +01:00
|
|
|
set -- $ls disks "$@"
|
|
|
|
exec "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Edit a virtual machine entrypoint with a text editor
|
2023-12-04 12:00:53 +01:00
|
|
|
public_edit() {
|
2024-01-22 23:45:05 +01:00
|
|
|
cd || return
|
2024-01-22 23:42:11 +01:00
|
|
|
local file="launchers/${1}"
|
|
|
|
|
2023-12-04 12:00:53 +01:00
|
|
|
"$EDITOR" "$file"
|
2023-12-04 22:56:16 +01:00
|
|
|
[ -f "$file" ] && exec chmod u+x "$file"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Delete a virtual machine entrypoint
|
2023-12-04 12:00:53 +01:00
|
|
|
public_rm() {
|
2024-01-23 01:13:56 +01:00
|
|
|
cd ~/launchers || return
|
|
|
|
exec rm -vi -- "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Invoke bash as qemu user in its home directory
|
2023-12-04 12:00:53 +01:00
|
|
|
public_shell() {
|
2023-12-05 22:56:54 +01:00
|
|
|
cd || return
|
2023-12-04 12:00:53 +01:00
|
|
|
exec bash -i
|
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Output the content of an entrypoint, with coloration if on a virtual
|
|
|
|
# terminal
|
2023-12-04 12:00:53 +01:00
|
|
|
public_cat() {
|
2024-01-23 01:16:19 +01:00
|
|
|
cd ~/launchers || return
|
|
|
|
cat -- "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Copy a file in entrypoints folder
|
2023-12-04 23:19:24 +01:00
|
|
|
public_add() {
|
|
|
|
trap return EXIT
|
|
|
|
set -e
|
2024-01-21 00:27:36 +01:00
|
|
|
|
|
|
|
local name
|
2024-01-22 23:42:11 +01:00
|
|
|
if [ -n "$2" ]; then
|
|
|
|
name="$2"
|
|
|
|
else
|
|
|
|
name=$(basename "$1")
|
|
|
|
fi
|
|
|
|
name="${HOME}/launchers/${name}"
|
|
|
|
|
|
|
|
cp -vi -- "$1" "$name"
|
|
|
|
chmod 740 "$name"
|
2024-01-21 00:27:08 +01:00
|
|
|
|
2023-12-04 23:19:24 +01:00
|
|
|
set +e
|
|
|
|
trap - EXIT
|
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Run shell commands as qemu
|
2023-12-08 11:49:32 +01:00
|
|
|
public_do() {
|
2023-12-08 11:01:33 +01:00
|
|
|
exec sh -c "$*"
|
2023-12-08 10:59:49 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Retrieve user requested function
|
2023-12-04 12:00:53 +01:00
|
|
|
function="$1"
|
|
|
|
shift
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Defauts to `active` if no function is supplied; else checks for a public
|
|
|
|
# function named after the argument; else fails
|
2023-12-04 12:00:53 +01:00
|
|
|
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
|