commit 5ad29a302cdd8c852ef43ce48ea8a02f8a2d6203 Author: Fierelier Date: Thu Feb 15 08:14:00 2024 +0100 Initial commit diff --git a/app/app b/app/app new file mode 100755 index 0000000..9a42c5a --- /dev/null +++ b/app/app @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -e +APP_CMD="$(realpath "$BASH_SOURCE")" +APP_DIR="$(dirname "$APP_CMD")" +source "$APP_DIR/env" +if [ -f "$APP_DIR/cmd/$1" ]; then + source "$APP_DIR/cmd/$1" + exit 0 +else + echo "ERROR: Action \"$1\" not found, use: \"$0\" help" + exit 1 +fi diff --git a/app/cmd/hash b/app/cmd/hash new file mode 100644 index 0000000..cad7ed7 --- /dev/null +++ b/app/cmd/hash @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -e +mm_hash=($(echo "$2" | md5sum)) +echo "$mm_hash" diff --git a/app/cmd/help b/app/cmd/help new file mode 100644 index 0000000..b5bb3d1 --- /dev/null +++ b/app/cmd/help @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +cat << EOF +* mountman list +List known partitions for humans: +* 8G [] (/dev/sda, disk) + * 8G [label] (/dev/sda1, crypto_LUKS) + * 0M [] (/dev/sda2, part) +... + +* mountman plist +List known partitions, programmatically: + mm_path='/dev/sda1'; mm_label='label'; mm_size='8G'; mm_type='part'; mm_format='crypto_LUKS'; mm_parent='/dev/sda'; mm_mountpath='' +... +(note: escape ' in strings with ") + +* mountman mount +Mount a partition + +* mountman umount +Unmount a partition + +* mountman eject +Eject the device the partition belongs to" +EOF diff --git a/app/cmd/list b/app/cmd/list new file mode 100644 index 0000000..157620a --- /dev/null +++ b/app/cmd/list @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set +e +"$APP_CMD" plist | while read mm_cmd; do + eval "$mm_cmd" + if ! [ "$mm_format" = "" ]; then + echo "[$mm_label] $mm_size - $mm_path ($mm_type/$mm_format)" + else + echo "[$mm_label] $mm_size - $mm_path ($mm_type/unknown)" + fi +done diff --git a/app/cmd/mount b/app/cmd/mount new file mode 100644 index 0000000..66b860d --- /dev/null +++ b/app/cmd/mount @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -e +eval "$("$APP_CMD" plist "$2")" + +if [ "$mm_format" = "crypto_LUKS" ]; then + mm_target="$("$APP_CMD" name "$mm_path")" + cryptsetup luksOpen "$mm_path" "$mm_target" + echo "/dev/mapper/$mm_target" + exit 0 +fi + +mm_target="$("$APP_CMD" path "$mm_path")" +mkdir -p "$mm_target" +mount "$mm_path" "$mm_target" +echo "$mm_target" diff --git a/app/cmd/name b/app/cmd/name new file mode 100644 index 0000000..8644fab --- /dev/null +++ b/app/cmd/name @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -e +eval "$("$APP_CMD" plist "$2")" +mm_label="${mm_label//-/_}" +mm_label="${mm_label//\//_}" +mm_uuid="$("$APP_CMD" hash "${mm_uuid}_${mm_partuuid}")" +mm_uuid="${mm_uuid//-/_}" +mm_uuid="${mm_uuid//\//_}" +echo "_$mm_label-$mm_uuid" diff --git a/app/cmd/path b/app/cmd/path new file mode 100644 index 0000000..cfc46d4 --- /dev/null +++ b/app/cmd/path @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -e +mm_name="$("$APP_CMD" name "$2")" +echo "/run/media/$mm_name" diff --git a/app/cmd/plist b/app/cmd/plist new file mode 100644 index 0000000..42e852c --- /dev/null +++ b/app/cmd/plist @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +set +e +function output_line() { + mm_path="$("$APP_CMD" standardize "$1")" + mm_label="$(blkid -o value -s LABEL "$mm_path")" + mm_size="$(lsblk -ndpo SIZE "$mm_path")" + mm_size="${mm_size// /}" + mm_type="$(lsblk -ndpo TYPE "$mm_path")" + mm_format="$(blkid -o value -s TYPE "$mm_path")" + mm_parent="$(lsblk -ndpo PKNAME "$mm_path")" # This is broken on devices of TYPE crypt. `lsblk -l -o NAME,PKNAME` yields PKNAME correctly. But `lsblk -l -o NAME,PKNAME /dev/mapper/cryptdisk` does not. + mm_mountpath="$(findmnt -no TARGET -S "$mm_path")" + mm_uuid="$(lsblk -ndpo UUID "$mm_path")" + mm_partuuid="$(lsblk -ndpo PARTUUID "$mm_path")" + mm_string="mm_path='${mm_path//\'/}'" + mm_string="${mm_string}; mm_label='${mm_label//\'/}'" + mm_string="${mm_string}; mm_size='${mm_size//\'/}'" + mm_string="${mm_string}; mm_type='${mm_type//\'/}'" + mm_string="${mm_string}; mm_format='${mm_format//\'/}'" + mm_string="${mm_string}; mm_parent='${mm_parent//\'/}'" + mm_string="${mm_string}; mm_mountpath='${mm_mountpath//\'/}'" + mm_string="${mm_string}; mm_uuid='${mm_uuid//\'/}'" + mm_string="${mm_string}; mm_partuuid='${mm_partuuid//\'/}'" + echo "$mm_string" +} + +if [ "$2" = "" ]; then + lsblk -npo PATH | while read mm_tpath; do + output_line "$mm_tpath" + done +else + output_line "$2" +fi diff --git a/app/cmd/standardize b/app/cmd/standardize new file mode 100644 index 0000000..40a3fa3 --- /dev/null +++ b/app/cmd/standardize @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +mm_path="$(realpath "$2")" +mm_path="$(lsblk -ndpo PATH "$mm_path")" +echo "$mm_path" diff --git a/app/cmd/umount b/app/cmd/umount new file mode 100644 index 0000000..9933dbe --- /dev/null +++ b/app/cmd/umount @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -e +eval "$("$APP_CMD" plist "$2")" + +if [ "$mm_format" = "crypto_LUKS" ]; then + mm_target="$("$APP_CMD" name "$mm_path")" + cryptsetup luksClose "$mm_target" + exit 0 +fi + +mm_target="$("$APP_CMD" path "$mm_path")" +set +e +umount "$mm_target" +set -e +rmdir "$mm_target" diff --git a/app/env b/app/env new file mode 100644 index 0000000..fdccc4d --- /dev/null +++ b/app/env @@ -0,0 +1,2 @@ +APP_NAME="mountman" +APP_INSTALL_PATH="/opt/$APP_NAME" diff --git a/install b/install new file mode 100755 index 0000000..361d399 --- /dev/null +++ b/install @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -e +APP_PATH="$(dirname "$(realpath "$BASH_SOURCE")")" +source "$APP_PATH/app/env" +if ! [ "$APP_UNINSTALL" = "1" ]; then + if [ -d "$APP_INSTALL_PATH" ]; then + APP_UNINSTALL=1 + APP_INSTALL=1 + fi +fi + +if [ "$APP_UNINSTALL" = "1" ]; then + echo "Uninstalling $APP_NAME ..." + rm -rf "$APP_INSTALL_PATH" + rm "/usr/local/bin/$APP_NAME" + if ! [ "$APP_INSTALL" = "1" ]; then + exit 0 + fi +fi + +mkdir -p "$APP_INSTALL_PATH" +cp -r "$APP_PATH/app/." "$APP_INSTALL_PATH" +ln -s "$APP_INSTALL_PATH/app" "/usr/local/bin/$APP_NAME" diff --git a/uninstall b/uninstall new file mode 100755 index 0000000..78b7441 --- /dev/null +++ b/uninstall @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +APP_PATH="$(dirname "$(realpath "$BASH_SOURCE")")" +export APP_UNINSTALL=1 +exec "$APP_PATH/install"