Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit c428f6b

Browse files
committed
minor tweaks and improvements
1 parent dca5448 commit c428f6b

File tree

2 files changed

+74
-57
lines changed

2 files changed

+74
-57
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A lightweight, robust, flexible, and containerized NFS server.
66

77
This is the only containerized NFS server that offers **all** of the following features:
88

9-
- supports NFS versions 3, 4, or both simultaneously
9+
- NFS versions 3, 4, or both simultaneously
1010
- clean teardown of services upon `SIGTERM` or `SIGKILL` (no lingering `nfsd` processes on Docker host)
1111
- flexible construction of `/etc/exports` via a Docker bind mount *or* environment variables
1212
- lightweight image based on [Alpine Linux](https://alpinelinux.org/)
@@ -52,7 +52,7 @@ Via optional environment variables, you can adjust the server settings to your n
5252

5353
- **`NFS_VERSION`** (default is `4.2`)
5454

55-
Set to `3`, `4`, `4.1`, or `4.2` to fine tune the NFS protocol version. Note that any minor version will also enable any lesser minor versions. e.g. `4.2` will enable versions 4.2, 4.1, 4, **and** 3.
55+
Set to `3`, `4`, `4.1`, or `4.2` to fine tune the NFS protocol version. Enabling any version will also enable any lesser versions. e.g. `4.2` will enable versions 4.2, 4.1, 4, **and** 3.
5656

5757
- **`NFS_VERSION_DISABLE_V3`** (*not set by default*)
5858

@@ -68,15 +68,15 @@ Via optional environment variables, you can adjust the server settings to your n
6868

6969
- **`NFS_MOUNTD_PORT`** (default is `32767`)
7070

71-
*Not needed for NFS 4*. Set this to any valid port number (`1` - `65535` inclusive) to change `rpc.mountd`'s listening port.
71+
*Only needed for NFS 3*. Set this to any valid port number (`1` - `65535` inclusive) to change `rpc.mountd`'s listening port.
7272

7373
- **`NFS_STATD_IN_PORT`** (default is `32765`)
7474

75-
*Not needed for NFS 4*. Set this to any valid port number (`1` - `65535` inclusive) to change `rpc.statd`'s listening port.
75+
*Only needed for NFS 3*. Set this to any valid port number (`1` - `65535` inclusive) to change `rpc.statd`'s listening port.
7676

7777
- **`NFS_STATD_OUT_PORT`** (default is `32766`)
7878

79-
*Not needed for NFS 4*. Set this to any valid port number (`1` - `65535` inclusive) to change `rpc.statd`'s outgoing connection port.
79+
*Only needed for NFS 3*. Set this to any valid port number (`1` - `65535` inclusive) to change `rpc.statd`'s outgoing connection port.
8080

8181
### Mounting filesystems from a client
8282

entrypoint.sh

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ readonly DEFAULT_NFS_MOUNTD_PORT=32767
3636
readonly DEFAULT_NFS_STATD_IN_PORT=32765
3737
readonly DEFAULT_NFS_STATD_OUT_PORT=32766
3838

39+
readonly PATH_NFSD='/usr/sbin/rpc.nfsd'
40+
readonly PATH_EXPORTFS='/usr/sbin/exportfs'
41+
readonly PATH_MOUNTD='/usr/sbin/rpc.mountd'
42+
readonly PATH_RPCBIND='/sbin/rpcbind'
43+
readonly PATH_STATD='/usr/sbin/rpc.statd'
44+
readonly PATH_EXPORTS_FILE='/etc/exports'
45+
46+
readonly MOUNT_PATH_NFSD='/proc/fs/nfsd'
47+
readonly MOUNT_PATH_RPC_PIPEFS='/var/lib/nfs/rpc_pipefs'
3948

4049
######################################################################################
4150
### general purpose utilities
@@ -69,59 +78,64 @@ exit_on_failure() {
6978
fi
7079
}
7180

72-
######################################################################################
73-
### teardown
74-
######################################################################################
75-
76-
stop_process_if_running() {
81+
kill_process_if_running() {
7782

78-
local -r pid=$(pidof "$1")
83+
local -r base=$(basename "$1")
84+
local -r pid=$(pidof "$base")
7985

8086
if [[ -n $pid ]]; then
81-
log "killing $1"
87+
log "killing $base"
8288
kill -TERM "$pid"
83-
warn_on_failure "unable to kill $1"
89+
warn_on_failure "unable to kill $base"
8490
else
85-
log "$1 was not running"
91+
log "$base was not running"
8692
fi
8793
}
8894

89-
stop_unmount() {
9095

91-
if mount | grep -Eq ^"$1 on $2\\s+"; then
92-
log "unmounting $1 from $2"
93-
umount "$2"
94-
warn_on_failure "unable to unmount $1 from $2"
96+
######################################################################################
97+
### teardown
98+
######################################################################################
99+
100+
stop_mount() {
101+
102+
local -r path=$1
103+
local -r type=$(basename $path)
104+
105+
if mount | grep -Eq ^"$type on $path\\s+"; then
106+
log "un-mounting $type from $path"
107+
umount -v "$path"
108+
warn_on_failure "unable to un-mount $type from $path"
95109
else
96-
log "$1 was not mounted on $2"
110+
log "$type was not mounted on $path"
97111
fi
98112
}
99113

100114
stop_nfsd() {
101115

102116
log 'stopping nfsd'
103-
/usr/sbin/rpc.nfsd 0
117+
$PATH_NFSD 0
104118
warn_on_failure 'unable to stop nfsd. if it had started already, check Docker host for lingering [nfsd] processes'
105119
}
106120

107121
stop_exportfs() {
108122

109-
log 'unexporting filesystems'
110-
/usr/sbin/exportfs -ua
111-
warn_on_failure 'unable to unexport filesystems'
123+
log 'un-exporting filesystems'
124+
$PATH_EXPORTFS -ua
125+
warn_on_failure 'unable to un-export filesystems'
112126
}
113127

114128
stop() {
115129

116-
logHeader 'terminating'
130+
logHeader 'terminating ...'
117131

118132
stop_nfsd
119-
stop_process_if_running 'rpc.statd'
120-
stop_process_if_running 'rpc.mountd'
133+
kill_process_if_running "$PATH_STATD"
134+
kill_process_if_running "$PATH_MOUNTD"
121135
stop_exportfs
122-
stop_process_if_running 'rpcbind'
123-
stop_unmount 'nfsd' '/proc/fs/nfsd'
124-
stop_unmount 'rpc_pipefs' '/var/lib/nfs/rpc_pipefs'
136+
kill_process_if_running "$PATH_RPCBIND"
137+
stop_mount "$MOUNT_PATH_NFSD"
138+
stop_mount "$MOUNT_PATH_RPC_PIPEFS"
125139

126140
logHeader 'terminated'
127141

@@ -248,8 +262,8 @@ init_trap() {
248262

249263
init_exports() {
250264

251-
if mount | grep -Eq '^[^ ]+ on /etc/exports type '; then
252-
log '/etc/exports appears to be mounted via Docker'
265+
if mount | grep -Eq "^[^ ]+ on $PATH_EXPORTS_FILE type "; then
266+
log "$PATH_EXPORTS_FILE appears to be mounted via Docker"
253267
return
254268
fi
255269

@@ -258,15 +272,15 @@ init_exports() {
258272
local candidateExportVariables
259273

260274
candidateExportVariables=$(compgen -A variable | grep -E 'NFS_EXPORT_[0-9]+' | sort)
261-
exit_on_failure 'please bind mount /etc/exports or supply NFS_EXPORT_* environment variables'
275+
exit_on_failure "please bind mount $PATH_EXPORTS_FILE or supply NFS_EXPORT_* environment variables"
262276

263-
log 'building /etc/exports'
277+
log "building $PATH_EXPORTS_FILE"
264278

265279
for exportVariable in $candidateExportVariables; do
266280

267281
local line=${!exportVariable}
268282
local lineAsArray
269-
IFS=' ' read -r -a lineAsArray <<< "$line"
283+
read -r -a lineAsArray <<< "$line"
270284
local dir="${lineAsArray[0]}"
271285

272286
if [[ ! -d "$dir" ]]; then
@@ -293,10 +307,10 @@ init_exports() {
293307

294308
log "will export $collected filesystem(s)"
295309

296-
echo "$exports" > /etc/exports
310+
echo "$exports" > $PATH_EXPORTS_FILE
297311

298-
log '/etc/exports now contains the following contents:'
299-
cat /etc/exports
312+
log "$PATH_EXPORTS_FILE now contains the following contents:"
313+
cat $PATH_EXPORTS_FILE
300314
}
301315

302316
init_assertions() {
@@ -315,25 +329,23 @@ init_assertions() {
315329
assert_kernel_mod nfsd
316330

317331
# ensure /etc/exports has at least one line
318-
grep -Evq '^\s*#|^\s*$' /etc/exports
319-
exit_on_failure '/etc/exports has no exports'
332+
grep -Evq '^\s*#|^\s*$' $PATH_EXPORTS_FILE
333+
exit_on_failure "$PATH_EXPORTS_FILE has no exports"
320334

321335
# ensure we have CAP_SYS_ADMIN
322336
capsh --print | grep -Eq "^Current: = .*,?cap_sys_admin(,|$)"
323337
exit_on_failure 'missing CAP_SYS_ADMIN. be sure to run Docker with --cap-add SYS_ADMIN or --privileged'
324-
325-
log 'requirements look good'
326338
}
327339

328340

329341
######################################################################################
330342
### boot helpers
331343
######################################################################################
332344

333-
boot_helper_do_mount() {
345+
boot_helper_mount() {
334346

335-
local -r type=$1
336-
local -r path=$2
347+
local -r path=$1
348+
local -r type=$(basename $path)
337349
local -r args=('-vt' "$type" "$path")
338350

339351
log "mounting $type onto $path"
@@ -362,28 +374,28 @@ boot_helper_get_version_flags() {
362374
boot_main_mounts() {
363375

364376
# http://wiki.linux-nfs.org/wiki/index.php/Nfsv4_configuration
365-
boot_helper_do_mount 'rpc_pipefs' '/var/lib/nfs/rpc_pipefs'
366-
boot_helper_do_mount 'nfsd' '/proc/fs/nfsd'
377+
boot_helper_mount "$MOUNT_PATH_RPC_PIPEFS"
378+
boot_helper_mount "$MOUNT_PATH_NFSD"
367379
}
368380

369381
boot_main_exportfs() {
370382

371383
log 'exporting filesystems'
372-
/usr/sbin/exportfs -arv
384+
$PATH_EXPORTFS -arv
373385
stop_on_failure 'exportfs failed'
374386
}
375387

376388
boot_main_mountd() {
377389

378390
local versionFlags
379-
IFS=' ' read -r -a versionFlags <<< "$(boot_helper_get_version_flags)"
391+
read -r -a versionFlags <<< "$(boot_helper_get_version_flags)"
380392
local -r port=$(get_reqd_mountd_port)
381393
local -r version=$(get_reqd_nfs_version)
382394
local -r args=('--debug' 'all' '--port' "$port" "${versionFlags[@]}")
383395

384396
# yes, rpc.mountd is required even for NFS v4: https://forums.gentoo.org/viewtopic-p-7724856.html#7724856
385397
log "starting rpc.mountd for NFS version $version on port $port"
386-
/usr/sbin/rpc.mountd "${args[@]}"
398+
$PATH_MOUNTD "${args[@]}"
387399
stop_on_failure 'rpc.mountd failed'
388400
}
389401

@@ -393,7 +405,7 @@ boot_main_rpcbind() {
393405
# it's a bug in either nfs-utils on the kernel, and the code of both is over my head.
394406
# so as a workaround we start rpcbind now and (in v4-only scenarios) kill it after nfsd starts up
395407
log 'starting rpcbind'
396-
/sbin/rpcbind -ds
408+
$PATH_RPCBIND -ds
397409
stop_on_failure 'rpcbind failed'
398410
}
399411

@@ -408,25 +420,25 @@ boot_main_statd() {
408420
local -r args=('--no-notify' '--port' "$inPort" '--outgoing-port' "$outPort")
409421

410422
log "starting statd on port $inPort (outgoing connections on port $outPort)"
411-
/usr/sbin/rpc.statd "${args[@]}"
423+
$PATH_STATD "${args[@]}"
412424
stop_on_failure 'statd failed'
413425
}
414426

415427
boot_main_nfsd() {
416428

417429
local versionFlags
418-
IFS=' ' read -r -a versionFlags <<< "$(boot_helper_get_version_flags)"
430+
read -r -a versionFlags <<< "$(boot_helper_get_version_flags)"
419431
local -r threads=$(get_reqd_nfsd_threads)
420432
local -r port=$(get_reqd_nfsd_port)
421433
local -r version=$(get_reqd_nfs_version)
422434
local -r args=('--debug' 8 '--port' "$port" "${versionFlags[@]}" "$threads")
423435

424436
log "starting rpc.nfsd on port $port with version $version and $threads server thread(s)"
425-
/usr/sbin/rpc.nfsd "${args[@]}"
437+
$PATH_NFSD "${args[@]}"
426438
stop_on_failure 'rpc.nfsd failed'
427439

428440
if [ -z "$(is_nfs3_enabled)" ]; then
429-
stop_process_if_running 'rpcbind'
441+
kill_process_if_running "$PATH_RPCBIND"
430442
fi
431443
}
432444

@@ -438,26 +450,31 @@ boot_main_nfsd() {
438450
init() {
439451

440452
logHeader 'setting up'
453+
441454
init_trap
442455
init_exports
443456
init_assertions
457+
458+
log 'setup complete'
444459
}
445460

446461
boot() {
447462

448463
logHeader 'starting services'
464+
449465
boot_main_mounts
450466
boot_main_rpcbind
451467
boot_main_exportfs
452468
boot_main_mountd
453469
boot_main_statd
454470
boot_main_nfsd
471+
472+
logHeader "ready and waiting for connections on port $(get_reqd_nfsd_port)"
455473
}
456474

457475
hangout() {
458476

459-
logHeader "server ready and waiting for connections on port $(get_reqd_nfsd_port)"
460-
477+
# wait forever or until we get SIGTERM or SIGINT
461478
# https://stackoverflow.com/a/41655546/229920
462479
# https://stackoverflow.com/a/27694965/229920
463480
while :; do sleep 2073600 & wait; done

0 commit comments

Comments
 (0)