Compare commits

...

7 commits

2 changed files with 28 additions and 27 deletions

View file

@ -47,8 +47,7 @@ need it
- `bash` - the `qemush` interpreter
- `coreutils` - used for basic OS operations
- `sudo` - execute commands as `qemu`
- `source-highlight` - for syntax highlighting when displaying launching
scripts
- `socat` - monitor machines via Unix sockets
- `diskpath` and `sockpath` - see [Installation
instructions](#installation-instructions)
- any text editor - used for builtin function to edit launching scripts

View file

@ -15,24 +15,14 @@ exec_as() {
# 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}"
PATH="${HOME}/launchers:${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
@ -46,7 +36,7 @@ perror() {
# Function to show the usage
public_help() {
local name
name="$(basename "$0")"
name=$(basename "$0")
exec cat << EOF
${name}: usage:
@ -76,9 +66,11 @@ error_usage() {
# Function to start a virtual machine
public_start() {
QEMUSH_NAME="$1"
shift
"$QEMUSH_NAME" "$@" || perror "error launching virtual machine \"${QEMUSH_NAME}\""
if ! "$@"; then
perror "error launching virtual machine \"${QEMUSH_NAME}\""
return 2
fi
}
# Attach to a running virtual machine output, the latest opened if no
@ -92,21 +84,22 @@ public_watch() {
# List running virtual machines
public_active() {
cd || return
echo "Running machines:"
exec ls -t "$sockets"
exec ls -t sockets
}
# List available virtual machines entrypoints
public_ls() {
cd || return
echo "Available machines:"
exec ls "$bin"
exec ls launchers
}
# Create a copy-on-write disk for a virtual machine
public_diskadd() {
QEMUSH_NAME="$1"
shift
exec qemu-img create -f qcow2 "$(diskpath)" "$1"
}
@ -114,26 +107,29 @@ public_diskadd() {
public_diskrm() {
QEMUSH_NAME="$1"
shift
exec rm -vi -- "$(diskpath)"
}
# List available disks
public_diskls() {
cd || return
echo "Available disks:"
exec ls "$images"
exec ls disks
}
# Edit a virtual machine entrypoint with a text editor
public_edit() {
local file="${bin}/${1}"
cd || return
local file="launchers/${1}"
"$EDITOR" "$file"
[ -f "$file" ] && exec chmod u+x "$file"
}
# Delete a virtual machine entrypoint
public_rm() {
exec rm -vi -- "${bin}/${1}"
cd || return
exec rm -vi -- "launchers/${1}"
}
# Invoke bash as qemu user in its home directory
@ -145,7 +141,7 @@ public_shell() {
# Output the content of an entrypoint, with coloration if on a virtual
# terminal
public_cat() {
pretty_cat "${bin}/${1}"
cat "launchers/${1}"
}
# Copy a file in entrypoints folder
@ -154,9 +150,15 @@ public_add() {
set -e
local name
name="${2:-$(basename "$1")}"
cp -v -i -- "$1" "${bin}/${2}"
chmod 740 "${bin}/${name}"
if [ -n "$2" ]; then
name="$2"
else
name=$(basename "$1")
fi
name="${HOME}/launchers/${name}"
cp -vi -- "$1" "$name"
chmod 740 "$name"
set +e
trap - EXIT