3 #==============================================================================
5 # File ID: 0548a2d2-ea04-11ed-b5e0-83850402c3ce
7 # Mount a LUKS-encrypted disk under /media/
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #==============================================================================
20 while test -n "$1"; do
22 -h|
--help) opt_help
=1; shift ;;
23 -q|
--quiet) opt_quiet
=$
(($opt_quiet + 1)); shift ;;
24 -u|
--umount) opt_umount
=1; shift ;;
25 -v|
--verbose) opt_verbose
=$
(($opt_verbose + 1)); shift ;;
26 --version) echo $progname $VERSION; exit 0 ;;
29 if printf '%s\n' "$1" |
grep -q ^
-; then
30 echo "$progname: $1: Unknown option" >&2
38 opt_verbose
=$
(($opt_verbose - $opt_quiet))
40 [ -e "$HOME/.mdiskrc" ] && .
"$HOME/.mdiskrc"
43 T_GREEN
=$
(tput setaf
2)
47 if test "$opt_help" = "1"; then
48 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
51 Mount a LUKS-encrypted disk under /media/.
53 Usage: $progname [options] DISKNAME
60 Be more quiet. Can be repeated to increase silence.
62 Increase level of verbosity. Can be repeated.
66 Print version information.
71 Full path to text file with passphrase for encrypted drives.
76 Init file. A regular shell script that can be used to initialize
77 environment variables and run commands before normal program
85 test $1 -gt $opt_verbose && return;
87 echo "$progname: $*" >&2
91 echo "$progname: $T_BOLD$T_RED$*$T_RESET" >&2
96 sudo cryptsetup status
$disk |
grep -q "/dev/mapper/$disk is active"
100 df |
grep -q "/media/$disk\$" && return 0
104 disk
=$
(echo "$1" |
sed 's.^/media/..; s./$..')
105 [ -z "$disk" ] && err No disk specified
107 sudo blkid |
grep -q "LABEL=\"$disk\"" || err
$disk: Disk not found
in blkid
109 if [ "$opt_umount" != "1" ]; then
110 is_mounted
$disk && err
$disk: Already mounted
111 device
=$
(blkid
-L $disk)
112 msg
1 device
= \"$device\"
113 [ -z "$device" ] && err Cannot get device
for $disk
114 echo "$device" |
grep -q ^
/dev
/ || err
$device: Malformed device name
116 || sudo cryptsetup luksOpen \
117 $
([ -n "$MDISK_KEY_FILE" ] && echo "--key-file $MDISK_KEY_FILE") \
118 $device $disk && mount
/media
/$disk
120 && msg
0 $T_BOLD$T_GREEN$disk mounted on
/media
/$disk$T_RESET \
121 || err
$disk: mount failed
123 is_mounted
$disk && umount
/media
/$disk
124 is_mounted
$disk && err
/media
/$disk: umount failed
125 sudo cryptsetup luksClose
$disk
127 is_active
$disk && err
$disk is still active
128 msg
0 $T_BOLD$T_GREEN$disk is unmounted and closed
$T_RESET
131 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :