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 -a|
--all) opt_all
=1; shift ;;
23 -h|
--help) opt_help
=1; shift ;;
24 -q|
--quiet) opt_quiet
=$
(($opt_quiet + 1)); shift ;;
25 -u|
--umount) opt_umount
=1; shift ;;
26 -v|
--verbose) opt_verbose
=$
(($opt_verbose + 1)); shift ;;
27 --version) echo $progname $VERSION; exit 0 ;;
30 if printf '%s\n' "$1" |
grep -q ^
-; then
31 echo "$progname: $1: Unknown option" >&2
39 opt_verbose
=$
(($opt_verbose - $opt_quiet))
41 [ -e "$HOME/.mdiskrc" ] && .
"$HOME/.mdiskrc"
44 T_GREEN
=$
(tput setaf
2)
48 if test "$opt_help" = "1"; then
49 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
52 Mount a LUKS-encrypted disk under /media/.
54 Usage: $progname [options] DISKNAME
59 Loop through all directory names under /media/ and mount or unmount
60 all corresponding disks.
64 Be more quiet. Can be repeated to increase silence.
66 Increase level of verbosity. Can be repeated.
70 Print version information.
75 Full path to text file with passphrase for encrypted drives.
80 Init file. A regular shell script that can be used to initialize
81 environment variables and run commands before normal program
89 test $1 -gt $opt_verbose && return;
91 echo "$progname: $*" >&2
95 echo "$progname: $T_BOLD$T_RED$*$T_RESET" >&2
100 sudo cryptsetup status
$disk |
grep -q "/dev/mapper/$disk is active"
104 sudo blkid |
grep -q "LABEL=\"$1\"" && return 0
109 df |
grep -q "/media/$disk\$" && return 0
114 echo $
(echo "$1" |
sed 's.^/media/..; s./$..')
117 disk
=$
(strip_diskname
$1)
119 if [ "$opt_all" = "1" ]; then
120 for f
in /media
/*; do
121 is_in_blkid $
(strip_diskname
$f) ||
continue
122 if [ "$opt_umount" = "1" ]; then
131 disk
=$
(echo "$1" |
sed 's.^/media/..; s./$..')
132 [ -z "$disk" ] && err No disk specified
134 is_in_blkid
$disk || err
$disk: Disk not found
in blkid
136 if [ "$opt_umount" != "1" ]; then
137 is_mounted
$disk && {
138 msg
0 $T_BOLD$T_GREEN$disk: Already mounted
$T_RESET
141 device
=$
(blkid
-L $disk)
142 msg
1 device
= \"$device\"
143 [ -z "$device" ] && err Cannot get device
for $disk
144 echo "$device" |
grep -q ^
/dev
/ || err
$device: Malformed device name
146 || sudo cryptsetup luksOpen \
147 $
([ -n "$MDISK_KEY_FILE" ] && echo "--key-file $MDISK_KEY_FILE") \
148 $device $disk && mount
/media
/$disk
150 && msg
0 $T_BOLD$T_GREEN$disk mounted on
/media
/$disk$T_RESET \
151 || err
$disk: mount failed
153 is_mounted
$disk && umount
/media
/$disk
154 is_mounted
$disk && err
/media
/$disk: umount failed
155 sudo cryptsetup luksClose
$disk
157 is_active
$disk && err
$disk is still active
158 msg
0 $T_BOLD$T_GREEN$disk is unmounted and closed
$T_RESET
161 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :