|
7 | 7 |
|
8 | 8 | # START jpetazzo/dind wrapper |
9 | 9 |
|
10 | | -# First, make sure that cgroups are mounted correctly. |
11 | | -CGROUP=/sys/fs/cgroup |
| 10 | +# DinD: a wrapper script which allows docker to be run inside a docker container. |
| 11 | +# Original version by Jerome Petazzoni <jerome@docker.com> |
| 12 | +# See the blog post: https://blog.docker.com/2013/09/docker-can-now-run-within-docker/ |
| 13 | +# |
| 14 | +# This script should be executed inside a docker container in privilieged mode |
| 15 | +# ('docker run --privileged', introduced in docker 0.6). |
12 | 16 |
|
13 | | -[ -d $CGROUP ] || |
14 | | - mkdir $CGROUP |
| 17 | +# Usage: dind CMD [ARG...] |
15 | 18 |
|
16 | | -mountpoint -q $CGROUP || |
17 | | - mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup $CGROUP || { |
18 | | - echo "Could not make a tmpfs mount. Did you use -privileged?" |
19 | | - exit 1 |
20 | | - } |
| 19 | +# apparmor sucks and Docker needs to know that it's in a container (c) @tianon |
| 20 | +export container=docker |
| 21 | + |
| 22 | +# as of docker 1.8, cgroups will be mounted in the container |
| 23 | +if ! mountpoint -q /sys/fs/cgroup; then |
| 24 | + |
| 25 | + # First, make sure that cgroups are mounted correctly. |
| 26 | + CGROUP=/cgroup |
| 27 | + |
| 28 | + mkdir -p "$CGROUP" |
| 29 | + |
| 30 | + if ! mountpoint -q "$CGROUP"; then |
| 31 | + mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup $CGROUP || { |
| 32 | + echo >&2 'Could not make a tmpfs mount. Did you use --privileged?' |
| 33 | + exit 1 |
| 34 | + } |
| 35 | + fi |
| 36 | + |
| 37 | + # Mount the cgroup hierarchies exactly as they are in the parent system. |
| 38 | + for HIER in $(cut -d: -f2 /proc/1/cgroup); do |
| 39 | + |
| 40 | + # The following sections address a bug which manifests itself |
| 41 | + # by a cryptic "lxc-start: no ns_cgroup option specified" when |
| 42 | + # trying to start containers within a container. |
| 43 | + # The bug seems to appear when the cgroup hierarchies are not |
| 44 | + # mounted on the exact same directories in the host, and in the |
| 45 | + # container. |
| 46 | + |
| 47 | + SUBSYSTEMS="${HIER%name=*}" |
| 48 | + |
| 49 | + # If cgroup hierarchy is named(mounted with "-o name=foo") we |
| 50 | + # need to mount it in $CGROUP/foo to create exect same |
| 51 | + # directoryes as on host. Else we need to mount it as is e.g. |
| 52 | + # "subsys1,subsys2" if it has two subsystems |
| 53 | + |
| 54 | + # Named, control-less cgroups are mounted with "-o name=foo" |
| 55 | + # (and appear as such under /proc/<pid>/cgroup) but are usually |
| 56 | + # mounted on a directory named "foo" (without the "name=" prefix). |
| 57 | + # Systemd and OpenRC (and possibly others) both create such a |
| 58 | + # cgroup. So just mount them on directory $CGROUP/foo. |
| 59 | + |
| 60 | + OHIER=$HIER |
| 61 | + HIER="${HIER#*name=}" |
| 62 | + |
| 63 | + mkdir -p "$CGROUP/$HIER" |
| 64 | + |
| 65 | + if ! mountpoint -q "$CGROUP/$HIER"; then |
| 66 | + mount -n -t cgroup -o "$OHIER" cgroup "$CGROUP/$HIER" |
| 67 | + fi |
21 | 68 |
|
22 | | -if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security |
23 | | -then |
24 | | - mount -t securityfs none /sys/kernel/security || { |
25 | | - echo "Could not mount /sys/kernel/security." |
26 | | - echo "AppArmor detection and -privileged mode might break." |
27 | | - } |
| 69 | + # Likewise, on at least one system, it has been reported that |
| 70 | + # systemd would mount the CPU and CPU accounting controllers |
| 71 | + # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu" |
| 72 | + # but on a directory called "cpu,cpuacct" (note the inversion |
| 73 | + # in the order of the groups). This tries to work around it. |
| 74 | + |
| 75 | + if [ "$HIER" = 'cpuacct,cpu' ]; then |
| 76 | + ln -s "$HIER" "$CGROUP/cpu,cpuacct" |
| 77 | + fi |
| 78 | + |
| 79 | + # If hierarchy has multiple subsystems, in /proc/<pid>/cgroup |
| 80 | + # we will see ":subsys1,subsys2,subsys3,name=foo:" substring, |
| 81 | + # we need to mount it to "$CGROUP/foo" and if there were no |
| 82 | + # name to "$CGROUP/subsys1,subsys2,subsys3", so we must create |
| 83 | + # symlinks for docker daemon to find these subsystems: |
| 84 | + # ln -s $CGROUP/foo $CGROUP/subsys1 |
| 85 | + # ln -s $CGROUP/subsys1,subsys2,subsys3 $CGROUP/subsys1 |
| 86 | + |
| 87 | + if [ "$SUBSYSTEMS" != "${SUBSYSTEMS//,/ }" ]; then |
| 88 | + SUBSYSTEMS="${SUBSYSTEMS//,/ }" |
| 89 | + for SUBSYS in $SUBSYSTEMS |
| 90 | + do |
| 91 | + ln -s "$CGROUP/$HIER" "$CGROUP/$SUBSYS" |
| 92 | + done |
| 93 | + fi |
| 94 | + done |
28 | 95 | fi |
29 | 96 |
|
30 | | -# Mount the cgroup hierarchies exactly as they are in the parent system. |
31 | | -for SUBSYS in $(cut -d: -f2 /proc/1/cgroup) |
32 | | -do |
33 | | - [ -d $CGROUP/$SUBSYS ] || mkdir $CGROUP/$SUBSYS |
34 | | - mountpoint -q $CGROUP/$SUBSYS || |
35 | | - mount -n -t cgroup -o $SUBSYS cgroup $CGROUP/$SUBSYS |
36 | | - |
37 | | - # The two following sections address a bug which manifests itself |
38 | | - # by a cryptic "lxc-start: no ns_cgroup option specified" when |
39 | | - # trying to start containers withina container. |
40 | | - # The bug seems to appear when the cgroup hierarchies are not |
41 | | - # mounted on the exact same directories in the host, and in the |
42 | | - # container. |
43 | | - |
44 | | - # Named, control-less cgroups are mounted with "-o name=foo" |
45 | | - # (and appear as such under /proc/<pid>/cgroup) but are usually |
46 | | - # mounted on a directory named "foo" (without the "name=" prefix). |
47 | | - # Systemd and OpenRC (and possibly others) both create such a |
48 | | - # cgroup. To avoid the aforementioned bug, we symlink "foo" to |
49 | | - # "name=foo". This shouldn't have any adverse effect. |
50 | | - echo $SUBSYS | grep -q ^name= && { |
51 | | - NAME=$(echo $SUBSYS | sed s/^name=//) |
52 | | - ln -s $SUBSYS $CGROUP/$NAME |
53 | | - } |
54 | | - |
55 | | - # Likewise, on at least one system, it has been reported that |
56 | | - # systemd would mount the CPU and CPU accounting controllers |
57 | | - # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu" |
58 | | - # but on a directory called "cpu,cpuacct" (note the inversion |
59 | | - # in the order of the groups). This tries to work around it. |
60 | | - [ $SUBSYS = cpuacct,cpu ] && ln -s $SUBSYS $CGROUP/cpu,cpuacct |
61 | | -done |
| 97 | +if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then |
| 98 | + mount -t securityfs none /sys/kernel/security || { |
| 99 | + echo >&2 'Could not mount /sys/kernel/security.' |
| 100 | + echo >&2 'AppArmor detection and --privileged mode might break.' |
| 101 | + } |
| 102 | +fi |
62 | 103 |
|
63 | 104 | # Note: as I write those lines, the LXC userland tools cannot setup |
64 | 105 | # a "sub-container" properly if the "devices" cgroup is not in its |
65 | 106 | # own hierarchy. Let's detect this and issue a warning. |
66 | | -grep -q :devices: /proc/1/cgroup || |
67 | | - echo "WARNING: the 'devices' cgroup should be in its own hierarchy." |
68 | | -grep -qw devices /proc/1/cgroup || |
69 | | - echo "WARNING: it looks like the 'devices' cgroup is not mounted." |
70 | | - |
71 | | -# Now, close extraneous file descriptors. |
72 | | -pushd /proc/self/fd >/dev/null |
73 | | -for FD in * |
74 | | -do |
75 | | - case "$FD" in |
76 | | - # Keep stdin/stdout/stderr |
77 | | - [012]) |
78 | | - ;; |
79 | | - # Nuke everything else |
80 | | - *) |
81 | | - eval exec "$FD>&-" |
82 | | - ;; |
83 | | - esac |
84 | | -done |
85 | | -popd >/dev/null |
| 107 | +if ! grep -q :devices: /proc/1/cgroup; then |
| 108 | + echo >&2 'WARNING: the "devices" cgroup should be in its own hierarchy.' |
| 109 | +fi |
| 110 | +if ! grep -qw devices /proc/1/cgroup; then |
| 111 | + echo >&2 'WARNING: it looks like the "devices" cgroup is not mounted.' |
| 112 | +fi |
86 | 113 |
|
87 | | -# END jpetazzo/dind wrapper |
| 114 | +# Mount /tmp (conditionally) |
| 115 | +if ! mountpoint -q /tmp; then |
| 116 | + mount -t tmpfs none /tmp |
| 117 | +fi |
88 | 118 |
|
89 | | -exec $@ |
| 119 | +if [ $# -gt 0 ]; then |
| 120 | + exec "$@" |
| 121 | +fi |
| 122 | + |
| 123 | +echo >&2 'ERROR: No command specified.' |
| 124 | +echo >&2 'You probably want to run hack/make.sh, or maybe a shell?' |
| 125 | +# END jpetazzo/dind wrapper |
0 commit comments