2024-09-25 21:03:33 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
#
|
|
|
|
# Author: Adrian Erik Hoemann
|
|
|
|
# Date: 9.18.2024
|
|
|
|
|
2024-10-31 13:18:42 +00:00
|
|
|
# TODO: Implement operating system installer variable list.
|
|
|
|
|
2024-09-25 21:03:33 +00:00
|
|
|
# Debug mode
|
2024-10-31 11:12:58 +00:00
|
|
|
# Default: true
|
|
|
|
DEBUG=true
|
|
|
|
|
|
|
|
# Debug mode check
|
|
|
|
if [$DEBUG -eq true]
|
|
|
|
set +x
|
|
|
|
fi
|
2024-09-25 21:03:33 +00:00
|
|
|
|
|
|
|
# Arguments
|
|
|
|
OPTION=$1
|
|
|
|
OS_NAME=$2
|
|
|
|
|
|
|
|
# Defining paths
|
2024-09-25 22:07:04 +00:00
|
|
|
ROOT_PATH="$HOME/Machines"
|
2024-09-25 21:03:33 +00:00
|
|
|
ISO_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.iso"
|
|
|
|
IMG_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.img"
|
2024-09-25 22:07:04 +00:00
|
|
|
|
2024-11-04 12:55:47 +00:00
|
|
|
# Sources default values
|
2024-10-08 10:56:46 +00:00
|
|
|
. ".env"
|
|
|
|
|
|
|
|
# Sources virtual machine specific values
|
|
|
|
. "$ROOT_PATH/$OS_NAME/.env"
|
2024-09-25 21:03:33 +00:00
|
|
|
|
2024-10-31 11:41:40 +00:00
|
|
|
# Functions
|
|
|
|
|
2024-10-31 13:18:42 +00:00
|
|
|
# Defines install image source
|
2024-11-04 13:02:29 +00:00
|
|
|
function def_os_source() {
|
2024-10-31 13:18:42 +00:00
|
|
|
case $OS_NAME in
|
|
|
|
"openbsd")
|
|
|
|
INSTALL_IMG="https://cdn.openbsd.org/pub/OpenBSD/7.5/i386/cd75.iso"
|
|
|
|
;;
|
|
|
|
"debian")
|
|
|
|
INSTALL_IMG=""
|
|
|
|
;;
|
|
|
|
*)
|
2024-11-04 13:02:29 +00:00
|
|
|
error "INSTALL_IMG for $OS_NAME not found"
|
2024-10-31 13:18:42 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2024-11-04 13:02:29 +00:00
|
|
|
function check() {
|
2024-10-31 13:18:42 +00:00
|
|
|
if [ -z $OS_NAME ]
|
2024-11-04 13:02:29 +00:00
|
|
|
error "Operating system is empty"
|
2024-10-31 13:18:42 +00:00
|
|
|
fi
|
|
|
|
if [ -z $INSTALL_IMG ]
|
2024-11-04 13:02:29 +00:00
|
|
|
info "$INSTALL_IMG is not set"
|
2024-10-31 13:18:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-04 13:02:29 +00:00
|
|
|
function error() {
|
2024-10-31 13:18:42 +00:00
|
|
|
printf "$@" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2024-11-04 13:02:29 +00:00
|
|
|
function warn() {
|
2024-10-31 13:18:42 +00:00
|
|
|
printf "$@" >&2
|
|
|
|
}
|
|
|
|
|
2024-11-04 13:02:29 +00:00
|
|
|
function info() {
|
2024-10-31 13:18:42 +00:00
|
|
|
printf "$@" >&1
|
|
|
|
}
|
|
|
|
|
2024-10-31 11:41:40 +00:00
|
|
|
# Virtual machine removal
|
2024-11-04 13:02:29 +00:00
|
|
|
function remove() {
|
2024-10-31 11:41:40 +00:00
|
|
|
rm -r $ROOT_PATH/$OS_NAME
|
|
|
|
}
|
|
|
|
|
2024-10-31 11:47:03 +00:00
|
|
|
# Virtual machine start without a console
|
2024-11-04 13:02:29 +00:00
|
|
|
function start() {
|
2024-10-31 11:43:13 +00:00
|
|
|
qemu-system-x86_64 -boot c \
|
|
|
|
-display none \
|
|
|
|
-drive file=$IMG_PATH,format=raw \
|
|
|
|
-m $RAM -smp $CPU
|
|
|
|
}
|
2024-10-31 11:41:40 +00:00
|
|
|
|
2024-10-31 11:47:03 +00:00
|
|
|
# Virtual machine start with a console
|
2024-11-04 13:02:29 +00:00
|
|
|
function console() {
|
2024-10-31 11:47:03 +00:00
|
|
|
qemu-system-x86_64 -boot c \
|
|
|
|
-display curses \
|
|
|
|
-drive file=$IMG_PATH,format=raw \
|
|
|
|
-m $RAM -smp $CPU
|
|
|
|
}
|
|
|
|
|
2024-10-31 12:01:37 +00:00
|
|
|
# Virtual machine initialization with a console
|
2024-11-04 13:02:29 +00:00
|
|
|
function init() {
|
2024-10-31 13:18:42 +00:00
|
|
|
def_os_source
|
2024-10-31 11:56:44 +00:00
|
|
|
mkdir -p "$ROOT_PATH/$OS_NAME"
|
|
|
|
qemu-img create $IMG_PATH $DISK_SIZE
|
|
|
|
if [ ! -f $ISO_PATH ] && [ ! -z $INSTALL_IMG ]; then
|
2024-11-04 13:02:29 +00:00
|
|
|
info "Fetching $OSNAME installer at: $INSTALL_IMG"
|
2024-10-31 11:56:44 +00:00
|
|
|
curl -o $ISO_PATH $INSTALL_IMG
|
|
|
|
elif [ ! -z $INSTALL_IMG ]; then
|
2024-11-04 13:02:29 +00:00
|
|
|
info "Current INSTALL_IMG for $OS_NAME: $ISO_PATH"
|
2024-10-31 13:18:42 +00:00
|
|
|
else
|
2024-11-04 13:02:29 +00:00
|
|
|
error "Install image path is missing"
|
2024-10-31 11:56:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-31 12:01:37 +00:00
|
|
|
# Display usage
|
2024-11-04 13:02:29 +00:00
|
|
|
function usage() {
|
2024-10-31 12:01:37 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2024-09-25 21:03:33 +00:00
|
|
|
# Options
|
|
|
|
case $OPTION in
|
|
|
|
"init")
|
2024-11-04 13:02:29 +00:00
|
|
|
init
|
2024-09-25 21:03:33 +00:00
|
|
|
;;
|
|
|
|
"console")
|
2024-11-04 13:02:29 +00:00
|
|
|
console
|
2024-09-25 21:03:33 +00:00
|
|
|
;;
|
|
|
|
"start")
|
2024-11-04 13:02:29 +00:00
|
|
|
start
|
2024-09-25 21:03:33 +00:00
|
|
|
;;
|
2024-10-08 10:56:46 +00:00
|
|
|
"remove")
|
2024-11-04 13:02:29 +00:00
|
|
|
remove
|
2024-10-08 10:56:46 +00:00
|
|
|
;;
|
2024-09-25 21:03:33 +00:00
|
|
|
*)
|
2024-11-04 13:02:29 +00:00
|
|
|
usage
|
2024-09-25 21:03:33 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|