No functional changes here, just coding style and maintainability.
Docker-DCO-1.1-Signed-off-by: Andrew Page <admwiggin@gmail.com> (github: tianon)
| ... | ... |
@@ -1,4 +1,5 @@ |
| 1 | 1 |
#!/bin/bash |
| 2 |
+set -e |
|
| 2 | 3 |
|
| 3 | 4 |
# DinD: a wrapper script which allows docker to be run inside a docker container. |
| 4 | 5 |
# Original version by Jerome Petazzoni <jerome@dotcloud.com> |
| ... | ... |
@@ -12,29 +13,28 @@ |
| 12 | 12 |
# First, make sure that cgroups are mounted correctly. |
| 13 | 13 |
CGROUP=/sys/fs/cgroup |
| 14 | 14 |
|
| 15 |
-[ -d $CGROUP ] || |
|
| 16 |
- mkdir $CGROUP |
|
| 15 |
+mkdir -p "$CGROUP" |
|
| 17 | 16 |
|
| 18 |
-mountpoint -q $CGROUP || |
|
| 17 |
+if ! mountpoint -q "$CGROUP"; then |
|
| 19 | 18 |
mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup $CGROUP || {
|
| 20 |
- echo "Could not make a tmpfs mount. Did you use --privileged?" |
|
| 19 |
+ echo >&2 'Could not make a tmpfs mount. Did you use --privileged?' |
|
| 21 | 20 |
exit 1 |
| 22 | 21 |
} |
| 22 |
+fi |
|
| 23 | 23 |
|
| 24 |
-if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security |
|
| 25 |
-then |
|
| 26 |
- mount -t securityfs none /sys/kernel/security || {
|
|
| 27 |
- echo "Could not mount /sys/kernel/security." |
|
| 28 |
- echo "AppArmor detection and -privileged mode might break." |
|
| 29 |
- } |
|
| 24 |
+if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then |
|
| 25 |
+ mount -t securityfs none /sys/kernel/security || {
|
|
| 26 |
+ echo >&2 'Could not mount /sys/kernel/security.' |
|
| 27 |
+ echo >&2 'AppArmor detection and -privileged mode might break.' |
|
| 28 |
+ } |
|
| 30 | 29 |
fi |
| 31 | 30 |
|
| 32 | 31 |
# Mount the cgroup hierarchies exactly as they are in the parent system. |
| 33 |
-for SUBSYS in $(cut -d: -f2 /proc/1/cgroup) |
|
| 34 |
-do |
|
| 35 |
- [ -d $CGROUP/$SUBSYS ] || mkdir $CGROUP/$SUBSYS |
|
| 36 |
- mountpoint -q $CGROUP/$SUBSYS || |
|
| 37 |
- mount -n -t cgroup -o $SUBSYS cgroup $CGROUP/$SUBSYS |
|
| 32 |
+for SUBSYS in $(cut -d: -f2 /proc/1/cgroup); do |
|
| 33 |
+ mkdir -p "$CGROUP/$SUBSYS" |
|
| 34 |
+ if ! mountpoint -q $CGROUP/$SUBSYS; then |
|
| 35 |
+ mount -n -t cgroup -o "$SUBSYS" cgroup "$CGROUP/$SUBSYS" |
|
| 36 |
+ fi |
|
| 38 | 37 |
|
| 39 | 38 |
# The two following sections address a bug which manifests itself |
| 40 | 39 |
# by a cryptic "lxc-start: no ns_cgroup option specified" when |
| ... | ... |
@@ -49,26 +49,30 @@ do |
| 49 | 49 |
# Systemd and OpenRC (and possibly others) both create such a |
| 50 | 50 |
# cgroup. To avoid the aforementioned bug, we symlink "foo" to |
| 51 | 51 |
# "name=foo". This shouldn't have any adverse effect. |
| 52 |
- echo $SUBSYS | grep -q ^name= && {
|
|
| 53 |
- NAME=$(echo $SUBSYS | sed s/^name=//) |
|
| 54 |
- ln -s $SUBSYS $CGROUP/$NAME |
|
| 55 |
- } |
|
| 52 |
+ name="${SUBSYS#name=}"
|
|
| 53 |
+ if [ "$name" != "$SUBSYS" ]; then |
|
| 54 |
+ ln -s "$SUBSYS" "$CGROUP/$name" |
|
| 55 |
+ fi |
|
| 56 | 56 |
|
| 57 | 57 |
# Likewise, on at least one system, it has been reported that |
| 58 | 58 |
# systemd would mount the CPU and CPU accounting controllers |
| 59 | 59 |
# (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu" |
| 60 | 60 |
# but on a directory called "cpu,cpuacct" (note the inversion |
| 61 | 61 |
# in the order of the groups). This tries to work around it. |
| 62 |
- [ $SUBSYS = cpuacct,cpu ] && ln -s $SUBSYS $CGROUP/cpu,cpuacct |
|
| 62 |
+ if [ "$SUBSYS" = 'cpuacct,cpu' ]; then |
|
| 63 |
+ ln -s "$SUBSYS" "$CGROUP/cpu,cpuacct" |
|
| 64 |
+ fi |
|
| 63 | 65 |
done |
| 64 | 66 |
|
| 65 | 67 |
# Note: as I write those lines, the LXC userland tools cannot setup |
| 66 | 68 |
# a "sub-container" properly if the "devices" cgroup is not in its |
| 67 | 69 |
# own hierarchy. Let's detect this and issue a warning. |
| 68 |
-grep -q :devices: /proc/1/cgroup || |
|
| 69 |
- echo "WARNING: the 'devices' cgroup should be in its own hierarchy." |
|
| 70 |
-grep -qw devices /proc/1/cgroup || |
|
| 71 |
- echo "WARNING: it looks like the 'devices' cgroup is not mounted." |
|
| 70 |
+if ! grep -q :devices: /proc/1/cgroup; then |
|
| 71 |
+ echo >&2 'WARNING: the "devices" cgroup should be in its own hierarchy.' |
|
| 72 |
+fi |
|
| 73 |
+if ! grep -qw devices /proc/1/cgroup; then |
|
| 74 |
+ echo >&2 'WARNING: it looks like the "devices" cgroup is not mounted.' |
|
| 75 |
+fi |
|
| 72 | 76 |
|
| 73 | 77 |
# Now, close extraneous file descriptors. |
| 74 | 78 |
pushd /proc/self/fd >/dev/null |
| ... | ... |
@@ -89,5 +93,9 @@ popd >/dev/null |
| 89 | 89 |
# Mount /tmp |
| 90 | 90 |
mount -t tmpfs none /tmp |
| 91 | 91 |
|
| 92 |
-[ "$1" ] && exec "$@" |
|
| 93 |
-echo "You probably want to run hack/make.sh, or maybe a shell?" |
|
| 92 |
+if [ $# -gt 0 ]; then |
|
| 93 |
+ exec "$@" |
|
| 94 |
+fi |
|
| 95 |
+ |
|
| 96 |
+echo >&2 'ERROR: No command specified.' |
|
| 97 |
+echo >&2 'You probably want to run hack/make.sh, or maybe a shell?' |