-
Notifications
You must be signed in to change notification settings - Fork 1
/
volmand.in
507 lines (489 loc) · 11.4 KB
/
volmand.in
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/bin/sh
#
# Volume manager daemon for FreeBSD's devd(8)
# version 0.7
if [ "${1}" != "daemon" ]; then
daemon -cf -p /var/run/volmand.pid %%PREFIX%%/sbin/volmand daemon
exit $?
fi
if [ -r %%PREFIX%%/etc/volman.conf ]; then
. %%PREFIX%%/etc/volman.conf
fi
: ${WRKDIR:=/tmp/volman}
: ${DEVMATCH:="^(da|mmcsd)"}
: ${LABELMATCH:="^(msdosfs|ntfs|ufs)/"}
: ${MNTPREFIX=/media}
: ${DEVMPOINT=NO}
cleanup () {
trap - 1 15 EXIT
rm -rf ${WRKDIR}
kill -1 -${MASTERPID}
kill -15 -${MASTERPID}
exit 0
}
log () {
local level
level=${1}
shift
logger -p user.${level} -t volmand $*
}
pollDev () {
local status
status=0
while true; do
log info polling ${1}
sleep 5
if camcontrol tur ${1} >/dev/null 2>/dev/null; then
[ ${status} -eq 0 ] || continue
status=1
echo "!system=DEVFS subsystem=CDEV type=CREATE cdev=${1}" >${WRKDIR}/internal
sleep 1
:>/dev/${1}
else
if ! camcontrol devlist |grep -q "[(,]${1}[,)]"; then
log info lost device ${1}
echo "!system=VOLMAND _poller ENDPOLL ${1}" >${WRKDIR}/internal
return 0
fi
[ ${status} -eq 1 ] || continue
status=0
echo "!system=DEVFS subsystem=CDEV type=DESTROY cdev=${1}" >${WRKDIR}/internal
fi
done
}
probeMagic () {
local probe size
probe=$(dd if=/dev/${1} bs=128k count=1 2>/dev/null |file -Ls /dev/stdin)
size=$(diskinfo ${1} |cut -f 3 -d " ")
case "${probe}" in
*FAT*)
addDev ${1} ${size} msdosfs
return 0
;;
*Linux*ext2*)
addDev ${1} ${size} ext2fs
return 0
;;
*NTFS*)
addDev ${1} ${size} ntfs
return 0
;;
esac
return 1
}
probeDev () {
local dev gtype gprov gsize status pollpid mntcmd
dev=$1
eval "status=\$STATUS_${dev}"
eval "pollpid=\$POLLPID_${dev}"
if [ -n "${status}" ]; then return 0; fi
if [ -z "${pollpid}" ]; then
if camcontrol devlist |grep -q "[(,]${dev}[,)]"; then
if ! camcontrol tur ${dev} >/dev/null; then
pollDev ${dev} &
eval "POLLPID_${dev}=$!"
log info forking poller at PID $!
return 1
fi
fi
fi
if [ ${OSREL} -lt 802503 ]; then
probe=$(gpart show -r ${dev} 2>/dev/null)
else
probe=$(gpart show -rp ${dev} 2>/dev/null)
fi
if [ $? -ne 0 ]; then
probeMagic ${dev}
return $?
fi
IFS="
"
for line in ${probe}; do
IFS=" "
set -- ${line}
[ "${3}" != "-" ] || continue
if [ "${1}" = "=>" ]; then
shift
gtype=${4}
continue
fi
if [ ${OSREL} -lt 802503 ]; then
gprov=$(gpart modify -i ${3} -f x ${dev} 2>/dev/null |cut -f 1 -d " ")
gpart undo ${dev} >/dev/null 2>&1
else
gprov=${3}
fi
gsize=$(diskinfo ${gprov} |cut -f 3 -d " ")
case "${gtype}" in
MBR)
case "${4}" in
1|4|5|6|12|13|15|16)
addDev ${gprov} ${gsize} msdosfs
;;
7)
if dd if=/dev/${gprov} bs=128k count=1 2>/dev/null \
|file -Ls /dev/stdin |grep -q "NTFS"; then
addDev ${gprov} ${gsize} ntfs
fi
;;
83)
if dd if=/dev/${gprov} bs=128k count=1 2>/dev/null \
|file -Ls /dev/stdin |grep -q "Linux.*ext2"; then
addDev ${gprov} ${gsize} ext2fs
fi
;;
165)
probeDev ${gprov}
if [ $? -eq 1 ]; then
addDev ${gprov} ${gsize} ufs
fi
;;
esac
;;
BSD)
case "${4}" in
7)
addDev ${gprov} ${gsize} ufs
;;
esac
;;
esac
done
# partitionless devices with a boot record (eg. emulated floppy disks)
# look like empty MBR providers to gpart, so must be probed with file(1)
if [ "${gtype}" = "MBR" -a -z "${gprov}" ]; then
if ! probeMagic ${dev}; then
eval "STATUS_${dev}=novolume"
fi
fi
unset IFS
return 0
}
probeLabel () {
local label curlabel curstatus glabel subscriber
label=${1}
glabel=$(glabel status |grep "${label}")
[ $? -eq 0 ] || return 1
set -- ${glabel}
shift $((${#}-1))
eval "curlabel=\$LABEL_${1}"
[ -z "${curlabel}" ] || return 0
eval "curstatus=\$STATUS_${1}"
case "${curstatus}" in
novolume)
return 1
;;
umounted|mounted)
;;
*)
addDev ${1} $(diskinfo ${1} |cut -f 3 -d " ") ${label%/*}
sleep 0.5
;;
esac
for subscriber in ${SUBSCRIBERS}; do
echo "VOLUME LABEL ${1} ${label#*/}" >>${WRKDIR}/fifo.${subscriber} &
done
eval "LABEL_${1}=\"${label#*/}\""
return 0
}
# $1 = dev node
# $2 = size in bytes
# $3 = filesystem
addDev () {
local status subscriber
eval "status=\$STATUS_${1}"
case "${status}" in
umounted|mounted)
;;
*)
eval "STATUS_${1}=umounted"
eval "SIZE_${1}=${2}"
eval "FS_${1}=${3}"
if [ "${3}" = "msdosfs" -a ${2} -gt 68719476736 ]; then
# MSDOS volumes larger than 64 GiB
eval "MNTCMD_${1}=\"\$MNTCMD_${3} -o large\""
else
eval "MNTCMD_${1}=\"\$MNTCMD_${3}\""
fi
MOUNTABLE="${MOUNTABLE} ${1}"
for subscriber in ${SUBSCRIBERS}; do
echo "VOLUME CREATE ${1} ${3} ${2}" >>${WRKDIR}/fifo.${subscriber} &
done
;;
esac
}
getInput () {
local src cmd data
while true; do
while read src cmd data <&3; do
echo "${src}" |grep -Eq "^[a-z][a-z0-9]+" || continue
case "${cmd}" in
SUBSCRIBE)
echo "!system=VOLMAND ${src} SUBSCRIBE" >${WRKDIR}/internal
;;
VOLUMES)
echo "!system=VOLMAND ${src} VOLUMES" >${WRKDIR}/internal
;;
MOUNT|UMOUNT)
if echo "${data}" |grep -Eq ${DEVMATCH}; then
echo "!system=VOLMAND ${src} ${cmd} ${data}" >${WRKDIR}/internal
else
if [ -e ${WRKDIR}/fifo.${src} ]; then
echo "ERROR ${data} unmountable" >${WRKDIR}/fifo.${src}
fi
fi
;;
PONG)
echo "!system=VOLMAND ${src} PONG" >${WRKDIR}/internal
;;
esac
done 3<${WRKDIR}/fifo.volmand
done
}
pinger () {
local subscriber pings subpids keep
for subscriber in ${SUBSCRIBERS}; do
eval "pings=\$PINGS_${subscriber}"
pings=$((${pings}+1))
if [ ${pings} -le 3 ]; then
keep="${keep} ${subscriber}"
echo "PING" >>${WRKDIR}/fifo.${subscriber} &
eval "PINGS_${subscriber}=${pings}"
else
eval "subpids=\"\$SUBPIDS_${subscriber}\""
kill ${subpids}
rm -f ${WRKDIR}/fifo.${subscriber}
unset PINGS_${subscriber} SUBPIDS_${subscriber}
fi
done
SUBSCRIBERS="${keep}"
LASTPING=$(date +%s)
}
# $1 = type paramter
# $2 = cdev paramter
msgDevfs () {
local probefunc oldmountable mountable fs mntcmd label subscriber
if echo ${2} |grep -qE ${DEVMATCH}; then
probefunc=probeDev
elif echo ${2} |grep -qE ${LABELMATCH}; then
probefunc=probeLabel
else
continue
fi
log notice ${1} ${2}
case "${1}" in
CREATE)
${probefunc} "${2}"
;;
DESTROY)
if [ "${probefunc}" = "probeDev" ]; then
eval "fs=\$FS_${2}"
if [ -n "${fs}" ]; then
oldmountable=${MOUNTABLE}
MOUNTABLE=""
for mountable in ${oldmountable}; do
[ "${mountable}" != "${2}" ] || continue
MOUNTABLE="${MOUNTABLE} ${mountable}"
done
for subscriber in ${SUBSCRIBERS}; do
echo "VOLUME REMOVE ${2}" >>${WRKDIR}/fifo.${subscriber} &
done
unset FS_${2} SIZE_${2} MNTCMD_${2} LABEL_${2}
fi
unset STATUS_${2}
fi
;;
esac
:>${WRKDIR}/mountable
for mountable in ${MOUNTABLE}; do
eval "fs=\$FS_${mountable}"
eval "mntcmd=\"\$MNTCMD_${mountable}\""
eval "label=\$LABEL_${mountable}"
printf '%s\t%s\t%s\t%s\n' ${mountable} ${fs} "${mntcmd}" "${label}" >>${WRKDIR}/mountable
done
}
# $1 = subsys
# $2 = cmd
# $3+ = data
msgVolmand () {
local pings vol status fs size label mntcmd mdev mpoint subscriber
case "${2}" in
SUBSCRIBE)
eval "pings=\$PINGS_${1}"
if [ -z "${pings}" ]; then
if mkfifo -m 644 ${WRKDIR}/fifo.${1}; then
SUBSCRIBERS="${SUBSCRIBERS} ${1}"
cat ${WRKDIR}/block >${WRKDIR}/fifo.${1} &
eval "SUBPIDS_${1}=$!"
eval "PINGS_${1}=0"
fi
fi
;;
VOLUMES)
if [ -e ${WRKDIR}/fifo.${1} ]; then
(
for vol in ${MOUNTABLE}; do
eval "status=\$STATUS_${vol}"
eval "fs=\$FS_${vol}"
eval "size=\$SIZE_${vol}"
eval "label=\"\$LABEL_${vol}\""
case "${status}" in
umounted)
echo "VOLUME LIST u ${vol} ${fs} ${size} ${label}" >${WRKDIR}/fifo.${1}
;;
mounted)
echo "VOLUME LIST m ${vol} ${fs} ${size} ${label}" >${WRKDIR}/fifo.${1}
;;
esac
done
echo "VOLUME LIST END" >${WRKDIR}/fifo.${1}
) &
fi
;;
MOUNT)
if [ -e ${WRKDIR}/fifo.${1} ]; then
(
eval "status=\$STATUS_${3}"
case "${status}" in
umounted)
eval "fs=\$FS_${3}"
eval "label=\"\$LABEL_${3}\""
eval "mntcmd=\"\$MNTCMD_${3}\""
if [ -n "${label}" -a "${DEVMPOINT}" = "NO" ]; then
mdev="/dev/${fs}/${label}"
mpoint="/media/${label}"
else
mdev="/dev/${3}"
mpoint="/media/${3}"
fi
if [ -n "${mdev}" -a -n "${mpoint}" ]; then
log notice mounting ${mdev} to ${mpoint}
if mkdir -p "${mpoint}" && ${mntcmd} "${mdev}" "${mpoint}"; then
echo "!system=VOLMAND _mounter STATUS ${3} mounted ${mpoint}" >${WRKDIR}/internal
else
echo "ERROR ${3} mount error occured" >${WRKDIR}/fifo.${1}
fi
fi
;;
mounted)
echo "ERROR ${3} volume already mounted" >${WRKDIR}/fifo.${1}
;;
*)
echo "ERROR ${3} volume unmountable" >${WRKDIR}/fifo.${1}
;;
esac
) &
fi
;;
UMOUNT)
if [ -e ${WRKDIR}/fifo.${1} ]; then
(
eval "status=\$STATUS_${3}"
case "${status}" in
mounted)
eval "fs=\$FS_${3}"
eval "label=\"\$LABEL_${3}\""
if [ -n "${label}" -a "${DEVMPOINT}" = "NO" ]; then
mdev="/dev/${fs}/${label}"
mpoint="/media/${label}"
else
mdev="/dev/${3}"
mpoint="/media/${3}"
fi
if [ -n "${mdev}" ]; then
log notice umounting ${mdev} from ${mpoint}
if umount "${mpoint}"; then
rmdir "${mpoint}"
echo "!system=VOLMAND _mounter STATUS ${3} umounted ${mpoint}" >${WRKDIR}/internal
else
echo "ERROR ${3} umount error occured" >${WRKDIR}/fifo.${1}
fi
fi
;;
*)
echo "ERROR ${3} volume not mounted" >${WRKDIR}/fifo.${1}
;;
esac
) &
fi
;;
PONG)
eval "pings=\$PINGS_${1}"
if [ -n "${pings}" ]; then
eval "PINGS_${1}=0"
fi
;;
ENDPOLL)
if [ "${1}" = "_poller" ]; then
eval "unset POLLPID_${3}"
fi
;;
STATUS)
if [ "${1}" = "_mounter" ]; then
vol=${3}
status=${4}
case "${4}" in
mounted)
shift 4
mntcmd="MOUNT ${vol} ${*}"
;;
umounted)
shift 4
mntcmd="UMOUNT ${vol} ${*}"
;;
esac
eval "STATUS_${vol}=${status}"
for subscriber in ${SUBSCRIBERS}; do
if [ -e ${WRKDIR}/fifo.${subscriber} ]; then
echo ${mntcmd} >${WRKDIR}/fifo.${subscriber} &
fi
done
fi
;;
esac
}
MASTERPID=$$
MNTCMD_msdosfs="mount_msdosfs -m 666 -M 777"
MNTCMD_ext2fs="mount -t ext2fs"
MNTCMD_ufs="mount -t ufs"
if kldstat -q -m fuse && test -x /usr/local/bin/ntfs-3g; then
MNTCMD_ntfs="/usr/local/bin/ntfs-3g"
else
MNTCMD_ntfs="mount_ntfs -o ro -m 555"
fi
OSREL=$(sysctl -n kern.osreldate)
MNTFILE=mountable
trap cleanup 1 2 13 14 15 EXIT
test -d ${WRKDIR} && rm -rf ${WRKDIR}
mkdir ${WRKDIR} || ( log err unable to create WRKDIR at ${WRKDIR} && exit 1 )
mkfifo -m 666 ${WRKDIR}/fifo.volmand || ( log err unable to create FIFO at ${WRKDIR}/fifo.volmand && exit 1 )
mkfifo -m 600 ${WRKDIR}/internal || ( log err unable to create FIFO at ${WRKDIR}/internal && exit 1 )
mkfifo -m 600 ${WRKDIR}/block || ( log err unable to create FIFO at ${WRKDIR}/block && exit 1 )
if [ -S /var/run/devd.pipe -a -r /var/run/devd.pipe ]; then
cat /var/run/devd.pipe >${WRKDIR}/internal 2>/dev/null &
DEVDCATPID=$!
else
log err devd domain socket non-existant or unreadable
exit 1
fi
getInput &
GETINPUTPID=$!
MOUNTABLE=""
SUBSCRIBERS=""
LASTPING=$(date +%s)
:>${WRKDIR}/mountable
while read SYSTEM SUBSYS TYPE EXTRA <&3; do
case "${SYSTEM#*=}" in
DEVFS)
if [ "${SUBSYS#*=}" = "CDEV" ]; then
msgDevfs "${TYPE#*=}" "${EXTRA#cdev=}"
fi
;;
VOLMAND)
msgVolmand "${SUBSYS}" "${TYPE}" ${EXTRA}
;;
esac
if [ $(date +%s) -gt $((${LASTPING}+10)) ]; then
pinger
fi
done 3<${WRKDIR}/internal