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

Commit dca5448

Browse files
committed
improving /etc/exports construction from environment variables
1 parent 720efe3 commit dca5448

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This is the only containerized NFS server that offers **all** of the following f
2424

2525
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:
2626

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

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

38-
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.
38+
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.
3939

4040
docker run \
41-
-e NFS_EXPORT_DIR_0=/nfs \
42-
-e NFS_EXPORT_CLIENT_0=192.168.1.0/24 \
43-
-e NFS_EXPORT_OPTIONS_0=rw,no_subtree_check,fsid=0 \
44-
-v /host/files:/nfs \
41+
-e NFS_EXPORT_0='/nfs/foo 192.168.1.0/24(ro,no_subtree_check)' \
42+
-e NFS_EXPORT_1='/nfs/bar 123.123.123.123/32(rw,no_subtree_check)' \
43+
-v /host/path/foo:/nfs/foo \
44+
-v /host/path/bar:/nfs/bar \
4545
--cap-add SYS_ADMIN \
4646
-p 2049:2049 \
4747
erichough/nfs4-server:latest`

entrypoint.sh

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -246,54 +246,48 @@ init_trap() {
246246
trap stop SIGTERM SIGINT
247247
}
248248

249-
init_exports()
250-
{
249+
init_exports() {
250+
251251
if mount | grep -Eq '^[^ ]+ on /etc/exports type '; then
252252
log '/etc/exports appears to be mounted via Docker'
253253
return
254254
fi
255255

256256
local collected=0
257257
local exports=''
258-
local candidateDirs
258+
local candidateExportVariables
259259

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

263263
log 'building /etc/exports'
264264

265-
for dir in $candidateDirs; do
265+
for exportVariable in $candidateExportVariables; do
266266

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

271-
if [[ ! -d "${!dir}" ]]; then
272-
log "skipping $dir (${!dir}) since it is not a directory"
272+
if [[ ! -d "$dir" ]]; then
273+
log "skipping $line since $dir is not a directory"
273274
continue
274275
fi
275276

276-
if [[ -n ${!net} ]] && [[ -n ${!opt} ]]; then
277-
278-
log "will export ${!dir} to ${!net} with options ${!opt}"
277+
log "will export $line"
279278

280-
local line="${!dir} ${!net}(${!opt})"
281-
282-
if [[ $collected -gt 0 ]]; then
283-
exports=$exports$'\n'
284-
fi
279+
if [[ $collected -gt 0 ]]; then
280+
exports=$exports$'\n'
281+
fi
285282

286-
exports=$exports$line
283+
exports=$exports$line
287284

288-
(( collected++ ))
285+
(( collected++ ))
289286

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

295289
if [[ $collected -eq 0 ]]; then
296-
log 'no directories to export.'
290+
log 'no valid exports'
297291
exit 1
298292
fi
299293

0 commit comments

Comments
 (0)