Compare commits
16 commits
0da83df0a0
...
9cd6634528
Author | SHA1 | Date | |
---|---|---|---|
9cd6634528 | |||
0f6582be0a | |||
8dfc9bdf8c | |||
c86c7a2e01 | |||
29fc93c188 | |||
db2b517194 | |||
42734333b0 | |||
2fb08080bb | |||
54fb0f7929 | |||
69e5bf4a4e | |||
ad284ca57b | |||
f2f038d24b | |||
c86cbbf089 | |||
b9b96f6467 | |||
d1d579fcd2 | |||
11fa69c866 |
1 changed files with 63 additions and 43 deletions
106
bin/qemush
106
bin/qemush
|
@ -1,19 +1,19 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# version=0.7.0
|
# version=0.8.0
|
||||||
|
|
||||||
# Re-exec the script as qemu via sudo (only if needed)
|
# Re-exec the script as qemu via sudo (only if needed)
|
||||||
[ "$(whoami)" != qemu ] && exec sudo -E -H -u qemu -- "$0" "$@"
|
[ "$(whoami)" != qemu ] && exec sudo -E -H -u qemu -- "$0" "$@"
|
||||||
|
|
||||||
# Environment
|
# Environment
|
||||||
PATH="${HOME}/launchers:${HOME}/bin:${PATH}"
|
PATH="${HOME}/launchers:${HOME}/bin:${PATH}"
|
||||||
EDITOR="${EDITOR:-nvim}"
|
|
||||||
export QEMUSH_NAME
|
|
||||||
|
|
||||||
# Aliases
|
# Aliases
|
||||||
ls='ls --color=auto'
|
alias ls='ls --color=auto'
|
||||||
|
alias exec='exec '
|
||||||
|
shopt -s expand_aliases
|
||||||
|
|
||||||
# Set a restrictive umask to make sure qemu user files are private
|
# Set a restrictive umask to make sure qemu user files are private
|
||||||
umask 027
|
umask 7027
|
||||||
|
|
||||||
# Function to print a colored error
|
# Function to print a colored error
|
||||||
perror() {
|
perror() {
|
||||||
|
@ -22,7 +22,6 @@ perror() {
|
||||||
|
|
||||||
# Function to show the usage
|
# Function to show the usage
|
||||||
public_help() {
|
public_help() {
|
||||||
local name
|
|
||||||
name=$(basename "$0")
|
name=$(basename "$0")
|
||||||
|
|
||||||
exec cat << EOF
|
exec cat << EOF
|
||||||
|
@ -46,19 +45,21 @@ EOF
|
||||||
|
|
||||||
# Function to throw an invalid usage error (skill issue)
|
# 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
|
# Function to start a virtual machine
|
||||||
public_start() {
|
public_start() {
|
||||||
QEMUSH_NAME="$1"
|
export QEMUSH_NAME="$1"
|
||||||
|
|
||||||
set -- "$@" \
|
set -- "$@" \
|
||||||
-name "$QEMUSH_NAME" \
|
-name "$QEMUSH_NAME" \
|
||||||
-monitor "unix:$(pathof socket),server,nowait" \
|
-monitor "unix:$(pathof socket),server,nowait" \
|
||||||
-daemonize
|
-daemonize
|
||||||
|
|
||||||
if ! "$@"; then
|
if ! "$@"; then
|
||||||
perror "error launching virtual machine \"${QEMUSH_NAME}\""
|
perror "error launching virtual machine \"${QEMUSH_NAME}\""
|
||||||
return 2
|
return 2
|
||||||
|
@ -68,8 +69,7 @@ public_start() {
|
||||||
# Attach to a running virtual machine output, the latest opened if no
|
# Attach to a running virtual machine output, the latest opened if no
|
||||||
# argument is provided
|
# argument is provided
|
||||||
public_attach() {
|
public_attach() {
|
||||||
QEMUSH_NAME="$1"
|
export QEMUSH_NAME="$1"
|
||||||
shift
|
|
||||||
|
|
||||||
exec socat -,rawer,escape=15 "UNIX-CONNECT:$(pathof socket)"
|
exec socat -,rawer,escape=15 "UNIX-CONNECT:$(pathof socket)"
|
||||||
}
|
}
|
||||||
|
@ -77,30 +77,31 @@ public_attach() {
|
||||||
# List running virtual machines
|
# List running virtual machines
|
||||||
public_running() {
|
public_running() {
|
||||||
cd || return
|
cd || return
|
||||||
|
|
||||||
echo "Running machines:"
|
echo "Running machines:"
|
||||||
set -- $ls -t sockets/monitors "$@"
|
exec ls -t sockets/monitors
|
||||||
exec "$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# List available virtual machines entrypoints
|
# List available virtual machines entrypoints
|
||||||
public_ls() {
|
public_ls() {
|
||||||
cd || return
|
cd || return
|
||||||
|
|
||||||
echo "Available machines:"
|
echo "Available machines:"
|
||||||
set -- $ls launchers "$@"
|
exec ls launchers
|
||||||
exec "$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create a copy-on-write disk for a virtual machine
|
# Create a copy-on-write disk for a virtual machine
|
||||||
public_diskadd() {
|
public_diskadd() {
|
||||||
QEMUSH_NAME="$1"
|
export QEMUSH_NAME="$1"
|
||||||
shift
|
|
||||||
exec qemu-img create -f qcow2 "$(pathof disk)" "$1"
|
exec qemu-img create -f qcow2 "$(pathof disk)" "$2"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete a disk
|
# Delete a disk
|
||||||
public_diskrm() {
|
public_diskrm() {
|
||||||
for disk in "$@"; do
|
for disk in "$@"; do
|
||||||
QEMUSH_NAME="$disk"
|
export QEMUSH_NAME="$disk"
|
||||||
|
|
||||||
rm -vi -- "$(pathof disk)"
|
rm -vi -- "$(pathof disk)"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
@ -108,55 +109,66 @@ public_diskrm() {
|
||||||
# List available disks
|
# List available disks
|
||||||
public_diskls() {
|
public_diskls() {
|
||||||
cd || return
|
cd || return
|
||||||
|
|
||||||
echo "Available disks:"
|
echo "Available disks:"
|
||||||
set -- $ls disks "$@"
|
exec ls disks
|
||||||
exec "$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Edit a virtual machine entrypoint with a text editor
|
# Edit a virtual machine entrypoint with a text editor
|
||||||
public_edit() {
|
public_edit() {
|
||||||
cd || return
|
file="launchers/${1}"
|
||||||
local file="launchers/${1}"
|
# I don't even know why shellcheck gives me this warning
|
||||||
|
# shellcheck disable=2209
|
||||||
|
[ -z "$EDITOR" ] && EDITOR=vi
|
||||||
|
|
||||||
"$EDITOR" "$file"
|
set -e
|
||||||
[ -f "$file" ] && exec chmod u+x "$file"
|
cd
|
||||||
|
touch -- "$file"
|
||||||
|
chmod u+x -- "$file"
|
||||||
|
exec "$EDITOR" -- "$file"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete a virtual machine entrypoint
|
# Delete a virtual machine entrypoint
|
||||||
public_rm() {
|
public_rm() {
|
||||||
cd ~/launchers || return
|
cd ~/launchers || return
|
||||||
|
|
||||||
exec rm -vi -- "$@"
|
exec rm -vi -- "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Invoke bash as qemu user in its home directory
|
# Invoke bash as qemu user in its home directory
|
||||||
public_shell() {
|
public_shell() {
|
||||||
cd || return
|
cd || return
|
||||||
set -- bash -i "$@"
|
|
||||||
exec "$@"
|
exec bash -i
|
||||||
}
|
}
|
||||||
|
|
||||||
# Output the content of an entrypoint, with coloration if on a virtual
|
# Output the content of an entrypoint, with coloration if on a virtual
|
||||||
# terminal
|
# terminal
|
||||||
public_cat() {
|
public_cat() {
|
||||||
cd ~/launchers || return
|
cd ~/launchers || return
|
||||||
cat -- "$@"
|
|
||||||
|
exec cat -- "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Copy a file in entrypoints folder
|
# Copy a file in entrypoints folder
|
||||||
public_add() {
|
public_add() {
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
perror "specify the path of a launching script you want to add"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
destination="$2"
|
||||||
|
else
|
||||||
|
destination=$(basename "$1")
|
||||||
|
fi
|
||||||
|
destination="${HOME}/launchers/${destination}"
|
||||||
|
|
||||||
trap return EXIT
|
trap return EXIT
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
local name
|
cp -vi -- "$1" "$destination"
|
||||||
if [ -n "$2" ]; then
|
chmod 0740 -- "$destination"
|
||||||
name="$2"
|
|
||||||
else
|
|
||||||
name=$(basename "$1")
|
|
||||||
fi
|
|
||||||
name="${HOME}/launchers/${name}"
|
|
||||||
|
|
||||||
cp -vi -- "$1" "$name"
|
|
||||||
chmod 740 "$name"
|
|
||||||
|
|
||||||
set +e
|
set +e
|
||||||
trap - EXIT
|
trap - EXIT
|
||||||
|
@ -169,7 +181,8 @@ public_do() {
|
||||||
|
|
||||||
# Expose SPICE via TCP
|
# Expose SPICE via TCP
|
||||||
public_spice() {
|
public_spice() {
|
||||||
QEMUSH_NAME="$1"
|
export QEMUSH_NAME="$1"
|
||||||
|
|
||||||
if [ -n "$2" ]; then
|
if [ -n "$2" ]; then
|
||||||
port=$2
|
port=$2
|
||||||
else
|
else
|
||||||
|
@ -180,15 +193,22 @@ public_spice() {
|
||||||
socat TCP-LISTEN:"${port},reuseaddr,fork" UNIX-CLIENT:"$(pathof spice)"
|
socat TCP-LISTEN:"${port},reuseaddr,fork" UNIX-CLIENT:"$(pathof spice)"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Retrieve user requested function
|
function_exists() {
|
||||||
function="$1"
|
declare -F \
|
||||||
shift
|
| cut -d \ -f 3- \
|
||||||
|
| grep '^public_' \
|
||||||
|
| sed 's/^public_//' \
|
||||||
|
| grep -q "^${1}\$"
|
||||||
|
}
|
||||||
|
|
||||||
# Defauts to `active` if no function is supplied; else checks for a public
|
# Defauts to `active` if no function is supplied; else checks for a public
|
||||||
# function named after the argument; else fails
|
# function named after the argument; else fails
|
||||||
if [ -z "$function" ]; then
|
if [ -z "$1" ]; then
|
||||||
public_running
|
public_running
|
||||||
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
elif function_exists "$1"; then
|
||||||
|
function=$1
|
||||||
|
shift
|
||||||
|
|
||||||
"public_${function}" "$@"
|
"public_${function}" "$@"
|
||||||
else
|
else
|
||||||
error_usage
|
error_usage
|
||||||
|
|
Loading…
Reference in a new issue