qmnt - Added dry run mode for testing

This commit is contained in:
Logan G 2024-09-25 12:35:15 -06:00
parent d9e58a5c3b
commit d4736bc591
Signed by: logan
GPG key ID: E328528C921E7A7A

View file

@ -31,6 +31,7 @@ Options:
-p --permissions Force mount permissions to current user
-u --uuid Allow UUIDs as the target
-l --label Allow labels as the target
-d --dry-run Does nothing
-ro --read-only Mounts target as read only
EOF
@ -111,6 +112,7 @@ get_mapped () {
echo "Target \"${TARGET}\" is not unlocked"
if [[ -z $DRY_RUN ]]; then
MAPPED_NAME=$(blkid -o value -s UUID "${TARGET}")
if ! cryptsetup open "${TARGET}" "${MAPPED_NAME}"; then
echo -e "\e[1;31mCould not unlock target \"${TARGET}\"!""\e[0m" 1>&2
@ -124,6 +126,7 @@ get_mapped () {
[[ ! -z $(blkid -o value -s LABEL "${TARGET}") ]] && LABEL=$(blkid -o value -s LABEL "${TARGET}")
TYPE=$(blkid -o value -s TYPE "${TARGET}" || true) # Fuck you bash
fi
}
if [[ "$#" -eq 0 ]]; then
@ -144,6 +147,9 @@ for arg in "$@"; do
"-l" | "--label")
ALLOW_LABEL="1"
;;
"-d" | "--dry-run")
DRY_RUN="1"
;;
"-ro" | "--read-only")
MOUNT_OPTS+=",ro"
;;
@ -178,9 +184,11 @@ else
MOUNT_OPTS="${DEFAULT_MOUNT_OPTS["$TYPE"]}""${MOUNT_OPTS}"
fi
[[ ! -d "${MOUNT_FOLDER}" ]] && mkdir "${MOUNT_FOLDER}"
[[ ! -d "${MOUNT_FOLDER}/${LABEL}" ]] && mkdir "${MOUNT_FOLDER}/${LABEL}"
[[ ! -d "${MOUNT_FOLDER}" && -z $DRY_RUN ]] && mkdir "${MOUNT_FOLDER}"
[[ ! -d "${MOUNT_FOLDER}/${LABEL}" && -z $DRY_RUN ]] && mkdir "${MOUNT_FOLDER}/${LABEL}"
if [[ -z $DRY_RUN ]]; then
if ! mount -o "${MOUNT_OPTS}" --source "${TARGET}" --target "${MOUNT_FOLDER}/${LABEL}"; then
echo -e "\e[1;31mFailed to mount \"${TARGET}\"!""\e[0m" 1>&2
exit 1
@ -201,5 +209,6 @@ fi
[ -b /dev/mapper/"${2}" ] && cryptsetup close /dev/mapper/"${2}"
' -- "${MOUNT_FOLDER}/${LABEL}" "${MAPPED_NAME}" >/dev/null 2>&1 &
) &
fi
echo -e "\e[1;32mDevice \"${TARGET}\" successfully mounted at \"${MOUNT_FOLDER}/${LABEL}\"""\e[0m"