qemush : ajout de commentaires décrivant les fonctions

This commit is contained in:
Ahurac 2024-01-21 00:27:08 +01:00
parent 3f712ee67d
commit b69251bc56

View file

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
# Function to re-exec the script as another user via sudo (only if needed)
exec_as() { exec_as() {
local user local user
user="$1" user="$1"
@ -10,13 +11,19 @@ exec_as() {
fi fi
} }
# Exec the script as qemu
exec_as qemu "$@" exec_as qemu "$@"
# Directories used in the program
bin="${HOME}/launchers" bin="${HOME}/launchers"
images="${HOME}/disks" images="${HOME}/disks"
# Environment
PATH="${bin}:${HOME}/bin:${PATH}" PATH="${bin}:${HOME}/bin:${PATH}"
EDITOR="${EDITOR:-nvim}" EDITOR="${EDITOR:-nvim}"
export QEMUSH_NAME export QEMUSH_NAME
# Aliases
alias ls='ls --color=auto' alias ls='ls --color=auto'
alias exec='exec ' alias exec='exec '
if [ -t 1 ]; then if [ -t 1 ]; then
@ -25,15 +32,20 @@ else
alias pretty_cat=cat alias pretty_cat=cat
fi fi
shopt -s expand_aliases shopt -s expand_aliases
# Set a restrictive umask to make sure qemu user files are private
umask 027 umask 027
# Function to print a colored error
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' "$*"
} }
# Function to show the usage
public_help() { public_help() {
local name local name
name="$(basename "$0")" name="$(basename "$0")"
exec cat << EOF exec cat << EOF
${name}: usage: ${name}: usage:
${name} active - (default behaviour) list active VMs ${name} active - (default behaviour) list active VMs
@ -53,12 +65,15 @@ ${name}: usage:
EOF EOF
} }
# Function to throw an invalid usage error (skill issue)
error_usage() { error_usage() {
perror "Invalid usage" perror "Invalid usage"
>&2 public_help >&2 public_help
return 1 return 1
} }
# Function to start a virtual machine ; fails if the virtual machine is
# already started thanks to tmux
public_start() { public_start() {
QEMUSH_NAME="$1" QEMUSH_NAME="$1"
shift shift
@ -69,21 +84,26 @@ public_start() {
"$@" "$@"
} }
# Attach to a running virtual machine output, the latest opened if no
# argument is provided
public_watch() { public_watch() {
[ -n "$1" ] && set -- -t "$1" [ -n "$1" ] && set -- -t "$1"
exec tmux attach "$@" exec tmux attach "$@"
} }
# List running virtual machines
public_active() { public_active() {
exec tmux list-sessions exec tmux list-sessions
} }
# List available virtual machines entrypoints
public_ls() { public_ls() {
echo "Available machines:" echo "Available machines:"
exec ls "$bin" exec ls "$bin"
} }
# Create a copy-on-write disk for a virtual machine
public_diskadd() { public_diskadd() {
QEMUSH_NAME="$1" QEMUSH_NAME="$1"
shift shift
@ -91,6 +111,7 @@ public_diskadd() {
exec qemu-img create -f qcow2 "$(pathof disk)" "$1" exec qemu-img create -f qcow2 "$(pathof disk)" "$1"
} }
# Delete a disk
public_diskrm() { public_diskrm() {
QEMUSH_NAME="$1" QEMUSH_NAME="$1"
shift shift
@ -98,47 +119,59 @@ public_diskrm() {
exec rm -vi -- "$(pathof disk)" exec rm -vi -- "$(pathof disk)"
} }
# List available disks
public_diskls() { public_diskls() {
echo "Available disks:" echo "Available disks:"
exec ls "$images" exec ls "$images"
} }
# Edit a virtual machine entrypoint with a text editor
public_edit() { public_edit() {
local file="${bin}/${1}" local file="${bin}/${1}"
"$EDITOR" "$file" "$EDITOR" "$file"
[ -f "$file" ] && exec chmod u+x "$file" [ -f "$file" ] && exec chmod u+x "$file"
} }
# Delete a virtual machine entrypoint
public_rm() { public_rm() {
exec rm -vi -- "${bin}/${1}" exec rm -vi -- "${bin}/${1}"
} }
# Invoke bash as qemu user in its home directory
public_shell() { public_shell() {
cd || return cd || return
exec bash -i exec bash -i
} }
# Output the content of an entrypoint, with coloration if on a virtual
# terminal
public_cat() { public_cat() {
pretty_cat "${bin}/${1}" pretty_cat "${bin}/${1}"
} }
# Copy a file in entrypoints folder
public_add() { public_add() {
trap return EXIT trap return EXIT
set -e set -e
local name="${2:-$(basename "$1")}" local name="${2:-$(basename "$1")}"
cp -v -i -- "$1" "${bin}/${2}" cp -v -i -- "$1" "${bin}/${2}"
chmod 740 "${bin}/${name}" chmod 740 "${bin}/${name}"
set +e set +e
trap - EXIT trap - EXIT
} }
# Run shell commands as qemu
public_do() { public_do() {
exec sh -c "$*" exec sh -c "$*"
} }
# Retrieve user requested function
function="$1" function="$1"
shift 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 if [ -z "$function" ]; then
public_active public_active
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then