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 - `bash` - the `qemush` interpreter
- `coreutils` - used for basic OS operations - `coreutils` - used for basic OS operations
- `sudo` - execute commands as `qemu` - `sudo` - execute commands as `qemu`
- `source-highlight` - for syntax highlighting when displaying launching - `socat` - monitor machines via Unix sockets
scripts
- `diskpath` and `sockpath` - see [Installation - `diskpath` and `sockpath` - see [Installation
instructions](#installation-instructions) instructions](#installation-instructions)
- any text editor - used for builtin function to edit launching scripts - 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 the script as qemu
exec_as qemu "$@" exec_as qemu "$@"
# Directories used in the program
bin="${HOME}/launchers"
images="${HOME}/disks"
sockets="${HOME}/sockets"
# Environment # Environment
PATH="${bin}:${HOME}/bin:${PATH}" PATH="${HOME}/launchers:${HOME}/bin:${PATH}"
EDITOR="${EDITOR:-nvim}" EDITOR="${EDITOR:-nvim}"
export QEMUSH_NAME export QEMUSH_NAME
# Aliases # Aliases
alias ls='ls --color=auto' alias ls='ls --color=auto'
alias exec='exec ' 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 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
@ -46,7 +36,7 @@ perror() {
# Function to show the usage # 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:
@ -76,9 +66,11 @@ error_usage() {
# Function to start a virtual machine # Function to start a virtual machine
public_start() { public_start() {
QEMUSH_NAME="$1" 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 # Attach to a running virtual machine output, the latest opened if no
@ -92,21 +84,22 @@ public_watch() {
# List running virtual machines # List running virtual machines
public_active() { public_active() {
cd || return
echo "Running machines:" echo "Running machines:"
exec ls -t "$sockets" exec ls -t sockets
} }
# List available virtual machines entrypoints # List available virtual machines entrypoints
public_ls() { public_ls() {
cd || return
echo "Available machines:" echo "Available machines:"
exec ls "$bin" exec ls launchers
} }
# 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" QEMUSH_NAME="$1"
shift shift
exec qemu-img create -f qcow2 "$(diskpath)" "$1" exec qemu-img create -f qcow2 "$(diskpath)" "$1"
} }
@ -114,26 +107,29 @@ public_diskadd() {
public_diskrm() { public_diskrm() {
QEMUSH_NAME="$1" QEMUSH_NAME="$1"
shift shift
exec rm -vi -- "$(diskpath)" exec rm -vi -- "$(diskpath)"
} }
# List available disks # List available disks
public_diskls() { public_diskls() {
cd || return
echo "Available disks:" echo "Available disks:"
exec ls "$images" exec ls disks
} }
# Edit a virtual machine entrypoint with a text editor # Edit a virtual machine entrypoint with a text editor
public_edit() { public_edit() {
local file="${bin}/${1}" cd || return
local file="launchers/${1}"
"$EDITOR" "$file" "$EDITOR" "$file"
[ -f "$file" ] && exec chmod u+x "$file" [ -f "$file" ] && exec chmod u+x "$file"
} }
# Delete a virtual machine entrypoint # Delete a virtual machine entrypoint
public_rm() { public_rm() {
exec rm -vi -- "${bin}/${1}" cd || return
exec rm -vi -- "launchers/${1}"
} }
# Invoke bash as qemu user in its home directory # 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 # Output the content of an entrypoint, with coloration if on a virtual
# terminal # terminal
public_cat() { public_cat() {
pretty_cat "${bin}/${1}" cat "launchers/${1}"
} }
# Copy a file in entrypoints folder # Copy a file in entrypoints folder
@ -154,9 +150,15 @@ public_add() {
set -e set -e
local name local name
name="${2:-$(basename "$1")}" if [ -n "$2" ]; then
cp -v -i -- "$1" "${bin}/${2}" name="$2"
chmod 740 "${bin}/${name}" else
name=$(basename "$1")
fi
name="${HOME}/launchers/${name}"
cp -vi -- "$1" "$name"
chmod 740 "$name"
set +e set +e
trap - EXIT trap - EXIT