Shemum/shemum
2024-11-04 14:02:29 +01:00

141 lines
2.9 KiB
Bash
Executable file

#!/usr/bin/env sh
#
# Author: Adrian Erik Hoemann
# Date: 9.18.2024
# TODO: Implement operating system installer variable list.
# Debug mode
# Default: true
DEBUG=true
# Debug mode check
if [$DEBUG -eq true]
set +x
fi
# Arguments
OPTION=$1
OS_NAME=$2
# Defining paths
ROOT_PATH="$HOME/Machines"
ISO_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.iso"
IMG_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.img"
# Sources default values
. ".env"
# Sources virtual machine specific values
. "$ROOT_PATH/$OS_NAME/.env"
# Functions
# Defines install image source
function def_os_source() {
case $OS_NAME in
"openbsd")
INSTALL_IMG="https://cdn.openbsd.org/pub/OpenBSD/7.5/i386/cd75.iso"
;;
"debian")
INSTALL_IMG=""
;;
*)
error "INSTALL_IMG for $OS_NAME not found"
;;
esac
}
function check() {
if [ -z $OS_NAME ]
error "Operating system is empty"
fi
if [ -z $INSTALL_IMG ]
info "$INSTALL_IMG is not set"
fi
}
function error() {
printf "$@" >&2
exit 1
}
function warn() {
printf "$@" >&2
}
function info() {
printf "$@" >&1
}
# Virtual machine removal
function remove() {
rm -r $ROOT_PATH/$OS_NAME
}
# Virtual machine start without a console
function start() {
qemu-system-x86_64 -boot c \
-display none \
-drive file=$IMG_PATH,format=raw \
-m $RAM -smp $CPU
}
# Virtual machine start with a console
function console() {
qemu-system-x86_64 -boot c \
-display curses \
-drive file=$IMG_PATH,format=raw \
-m $RAM -smp $CPU
}
# Virtual machine initialization with a console
function init() {
def_os_source
mkdir -p "$ROOT_PATH/$OS_NAME"
qemu-img create $IMG_PATH $DISK_SIZE
if [ ! -f $ISO_PATH ] && [ ! -z $INSTALL_IMG ]; then
info "Fetching $OSNAME installer at: $INSTALL_IMG"
curl -o $ISO_PATH $INSTALL_IMG
elif [ ! -z $INSTALL_IMG ]; then
info "Current INSTALL_IMG for $OS_NAME: $ISO_PATH"
else
error "Install image path is missing"
fi
qemu-system-x86_64 -boot d \
-display curses \
-drive file=$ISO_PATH,media=cdrom \
-drive file=$IMG_PATH,format=raw \
-m $RAM -smp $CPU
}
# Display usage
function usage() {
printf "Usage: shemum [init] <virtual machine name> <disk size>"
printf " init initialize a virtual machine"
printf " start start a QEMU virtual machine without console output"
printf " console start a QEMU virtual machine with console output"
printf " remove remove virtual machine directory and all it's contents"
}
# Options
case $OPTION in
"init")
init
;;
"console")
console
;;
"start")
start
;;
"remove")
remove
;;
*)
usage
esac