Compare commits

...

3 commits

View file

@ -19,6 +19,7 @@ MOUNT_FOLDER="/run/media/${CURRENT_USER}"
ALLOW_UUID="0" ALLOW_UUID="0"
ALLOW_LABEL="0" ALLOW_LABEL="0"
LABEL_BY="LABEL"
print_help () { print_help () {
cat /dev/stdin << EOF cat /dev/stdin << EOF
@ -31,6 +32,8 @@ 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
-U --label-uuid Use UUID instead of drive label for mountpoint name
-ro --read-only Mounts target as read only -ro --read-only Mounts target as read only
EOF EOF
@ -111,19 +114,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_BY}" "${TARGET}") ]] && LABEL=$(blkid -o value -s "${LABEL_BY}" "${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 +149,12 @@ for arg in "$@"; do
"-l" | "--label") "-l" | "--label")
ALLOW_LABEL="1" ALLOW_LABEL="1"
;; ;;
"-d" | "--dry-run")
DRY_RUN="1"
;;
"-U" | "--label-uuid")
LABEL_BY="UUID"
;;
"-ro" | "--read-only") "-ro" | "--read-only")
MOUNT_OPTS+=",ro" MOUNT_OPTS+=",ro"
;; ;;
@ -160,7 +171,7 @@ fi
MAPPED_NAME="" MAPPED_NAME=""
LABEL=$(blkid -o value -s LABEL "${TARGET}") LABEL=$(blkid -o value -s "${LABEL_BY}" "${TARGET}")
[[ -z $LABEL ]] && LABEL=$(basename "${TARGET}") [[ -z $LABEL ]] && LABEL=$(basename "${TARGET}")
TYPE=$(blkid -o value -s TYPE "${TARGET}") TYPE=$(blkid -o value -s TYPE "${TARGET}")
@ -178,28 +189,34 @@ 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 \
mountpoint "${MOUNT_FOLDER}/${LABEL}" 2>&1 &>/dev/null || \
findmnt -n -o SOURCE "${TARGET}" 2>&1 &>/dev/null || \
! 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"