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