sumdur: Define 1 year as 365.2425 days instead of 365.25
[sunny256-utils.git] / mdisk
blob74bd09a3b02821a01a945730c2dbd576079725e9
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.1.7
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 -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 ;;
27 --) shift; break ;;
29 if printf '%s\n' "$1" | grep -q ^-; then
30 echo "$progname: $1: Unknown option" >&2
31 exit 1
32 else
33 break
35 break ;;
36 esac
37 done
38 opt_verbose=$(($opt_verbose - $opt_quiet))
40 [ -e "$HOME/.mdiskrc" ] && . "$HOME/.mdiskrc"
42 T_BOLD=$(tput bold)
43 T_GREEN=$(tput setaf 2)
44 T_RED=$(tput setaf 1)
45 T_RESET=$(tput sgr0)
47 if test "$opt_help" = "1"; then
48 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
49 cat <<END
51 Mount a LUKS-encrypted disk under /media/.
53 Usage: $progname [options] DISKNAME
55 Options:
57 -h, --help
58 Show this help.
59 -q, --quiet
60 Be more quiet. Can be repeated to increase silence.
61 -v, --verbose
62 Increase level of verbosity. Can be repeated.
63 -u, --umount
64 Unmount the disk.
65 --version
66 Print version information.
68 Environment variables
70 MDISK_KEY_FILE
71 Full path to text file with passphrase for encrypted drives.
73 Files
75 \$HOME/.mdiskrc
76 Init file. A regular shell script that can be used to initialize
77 environment variables and run commands before normal program
78 execution.
80 END
81 exit 0
84 msg() {
85 test $1 -gt $opt_verbose && return;
86 shift
87 echo "$progname: $*" >&2
90 err() {
91 echo "$progname: $T_BOLD$T_RED$*$T_RESET" >&2
92 exit 1
95 is_active() {
96 sudo cryptsetup status $disk | grep -q "/dev/mapper/$disk is active"
99 is_mounted() {
100 df | grep -q "/media/$disk\$" && return 0
101 return 1
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
115 is_active $disk \
116 || sudo cryptsetup luksOpen \
117 $([ -n "$MDISK_KEY_FILE" ] && echo "--key-file $MDISK_KEY_FILE") \
118 $device $disk && mount /media/$disk
119 is_mounted $disk \
120 && msg 0 $T_BOLD$T_GREEN$disk mounted on /media/$disk$T_RESET \
121 || err $disk: mount failed
122 else
123 is_mounted $disk && umount /media/$disk
124 is_mounted $disk && err /media/$disk: umount failed
125 sudo cryptsetup luksClose $disk
126 sync
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 :