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

Commit 3a7c17f

Browse files
committed
tidy up main boot logic and CPU count detection
1 parent e5cec8c commit 3a7c17f

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

entrypoint.sh

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ readonly ENV_VAR_NFS_PORT_STATD_OUT='NFS_PORT_STATD_OUT'
3434
readonly ENV_VAR_NFS_VERSION='NFS_VERSION'
3535
readonly ENV_VAR_NFS_LOG_LEVEL='NFS_LOG_LEVEL'
3636

37-
readonly DEFAULT_NFS_SERVER_THREAD_COUNT="$(grep -Ec ^processor /proc/cpuinfo)"
3837
readonly DEFAULT_NFS_PORT=2049
3938
readonly DEFAULT_NFS_PORT_MOUNTD=32767
4039
readonly DEFAULT_NFS_PORT_STATD_IN=32765
@@ -225,11 +224,6 @@ get_requested_nfs_version() {
225224
echo "${!ENV_VAR_NFS_VERSION:-$DEFAULT_NFS_VERSION}"
226225
}
227226

228-
get_requested_count_nfsd_threads() {
229-
230-
echo "${!ENV_VAR_NFS_SERVER_THREAD_COUNT:-$DEFAULT_NFS_SERVER_THREAD_COUNT}"
231-
}
232-
233227
get_requested_port_mountd() {
234228

235229
echo "${!ENV_VAR_NFS_PORT_MOUNTD:-$DEFAULT_NFS_PORT_MOUNTD}"
@@ -308,6 +302,17 @@ is_debug_requested() {
308302
return 1
309303
}
310304

305+
get_requested_count_nfsd_threads() {
306+
307+
if [[ -n "${!ENV_VAR_NFS_SERVER_THREAD_COUNT}" ]]; then
308+
echo "${!ENV_VAR_NFS_SERVER_THREAD_COUNT}"
309+
else
310+
local -r cpu_count="$(grep -Ec ^processor /proc/cpuinfo)"
311+
on_failure bail 'unable to detect CPU count. set NFS_SERVER_THREAD_COUNT environment variable'
312+
echo "$cpu_count";
313+
fi
314+
}
315+
311316
######################################################################################
312317
### runtime configuration assertions
313318
######################################################################################
@@ -383,6 +388,13 @@ assert_cap_sysadmin() {
383388
fi
384389
}
385390

391+
assert_log_level() {
392+
393+
if ! echo "${!ENV_VAR_NFS_LOG_LEVEL}" | grep -Eqi "^$|^DEBUG$"; then
394+
bail "the only acceptable value for $ENV_VAR_NFS_LOG_LEVEL is DEBUG"
395+
fi
396+
}
397+
386398

387399
######################################################################################
388400
### initialization
@@ -470,6 +482,7 @@ init_assertions() {
470482
assert_port "$ENV_VAR_NFS_PORT_STATD_OUT"
471483
assert_nfs_version
472484
assert_nfsd_threads
485+
assert_log_level
473486

474487
# check kernel modules
475488
assert_kernel_mod nfs
@@ -527,15 +540,35 @@ boot_helper_get_version_flags() {
527540
echo "${flags[@]}"
528541
}
529542

530-
boot_helper_check_backgrounded_process() {
543+
boot_helper_start_daemon() {
544+
545+
local -r msg="$1"
546+
local -r daemon="$2"
547+
shift 2
548+
local -r daemon_args=("$@")
549+
550+
log "$msg"
551+
"$daemon" "${daemon_args[@]}"
552+
on_failure stop "$daemon failed"
553+
}
554+
555+
boot_helper_start_non_daemon() {
556+
557+
local -r msg="$1"
558+
local -r process="$2"
559+
shift 2
560+
local -r process_args=("$@")
561+
562+
log "$msg"
563+
"$process" "${process_args[@]}" &
531564

532565
local -r bg_pid=$!
533566

534567
# somewhat arbitrary assumption that if the process isn't dead already, it will die within 1/20 of a second. for our
535568
# purposes this works just fine, but if someone has a better solution please open a PR.
536569
sleep .05
537570
kill -0 $bg_pid 2> /dev/null
538-
on_failure stop "$1 failed"
571+
on_failure stop "$process failed"
539572
}
540573

541574
######################################################################################
@@ -556,9 +589,7 @@ boot_main_exportfs() {
556589
args+=('-v')
557590
fi
558591

559-
log 'exporting filesystem(s)'
560-
$PATH_BIN_EXPORTFS "${args[@]}"
561-
on_failure stop 'exportfs failed'
592+
boot_helper_start_daemon 'exporting filesystem(s)' $PATH_BIN_EXPORTFS "${args[@]}"
562593
}
563594

564595
boot_main_mountd() {
@@ -572,19 +603,16 @@ boot_main_mountd() {
572603
fi
573604

574605
# yes, rpc.mountd is required even for NFS v4: https://forums.gentoo.org/viewtopic-p-7724856.html#7724856
575-
log "starting rpc.mountd on port $port"
576-
$PATH_BIN_MOUNTD "${args[@]}"
577-
on_failure stop 'rpc.mountd failed'
606+
boot_helper_start_daemon "starting rpc.mountd on port $port" $PATH_BIN_MOUNTD "${args[@]}"
578607
}
579608

580609
boot_main_rpcbind() {
581610

582611
# rpcbind isn't required for NFSv4, but if it's not running then nfsd takes over 5 minutes to start up.
583612
# it's a bug in either nfs-utils or the kernel, and the code of both is over my head.
584613
# so as a workaround we start rpcbind now and (in v4-only scenarios) kill it after nfsd starts up
585-
log 'starting rpcbind'
586-
$PATH_BIN_RPCBIND -ds
587-
on_failure stop 'rpcbind failed'
614+
local -r args=('-ds')
615+
boot_helper_start_daemon 'starting rpcbind' $PATH_BIN_RPCBIND "${args[@]}"
588616
}
589617

590618
boot_main_idmapd() {
@@ -593,14 +621,14 @@ boot_main_idmapd() {
593621
return
594622
fi
595623

596-
local args=('-f -S')
624+
local args=('-S')
625+
local func=boot_helper_start_daemon
597626
if is_debug_requested; then
598-
args+=('-vvv')
627+
args+=('-vvv' '-f')
628+
func=boot_helper_start_non_daemon
599629
fi
600630

601-
log 'starting idmapd'
602-
$PATH_BIN_IDMAPD "${args[@]}" &
603-
boot_helper_check_backgrounded_process $PATH_BIN_IDMAPD
631+
$func 'starting idmapd' $PATH_BIN_IDMAPD "${args[@]}"
604632
}
605633

606634
boot_main_statd() {
@@ -613,9 +641,7 @@ boot_main_statd() {
613641
local -r port_out=$(get_requested_port_statd_out)
614642
local -r args=('--no-notify' '--port' "$port_in" '--outgoing-port' "$port_out")
615643

616-
log "starting statd on port $port_in (outgoing connections from port $port_out)"
617-
$PATH_BIN_STATD "${args[@]}"
618-
on_failure stop 'statd failed'
644+
boot_helper_start_daemon "starting statd on port $port_in (outgoing from port $port_out)" $PATH_BIN_STATD "${args[@]}"
619645
}
620646

621647
boot_main_nfsd() {
@@ -630,9 +656,7 @@ boot_main_nfsd() {
630656
args+=('--debug')
631657
fi
632658

633-
log "starting rpc.nfsd on port $port with $threads server thread(s)"
634-
$PATH_BIN_NFSD "${args[@]}"
635-
on_failure stop 'rpc.nfsd failed'
659+
boot_helper_start_daemon "starting rpc.nfsd on port $port with $threads server thread(s)" $PATH_BIN_NFSD "${args[@]}"
636660

637661
if ! is_nfs3_enabled; then
638662
kill_process_if_running "$PATH_BIN_RPCBIND"
@@ -650,9 +674,7 @@ boot_main_svcgssd() {
650674
args+=('-vvv')
651675
fi
652676

653-
log 'starting rpc.svcgssd'
654-
$PATH_BIN_RPC_SVCGSSD "${args[@]}" &
655-
boot_helper_check_backgrounded_process $PATH_BIN_RPC_SVCGSSD
677+
boot_helper_start_non_daemon 'starting rpc.svcgssd' $PATH_BIN_RPC_SVCGSSD "${args[@]}"
656678
}
657679

658680

0 commit comments

Comments
 (0)