2024-03-25 14:40:52 +01:00
|
|
|
#!/usr/bin/env sh
|
2024-03-25 23:32:34 +01:00
|
|
|
# version=0.11.0
|
2023-12-04 12:00:53 +01:00
|
|
|
|
2024-03-12 11:20:11 +01:00
|
|
|
# Re-exec the script as qemu via sudo (only if needed)
|
|
|
|
[ "$(whoami)" != qemu ] && exec sudo -E -H -u qemu -- "$0" "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# Environment
|
2024-01-22 23:42:11 +01:00
|
|
|
PATH="${HOME}/launchers:${HOME}/bin:${PATH}"
|
2024-01-21 00:27:08 +01:00
|
|
|
|
|
|
|
# Aliases
|
2024-03-24 23:23:09 +01:00
|
|
|
alias ls='ls --color=auto'
|
2024-03-24 23:31:57 +01:00
|
|
|
alias exec='exec '
|
2024-01-21 00:27:08 +01:00
|
|
|
|
|
|
|
# Set a restrictive umask to make sure qemu user files are private
|
2024-03-24 23:23:31 +01:00
|
|
|
umask 7027
|
2023-12-04 12:00:53 +01:00
|
|
|
|
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() {
|
2024-01-22 23:43:41 +01:00
|
|
|
name=$(basename "$0")
|
2024-01-21 00:27:08 +01:00
|
|
|
|
2024-03-25 18:15:54 +01:00
|
|
|
cat << EOF
|
2023-12-04 12:00:53 +01:00
|
|
|
${name}: usage:
|
2024-01-25 14:41:20 +01:00
|
|
|
${name} running - (default behaviour) list running VMs
|
2023-12-06 11:25:12 +01:00
|
|
|
${name} ls - list available VMs
|
2024-03-25 11:41:40 +01:00
|
|
|
${name} add <path to script> [<VM name>] - add a launching script
|
2023-12-06 11:25:12 +01:00
|
|
|
${name} edit <VM name> - edit VM launching script
|
2024-03-25 16:08:06 +01:00
|
|
|
${name} cat <VM name> - print the content of a launching script
|
2024-03-25 11:41:40 +01:00
|
|
|
${name} start <VM name> - start a VM
|
|
|
|
${name} attach <VM name> - attach to the VM monitor socket
|
2023-12-06 11:25:12 +01:00
|
|
|
${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
|
2024-03-25 11:41:40 +01:00
|
|
|
${name} do <shell code> - run shell input as user qemu
|
2024-03-12 11:55:50 +01:00
|
|
|
${name} spice <running VM> [<TCP port>] - expose a SPICE socket to TCP
|
2024-03-25 11:41:40 +01:00
|
|
|
${name} help - show this help
|
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() {
|
2024-03-24 23:26:08 +01:00
|
|
|
perror "invalid usage"
|
2023-12-04 12:00:53 +01:00
|
|
|
>&2 public_help
|
2024-03-24 23:27:07 +01:00
|
|
|
|
2023-12-04 12:00:53 +01:00
|
|
|
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-03-25 23:29:12 +01:00
|
|
|
if [ "$1" = -f ] || [ "$1" = --foreground ]; then
|
|
|
|
daemonize=
|
|
|
|
else
|
|
|
|
daemonize=-daemonize
|
|
|
|
fi
|
|
|
|
|
|
|
|
while echo "$1" | grep -q '^-'; do shift; done;
|
|
|
|
|
2024-03-24 22:52:37 +01:00
|
|
|
export QEMUSH_NAME="$1"
|
2023-12-04 12:00:53 +01:00
|
|
|
|
2024-01-23 10:30:54 +01:00
|
|
|
set -- "$@" \
|
2024-03-11 15:10:25 +01:00
|
|
|
-name "$QEMUSH_NAME" \
|
2024-01-25 16:13:58 +01:00
|
|
|
-monitor "unix:$(pathof socket),server,nowait" \
|
2024-03-25 23:29:12 +01:00
|
|
|
$daemonize
|
2024-03-24 23:27:07 +01:00
|
|
|
|
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
|
2024-01-25 14:42:59 +01:00
|
|
|
public_attach() {
|
2024-03-25 15:37:56 +01:00
|
|
|
export QEMUSH_NAME
|
|
|
|
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
QEMUSH_NAME=$1
|
|
|
|
socket_path=$(pathof socket)
|
|
|
|
else
|
|
|
|
socket_path=$(find ~/sockets/monitors -type s -printf '%T@ %p\n' | sort -r -n | head -1 | cut -d \ -f 2-)
|
|
|
|
QEMUSH_NAME=$(basename "$socket_path")
|
|
|
|
fi
|
2024-01-21 00:23:33 +01:00
|
|
|
|
2024-03-25 15:37:56 +01:00
|
|
|
printf 'Attaching to \033[1m%s\033[0m, escape with C-d (EOF)\n' "$QEMUSH_NAME"
|
|
|
|
socat -,rawer,escape=4 "UNIX-CONNECT:${socket_path}"
|
|
|
|
echo
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 00:27:08 +01:00
|
|
|
# List running virtual machines
|
2024-01-25 14:41:20 +01:00
|
|
|
public_running() {
|
2024-01-22 23:45:05 +01:00
|
|
|
cd || return
|
2024-03-24 23:27:07 +01:00
|
|
|
|
2024-01-21 01:51:07 +01:00
|
|
|
echo "Running machines:"
|
2024-03-24 23:23:09 +01:00
|
|
|
exec ls -t sockets/monitors
|
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
|
2024-03-24 23:27:07 +01:00
|
|
|
|
2023-12-04 12:00:53 +01:00
|
|
|
echo "Available machines:"
|
2024-03-24 23:23:09 +01:00
|
|
|
exec ls launchers
|
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-03-24 22:52:37 +01:00
|
|
|
export QEMUSH_NAME="$1"
|
2024-03-24 23:27:56 +01:00
|
|
|
|
|
|
|
exec qemu-img create -f qcow2 "$(pathof disk)" "$2"
|
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
|
2024-03-24 22:52:37 +01:00
|
|
|
export QEMUSH_NAME="$disk"
|
2024-03-24 23:27:07 +01:00
|
|
|
|
2024-01-25 16:13:58 +01:00
|
|
|
rm -vi -- "$(pathof disk)"
|
2024-01-23 01:13:56 +01:00
|
|
|
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
|
2024-03-24 23:27:07 +01:00
|
|
|
|
2023-12-04 12:00:53 +01:00
|
|
|
echo "Available disks:"
|
2024-03-24 23:23:09 +01:00
|
|
|
exec ls disks
|
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-03-24 23:25:05 +01:00
|
|
|
file="launchers/${1}"
|
2024-03-24 22:51:42 +01:00
|
|
|
# I don't even know why shellcheck gives me this warning
|
|
|
|
# shellcheck disable=2209
|
|
|
|
[ -z "$EDITOR" ] && EDITOR=vi
|
2024-01-22 23:42:11 +01:00
|
|
|
|
2024-03-24 22:52:57 +01:00
|
|
|
set -e
|
|
|
|
cd
|
|
|
|
touch -- "$file"
|
|
|
|
chmod u+x -- "$file"
|
|
|
|
exec "$EDITOR" -- "$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
|
2024-03-24 23:27:07 +01:00
|
|
|
|
2024-01-23 01:13:56 +01:00
|
|
|
exec rm -vi -- "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
2024-03-25 16:41:19 +01:00
|
|
|
# Invoke a shell 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
|
2024-03-24 23:28:17 +01:00
|
|
|
|
2024-03-25 16:41:19 +01:00
|
|
|
# Again...
|
|
|
|
# shellcheck disable=2209
|
|
|
|
[ -z "$SHELL" ] && SHELL=sh
|
|
|
|
|
|
|
|
set -- "$SHELL"
|
|
|
|
case "$SHELL" in
|
|
|
|
sh|bash|zsh|ksh|mksh|oksh)
|
|
|
|
set -- "$@" -i
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
exec "$@"
|
2023-12-04 12:00:53 +01:00
|
|
|
}
|
|
|
|
|
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
|
2024-03-24 22:53:11 +01:00
|
|
|
|
|
|
|
exec 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() {
|
2024-03-24 23:29:50 +01:00
|
|
|
if [ -z "$1" ]; then
|
|
|
|
perror "specify the path of a launching script you want to add"
|
|
|
|
return 1
|
|
|
|
fi
|
2024-01-21 00:27:36 +01:00
|
|
|
|
2024-01-22 23:42:11 +01:00
|
|
|
if [ -n "$2" ]; then
|
2024-03-24 23:30:26 +01:00
|
|
|
destination="$2"
|
2024-01-22 23:42:11 +01:00
|
|
|
else
|
2024-03-24 23:30:26 +01:00
|
|
|
destination=$(basename "$1")
|
2024-01-22 23:42:11 +01:00
|
|
|
fi
|
2024-03-24 23:25:05 +01:00
|
|
|
destination="${HOME}/launchers/${destination}"
|
2024-01-22 23:42:11 +01:00
|
|
|
|
2024-03-24 23:28:52 +01:00
|
|
|
trap return EXIT
|
|
|
|
set -e
|
|
|
|
|
2024-03-24 23:25:05 +01:00
|
|
|
cp -vi -- "$1" "$destination"
|
|
|
|
chmod 0740 -- "$destination"
|
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-03-12 11:55:50 +01:00
|
|
|
# Expose SPICE via TCP
|
|
|
|
public_spice() {
|
2024-03-24 22:52:37 +01:00
|
|
|
export QEMUSH_NAME="$1"
|
2024-03-24 23:27:07 +01:00
|
|
|
|
2024-03-12 11:55:50 +01:00
|
|
|
if [ -n "$2" ]; then
|
|
|
|
port=$2
|
|
|
|
else
|
|
|
|
port=$(first-free-port 5900)
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec tmux new -d -s "TCP port ${port} -> ${QEMUSH_NAME}" \
|
|
|
|
socat TCP-LISTEN:"${port},reuseaddr,fork" UNIX-CLIENT:"$(pathof spice)"
|
|
|
|
}
|
|
|
|
|
2024-03-25 11:43:20 +01:00
|
|
|
case "$1" in
|
|
|
|
"")
|
|
|
|
public_running
|
|
|
|
;;
|
2024-03-25 16:08:06 +01:00
|
|
|
running|ls|add|edit|cat|start|attach|rm|diskls|diskadd|diskrm|shell|do|spice|help)
|
2024-03-25 11:43:20 +01:00
|
|
|
function=$1
|
|
|
|
shift
|
|
|
|
|
|
|
|
"public_${function}" "$@"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
error_usage
|
|
|
|
;;
|
|
|
|
esac
|