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