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

Commit 3919a98

Browse files
committed
Merge branch 'develop'
2 parents b94f257 + 148363c commit 3919a98

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [2.1.0] - 2019-10-31
8+
### Added
9+
* Ability to automatically load kernel modules. ([#18](https://github.com/ehough/docker-nfs-server/issues/18)). Credit to [@andyneff](https://github.com/andyneff).
10+
### Fixed
11+
* Minor bugs in `entrypoint.sh`
12+
713
## [2.0.0] - 2019-01-31
814

915
### Changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This is the only containerized NFS server that offers **all** of the following f
2828
* [NFSv4 user ID mapping](doc/feature/nfsv4-user-id-mapping.md)
2929
* [AppArmor integration](doc/feature/apparmor.md)
3030
* Advanced
31+
* [automatically load required kernel modules](doc/feature/auto-load-kernel-modules.md)
3132
* [custom server ports](doc/advanced/ports.md)
3233
* [custom NFS versions offered](doc/advanced/nfs-versions.md)
3334
* [performance tuning](doc/advanced/performance-tuning.md)
@@ -42,7 +43,11 @@ This is the only containerized NFS server that offers **all** of the following f
4243
- `nfsd`
4344
- `rpcsec_gss_krb5` (*only if Kerberos is used*)
4445

45-
Usually you can enable these modules with: `modprobe {nfs,nfsd,rpcsec_gss_krb5}`
46+
You can manually enable these modules on the Docker host with:
47+
48+
`modprobe {nfs,nfsd,rpcsec_gss_krb5}`
49+
50+
or you can just allow the container to [load them automatically](doc/feature/auto-load-kernel-modules.md).
4651
1. The container will need to run with `CAP_SYS_ADMIN` (or `--privileged`). This is necessary as the server needs to mount several filesystems *inside* the container to support its operation, and performing mounts from inside a container is impossible without these capabilities.
4752
1. The container will need local access to the files you'd like to serve via NFS. You can use Docker volumes, bind mounts, files baked into a custom image, or virtually any other means of supplying files to a Docker container.
4853

@@ -145,6 +150,7 @@ If you pay close attention to each of the items in this section, the server shou
145150

146151
## Advanced
147152

153+
* [automatically load required kernel modules](doc/feature/auto-load-kernel-modules.md)
148154
* [customizing which ports are used](doc/advanced/ports.md)
149155
* [customizing NFS versions offered](doc/advanced/nfs-versions.md)
150156
* [performance tuning](doc/advanced/performance-tuning.md)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Automatically load required kernel modules
2+
3+
*Credit to Andy Neff [@andyneff](https://github.com/andyneff) for this idea.*
4+
5+
As noted in the `README`, the Docker host kernel needs a few modules for proper operation of an NFS server. You can manually enable these on the host - i.e. with `modprobe` - or you can allow the container to do this on your behalf. Here's how:
6+
7+
1. Add `--cap-add SYS_MODULE` to your Docker run command to allow the container to load/unload kernel modules.
8+
1. Bind-mount the Docker host's `/lib/modules` directory into the container. e.g. `-v /lib/modules:/lib/modules:ro`
9+
10+
Here's an example `docker-compose.yml`:
11+
12+
```YAML
13+
version: 3
14+
services:
15+
nfs:
16+
image: erichough/nfs-server
17+
volumes:
18+
- /path/to/share:/nfs
19+
- /path/to/exports.txt:/etc/exports:ro
20+
- /lib/modules:/lib/modules:ro
21+
cap_add:
22+
- SYS_ADMIN
23+
- SYS_MODULE
24+
ports:
25+
- 2049:2049
26+
```

entrypoint.sh

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,27 @@ is_idmapd_enabled() {
263263
return 1
264264
}
265265

266+
is_kernel_module_loaded() {
267+
268+
local -r module=$1
269+
270+
if lsmod | grep -Eq "^$module\\s+" || [[ -d "/sys/module/$module" ]]; then
271+
log "kernel module $module is loaded"
272+
return 0
273+
fi
274+
275+
log "kernel module $module is missing"
276+
return 1
277+
}
278+
279+
has_linux_capability() {
280+
281+
if capsh --print | grep -Eq "^Current: = .*,?${1}(,|$)"; then
282+
return 0
283+
fi
284+
285+
return 1
286+
}
266287

267288
######################################################################################
268289
### runtime configuration assertions
@@ -279,11 +300,21 @@ assert_kernel_mod() {
279300

280301
local -r module=$1
281302

282-
log "checking for presence of kernel module: $module"
303+
if is_kernel_module_loaded "$module"; then
304+
return
305+
fi
306+
307+
if [[ ! -d /lib/modules ]] || ! has_linux_capability 'sys_module'; then
308+
bail "$module module is not loaded in the Docker host's kernel (try: modprobe $module)"
309+
fi
283310

284-
lsmod | grep -Eq "^$module\\s+" || [ -d "/sys/module/$module" ]
311+
log "attempting to load kernel module $module"
312+
modprobe -v "$module"
313+
on_failure bail "unable to dynamically load kernel module $module. try modproble $module on the Docker host"
285314

286-
on_failure bail "$module module is not loaded in the Docker host's kernel (try: modprobe $module)"
315+
if ! is_kernel_module_loaded "$module"; then
316+
bail "modprobe claims that it loaded kernel module $module, but it still appears to be missing"
317+
fi
287318
}
288319

289320
assert_port() {
@@ -303,7 +334,7 @@ assert_nfs_version() {
303334
echo "$requested_version" | grep -Eq '^3|4(\.[1-2])?$'
304335
on_failure bail "please set $ENV_VAR_NFS_VERSION to one of: 4.2, 4.1, 4, 3"
305336

306-
if [[ ( ! is_nfs3_enabled ) && "$requested_version" = '3' ]]; then
337+
if ! is_nfs3_enabled && [[ "$requested_version" = '3' ]]; then
307338
bail 'you cannot simultaneously enable and disable NFS version 3'
308339
fi
309340
}
@@ -322,10 +353,11 @@ assert_at_least_one_export() {
322353
on_failure bail "$PATH_FILE_ETC_EXPORTS has no exports"
323354
}
324355

325-
assert_linux_capabilities() {
356+
assert_cap_sysadmin() {
326357

327-
capsh --print | grep -Eq "^Current: = .*,?cap_sys_admin(,|$)"
328-
on_failure bail 'missing CAP_SYS_ADMIN. be sure to run this image with --cap-add SYS_ADMIN or --privileged'
358+
if ! has_linux_capability 'cap_sys_admin'; then
359+
bail 'missing CAP_SYS_ADMIN. be sure to run this image with --cap-add SYS_ADMIN or --privileged'
360+
fi
329361
}
330362

331363

@@ -424,7 +456,7 @@ init_assertions() {
424456
assert_at_least_one_export
425457

426458
# ensure we have CAP_SYS_ADMIN
427-
assert_linux_capabilities
459+
assert_cap_sysadmin
428460

429461
# perform Kerberos assertions
430462
if is_kerberos_enabled; then
@@ -588,7 +620,7 @@ summarize_nfs_versions() {
588620
;;
589621
esac
590622

591-
if [[ is_nfs3_enabled && "$reqd_version" =~ ^4 ]]; then
623+
if is_nfs3_enabled && [[ "$reqd_version" =~ ^4 ]]; then
592624
versions="$versions, 3"
593625
fi
594626

0 commit comments

Comments
 (0)