|
| 1 | +#!/bin/bash -e |
| 2 | +# |
| 3 | +# The MIT License (MIT) |
| 4 | +# |
| 5 | +# Copyright (c) 2014 Dan Tehranian |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | + |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | +# |
| 25 | +# Original from: |
| 26 | +# https://github.com/tehranian/dind-jenkins-slave/commit/bdaea009f2ec982f07c89408906a64ce24ab7637 |
| 27 | + |
| 28 | +# First, make sure that cgroups are mounted correctly. |
| 29 | +CGROUP=/sys/fs/cgroup |
| 30 | +: {LOG:=stdio} |
| 31 | + |
| 32 | +[ -d $CGROUP ] || |
| 33 | + mkdir $CGROUP |
| 34 | + |
| 35 | +mountpoint -q $CGROUP || |
| 36 | + mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup $CGROUP || { |
| 37 | + echo "Could not make a tmpfs mount. Did you use --privileged?" |
| 38 | + exit 1 |
| 39 | + } |
| 40 | + |
| 41 | +if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security |
| 42 | +then |
| 43 | + mount -t securityfs none /sys/kernel/security || { |
| 44 | + echo "Could not mount /sys/kernel/security." |
| 45 | + echo "AppArmor detection and --privileged mode might break." |
| 46 | + } |
| 47 | +fi |
| 48 | + |
| 49 | +# Mount the cgroup hierarchies exactly as they are in the parent system. |
| 50 | +for SUBSYS in $(cut -d: -f2 /proc/1/cgroup) |
| 51 | +do |
| 52 | + [ -d $CGROUP/$SUBSYS ] || mkdir $CGROUP/$SUBSYS |
| 53 | + mountpoint -q $CGROUP/$SUBSYS || |
| 54 | + mount -n -t cgroup -o $SUBSYS cgroup $CGROUP/$SUBSYS |
| 55 | + |
| 56 | + # The two following sections address a bug which manifests itself |
| 57 | + # by a cryptic "lxc-start: no ns_cgroup option specified" when |
| 58 | + # trying to start containers withina container. |
| 59 | + # The bug seems to appear when the cgroup hierarchies are not |
| 60 | + # mounted on the exact same directories in the host, and in the |
| 61 | + # container. |
| 62 | + |
| 63 | + # Named, control-less cgroups are mounted with "-o name=foo" |
| 64 | + # (and appear as such under /proc/<pid>/cgroup) but are usually |
| 65 | + # mounted on a directory named "foo" (without the "name=" prefix). |
| 66 | + # Systemd and OpenRC (and possibly others) both create such a |
| 67 | + # cgroup. To avoid the aforementioned bug, we symlink "foo" to |
| 68 | + # "name=foo". This shouldn't have any adverse effect. |
| 69 | + echo $SUBSYS | grep -q ^name= && { |
| 70 | + NAME=$(echo $SUBSYS | sed s/^name=//) |
| 71 | + ln -s $SUBSYS $CGROUP/$NAME || true |
| 72 | + } |
| 73 | + |
| 74 | + # Likewise, on at least one system, it has been reported that |
| 75 | + # systemd would mount the CPU and CPU accounting controllers |
| 76 | + # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu" |
| 77 | + # but on a directory called "cpu,cpuacct" (note the inversion |
| 78 | + # in the order of the groups). This tries to work around it. |
| 79 | + [ $SUBSYS = cpuacct,cpu ] && ln -s $SUBSYS $CGROUP/cpu,cpuacct |
| 80 | +done |
| 81 | + |
| 82 | +# Note: as I write those lines, the LXC userland tools cannot setup |
| 83 | +# a "sub-container" properly if the "devices" cgroup is not in its |
| 84 | +# own hierarchy. Let's detect this and issue a warning. |
| 85 | +grep -q :devices: /proc/1/cgroup || |
| 86 | + echo "WARNING: the 'devices' cgroup should be in its own hierarchy." |
| 87 | +grep -qw devices /proc/1/cgroup || |
| 88 | + echo "WARNING: it looks like the 'devices' cgroup is not mounted." |
| 89 | + |
| 90 | +# Now, close extraneous file descriptors. |
| 91 | +pushd /proc/self/fd >/dev/null |
| 92 | +for FD in * |
| 93 | +do |
| 94 | + case "$FD" in |
| 95 | + # Keep stdin/stdout/stderr |
| 96 | + [012]) |
| 97 | + ;; |
| 98 | + # Nuke everything else |
| 99 | + *) |
| 100 | + eval exec "$FD>&-" |
| 101 | + ;; |
| 102 | + esac |
| 103 | +done |
| 104 | +popd >/dev/null |
| 105 | + |
| 106 | + |
| 107 | +# If a pidfile is still around (for example after a container restart), |
| 108 | +# delete it so that docker can start. |
| 109 | +rm -rf /var/run/docker.pid |
| 110 | + |
| 111 | +# Force overlayfs for DinD or it won't work on Debian image (like this one) |
| 112 | +# XXX: Disabled for now since this will be run on local machines w/ overlay |
| 113 | +# as the default FS so we leave the default in |
| 114 | +# DOCKER_DAEMON_ARGS="$DOCKER_DAEMON_ARGS -s overlay" |
| 115 | + |
| 116 | +exec dockerd $DOCKER_DAEMON_ARGS >/var/log/docker.log |
0 commit comments