2021-02-13 11:28:59 -05:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Toggle mount status of a device using pmount
|
|
|
|
# If the device is not mounted, it will be mounted.
|
|
|
|
# If the device is mounted, it will be unmounted and powered down.
|
|
|
|
#
|
2021-06-02 01:58:01 -04:00
|
|
|
# Dependencies: lsblk, pmount
|
2021-02-13 11:28:59 -05:00
|
|
|
#
|
2021-06-02 01:58:01 -04:00
|
|
|
# Usage: Runs `lsblk` on 'l', exits on 'Return`.
|
|
|
|
#
|
|
|
|
# Notes:
|
|
|
|
# - The script uses Linux-specific lsblk to list block devices. Alternatives:
|
2021-02-13 11:28:59 -05:00
|
|
|
# macOS: "diskutil list"
|
|
|
|
# BSD: "geom disk list"
|
2021-06-02 01:58:01 -04:00
|
|
|
# - The script uses udisksctl (from udisks2) to power down devices. This is also Linux-specific.
|
|
|
|
# Users on non-Linux platforms can comment it and use an alterntive to power-down disks.
|
2021-02-13 11:28:59 -05:00
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
|
|
|
# Author: Arun Prakash Jana
|
|
|
|
|
2021-06-02 01:58:01 -04:00
|
|
|
prompt="device name [e.g. sdXn] ('l'ist, 'q'uit): "
|
2021-02-13 11:28:59 -05:00
|
|
|
|
|
|
|
lsblk
|
|
|
|
|
|
|
|
printf "\nEnsure you aren't still in the mounted device.\n"
|
|
|
|
printf "%s" "$prompt"
|
|
|
|
read -r dev
|
|
|
|
|
2021-06-02 01:58:01 -04:00
|
|
|
while [ -n "$dev" ]
|
2021-02-13 11:28:59 -05:00
|
|
|
do
|
|
|
|
if [ "$dev" = "l" ]; then
|
|
|
|
lsblk
|
|
|
|
elif [ "$dev" = "q" ]; then
|
|
|
|
exit
|
|
|
|
else
|
|
|
|
if grep -qs "$dev " /proc/mounts; then
|
|
|
|
sync
|
|
|
|
if pumount "$dev"
|
|
|
|
then
|
|
|
|
echo "$dev" unmounted.
|
|
|
|
if udisksctl power-off -b /dev/"$dev"
|
|
|
|
then
|
|
|
|
echo "$dev" ejected.
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
pmount "$dev"
|
|
|
|
echo "$dev" mounted to "$(lsblk -n /dev/"$dev" | rev | cut -d' ' -f1 | rev)".
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
printf "%s" "$prompt"
|
|
|
|
read -r dev
|
|
|
|
done
|