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

Commit 09874c9

Browse files
committed
move NFS_VERSION into state and further declutter non-debug output
1 parent fc93e6e commit 09874c9

File tree

1 file changed

+77
-83
lines changed

1 file changed

+77
-83
lines changed

entrypoint.sh

Lines changed: 77 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ readonly STATE_NFSD_PORT='nfsd_port'
6969
readonly STATE_MOUNTD_PORT='mountd_port'
7070
readonly STATE_STATD_PORT_IN='statd_port_in'
7171
readonly STATE_STATD_PORT_OUT='statd_port_out'
72+
readonly STATE_NFS_VERSION='nfs_version'
7273

7374
# "state" is our only global variable, which is an associative array of normalized data
7475
declare -A state
@@ -246,7 +247,7 @@ stop() {
246247

247248
get_requested_nfs_version() {
248249

249-
echo "${!ENV_VAR_NFS_VERSION:-$DEFAULT_NFS_VERSION}"
250+
echo "${state[$STATE_NFS_VERSION]}"
250251
}
251252

252253
get_requested_port_mountd() {
@@ -289,7 +290,10 @@ is_kernel_module_loaded() {
289290
local -r module=$1
290291

291292
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
293297
return 0
294298
fi
295299

@@ -359,32 +363,6 @@ assert_port() {
359363
fi
360364
}
361365

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-
388366

389367
######################################################################################
390368
### initialization
@@ -401,10 +379,10 @@ init_state_logging() {
401379

402380
state[$STATE_LOG_LEVEL]=$normalized_log_level;
403381
state[$STATE_IS_LOGGING_INFO]=1
404-
state[$STATE_IS_LOGGING_DEBUG]=0
405382

406383
if [[ $normalized_log_level = "$LOG_LEVEL_DEBUG" ]]; then
407384
state[$STATE_IS_LOGGING_DEBUG]=1
385+
log "log level set to $LOG_LEVEL_DEBUG"
408386
fi
409387
}
410388

@@ -451,6 +429,20 @@ init_state_ports() {
451429
state[$STATE_STATD_PORT_OUT]=${!ENV_VAR_NFS_PORT_STATD_OUT:-$DEFAULT_NFS_PORT_STATD_OUT}
452430
}
453431

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+
454446
init_trap() {
455447

456448
trap stop SIGTERM SIGINT
@@ -460,89 +452,90 @@ init_exports() {
460452

461453
# first, see if it's bind-mounted
462454
if mount | grep -Eq "^[^ ]+ on $PATH_FILE_ETC_EXPORTS type "; then
455+
463456
if is_logging_debug; then
464457
log "$PATH_FILE_ETC_EXPORTS is bind-mounted"
465458
fi
466-
return
467-
fi
468459

469460
# 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+
471463
if is_logging_debug; then
472464
log "$PATH_FILE_ETC_EXPORTS is baked into the image"
473465
fi
474-
return
475-
fi
476466

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
481469

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
485474

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'
489478

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
491482

492-
for candidate_export_var in $candidate_export_vars; do
483+
log "building $PATH_FILE_ETC_EXPORTS from environment variables"
493484

494-
local line="${!candidate_export_var}"
485+
for candidate_export_var in $candidate_export_vars; do
495486

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}"
501488

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
505494

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]}"
510498

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
514509

515-
exports=$exports$line
510+
(( count_valid_exports++ ))
516511

517-
(( count_valid_exports++ ))
512+
done
518513

519-
done
514+
log "collected $count_valid_exports valid export(s) from NFS_EXPORT_* environment variables"
520515

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
522519

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"
525522
fi
526523

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"
529527
}
530528

531-
init_assertions() {
529+
init_runtime_assertions() {
532530

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
535534

536535
# check kernel modules
537536
assert_kernel_mod nfs
538537
assert_kernel_mod nfsd
539538

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-
546539
# perform Kerberos assertions
547540
if is_kerberos_requested; then
548541

@@ -566,9 +559,9 @@ boot_helper_mount() {
566559

567560
if is_logging_debug; then
568561
args+=('-vvv')
562+
log "mounting $type filesystem onto $path"
569563
fi
570564

571-
log "mounting $type filesystem onto $path"
572565
mount "${args[@]}"
573566
on_failure stop "unable to mount $type filesystem onto $path"
574567
}
@@ -638,7 +631,7 @@ boot_main_exportfs() {
638631
args+=('-v')
639632
fi
640633

641-
boot_helper_start_daemon 'exporting filesystem(s)' $PATH_BIN_EXPORTFS "${args[@]}"
634+
boot_helper_start_daemon 'starting exportfs' $PATH_BIN_EXPORTFS "${args[@]}"
642635
}
643636

644637
boot_main_mountd() {
@@ -809,10 +802,11 @@ init() {
809802
log_header 'setting up ...'
810803

811804
init_state_logging
812-
init_exports
813805
init_state_nfsd_thread_count
814806
init_state_ports
815-
init_assertions
807+
init_state_nfs_version
808+
init_exports
809+
init_runtime_assertions
816810
init_trap
817811

818812
log 'setup complete'

0 commit comments

Comments
 (0)