`mdisk` works
[sunny256-utils.git] / mdisk
blobf712a7fa08a4834723fda8e8899bb5af9b1377bb
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.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 -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 if test "$opt_help" = "1"; then
41 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
42 cat <<END
44 Mount a LUKS-encrypted disk under /media/.
46 Usage: $progname [options]
48 Options:
50 -h, --help
51 Show this help.
52 -q, --quiet
53 Be more quiet. Can be repeated to increase silence.
54 -v, --verbose
55 Increase level of verbosity. Can be repeated.
56 -u, --umount
57 Unmount the disk.
58 --version
59 Print version information.
61 END
62 exit 0
65 msg() {
66 test $1 -gt $opt_verbose && return;
67 shift
68 echo "$progname: $*" >&2
71 err() {
72 echo "$progname: $*" >&2
73 exit 1
76 is_mounted() {
77 df | grep -q "/media/$disk\$" && return 0
78 return 1
81 disk=$1
82 [ -z "$disk" ] && err No disk specified
84 blkid | grep -q "LABEL=\"$disk\"" || err $disk: Disk not found in blkid
86 if [ "$opt_umount" != "1" ]; then
87 is_mounted $disk && err $disk: Already mounted
88 device=$(blkid -L $disk)
89 msg 1 device = \"$device\"
90 [ -z "$device" ] && err Cannot get device for $disk
91 echo "$device" | grep -q ^/dev/ || err $device: Malformed device name
92 sudo cryptsetup status $disk \
93 | grep -q "/dev/mapper/$disk is active" \
94 || sudo cryptsetup luksOpen $device $disk && mount /media/$disk
95 is_mounted $disk && msg 0 $disk mounted on /media/$disk \
96 || err $disk: mount failed
97 else
98 is_mounted $disk && umount /media/$disk
99 is_mounted $disk && err /media/$disk: umount failed
100 sudo cryptsetup luksClose $disk
101 sync
102 msg 0 $disk is unmounted and closed
105 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :