Merge branch '8080-arvbox' closes #8080
[arvados.git] / lib / arvbox / docker / service / docker / run
1 #!/bin/bash
2
3 # Taken from https://github.com/jpetazzo/dind
4
5 exec 2>&1
6
7 # Ensure that all nodes in /dev/mapper correspond to mapped devices currently loaded by the device-mapper kernel driver
8 dmsetup mknodes
9
10 : {LOG:=stdio}
11
12 # First, make sure that cgroups are mounted correctly.
13 CGROUP=/sys/fs/cgroup
14 [ -d $CGROUP ] || mkdir $CGROUP
15
16 if mountpoint -q $CGROUP ; then
17     break
18 else
19     mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup $CGROUP
20 fi
21
22 if ! mountpoint -q $CGROUP ; then
23     echo "Could not find or mount cgroups. Tried /sys/fs/cgroup and /cgroup.  Did you use --privileged?"
24     exit 1
25 fi
26
27 if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security
28 then
29     mount -t securityfs none /sys/kernel/security || {
30         echo "Could not mount /sys/kernel/security."
31         echo "AppArmor detection and --privileged mode might break."
32     }
33 fi
34
35 # Mount the cgroup hierarchies exactly as they are in the parent system.
36 for SUBSYS in $(cut -d: -f2 /proc/1/cgroup)
37 do
38         [ -d $CGROUP/$SUBSYS ] || mkdir $CGROUP/$SUBSYS
39         mountpoint -q $CGROUP/$SUBSYS ||
40                 mount -n -t cgroup -o $SUBSYS cgroup $CGROUP/$SUBSYS
41
42         # The two following sections address a bug which manifests itself
43         # by a cryptic "lxc-start: no ns_cgroup option specified" when
44         # trying to start containers withina container.
45         # The bug seems to appear when the cgroup hierarchies are not
46         # mounted on the exact same directories in the host, and in the
47         # container.
48
49         # Named, control-less cgroups are mounted with "-o name=foo"
50         # (and appear as such under /proc/<pid>/cgroup) but are usually
51         # mounted on a directory named "foo" (without the "name=" prefix).
52         # Systemd and OpenRC (and possibly others) both create such a
53         # cgroup. To avoid the aforementioned bug, we symlink "foo" to
54         # "name=foo". This shouldn't have any adverse effect.
55         echo $SUBSYS | grep -q ^name= && {
56                 NAME=$(echo $SUBSYS | sed s/^name=//)
57                 ln -s $SUBSYS $CGROUP/$NAME
58         }
59
60         # Likewise, on at least one system, it has been reported that
61         # systemd would mount the CPU and CPU accounting controllers
62         # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu"
63         # but on a directory called "cpu,cpuacct" (note the inversion
64         # in the order of the groups). This tries to work around it.
65         [ $SUBSYS = cpuacct,cpu ] && ln -s $SUBSYS $CGROUP/cpu,cpuacct
66 done
67
68 # Note: as I write those lines, the LXC userland tools cannot setup
69 # a "sub-container" properly if the "devices" cgroup is not in its
70 # own hierarchy. Let's detect this and issue a warning.
71 grep -q :devices: /proc/1/cgroup ||
72         echo "WARNING: the 'devices' cgroup should be in its own hierarchy."
73 grep -qw devices /proc/1/cgroup ||
74         echo "WARNING: it looks like the 'devices' cgroup is not mounted."
75
76 # Now, close extraneous file descriptors.
77 pushd /proc/self/fd >/dev/null
78 for FD in *
79 do
80         case "$FD" in
81         # Keep stdin/stdout/stderr
82         [012])
83                 ;;
84         # Nuke everything else
85         *)
86                 eval exec "$FD>&-"
87                 ;;
88         esac
89 done
90 popd >/dev/null
91
92
93 # If a pidfile is still around (for example after a container restart),
94 # delete it so that docker can start.
95 rm -rf /var/run/docker.pid
96
97 read pid cmd state ppid pgrp session tty_nr tpgid rest < /proc/self/stat
98 trap "kill -TERM -$pgrp; exit" EXIT TERM KILL SIGKILL SIGTERM SIGQUIT
99
100 if ! docker daemon --storage-driver=overlay $DOCKER_DAEMON_ARGS ; then
101     docker daemon $DOCKER_DAEMON_ARGS
102 fi