-
Notifications
You must be signed in to change notification settings - Fork 2
/
mnt
executable file
·249 lines (206 loc) · 4.49 KB
/
mnt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/sh
##
## Mounts or unmounts series of partitions
## Copyright (c) 2003-2006 by Tomekk (tomekk/AT/oswiecim.eu.org)
## Copyright (c) 2006 by Michal Nazarewicz (mina86/AT/mina86.com)
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
set -e
##
## Prints full absolute path to given file
##
fullname () {
# Empty
if [ -z "$1" ]; then
printf %s "$PWD"
return
fi
# Remove /s
while [ X"${1%/}" != X"$1" ]; do set -- "${1%/}"; done
while [ X"${1#//}" != X"$1" ]; do set -- "${1#/}"; done
# Those vere only slashes
if [ -z "$1" ]; then
printf /
return
fi
# No more slashes
if [ X"${1#*/}" = X"$1" ]; then
printf %s/%s "${PWD%/}" "$1"
return
fi
# Split
set -- "${1%/*}" "${1##*/}"
# Empty directory (was / )
if [ -z "$1" ]; then
printf /%s "$2"
return
fi
# Print
printf %s/%s "$( cd -- "$1"; printf %s "${PWD%/}" )" "$2"
}
##
## Printfs error messages
##
err () {
printf '%s: ' "${0##*/}"
printf "$@"
echo
}
die () {
__exit="$1"
shift
err "$@"
exit $__exit
}
##
## Escaping
##
esc_sed () {
if [ -n "$*" ]; then
printf %s "$*" | sed -e 's:[/.\\*^$[]:\\&:g'
fi
}
esc_sh () {
if [ -n "$*" ]; then
printf %s "$*" | sed -e "s/'/'\\\\''/g"
fi
}
##
## Runs command
##
run () {
if [ -z "$_SU" ]; then
"$@"
return $?
fi
__CMD=
while [ $# -ne 0 ]; do
__CMD="$__CMD '$(esc_sh "$1")'"
shift
done
su -c "$__CMD"
return $?
}
##
## Sets DEVICE and POINT variables
##
set_device_point () {
DEVICE=
POINT=
if [ -z "$1" ]; then return 1
elif [ -b "$1" ]; then DEVICE="$(fullname "$1")"
elif [ -b "/dev/$1" ]; then DEVICE="$(fullname "/dev/$1")"
elif [ -d "$1" ]; then POINT="$(fullname "$1")"
elif [ -d "/media/$1" ]; then POINT="$(fullname "/media/$1")"
elif [ -d "/mnt/$1" ]; then POINT="$(fullname "/mnt/$1")"
else return 1
fi
return 0
}
##
## Prints fstab line corresponding to given DEVICE/POINT
##
get_fstab_entry () {
if [ -n "$1" ] && [ -n "$2" ]; then set -- "$(esc_sed "$1") $(esc_sed "$2")"
elif [ -n "$1" ] ; then set -- "$(esc_sed "$1")"
elif [ -n "$2" ] ; then set -- "[^ ][^ ]* $(esc_sed "$2")"
else return
fi
sed -ne 's/#.*$//' -e 's/[[:space:]][[:space:]]*/ /g' \
-e "/^ \?$1 / {" -e p -e q -e "}" </etc/fstab
}
##
## Prints usage
##
usage () {
echo asd
}
##
## Parse arguments
##
_FORCE=
_SU=
while [ $# -ne 0 ] && [ X"${1#-}" != X"$1" ] && [ X"$1" != X-- ]; do
ARG="$1"
shift
# Long options
case "$ARG" in
--help) ARG="-h" ;;
--version) ARG="-V" ;;
--force) ARG="-f" ;;
--no-force) ARG="-nf" ;;
--su) ARG="-s" ;;
--no-su) ARG="-ns" ;;
--*|-) die 1 '%s: unknown option' "$ARG";;
esac
# Short options
VALUE=y
ARG="${ARG#-}"
while [ -n "$ARG" ]; do
CH="${ARG#?}"
CH="${ARG%$CH}"
ARG="${ARG#?}"
case "$CH" in
f) _FORCE=$VALUE; CH=;;
s) _SU=$VALUE; CH=;;
esac
if [ -z "$CH" ]; then
VALUE=y
continue
fi
[ -n "$VALUE" ] || die 1 'n option expects f or s option'
case "$CH" in
h) usage full; exit 0 ;;
V) usage ver; exit 0 ;;
n) VALUE=;;
*) die 1 '%s: unknown option' "$CH";;
esac
done
[ -n "$VALUE" ] || die 1 'n option expects f or s option'
done
unset ARG VALUE
[ X"$1" != X-- ] || shift
##
## One argument
##
if [ $# -eq 1 ]; then
if ! set_device_point "$1"; then
err '%s: no such device nor mount point' "$1"
exit 1
fi
# Find fstab entry
IFS=' ' set -- $(get_fstab_entry "$DEVICE" "$POINT")
if [ $# -eq 0 ]; then
err '%s: not present in /etc/fstab' "$DEVICE$POINT"
exit 1
fi
# auto?
if [ -z "$_FORCE" ] && \
! printf %s ",$4," | grep -F ',noauto,' >/dev/null 2>&1; then
err '%s: is mounted automatically; refuse to do anything' \
"$DEVICE$POINT"
exit 1
fi
# (u)mount
if mount | grep -e "^$1 on $2 " >/dev/null 2>&1; then
run umount "$DEVICE$POINT"
else
run mount "$DEVICE$POINT"
fi
exit $?
fi