Skip to content

Commit

Permalink
improving /etc/exports construction from environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ehough committed Nov 9, 2017
1 parent 720efe3 commit dca5448
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This is the only containerized NFS server that offers **all** of the following f

The container requires you to supply it with your desired [NFS exports](https://linux.die.net/man/5/exports) upon startup. You have **two choices** for doing this:

1. **Bind mount an exports file into the container at `/etc/exports`**.
1. **Bind mount a full exports file into the container at `/etc/exports`**.

docker run \
-v /host/path/to/exports.txt:/etc/exports:ro \
Expand All @@ -33,15 +33,15 @@ The container requires you to supply it with your desired [NFS exports](https://
-p 2049:2049 \
erichough/nfs4-server:latest`
1. **Supply environment variable triplets to the container to allow it to construct `/etc/exports`**.
1. **Supply each line of `/etc/exports` as an environment variable**.

Each triplet should consist of `NFS_EXPORT_DIR_*`, `NFS_EXPORT_CLIENT_*`, and `NFS_EXPORT_OPTIONS_*`. You can add as many triplets as you'd like.
The container will look for environment variables that start with `NFS_EXPORT_` and end with an integer. e.g. `NFS_EXPORT_0`, `NFS_EXPORT_1`, etc.

docker run \
-e NFS_EXPORT_DIR_0=/nfs \
-e NFS_EXPORT_CLIENT_0=192.168.1.0/24 \
-e NFS_EXPORT_OPTIONS_0=rw,no_subtree_check,fsid=0 \
-v /host/files:/nfs \
-e NFS_EXPORT_0='/nfs/foo 192.168.1.0/24(ro,no_subtree_check)' \
-e NFS_EXPORT_1='/nfs/bar 123.123.123.123/32(rw,no_subtree_check)' \
-v /host/path/foo:/nfs/foo \
-v /host/path/bar:/nfs/bar \
--cap-add SYS_ADMIN \
-p 2049:2049 \
erichough/nfs4-server:latest`
Expand Down
44 changes: 19 additions & 25 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -246,54 +246,48 @@ init_trap() {
trap stop SIGTERM SIGINT
}

init_exports()
{
init_exports() {

if mount | grep -Eq '^[^ ]+ on /etc/exports type '; then
log '/etc/exports appears to be mounted via Docker'
return
fi

local collected=0
local exports=''
local candidateDirs
local candidateExportVariables

candidateDirs=$(compgen -A variable | grep -E 'NFS_EXPORT_DIR_[0-9]*')
exit_on_failure 'missing NFS_EXPORT_DIR_* environment variable(s)'
candidateExportVariables=$(compgen -A variable | grep -E 'NFS_EXPORT_[0-9]+' | sort)
exit_on_failure 'please bind mount /etc/exports or supply NFS_EXPORT_* environment variables'

log 'building /etc/exports'

for dir in $candidateDirs; do
for exportVariable in $candidateExportVariables; do

local index=${dir##*_}
local net=NFS_EXPORT_CLIENT_$index
local opt=NFS_EXPORT_OPTIONS_$index
local line=${!exportVariable}
local lineAsArray
IFS=' ' read -r -a lineAsArray <<< "$line"
local dir="${lineAsArray[0]}"

if [[ ! -d "${!dir}" ]]; then
log "skipping $dir (${!dir}) since it is not a directory"
if [[ ! -d "$dir" ]]; then
log "skipping $line since $dir is not a directory"
continue
fi

if [[ -n ${!net} ]] && [[ -n ${!opt} ]]; then

log "will export ${!dir} to ${!net} with options ${!opt}"
log "will export $line"

local line="${!dir} ${!net}(${!opt})"

if [[ $collected -gt 0 ]]; then
exports=$exports$'\n'
fi
if [[ $collected -gt 0 ]]; then
exports=$exports$'\n'
fi

exports=$exports$line
exports=$exports$line

(( collected++ ))
(( collected++ ))

else
log "skipping $dir (${!dir}) as it is missing domain and/or options. be sure to set both $net and $opt."
fi
done

if [[ $collected -eq 0 ]]; then
log 'no directories to export.'
log 'no valid exports'
exit 1
fi

Expand Down

0 comments on commit dca5448

Please sign in to comment.