Debian Codegraph @

engine/hack/dind-systemd

raw symbols SCIP
1 #!/bin/bash
2 set -e
3
4 # Set the container env-var, so that AppArmor is enabled in the daemon and
5 # containerd when running docker-in-docker.
6 #
7 # see: https://github.com/containerd/containerd/blob/787943dc1027a67f3b52631e084db0d4a6be2ccc/pkg/apparmor/apparmor_linux.go#L29-L45
8 # see: https://github.com/moby/moby/commit/de191e86321f7d3136ff42ff75826b8107399497
9 container=docker
10 export container
11
12 if [ $# -eq 0 ]; then
13 echo >&2 'ERROR: No command specified. You probably want to run `journalctl -f`, or maybe `bash`?'
14 exit 1
15 fi
16
17 if [ ! -t 0 ]; then
18 echo >&2 'ERROR: TTY needs to be enabled (`docker run -t ...`).'
19 exit 1
20 fi
21
22 # Mount /tmp (conditionally)
23 # /tmp must be 'exec,rw', and 'dev' (defaults) to allow mknod to work for the
24 # pkg/archive/archive_linux_test.go tests.
25 if ! mountpoint -q /tmp; then
26 mount -t tmpfs none /tmp
27 fi
28
29 # Change mount propagation to shared, which SystemD PID 1 would normally do
30 # itself when started by the kernel. SystemD skips that when it detects it is
31 # running in a container.
32 mount --make-rshared /
33
34 # Allow AppArmor to work inside the container;
35 #
36 # aa-status
37 # apparmor filesystem is not mounted.
38 # apparmor module is loaded.
39 #
40 # mount -t securityfs none /sys/kernel/security
41 #
42 # aa-status
43 # apparmor module is loaded.
44 # 30 profiles are loaded.
45 # 30 profiles are in enforce mode.
46 # /snap/snapd/18357/usr/lib/snapd/snap-confine
47 # ...
48 #
49 # Note: https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts#sys-kernel-security
50 #
51 # ## /sys/kernel/security
52 #
53 # In /sys/kernel/security mounted the securityfs interface, which allows
54 # configuration of Linux Security Modules. This allows configuration of
55 # AppArmor policies, and so access to this may allow a container to disable
56 # its MAC system.
57 #
58 # Given that we're running privileged already, this should not be an issue.
59 if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then
60 mount -t securityfs none /sys/kernel/security || {
61 echo >&2 'Could not mount /sys/kernel/security.'
62 echo >&2 'AppArmor detection and --privileged mode might break.'
63 }
64 fi
65
66 # Allow connections coming from the host (through eth0). This is needed to
67 # access the daemon port (independently of which port is used), or run a
68 # 'remote' Delve session, etc...
69 if [ "${FIREWALLD:-}" = "true" ]; then
70 cat > /etc/firewalld/zones/trusted.xml << EOF
71 <?xml version="1.0" encoding="utf-8"?>
72 <zone target="ACCEPT">
73 <short>Trusted</short>
74 <description>All network connections are accepted.</description>
75 <interface name="eth0"/>
76 <forward/>
77 </zone>
78 EOF
79 fi
80
81 env > /etc/docker-entrypoint-env
82
83 cat > /etc/systemd/system/docker-entrypoint.target << EOF
84 [Unit]
85 Description=the target for docker-entrypoint.service
86 Requires=docker-entrypoint.service systemd-logind.service systemd-user-sessions.service $([ "${FIREWALLD:-}" = "true" ] && echo firewalld.service)
87 EOF
88
89 quoted_args="$(printf " %q" "${@}")"
90 echo "${quoted_args}" > /etc/docker-entrypoint-cmd
91
92 cat > /etc/systemd/system/docker-entrypoint.service << EOF
93 [Unit]
94 Description=docker-entrypoint.service
95
96 [Service]
97 ExecStart=/bin/bash -exc "source /etc/docker-entrypoint-cmd"
98 # EXIT_STATUS is either an exit code integer or a signal name string, see systemd.exec(5)
99 ExecStopPost=/bin/bash -ec "if echo \${EXIT_STATUS} | grep [A-Z] > /dev/null; then echo >&2 \"got signal \${EXIT_STATUS}\"; systemctl exit \$(( 128 + \$( kill -l \${EXIT_STATUS} ) )); else systemctl exit \${EXIT_STATUS}; fi"
100 StandardInput=tty-force
101 StandardOutput=inherit
102 StandardError=inherit
103 WorkingDirectory=$(pwd)
104 EnvironmentFile=/etc/docker-entrypoint-env
105
106 [Install]
107 WantedBy=multi-user.target
108 EOF
109
110 systemctl mask systemd-firstboot.service systemd-udevd.service
111 systemctl unmask systemd-logind
112 systemctl enable docker-entrypoint.service
113
114 systemd=
115 if [ -x /lib/systemd/systemd ]; then
116 systemd=/lib/systemd/systemd
117 elif [ -x /usr/lib/systemd/systemd ]; then
118 systemd=/usr/lib/systemd/systemd
119 elif [ -x /sbin/init ]; then
120 systemd=/sbin/init
121 else
122 echo >&2 'ERROR: systemd is not installed'
123 exit 1
124 fi
125 systemd_args="--show-status=false --unit=docker-entrypoint.target"
126 echo "$0: starting $systemd $systemd_args"
127 exec $systemd $systemd_args