2024-09-25 21:03:33 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
#
|
|
|
|
# Author: Adrian Erik Hoemann
|
|
|
|
# Date: 9.18.2024
|
|
|
|
|
|
|
|
# Debug mode
|
|
|
|
# To enable debug mode remove the comment.
|
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-10-08 10:56:46 +00:00
|
|
|
# Sources defualt values
|
|
|
|
. ".env"
|
|
|
|
|
|
|
|
# Sources virtual machine specific values
|
|
|
|
. "$ROOT_PATH/$OS_NAME/.env"
|
2024-09-25 21:03:33 +00:00
|
|
|
|
|
|
|
# Defines install image repository.
|
|
|
|
case $OS_NAME in
|
|
|
|
"openbsd")
|
|
|
|
INSTALL_IMG="https://cdn.openbsd.org/pub/OpenBSD/7.5/i386/cd75.iso"
|
|
|
|
;;
|
|
|
|
"debian")
|
|
|
|
INSTALL_IMG=""
|
|
|
|
;;
|
2024-09-25 22:07:04 +00:00
|
|
|
*)
|
|
|
|
echo "$OS_NAME is not an option" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
2024-09-25 21:03:33 +00:00
|
|
|
esac
|
|
|
|
|
2024-10-31 11:41:40 +00:00
|
|
|
# Functions
|
|
|
|
|
|
|
|
# Virtual machine removal
|
|
|
|
remove function {
|
|
|
|
rm -r $ROOT_PATH/$OS_NAME
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-25 21:03:33 +00:00
|
|
|
# Options
|
|
|
|
case $OPTION in
|
|
|
|
# Initializes the VM and starts the installer image.
|
|
|
|
"init")
|
|
|
|
mkdir -p "$ROOT_PATH/$OS_NAME"
|
|
|
|
qemu-img create $IMG_PATH $DISK_SIZE
|
|
|
|
if [ ! -f $ISO_PATH ] && [ ! -z $INSTALL_IMG ]; then
|
|
|
|
echo $INSTALL_IMG
|
|
|
|
curl -o $ISO_PATH $INSTALL_IMG
|
2024-09-25 22:07:04 +00:00
|
|
|
elif [ ! -z $INSTALL_IMG ]; then
|
|
|
|
echo "Current INSTALL_IMG for $OS_NAME: $ISO_PATH"
|
2024-09-25 21:03:33 +00:00
|
|
|
else:
|
2024-09-25 22:07:04 +00:00
|
|
|
echo "Install image path is missing" >&2
|
2024-09-25 21:03:33 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
qemu-system-x86_64 -boot d \
|
|
|
|
-display curses \
|
2024-09-25 22:07:04 +00:00
|
|
|
-drive file=$ISO_PATH,media=cdrom \
|
|
|
|
-drive file=$IMG_PATH,format=raw \
|
2024-09-25 21:03:33 +00:00
|
|
|
-m $RAM -smp $CPU
|
|
|
|
;;
|
|
|
|
"console")
|
|
|
|
qemu-system-x86_64 -boot c \
|
|
|
|
-display curses \
|
2024-09-25 22:07:04 +00:00
|
|
|
-drive file=$IMG_PATH,format=raw \
|
2024-09-25 21:03:33 +00:00
|
|
|
-m $RAM -smp $CPU
|
|
|
|
;;
|
|
|
|
"start")
|
|
|
|
qemu-system-x86_64 -boot c \
|
2024-09-25 22:07:04 +00:00
|
|
|
-display none \
|
|
|
|
-drive file=$IMG_PATH,format=raw \
|
2024-09-25 21:03:33 +00:00
|
|
|
-m $RAM -smp $CPU
|
|
|
|
;;
|
2024-10-08 10:56:46 +00:00
|
|
|
"remove")
|
2024-10-31 11:41:40 +00:00
|
|
|
remove
|
2024-10-08 10:56:46 +00:00
|
|
|
;;
|
2024-09-25 21:03:33 +00:00
|
|
|
*)
|
|
|
|
echo "Usage: shemum [init] <virtual machine name> <disk size>"
|
2024-10-08 10:56:46 +00:00
|
|
|
echo " init initialize a virtual machine"
|
2024-09-25 22:07:04 +00:00
|
|
|
echo " start start a QEMU virtual machine without console output"
|
2024-10-08 10:56:46 +00:00
|
|
|
echo " console start a QEMU virtual machine with console output"
|
|
|
|
echo " remove remove virtual machine directory and all it's contents"
|
2024-09-25 21:03:33 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
|