@@ -69,6 +69,7 @@ readonly STATE_NFSD_PORT='nfsd_port'
69
69
readonly STATE_MOUNTD_PORT=' mountd_port'
70
70
readonly STATE_STATD_PORT_IN=' statd_port_in'
71
71
readonly STATE_STATD_PORT_OUT=' statd_port_out'
72
+ readonly STATE_NFS_VERSION=' nfs_version'
72
73
73
74
# "state" is our only global variable, which is an associative array of normalized data
74
75
declare -A state
@@ -246,7 +247,7 @@ stop() {
246
247
247
248
get_requested_nfs_version () {
248
249
249
- echo " ${! ENV_VAR_NFS_VERSION :- $DEFAULT_NFS_VERSION } "
250
+ echo " ${state[$STATE_NFS_VERSION] } "
250
251
}
251
252
252
253
get_requested_port_mountd () {
@@ -289,7 +290,10 @@ is_kernel_module_loaded() {
289
290
local -r module=$1
290
291
291
292
if lsmod | grep -Eq " ^$module \\ s+" || [[ -d " /sys/module/$module " ]]; then
292
- log " kernel module $module is loaded"
293
+
294
+ if is_logging_debug; then
295
+ log " kernel module $module is loaded"
296
+ fi
293
297
return 0
294
298
fi
295
299
@@ -359,32 +363,6 @@ assert_port() {
359
363
fi
360
364
}
361
365
362
- assert_nfs_version () {
363
-
364
- local -r requested_version=" $( get_requested_nfs_version) "
365
-
366
- echo " $requested_version " | grep -Eq ' ^3$|^4(\.[1-2])?$'
367
- on_failure bail " please set $ENV_VAR_NFS_VERSION to one of: 4.2, 4.1, 4, 3"
368
-
369
- if ! is_nfs3_enabled && [[ " $requested_version " = ' 3' ]]; then
370
- bail ' you cannot simultaneously enable and disable NFS version 3'
371
- fi
372
- }
373
-
374
- assert_at_least_one_export () {
375
-
376
- # ensure /etc/exports has at least one line
377
- grep -Evq " $REGEX_EXPORTS_LINES_TO_SKIP " $PATH_FILE_ETC_EXPORTS
378
- on_failure bail " $PATH_FILE_ETC_EXPORTS has no exports"
379
- }
380
-
381
- assert_cap_sysadmin () {
382
-
383
- if ! has_linux_capability ' cap_sys_admin' ; then
384
- bail ' missing CAP_SYS_ADMIN. be sure to run this image with --cap-add SYS_ADMIN or --privileged'
385
- fi
386
- }
387
-
388
366
389
367
# #####################################################################################
390
368
# ## initialization
@@ -401,10 +379,10 @@ init_state_logging() {
401
379
402
380
state[$STATE_LOG_LEVEL ]=$normalized_log_level ;
403
381
state[$STATE_IS_LOGGING_INFO ]=1
404
- state[$STATE_IS_LOGGING_DEBUG ]=0
405
382
406
383
if [[ $normalized_log_level = " $LOG_LEVEL_DEBUG " ]]; then
407
384
state[$STATE_IS_LOGGING_DEBUG ]=1
385
+ log " log level set to $LOG_LEVEL_DEBUG "
408
386
fi
409
387
}
410
388
@@ -451,6 +429,20 @@ init_state_ports() {
451
429
state[$STATE_STATD_PORT_OUT ]=${! ENV_VAR_NFS_PORT_STATD_OUT:- $DEFAULT_NFS_PORT_STATD_OUT }
452
430
}
453
431
432
+ init_state_nfs_version () {
433
+
434
+ local -r requested_version=" ${! ENV_VAR_NFS_VERSION:- $DEFAULT_NFS_VERSION } "
435
+
436
+ echo " $requested_version " | grep -Eq ' ^3$|^4(\.[1-2])?$'
437
+ on_failure bail " please set $ENV_VAR_NFS_VERSION to one of: 4.2, 4.1, 4, 3"
438
+
439
+ if ! is_nfs3_enabled && [[ " $requested_version " = ' 3' ]]; then
440
+ bail ' you cannot simultaneously enable and disable NFS version 3'
441
+ fi
442
+
443
+ state[$STATE_NFS_VERSION ]=$requested_version
444
+ }
445
+
454
446
init_trap () {
455
447
456
448
trap stop SIGTERM SIGINT
@@ -460,89 +452,90 @@ init_exports() {
460
452
461
453
# first, see if it's bind-mounted
462
454
if mount | grep -Eq " ^[^ ]+ on $PATH_FILE_ETC_EXPORTS type " ; then
455
+
463
456
if is_logging_debug; then
464
457
log " $PATH_FILE_ETC_EXPORTS is bind-mounted"
465
458
fi
466
- return
467
- fi
468
459
469
460
# maybe it's baked-in to the image
470
- if [[ -f $PATH_FILE_ETC_EXPORTS && -r $PATH_FILE_ETC_EXPORTS && -s $PATH_FILE_ETC_EXPORTS ]]; then
461
+ elif [[ -f $PATH_FILE_ETC_EXPORTS && -r $PATH_FILE_ETC_EXPORTS && -s $PATH_FILE_ETC_EXPORTS ]]; then
462
+
471
463
if is_logging_debug; then
472
464
log " $PATH_FILE_ETC_EXPORTS is baked into the image"
473
465
fi
474
- return
475
- fi
476
466
477
- local count_valid_exports=0
478
- local exports=' '
479
- local candidate_export_vars
480
- local candidate_export_var
467
+ # fallback to environment variables
468
+ else
481
469
482
- # collect all candidate environment variable names
483
- candidate_export_vars=$( compgen -A variable | grep -E ' NFS_EXPORT_[0-9]+' | sort)
484
- on_failure bail ' failed to detect NFS_EXPORT_* variables'
470
+ local count_valid_exports=0
471
+ local exports=' '
472
+ local candidate_export_vars
473
+ local candidate_export_var
485
474
486
- if [[ -z " $candidate_export_vars " ]] ; then
487
- bail " please provide $PATH_FILE_ETC_EXPORTS to the container or set at least one NFS_EXPORT_* environment variable "
488
- fi
475
+ # collect all candidate environment variable names
476
+ candidate_export_vars= $( compgen -A variable | grep -E ' NFS_EXPORT_[0-9]+ ' | sort )
477
+ on_failure bail ' failed to detect NFS_EXPORT_* variables '
489
478
490
- log " building $PATH_FILE_ETC_EXPORTS from environment variables"
479
+ if [[ -z " $candidate_export_vars " ]]; then
480
+ bail " please provide $PATH_FILE_ETC_EXPORTS to the container or set at least one NFS_EXPORT_* environment variable"
481
+ fi
491
482
492
- for candidate_export_var in $candidate_export_vars ; do
483
+ log " building $PATH_FILE_ETC_EXPORTS from environment variables "
493
484
494
- local line= " ${ ! candidate_export_var} "
485
+ for candidate_export_var in $candidate_export_vars ; do
495
486
496
- # skip comments and empty lines
497
- if [[ " $line " =~ $REGEX_EXPORTS_LINES_TO_SKIP ]]; then
498
- log_warning " skipping $candidate_export_var environment variable since it contains only whitespace or a comment"
499
- continue ;
500
- fi
487
+ local line=" ${! candidate_export_var} "
501
488
502
- local line_as_array
503
- read -r -a line_as_array <<< " $line"
504
- local dir=" ${line_as_array[0]} "
489
+ # skip comments and empty lines
490
+ if [[ " $line " =~ $REGEX_EXPORTS_LINES_TO_SKIP ]]; then
491
+ log_warning " skipping $candidate_export_var environment variable since it contains only whitespace or a comment"
492
+ continue ;
493
+ fi
505
494
506
- if [[ ! -d " $dir " ]]; then
507
- log_warning " skipping $candidate_export_var environment variable since $dir is not a container directory"
508
- continue
509
- fi
495
+ local line_as_array
496
+ read -r -a line_as_array <<< " $line"
497
+ local dir=" ${line_as_array[0]} "
510
498
511
- if [[ $count_valid_exports -gt 0 ]]; then
512
- exports=$exports $' \n '
513
- fi
499
+ if [[ ! -d " $dir " ]]; then
500
+ log_warning " skipping $candidate_export_var environment variable since $dir is not a container directory"
501
+ continue
502
+ fi
503
+
504
+ if [[ $count_valid_exports -gt 0 ]]; then
505
+ exports=$exports $' \n '
506
+ fi
507
+
508
+ exports=$exports$line
514
509
515
- exports= $exports$line
510
+ (( count_valid_exports ++ ))
516
511
517
- (( count_valid_exports ++ ))
512
+ done
518
513
519
- done
514
+ log " collected $count_valid_exports valid export(s) from NFS_EXPORT_* environment variables "
520
515
521
- log " collected $count_valid_exports valid export(s) from NFS_EXPORT_* environment variables"
516
+ if [[ $count_valid_exports -eq 0 ]]; then
517
+ bail ' no valid exports'
518
+ fi
522
519
523
- if [[ $count_valid_exports -eq 0 ]] ; then
524
- bail ' no valid exports '
520
+ echo " $exports " > $PATH_FILE_ETC_EXPORTS
521
+ on_failure bail " unable to write to $PATH_FILE_ETC_EXPORTS "
525
522
fi
526
523
527
- echo " $exports " > $PATH_FILE_ETC_EXPORTS
528
- on_failure bail " unable to write to $PATH_FILE_ETC_EXPORTS "
524
+ # make sure we have at least one export
525
+ grep -Evq " $REGEX_EXPORTS_LINES_TO_SKIP " $PATH_FILE_ETC_EXPORTS
526
+ on_failure bail " $PATH_FILE_ETC_EXPORTS has no exports"
529
527
}
530
528
531
- init_assertions () {
529
+ init_runtime_assertions () {
532
530
533
- # validate any user-supplied environment variables
534
- assert_nfs_version
531
+ if ! has_linux_capability ' cap_sys_admin' ; then
532
+ bail ' missing CAP_SYS_ADMIN. be sure to run this image with --cap-add SYS_ADMIN or --privileged'
533
+ fi
535
534
536
535
# check kernel modules
537
536
assert_kernel_mod nfs
538
537
assert_kernel_mod nfsd
539
538
540
- # make sure we have at least one export
541
- assert_at_least_one_export
542
-
543
- # ensure we have CAP_SYS_ADMIN
544
- assert_cap_sysadmin
545
-
546
539
# perform Kerberos assertions
547
540
if is_kerberos_requested; then
548
541
@@ -566,9 +559,9 @@ boot_helper_mount() {
566
559
567
560
if is_logging_debug; then
568
561
args+=(' -vvv' )
562
+ log " mounting $type filesystem onto $path "
569
563
fi
570
564
571
- log " mounting $type filesystem onto $path "
572
565
mount " ${args[@]} "
573
566
on_failure stop " unable to mount $type filesystem onto $path "
574
567
}
@@ -638,7 +631,7 @@ boot_main_exportfs() {
638
631
args+=(' -v' )
639
632
fi
640
633
641
- boot_helper_start_daemon ' exporting filesystem(s) ' $PATH_BIN_EXPORTFS " ${args[@]} "
634
+ boot_helper_start_daemon ' starting exportfs ' $PATH_BIN_EXPORTFS " ${args[@]} "
642
635
}
643
636
644
637
boot_main_mountd () {
@@ -809,10 +802,11 @@ init() {
809
802
log_header ' setting up ...'
810
803
811
804
init_state_logging
812
- init_exports
813
805
init_state_nfsd_thread_count
814
806
init_state_ports
815
- init_assertions
807
+ init_state_nfs_version
808
+ init_exports
809
+ init_runtime_assertions
816
810
init_trap
817
811
818
812
log ' setup complete'
0 commit comments