Initial commit

This commit is contained in:
Fierelier 2024-02-15 08:14:00 +01:00
commit 5ad29a302c
13 changed files with 160 additions and 0 deletions

12
app/app Executable file
View File

@ -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

4
app/cmd/hash Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -e
mm_hash=($(echo "$2" | md5sum))
echo "$mm_hash"

24
app/cmd/help Normal file
View File

@ -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 </path>
Mount a partition
* mountman umount </path>
Unmount a partition
* mountman eject </path>
Eject the device the partition belongs to"
EOF

10
app/cmd/list Normal file
View File

@ -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

15
app/cmd/mount Normal file
View File

@ -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"

9
app/cmd/name Normal file
View File

@ -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"

4
app/cmd/path Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -e
mm_name="$("$APP_CMD" name "$2")"
echo "/run/media/$mm_name"

32
app/cmd/plist Normal file
View File

@ -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

5
app/cmd/standardize Normal file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -e
mm_path="$(realpath "$2")"
mm_path="$(lsblk -ndpo PATH "$mm_path")"
echo "$mm_path"

15
app/cmd/umount Normal file
View File

@ -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"

2
app/env Normal file
View File

@ -0,0 +1,2 @@
APP_NAME="mountman"
APP_INSTALL_PATH="/opt/$APP_NAME"

23
install Executable file
View File

@ -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"

5
uninstall Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -e
APP_PATH="$(dirname "$(realpath "$BASH_SOURCE")")"
export APP_UNINSTALL=1
exec "$APP_PATH/install"