logging: Check if typescripts with a similar name already exist
[sunny256-utils.git] / mdisk
blob1e594eb8c16b52c8b8c1db3975ae5a6a0e86d380
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 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
123 $0 -u $f
124 else
125 $0 $f
127 done
128 exit
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
139 exit 0
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
145 is_active $disk \
146 || sudo cryptsetup luksOpen \
147 $([ -n "$MDISK_KEY_FILE" ] && echo "--key-file $MDISK_KEY_FILE") \
148 $device $disk && mount /media/$disk
149 is_mounted $disk \
150 && msg 0 $T_BOLD$T_GREEN$disk mounted on /media/$disk$T_RESET \
151 || err $disk: mount failed
152 else
153 is_mounted $disk && umount /media/$disk
154 is_mounted $disk && err /media/$disk: umount failed
155 sudo cryptsetup luksClose $disk
156 sync
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 :