From 9048475efd2aa02b8ee2c4b028b3136564057928 Mon Sep 17 00:00:00 2001 From: BonzaiBrains <133261117+BonzaiBrains@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:41:40 +0100 Subject: [PATCH 01/10] add: remove function --- shemum | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/shemum b/shemum index 141698b..9c95ae1 100755 --- a/shemum +++ b/shemum @@ -42,6 +42,15 @@ case $OS_NAME in ;; esac +# Functions + +# Virtual machine removal +remove function { + rm -r $ROOT_PATH/$OS_NAME +} + + + # Options case $OPTION in # Initializes the VM and starts the installer image. @@ -76,7 +85,7 @@ case $OPTION in -m $RAM -smp $CPU ;; "remove") - rm -r $ROOT_PATH/$OS_NAME + remove ;; *) echo "Usage: shemum [init] " From 75420d0f32511ecb6265c78c51e2deddceabe00f Mon Sep 17 00:00:00 2001 From: BonzaiBrains <133261117+BonzaiBrains@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:43:13 +0100 Subject: [PATCH 02/10] add: start function --- shemum | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/shemum b/shemum index 9c95ae1..1b48fc9 100755 --- a/shemum +++ b/shemum @@ -49,7 +49,12 @@ remove function { rm -r $ROOT_PATH/$OS_NAME } - +start function { + qemu-system-x86_64 -boot c \ + -display none \ + -drive file=$IMG_PATH,format=raw \ + -m $RAM -smp $CPU +} # Options case $OPTION in @@ -79,10 +84,7 @@ case $OPTION in -m $RAM -smp $CPU ;; "start") - qemu-system-x86_64 -boot c \ - -display none \ - -drive file=$IMG_PATH,format=raw \ - -m $RAM -smp $CPU + start ;; "remove") remove From f95c0e5bfc1fcd4b9350c54591fec60ea099b1ac Mon Sep 17 00:00:00 2001 From: BonzaiBrains <133261117+BonzaiBrains@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:47:03 +0100 Subject: [PATCH 03/10] add: console function; style: function names For more information see: https://google.github.io/styleguide/shellguide.html#s7.1-function-names --- shemum | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/shemum b/shemum index 1b48fc9..1eb999c 100755 --- a/shemum +++ b/shemum @@ -45,17 +45,26 @@ esac # Functions # Virtual machine removal -remove function { +remove_func function { rm -r $ROOT_PATH/$OS_NAME } -start function { +# Virtual machine start without a console +start_func function { qemu-system-x86_64 -boot c \ -display none \ -drive file=$IMG_PATH,format=raw \ -m $RAM -smp $CPU } +# Virtual machine start with a console +console_func function { + qemu-system-x86_64 -boot c \ + -display curses \ + -drive file=$IMG_PATH,format=raw \ + -m $RAM -smp $CPU +} + # Options case $OPTION in # Initializes the VM and starts the installer image. @@ -78,16 +87,13 @@ case $OPTION in -m $RAM -smp $CPU ;; "console") - qemu-system-x86_64 -boot c \ - -display curses \ - -drive file=$IMG_PATH,format=raw \ - -m $RAM -smp $CPU + console_func ;; "start") - start + start_func ;; "remove") - remove + remove_func ;; *) echo "Usage: shemum [init] " From 442ff6bcbc557fee7f27613eba348e477ecfcd85 Mon Sep 17 00:00:00 2001 From: BonzaiBrains <133261117+BonzaiBrains@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:56:44 +0100 Subject: [PATCH 04/10] add: init function --- shemum | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/shemum b/shemum index 1eb999c..e9bf6e4 100755 --- a/shemum +++ b/shemum @@ -65,26 +65,30 @@ console_func function { -m $RAM -smp $CPU } +init_func function { + 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 + elif [ ! -z $INSTALL_IMG ]; then + echo "Current INSTALL_IMG for $OS_NAME: $ISO_PATH" + else: + echo "Install image path is missing" >&2 + exit 1 + 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 +} + # 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 - elif [ ! -z $INSTALL_IMG ]; then - echo "Current INSTALL_IMG for $OS_NAME: $ISO_PATH" - else: - echo "Install image path is missing" >&2 - exit 1 - 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 + init_func ;; "console") console_func From 1aaaf60b2d8874010fb87e6c7ae9d908d6f552c0 Mon Sep 17 00:00:00 2001 From: BonzaiBrains <133261117+BonzaiBrains@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:01:37 +0100 Subject: [PATCH 05/10] add: usage function --- shemum | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/shemum b/shemum index e9bf6e4..6036d6f 100755 --- a/shemum +++ b/shemum @@ -65,6 +65,7 @@ console_func function { -m $RAM -smp $CPU } +# Virtual machine initialization with a console init_func function { mkdir -p "$ROOT_PATH/$OS_NAME" qemu-img create $IMG_PATH $DISK_SIZE @@ -84,6 +85,15 @@ init_func function { -m $RAM -smp $CPU } +# Display usage +usage_func function { + printf "Usage: shemum [init] " + 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 # Initializes the VM and starts the installer image. @@ -100,11 +110,7 @@ case $OPTION in remove_func ;; *) - echo "Usage: shemum [init] " - echo " init initialize a virtual machine" - echo " start start a QEMU virtual machine without console output" - echo " console start a QEMU virtual machine with console output" - echo " remove remove virtual machine directory and all it's contents" + usage_func esac From 21404908d8593dd3502d9299d63c197c87a88cfe Mon Sep 17 00:00:00 2001 From: ahoemann Date: Thu, 31 Oct 2024 14:18:42 +0100 Subject: [PATCH 06/10] add: info error warn functions; fix: syntax error in function declaration --- shemum | 74 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/shemum b/shemum index 6036d6f..f60a354 100755 --- a/shemum +++ b/shemum @@ -3,6 +3,8 @@ # Author: Adrian Erik Hoemann # Date: 9.18.2024 +# TODO: Implement operating system installer variable list. + # Debug mode # To enable debug mode remove the comment. # Default: true @@ -28,29 +30,53 @@ IMG_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.img" # Sources virtual machine specific values . "$ROOT_PATH/$OS_NAME/.env" -# 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="" - ;; - *) - echo "$OS_NAME is not an option" >&2 - exit 1 - ;; -esac - # Functions +# Defines install image source +def_os_source() { + case $OS_NAME in + "openbsd") + INSTALL_IMG="https://cdn.openbsd.org/pub/OpenBSD/7.5/i386/cd75.iso" + ;; + "debian") + INSTALL_IMG="" + ;; + *) + err_func "INSTALL_IMG for $OS_NAME not found" + ;; + esac +} + +check_func() { + if [ -z $OS_NAME ] + err_func "Operating system is empty" + fi + if [ -z $INSTALL_IMG ] + info_func "$INSTALL_IMG is not set" + fi + +} + +err_func() { + printf "$@" >&2 + exit 1 +} + +warn_func() { + printf "$@" >&2 +} + +info_func() { + printf "$@" >&1 +} + # Virtual machine removal -remove_func function { +remove_func() { rm -r $ROOT_PATH/$OS_NAME } # Virtual machine start without a console -start_func function { +start_func() { qemu-system-x86_64 -boot c \ -display none \ -drive file=$IMG_PATH,format=raw \ @@ -58,7 +84,7 @@ start_func function { } # Virtual machine start with a console -console_func function { +console_func() { qemu-system-x86_64 -boot c \ -display curses \ -drive file=$IMG_PATH,format=raw \ @@ -66,17 +92,17 @@ console_func function { } # Virtual machine initialization with a console -init_func function { +init_func() { + def_os_source mkdir -p "$ROOT_PATH/$OS_NAME" qemu-img create $IMG_PATH $DISK_SIZE if [ ! -f $ISO_PATH ] && [ ! -z $INSTALL_IMG ]; then - echo $INSTALL_IMG + info_func "Fetching $OSNAME installer at: $INSTALL_IMG" curl -o $ISO_PATH $INSTALL_IMG elif [ ! -z $INSTALL_IMG ]; then - echo "Current INSTALL_IMG for $OS_NAME: $ISO_PATH" - else: - echo "Install image path is missing" >&2 - exit 1 + info_func "Current INSTALL_IMG for $OS_NAME: $ISO_PATH" + else + err_func "Install image path is missing" fi qemu-system-x86_64 -boot d \ -display curses \ @@ -86,7 +112,7 @@ init_func function { } # Display usage -usage_func function { +usage_func() { printf "Usage: shemum [init] " printf " init initialize a virtual machine" printf " start start a QEMU virtual machine without console output" From d4c06eda4368efc515610dadc5e83f635117b792 Mon Sep 17 00:00:00 2001 From: BonzaiBrains <133261117+BonzaiBrains@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:55:47 +0100 Subject: [PATCH 07/10] docs: fixed typos --- shemum | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/shemum b/shemum index f60a354..2be4da8 100755 --- a/shemum +++ b/shemum @@ -6,7 +6,6 @@ # TODO: Implement operating system installer variable list. # Debug mode -# To enable debug mode remove the comment. # Default: true DEBUG=true @@ -24,7 +23,7 @@ ROOT_PATH="$HOME/Machines" ISO_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.iso" IMG_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.img" -# Sources defualt values +# Sources default values . ".env" # Sources virtual machine specific values @@ -122,7 +121,6 @@ usage_func() { # Options case $OPTION in - # Initializes the VM and starts the installer image. "init") init_func ;; From d172c755e69c883992cb67e15d298810e0ba30a6 Mon Sep 17 00:00:00 2001 From: BonzaiBrains <133261117+BonzaiBrains@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:02:29 +0100 Subject: [PATCH 08/10] style: made functions more readable --- shemum | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/shemum b/shemum index 2be4da8..5d21bb5 100755 --- a/shemum +++ b/shemum @@ -32,7 +32,7 @@ IMG_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.img" # Functions # Defines install image source -def_os_source() { +function def_os_source() { case $OS_NAME in "openbsd") INSTALL_IMG="https://cdn.openbsd.org/pub/OpenBSD/7.5/i386/cd75.iso" @@ -41,41 +41,41 @@ def_os_source() { INSTALL_IMG="" ;; *) - err_func "INSTALL_IMG for $OS_NAME not found" + error "INSTALL_IMG for $OS_NAME not found" ;; esac } -check_func() { +function check() { if [ -z $OS_NAME ] - err_func "Operating system is empty" + error "Operating system is empty" fi if [ -z $INSTALL_IMG ] - info_func "$INSTALL_IMG is not set" + info "$INSTALL_IMG is not set" fi } -err_func() { +function error() { printf "$@" >&2 exit 1 } -warn_func() { +function warn() { printf "$@" >&2 } -info_func() { +function info() { printf "$@" >&1 } # Virtual machine removal -remove_func() { +function remove() { rm -r $ROOT_PATH/$OS_NAME } # Virtual machine start without a console -start_func() { +function start() { qemu-system-x86_64 -boot c \ -display none \ -drive file=$IMG_PATH,format=raw \ @@ -83,7 +83,7 @@ start_func() { } # Virtual machine start with a console -console_func() { +function console() { qemu-system-x86_64 -boot c \ -display curses \ -drive file=$IMG_PATH,format=raw \ @@ -91,17 +91,17 @@ console_func() { } # Virtual machine initialization with a console -init_func() { +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_func "Fetching $OSNAME installer at: $INSTALL_IMG" + info "Fetching $OSNAME installer at: $INSTALL_IMG" curl -o $ISO_PATH $INSTALL_IMG elif [ ! -z $INSTALL_IMG ]; then - info_func "Current INSTALL_IMG for $OS_NAME: $ISO_PATH" + info "Current INSTALL_IMG for $OS_NAME: $ISO_PATH" else - err_func "Install image path is missing" + error "Install image path is missing" fi qemu-system-x86_64 -boot d \ -display curses \ @@ -111,7 +111,7 @@ init_func() { } # Display usage -usage_func() { +function usage() { printf "Usage: shemum [init] " printf " init initialize a virtual machine" printf " start start a QEMU virtual machine without console output" @@ -122,19 +122,19 @@ usage_func() { # Options case $OPTION in "init") - init_func + init ;; "console") - console_func + console ;; "start") - start_func + start ;; "remove") - remove_func + remove ;; *) - usage_func + usage esac From ce339b69d223e86210809a32c2d69fb652751179 Mon Sep 17 00:00:00 2001 From: ahoemann Date: Thu, 7 Nov 2024 10:17:27 +0100 Subject: [PATCH 09/10] fix: general fixes for syntax and naming --- .sources | 3 +++ shemum | 52 ++++++++++++++++++++++------------------------------ 2 files changed, 25 insertions(+), 30 deletions(-) create mode 100644 .sources diff --git a/.sources b/.sources new file mode 100644 index 0000000..1a045a3 --- /dev/null +++ b/.sources @@ -0,0 +1,3 @@ +# Sources list +debian="" +openbsd="test" diff --git a/shemum b/shemum index 5d21bb5..a5c3a29 100755 --- a/shemum +++ b/shemum @@ -3,14 +3,12 @@ # 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] +if [ $DEBUG ]; then set +x fi @@ -24,58 +22,52 @@ ISO_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.iso" IMG_PATH="$ROOT_PATH/$OS_NAME/$OS_NAME.img" # Sources default values -. ".env" +#. ".env" # Sources virtual machine specific values -. "$ROOT_PATH/$OS_NAME/.env" +#. "$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 +source() { + while read LINE; do + if [ $LINE == $OS_NAME* ]; then + $OS_NAME=$LINE + fi + done < .sources } -function check() { - if [ -z $OS_NAME ] - error "Operating system is empty" +check() { + if [ -z $OS_NAME ]; then + error "Operating system is not defined" fi - if [ -z $INSTALL_IMG ] - info "$INSTALL_IMG is not set" + if [ -z $INSTALL_IMG ]; then + info "$OS_NAME source not set, please update your .sources file" fi } -function error() { +error() { printf "$@" >&2 exit 1 } -function warn() { +warn() { printf "$@" >&2 } -function info() { +info() { printf "$@" >&1 } # Virtual machine removal -function remove() { +remove() { rm -r $ROOT_PATH/$OS_NAME } # Virtual machine start without a console -function start() { +start() { qemu-system-x86_64 -boot c \ -display none \ -drive file=$IMG_PATH,format=raw \ @@ -83,7 +75,7 @@ function start() { } # Virtual machine start with a console -function console() { +console() { qemu-system-x86_64 -boot c \ -display curses \ -drive file=$IMG_PATH,format=raw \ @@ -91,7 +83,7 @@ function console() { } # Virtual machine initialization with a console -function init() { +init() { def_os_source mkdir -p "$ROOT_PATH/$OS_NAME" qemu-img create $IMG_PATH $DISK_SIZE @@ -111,7 +103,7 @@ function init() { } # Display usage -function usage() { +usage() { printf "Usage: shemum [init] " printf " init initialize a virtual machine" printf " start start a QEMU virtual machine without console output" From 580c9ceba5a60cec04b2d894d7137cc250e26e33 Mon Sep 17 00:00:00 2001 From: ahoemann Date: Thu, 7 Nov 2024 09:46:33 +0000 Subject: [PATCH 10/10] feat: added .env and .sources to make --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index f92bc3e..84d7737 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ install: cp $PWD/shemum /usr/bin/shemum + cp $PWD/.env $HOME/.env + cp $PWD/.sources $HOME/.sources clean: rm -f /usr/bin/shemum