From 7f3f8f1ba00d65973febedf67b22e657c89a52c0 Mon Sep 17 00:00:00 2001 From: Andre Date: Thu, 5 Sep 2024 16:47:53 -0400 Subject: [PATCH] Bin: fix installation script issues --- bin/format-drives.sh | 53 +++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/bin/format-drives.sh b/bin/format-drives.sh index b96bd66..00daa2c 100755 --- a/bin/format-drives.sh +++ b/bin/format-drives.sh @@ -3,6 +3,11 @@ set -e +if [ "$(id -u)" != "0" ]; then + echo "This script must be run as root" 1>&2 + exit 1 +fi + # Configuration parameters ask_root_password=false # Whether to prompt for a root user password boot_partition="" # The drive to install the bootloader to @@ -19,34 +24,36 @@ function usage() { exit 2 } +# Argument processing logic shamelessly stolen from https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash +POSITIONAL_ARGS=() while [[ $# -gt 0 ]]; do case "$1" in - --ask-root-password|-a) - ask_root_password=true - shift - ;; - --boot|-b) - boot_partition=$1 - shift - ;; - --luks|-l) - luks_partition=1 - shift - ;; - --help|-h) - usage - shift - ;; - *) - break - ;; + --ask-root-password|-a) + ask_root_password=true + shift + ;; + --boot|-b) + boot_partition="$2" + shift + shift + ;; + --luks|-l) + luks_partition="$2" + shift + shift + ;; + --help|-h) + usage + shift + ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift # past argument + ;; esac done -if [ "$(id -u)" != "0" ]; then - echo "This script must be run as root" 1>&2 - exit 1 -fi +set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters cryptsetup --label=nixos-crypt --type=luks2 luksFormat $luks_partition cryptsetup luksOpen $luks_partition nixos-crypt