mdisk: Delete unnecessary use of `strip_diskname` and use it elsewhere
[sunny256-utils.git] / mdisk
blob065d82877f194009bf124e0615bd8d80c3e6e8fa
1 #!/bin/sh
3 #==============================================================================
4 # mdisk
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 #==============================================================================
13 progname=mdisk
14 VERSION=0.2.0
16 opt_help=0
17 opt_quiet=0
18 opt_umount=0
19 opt_verbose=0
20 while test -n "$1"; do
21 case "$1" in
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 ;;
28 --) shift; break ;;
30 if printf '%s\n' "$1" | grep -q ^-; then
31 echo "$progname: $1: Unknown option" >&2
32 exit 1
33 else
34 break
36 break ;;
37 esac
38 done
39 opt_verbose=$(($opt_verbose - $opt_quiet))
41 [ -e "$HOME/.mdiskrc" ] && . "$HOME/.mdiskrc"
43 T_BOLD=$(tput bold)
44 T_GREEN=$(tput setaf 2)
45 T_RED=$(tput setaf 1)
46 T_RESET=$(tput sgr0)
48 if test "$opt_help" = "1"; then
49 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
50 cat <<END
52 Mount a LUKS-encrypted disk under /media/.
54 Usage: $progname [options] DISKNAME
56 Options:
58 -a, --all
59 Loop through all directory names under /media/ and mount or unmount
60 all corresponding disks.
61 -h, --help
62 Show this help.
63 -q, --quiet
64 Be more quiet. Can be repeated to increase silence.
65 -v, --verbose
66 Increase level of verbosity. Can be repeated.
67 -u, --umount
68 Unmount the disk.
69 --version
70 Print version information.
72 Environment variables
74 MDISK_KEY_FILE
75 Full path to text file with passphrase for encrypted drives.
77 Files
79 \$HOME/.mdiskrc
80 Init file. A regular shell script that can be used to initialize
81 environment variables and run commands before normal program
82 execution.
84 END
85 exit 0
88 msg() {
89 test $1 -gt $opt_verbose && return;
90 shift
91 echo "$progname: $*" >&2
94 err() {
95 echo "$progname: $T_BOLD$T_RED$*$T_RESET" >&2
96 exit 1
99 is_active() {
100 sudo cryptsetup status $disk | grep -q "/dev/mapper/$disk is active"
103 is_in_blkid() {
104 sudo blkid | grep -q "LABEL=\"$1\"" && return 0
105 return 1
108 is_mounted() {
109 df | grep -q "/media/$disk\$" && return 0
110 return 1
113 strip_diskname() {
114 echo $(echo "$1" | sed 's.^/media/..; s./$..')
117 if [ "$opt_all" = "1" ]; then
118 for f in /media/*; do
119 is_in_blkid $(strip_diskname $f) || continue
120 if [ "$opt_umount" = "1" ]; then
121 $0 -u $f
122 else
123 $0 $f
125 done
126 exit
129 disk=$(strip_diskname $1)
130 [ -z "$disk" ] && err No disk specified
132 is_in_blkid $disk || err $disk: Disk not found in blkid
134 if [ "$opt_umount" != "1" ]; then
135 is_mounted $disk && {
136 msg 0 $T_BOLD$T_GREEN$disk: Already mounted$T_RESET
137 exit 0
139 device=$(blkid -L $disk)
140 msg 1 device = \"$device\"
141 [ -z "$device" ] && err Cannot get device for $disk
142 echo "$device" | grep -q ^/dev/ || err $device: Malformed device name
143 is_active $disk \
144 || sudo cryptsetup luksOpen \
145 $([ -n "$MDISK_KEY_FILE" ] && echo "--key-file $MDISK_KEY_FILE") \
146 $device $disk && mount /media/$disk
147 is_mounted $disk \
148 && msg 0 $T_BOLD$T_GREEN$disk mounted on /media/$disk$T_RESET \
149 || err $disk: mount failed
150 else
151 is_mounted $disk && umount /media/$disk
152 is_mounted $disk && err /media/$disk: umount failed
153 sudo cryptsetup luksClose $disk
154 sync
155 is_active $disk && err $disk is still active
156 msg 0 $T_BOLD$T_GREEN$disk is unmounted and closed$T_RESET
159 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :