@@ -58,6 +58,26 @@ readonly MOUNT_PATH_RPC_PIPEFS='/var/lib/nfs/rpc_pipefs'
58
58
59
59
readonly REGEX_EXPORTS_LINES_TO_SKIP=' ^\s*#|^\s*$'
60
60
61
+ readonly LOG_LEVEL_INFO=' INFO'
62
+ readonly LOG_LEVEL_DEBUG=' DEBUG'
63
+
64
+ readonly STATE_LOG_LEVEL=' log_level'
65
+ readonly STATE_IS_LOGGING_DEBUG=' is_logging_debug'
66
+ readonly STATE_IS_LOGGING_INFO=' is_logging_info'
67
+
68
+ # "state" is our only global variable, which is an associative array of normalized data
69
+ declare -A state
70
+
71
+
72
+ # #####################################################################################
73
+ # ## string utils
74
+ # #####################################################################################
75
+
76
+ toupper () {
77
+
78
+ echo " $1 " | awk ' { print toupper($0) }'
79
+ }
80
+
61
81
62
82
# #####################################################################################
63
83
# ## logging
@@ -84,7 +104,7 @@ log_header() {
84
104
85
105
echo "
86
106
==================================================================
87
- $( echo " $1 " | awk ' {print toupper($0)} ' )
107
+ $( toupper " $1 " )
88
108
=================================================================="
89
109
}
90
110
@@ -152,7 +172,7 @@ stop_mount() {
152
172
if mount | grep -Eq ^" $type on $path \\ s+" ; then
153
173
154
174
local args=()
155
- if is_debug_requested ; then
175
+ if is_logging_debug ; then
156
176
args+=(' -v' )
157
177
fi
158
178
args+=(" $path " )
@@ -176,7 +196,7 @@ stop_nfsd() {
176
196
stop_exportfs () {
177
197
178
198
local args=(' -ua' )
179
- if is_debug_requested ; then
199
+ if is_logging_debug ; then
180
200
args+=(' -v' )
181
201
fi
182
202
@@ -293,15 +313,6 @@ has_linux_capability() {
293
313
return 1
294
314
}
295
315
296
- is_debug_requested () {
297
-
298
- if echo " ${! ENV_VAR_NFS_LOG_LEVEL} " | grep -Eqi ' ^DEBUG$' ; then
299
- return 0
300
- fi
301
-
302
- return 1
303
- }
304
-
305
316
get_requested_count_nfsd_threads () {
306
317
307
318
if [[ -n " ${! ENV_VAR_NFS_SERVER_THREAD_COUNT} " ]]; then
@@ -313,6 +324,12 @@ get_requested_count_nfsd_threads() {
313
324
fi
314
325
}
315
326
327
+ is_logging_debug () {
328
+
329
+ [[ -n ${state[$STATE_IS_LOGGING_DEBUG]} ]] && return 0 || return 1
330
+ }
331
+
332
+
316
333
# #####################################################################################
317
334
# ## runtime configuration assertions
318
335
# #####################################################################################
@@ -400,6 +417,23 @@ assert_log_level() {
400
417
# ## initialization
401
418
# #####################################################################################
402
419
420
+ init_state_logging () {
421
+
422
+ # if the user didn't request a specific log level, the default is INFO
423
+ local -r normalized_log_level=$( toupper " ${! ENV_VAR_NFS_LOG_LEVEL:- $LOG_LEVEL_INFO } " )
424
+
425
+ if ! echo " $normalized_log_level " | grep -Eq ' DEBUG|INFO' ; then
426
+ bail " the only acceptable values for $ENV_VAR_NFS_LOG_LEVEL are: DEBUG, INFO"
427
+ fi
428
+
429
+ state[$STATE_LOG_LEVEL ]=$normalized_log_level ;
430
+ state[$STATE_IS_LOGGING_INFO ]=1
431
+
432
+ if [[ $normalized_log_level = " $LOG_LEVEL_DEBUG " ]]; then
433
+ state[$STATE_IS_LOGGING_DEBUG ]=1
434
+ fi
435
+ }
436
+
403
437
init_trap () {
404
438
405
439
trap stop SIGTERM SIGINT
@@ -409,13 +443,17 @@ init_exports() {
409
443
410
444
# first, see if it's bind-mounted
411
445
if mount | grep -Eq " ^[^ ]+ on $PATH_FILE_ETC_EXPORTS type " ; then
412
- log " $PATH_FILE_ETC_EXPORTS is bind-mounted"
446
+ if is_logging_debug; then
447
+ log " $PATH_FILE_ETC_EXPORTS is bind-mounted"
448
+ fi
413
449
return
414
450
fi
415
451
416
452
# maybe it's baked-in to the image
417
453
if [[ -f $PATH_FILE_ETC_EXPORTS && -r $PATH_FILE_ETC_EXPORTS && -s $PATH_FILE_ETC_EXPORTS ]]; then
418
- log " $PATH_FILE_ETC_EXPORTS is baked into the image"
454
+ if is_logging_debug; then
455
+ log " $PATH_FILE_ETC_EXPORTS is baked into the image"
456
+ fi
419
457
return
420
458
fi
421
459
@@ -482,7 +520,6 @@ init_assertions() {
482
520
assert_port " $ENV_VAR_NFS_PORT_STATD_OUT "
483
521
assert_nfs_version
484
522
assert_nfsd_threads
485
- assert_log_level
486
523
487
524
# check kernel modules
488
525
assert_kernel_mod nfs
@@ -515,7 +552,7 @@ boot_helper_mount() {
515
552
local -r type=$( basename " $path " )
516
553
local args=(' -t' " $type " " $path " )
517
554
518
- if is_debug_requested ; then
555
+ if is_logging_debug ; then
519
556
args+=(' -vvv' )
520
557
fi
521
558
@@ -585,7 +622,7 @@ boot_main_mounts() {
585
622
boot_main_exportfs () {
586
623
587
624
local args=(' -ar' )
588
- if is_debug_requested ; then
625
+ if is_logging_debug ; then
589
626
args+=(' -v' )
590
627
fi
591
628
@@ -598,7 +635,7 @@ boot_main_mountd() {
598
635
read -r -a version_flags <<< " $(boot_helper_get_version_flags)"
599
636
local -r port=$( get_requested_port_mountd)
600
637
local args=(' --port' " $port " " ${version_flags[@]} " )
601
- if is_debug_requested ; then
638
+ if is_logging_debug ; then
602
639
args+=(' --debug' ' all' )
603
640
fi
604
641
@@ -623,7 +660,7 @@ boot_main_idmapd() {
623
660
624
661
local args=(' -S' )
625
662
local func=boot_helper_start_daemon
626
- if is_debug_requested ; then
663
+ if is_logging_debug ; then
627
664
args+=(' -vvv' ' -f' )
628
665
func=boot_helper_start_non_daemon
629
666
fi
@@ -652,7 +689,7 @@ boot_main_nfsd() {
652
689
local -r port=$( get_requested_port_nfsd)
653
690
local args=(' --tcp' ' --udp' ' --port' " $port " " ${version_flags[@]} " " $threads " )
654
691
655
- if is_debug_requested ; then
692
+ if is_logging_debug ; then
656
693
args+=(' --debug' )
657
694
fi
658
695
@@ -670,7 +707,7 @@ boot_main_svcgssd() {
670
707
fi
671
708
672
709
local args=(' -f' )
673
- if is_debug_requested ; then
710
+ if is_logging_debug ; then
674
711
args+=(' -vvv' )
675
712
fi
676
713
@@ -716,7 +753,7 @@ summarize_exports() {
716
753
# if debug is enabled, read /var/lib/nfs/etab as it contains the "real" export data. but it also contains more
717
754
# information that most people will usually need to see
718
755
local file_to_read=" $PATH_FILE_ETC_EXPORTS "
719
- if is_debug_requested ; then
756
+ if is_logging_debug ; then
720
757
file_to_read=' /var/lib/nfs/etab'
721
758
fi
722
759
@@ -759,6 +796,7 @@ init() {
759
796
760
797
log_header ' setting up ...'
761
798
799
+ init_state_logging
762
800
init_exports
763
801
init_assertions
764
802
init_trap
0 commit comments