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 -p --permissions Force mount permissions to current user
-u --uuid Allow UUIDs as the target -u --uuid Allow UUIDs as the target
-l --label Allow labels as the target -l --label Allow labels as the target
-d --dry-run Does nothing
-ro --read-only Mounts target as read only -ro --read-only Mounts target as read only
EOF EOF
@ -111,19 +112,21 @@ get_mapped () {
echo "Target \"${TARGET}\" is not unlocked" echo "Target \"${TARGET}\" is not unlocked"
MAPPED_NAME=$(blkid -o value -s UUID "${TARGET}") if [[ -z $DRY_RUN ]]; then
if ! cryptsetup open "${TARGET}" "${MAPPED_NAME}"; then MAPPED_NAME=$(blkid -o value -s UUID "${TARGET}")
echo -e "\e[1;31mCould not unlock target \"${TARGET}\"!""\e[0m" 1>&2 if ! cryptsetup open "${TARGET}" "${MAPPED_NAME}"; then
exit 1 echo -e "\e[1;31mCould not unlock target \"${TARGET}\"!""\e[0m" 1>&2
exit 1
fi
TARGET="/dev/mapper/${MAPPED_NAME}"
trap "cryptsetup close ${TARGET}" EXIT
[[ ! -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 fi
TARGET="/dev/mapper/${MAPPED_NAME}"
trap "cryptsetup close ${TARGET}" EXIT
[[ ! -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
} }
if [[ "$#" -eq 0 ]]; then if [[ "$#" -eq 0 ]]; then
@ -144,6 +147,9 @@ for arg in "$@"; do
"-l" | "--label") "-l" | "--label")
ALLOW_LABEL="1" ALLOW_LABEL="1"
;; ;;
"-d" | "--dry-run")
DRY_RUN="1"
;;
"-ro" | "--read-only") "-ro" | "--read-only")
MOUNT_OPTS+=",ro" MOUNT_OPTS+=",ro"
;; ;;
@ -178,28 +184,31 @@ else
MOUNT_OPTS="${DEFAULT_MOUNT_OPTS["$TYPE"]}""${MOUNT_OPTS}" MOUNT_OPTS="${DEFAULT_MOUNT_OPTS["$TYPE"]}""${MOUNT_OPTS}"
fi fi
[[ ! -d "${MOUNT_FOLDER}" ]] && mkdir "${MOUNT_FOLDER}" [[ ! -d "${MOUNT_FOLDER}" && -z $DRY_RUN ]] && mkdir "${MOUNT_FOLDER}"
[[ ! -d "${MOUNT_FOLDER}/${LABEL}" ]] && mkdir "${MOUNT_FOLDER}/${LABEL}" [[ ! -d "${MOUNT_FOLDER}/${LABEL}" && -z $DRY_RUN ]] && mkdir "${MOUNT_FOLDER}/${LABEL}"
if ! mount -o "${MOUNT_OPTS}" --source "${TARGET}" --target "${MOUNT_FOLDER}/${LABEL}"; then
echo -e "\e[1;31mFailed to mount \"${TARGET}\"!""\e[0m" 1>&2 if [[ -z $DRY_RUN ]]; then
exit 1 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
fi
(
set +e
trap '' SIGHUP SIGTERM
setsid sh -c '
while mountpoint "${1}"; do
sleep 1
done
rmdir "${1}"
[ -b /dev/mapper/"${2}" ] && cryptsetup close /dev/mapper/"${2}"
' -- "${MOUNT_FOLDER}/${LABEL}" "${MAPPED_NAME}" >/dev/null 2>&1 &
) &
fi fi
(
set +e
trap '' SIGHUP SIGTERM
setsid sh -c '
while mountpoint "${1}"; do
sleep 1
done
rmdir "${1}"
[ -b /dev/mapper/"${2}" ] && cryptsetup close /dev/mapper/"${2}"
' -- "${MOUNT_FOLDER}/${LABEL}" "${MAPPED_NAME}" >/dev/null 2>&1 &
) &
echo -e "\e[1;32mDevice \"${TARGET}\" successfully mounted at \"${MOUNT_FOLDER}/${LABEL}\"""\e[0m" echo -e "\e[1;32mDevice \"${TARGET}\" successfully mounted at \"${MOUNT_FOLDER}/${LABEL}\"""\e[0m"