forked from ehough/docker-nfs-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
562 lines (409 loc) · 13.6 KB
/
entrypoint.sh
File metadata and controls
562 lines (409 loc) · 13.6 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/env bash
#
# ehough/docker-nfs-server: A lightweight, robust, flexible, and containerized NFS server.
#
# Copyright (C) 2017-2018 Eric D. Hough
#
# 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/>.
######################################################################################
### constants
######################################################################################
readonly ENV_VAR_NFS_DISABLE_VERSION_3='NFS_DISABLE_VERSION_3'
readonly ENV_VAR_NFS_SERVER_THREAD_COUNT='NFS_SERVER_THREAD_COUNT'
readonly ENV_VAR_NFS_ENABLE_KERBEROS='NFS_ENABLE_KERBEROS'
readonly ENV_VAR_NFS_PORT_MOUNTD='NFS_PORT_MOUNTD'
readonly ENV_VAR_NFS_PORT='NFS_PORT'
readonly ENV_VAR_NFS_PORT_STATD_IN='NFS_PORT_STATD_IN'
readonly ENV_VAR_NFS_PORT_STATD_OUT='NFS_PORT_STATD_OUT'
readonly ENV_VAR_NFS_VERSION='NFS_VERSION'
readonly DEFAULT_NFS_SERVER_THREAD_COUNT="$(grep -Ec ^processor /proc/cpuinfo)"
readonly DEFAULT_NFS_PORT=2049
readonly DEFAULT_NFS_PORT_MOUNTD=32767
readonly DEFAULT_NFS_PORT_STATD_IN=32765
readonly DEFAULT_NFS_PORT_STATD_OUT=32766
readonly DEFAULT_NFS_VERSION='4.2'
readonly PATH_BIN_EXPORTFS='/usr/sbin/exportfs'
readonly PATH_BIN_IDMAPD='/usr/sbin/rpc.idmapd'
readonly PATH_BIN_MOUNTD='/usr/sbin/rpc.mountd'
readonly PATH_BIN_NFSD='/usr/sbin/rpc.nfsd'
readonly PATH_BIN_RPCBIND='/sbin/rpcbind'
readonly PATH_BIN_RPC_SVCGSSD='/usr/sbin/rpc.svcgssd'
readonly PATH_BIN_STATD='/sbin/rpc.statd'
readonly PATH_FILE_ETC_EXPORTS='/etc/exports'
readonly PATH_FILE_ETC_IDMAPD_CONF='/etc/idmapd.conf'
readonly PATH_FILE_ETC_KRB5_CONF='/etc/krb5.conf'
readonly PATH_FILE_ETC_KRB5_KEYTAB='/etc/krb5.keytab'
readonly MOUNT_PATH_NFSD='/proc/fs/nfsd'
readonly MOUNT_PATH_RPC_PIPEFS='/var/lib/nfs/rpc_pipefs'
######################################################################################
### general purpose utilities
######################################################################################
log() {
echo "----> $1"
}
logHeader() {
echo ''
echo '=================================================================='
echo " $1" | awk '{print toupper($0)}'
echo '=================================================================='
}
warn_on_failure() {
if [[ $? -ne 0 ]]; then
log "WARNING: $1"
fi
}
exit_on_failure() {
if [[ $? -ne 0 ]]; then
log "$1"
exit 1
fi
}
kill_process_if_running() {
local -r base=$(basename "$1")
local -r pid=$(pidof "$base")
if [[ -n $pid ]]; then
log "killing $base"
kill -TERM "$pid"
warn_on_failure "unable to kill $base"
else
log "$base was not running"
fi
}
######################################################################################
### teardown
######################################################################################
stop_mount() {
local -r path=$1
local -r type=$(basename "$path")
if mount | grep -Eq ^"$type on $path\\s+"; then
log "un-mounting $type from $path"
umount -v "$path"
warn_on_failure "unable to un-mount $type from $path"
else
log "$type was not mounted on $path"
fi
}
stop_nfsd() {
log 'stopping nfsd'
$PATH_BIN_NFSD 0
warn_on_failure 'unable to stop nfsd. if it had started already, check Docker host for lingering [nfsd] processes'
}
stop_exportfs() {
log 'un-exporting filesystems'
$PATH_BIN_EXPORTFS -ua
warn_on_failure 'unable to un-export filesystems'
}
stop() {
logHeader 'terminating ...'
kill_process_if_running "$PATH_BIN_RPC_SVCGSSD"
stop_nfsd
kill_process_if_running "$PATH_BIN_IDMAPD"
kill_process_if_running "$PATH_BIN_STATD"
kill_process_if_running "$PATH_BIN_MOUNTD"
stop_exportfs
kill_process_if_running "$PATH_BIN_RPCBIND"
stop_mount "$MOUNT_PATH_NFSD"
stop_mount "$MOUNT_PATH_RPC_PIPEFS"
logHeader 'terminated'
exit 0
}
stop_on_failure() {
if [[ $? -ne 0 ]]; then
log "$1"
stop
fi
}
######################################################################################
### runtime environment detection
######################################################################################
get_reqd_nfs_version() {
echo "${!ENV_VAR_NFS_VERSION:-$DEFAULT_NFS_VERSION}"
}
get_reqd_nfsd_threads() {
echo "${!ENV_VAR_NFS_SERVER_THREAD_COUNT:-$DEFAULT_NFS_SERVER_THREAD_COUNT}"
}
get_reqd_mountd_port() {
echo "${!ENV_VAR_NFS_PORT_MOUNTD:-$DEFAULT_NFS_PORT_MOUNTD}"
}
get_reqd_nfsd_port() {
echo "${!ENV_VAR_NFS_PORT:-$DEFAULT_NFS_PORT}"
}
get_reqd_statd_in_port() {
echo "${!ENV_VAR_NFS_PORT_STATD_IN:-$DEFAULT_NFS_PORT_STATD_IN}"
}
get_reqd_statd_out_port() {
echo "${!ENV_VAR_NFS_PORT_STATD_OUT:-$DEFAULT_NFS_PORT_STATD_OUT}"
}
is_kerberos_enabled() {
if [[ -n "${!ENV_VAR_NFS_ENABLE_KERBEROS}" ]]; then
echo 1
fi
}
is_nfs3_enabled() {
if [[ -z "${!ENV_VAR_NFS_DISABLE_VERSION_3}" ]]; then
echo 1
fi
}
is_nfs4_enabled() {
if [[ "$(get_reqd_nfs_version)" =~ '^4' ]]; then
echo 1
fi
}
######################################################################################
### runtime configuration assertions
######################################################################################
assert_kernel_mod() {
local -r moduleName=$1
log "checking for presence of kernel module: $moduleName"
lsmod | grep -Eq "^$moduleName\\s+"
exit_on_failure "$moduleName module is not loaded on the Docker host's kernel (try: modprobe $moduleName)"
}
assert_port() {
local -r envName=$1
local -r value=${!envName}
if [[ -n "$value" && ( "$value" -lt 1 || "$value" -gt 65535 ) ]]; then
log "Please set $1 to a value between 1 and 65535 inclusive"
exit 1
fi
}
assert_nfs_version() {
get_reqd_nfs_version | grep -Eq '^(3|4|4\.1|4\.2)$'
exit_on_failure "please set $ENV_VAR_NFS_VERSION to 3, 4, 4.1, or 4.2"
}
assert_disabled_nfs3() {
if [[ -z "$(is_nfs3_enabled)" && "$(get_reqd_nfs_version)" == '3' ]]; then
log 'you cannot simultaneously enable and disable NFS version 3'
exit 1
fi
}
assert_nfsd_threads() {
local -r requested=$(get_reqd_nfsd_threads)
if [[ "$requested" -lt 1 ]]; then
log "Please set $ENV_VAR_NFS_SERVER_THREAD_COUNT to a positive value"
exit 1
fi
}
assert_kerberos_requirements() {
if [[ -z "$(is_kerberos_enabled)" ]]; then
return
fi
if [[ ! -f "$PATH_FILE_ETC_IDMAPD_CONF" ]]; then
log "Please provide $PATH_FILE_ETC_IDMAPD_CONF to the container"
exit 1
fi
if [[ ! -f "$PATH_FILE_ETC_KRB5_KEYTAB" ]]; then
log "Please provide $PATH_FILE_ETC_KRB5_KEYTAB to the container"
exit 1
fi
if [[ ! -f "$PATH_FILE_ETC_KRB5_CONF" ]]; then
log "Please provide $PATH_FILE_ETC_KRB5_CONF to the container"
exit 1
fi
assert_kernel_mod rpcsec_gss_krb5
}
######################################################################################
### initialization
######################################################################################
init_trap() {
trap stop SIGTERM SIGINT
}
init_exports() {
if mount | grep -Eq "^[^ ]+ on $PATH_FILE_ETC_EXPORTS type "; then
log "$PATH_FILE_ETC_EXPORTS already exists in the container"
return
fi
local collected=0
local exports=''
local candidateExportVariables
candidateExportVariables=$(compgen -A variable | grep -E 'NFS_EXPORT_[0-9]+' | sort)
exit_on_failure "please provide $PATH_FILE_ETC_EXPORTS or set NFS_EXPORT_* environment variables"
log "building $PATH_FILE_ETC_EXPORTS"
for exportVariable in $candidateExportVariables; do
local line=${!exportVariable}
local lineAsArray
read -r -a lineAsArray <<< "$line"
local dir="${lineAsArray[0]}"
if [[ ! -d "$dir" ]]; then
log "skipping $line since $dir is not a directory"
continue
fi
log "will export $line"
if [[ $collected -gt 0 ]]; then
exports=$exports$'\n'
fi
exports=$exports$line
(( collected++ ))
done
if [[ $collected -eq 0 ]]; then
log 'no valid exports'
exit 1
fi
log "will export $collected filesystem(s)"
echo "$exports" > $PATH_FILE_ETC_EXPORTS
}
init_assertions() {
# validate any user-supplied environment variables
assert_port "$ENV_VAR_NFS_PORT"
assert_port "$ENV_VAR_NFS_PORT_MOUNTD"
assert_port "$ENV_VAR_NFS_PORT_STATD_IN"
assert_port "$ENV_VAR_NFS_PORT_STATD_OUT"
assert_nfs_version
assert_disabled_nfs3
assert_nfsd_threads
# check kernel modules
assert_kernel_mod nfs
assert_kernel_mod nfsd
# ensure /etc/exports has at least one line
grep -Evq '^\s*#|^\s*$' $PATH_FILE_ETC_EXPORTS
exit_on_failure "$PATH_FILE_ETC_EXPORTS has no exports"
# ensure we have CAP_SYS_ADMIN
capsh --print | grep -Eq "^Current: = .*,?cap_sys_admin(,|$)"
exit_on_failure 'missing CAP_SYS_ADMIN. be sure to run Docker with --cap-add SYS_ADMIN or --privileged'
# perform Kerberos assertions
assert_kerberos_requirements
}
######################################################################################
### boot helpers
######################################################################################
boot_helper_mount() {
local -r path=$1
local -r type=$(basename "$path")
local -r args=('-vt' "$type" "$path")
log "mounting $type onto $path"
mount "${args[@]}"
stop_on_failure "unable to mount $type onto $path"
}
boot_helper_get_version_flags() {
local versionFlags
local -r requestedVersion="$(get_reqd_nfs_version)"
versionFlags=('--nfs-version' "$requestedVersion" '--no-nfs-version' 2)
if [[ -z "$(is_nfs3_enabled)" ]]; then
versionFlags+=('--no-nfs-version' 3)
fi
if [[ "$requestedVersion" == '3' ]]; then
versionFlags+=('--no-nfs-version' 4)
fi
echo "${versionFlags[@]}"
}
######################################################################################
### primary boot
######################################################################################
boot_main_mounts() {
# http://wiki.linux-nfs.org/wiki/index.php/Nfsv4_configuration
boot_helper_mount "$MOUNT_PATH_RPC_PIPEFS"
boot_helper_mount "$MOUNT_PATH_NFSD"
}
boot_main_exportfs() {
log 'exporting filesystems'
$PATH_BIN_EXPORTFS -arv
stop_on_failure 'exportfs failed'
}
boot_main_mountd() {
local versionFlags
read -r -a versionFlags <<< "$(boot_helper_get_version_flags)"
local -r port=$(get_reqd_mountd_port)
local -r version=$(get_reqd_nfs_version)
local -r args=('--debug' 'all' '--port' "$port" "${versionFlags[@]}")
# yes, rpc.mountd is required even for NFS v4: https://forums.gentoo.org/viewtopic-p-7724856.html#7724856
log "starting rpc.mountd for NFS version $version on port $port"
$PATH_BIN_MOUNTD "${args[@]}"
stop_on_failure 'rpc.mountd failed'
}
boot_main_rpcbind() {
# rpcbind isn't required for NFSv4, but if it's not running then nfsd takes over 5 minutes to start up.
# it's a bug in either nfs-utils on the kernel, and the code of both is over my head.
# so as a workaround we start rpcbind now and (in v4-only scenarios) kill it after nfsd starts up
log 'starting rpcbind'
$PATH_BIN_RPCBIND -ds
stop_on_failure 'rpcbind failed'
}
boot_main_idmapd() {
if [[ "$(get_reqd_nfs_version)" != '3' && -f "$PATH_FILE_ETC_IDMAPD_CONF" ]]; then
log 'starting idmapd'
$PATH_BIN_IDMAPD -v -S
stop_on_failure 'idmapd failed'
fi
}
boot_main_statd() {
if [[ -z "$(is_nfs3_enabled)" ]]; then
return
fi
local -r inPort=$(get_reqd_statd_in_port)
local -r outPort=$(get_reqd_statd_out_port)
local -r args=('--no-notify' '--port' "$inPort" '--outgoing-port' "$outPort")
log "starting statd on port $inPort (outgoing connections on port $outPort)"
$PATH_BIN_STATD "${args[@]}"
stop_on_failure 'statd failed'
}
boot_main_nfsd() {
local versionFlags
read -r -a versionFlags <<< "$(boot_helper_get_version_flags)"
local -r threads=$(get_reqd_nfsd_threads)
local -r port=$(get_reqd_nfsd_port)
local -r version=$(get_reqd_nfs_version)
local -r args=('--debug' 8 '--port' "$port" "${versionFlags[@]}" "$threads")
log "starting rpc.nfsd on port $port with version $version and $threads server thread(s)"
$PATH_BIN_NFSD "${args[@]}"
stop_on_failure 'rpc.nfsd failed'
if [ -z "$(is_nfs3_enabled)" ]; then
kill_process_if_running "$PATH_BIN_RPCBIND"
fi
}
boot_main_svcgssd() {
if [[ -z "$(is_kerberos_enabled)" ]]; then
return
fi
log 'starting rpc.svcgssd'
$PATH_BIN_RPC_SVCGSSD -f &
stop_on_failure 'rpc.svcgssd failed'
}
boot_main_print_ready_message() {
logHeader "ready and waiting for connections on port $(get_reqd_nfsd_port)"
log 'list of exports:'
cat $PATH_FILE_ETC_EXPORTS
}
######################################################################################
### main routines
######################################################################################
init() {
logHeader 'setting up'
init_trap
init_exports
init_assertions
log 'setup complete'
}
boot() {
logHeader 'starting services'
boot_main_mounts
boot_main_rpcbind
boot_main_exportfs
boot_main_mountd
boot_main_statd
boot_main_idmapd
boot_main_nfsd
boot_main_svcgssd
boot_main_print_ready_message
}
hangout() {
# wait forever or until we get SIGTERM or SIGINT
# https://stackoverflow.com/a/41655546/229920
# https://stackoverflow.com/a/27694965/229920
while :; do sleep 2073600 & wait; done
}
main() {
init
boot
hangout
}
main