github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/hack/dind-systemd (about)

     1  #!/bin/bash
     2  set -e
     3  container=docker
     4  export container
     5  
     6  if [ $# -eq 0 ]; then
     7  	echo >&2 'ERROR: No command specified. You probably want to run `journalctl -f`, or maybe `bash`?'
     8  	exit 1
     9  fi
    10  
    11  if [ ! -t 0 ]; then
    12  	echo >&2 'ERROR: TTY needs to be enabled (`docker run -t ...`).'
    13  	exit 1
    14  fi
    15  
    16  # Change mount propagation to shared, which SystemD PID 1 would normally do
    17  # itself when started by the kernel. SystemD skips that when it detects it is
    18  # running in a container.
    19  mount --make-rshared /
    20  
    21  env > /etc/docker-entrypoint-env
    22  
    23  cat > /etc/systemd/system/docker-entrypoint.target << EOF
    24  [Unit]
    25  Description=the target for docker-entrypoint.service
    26  Requires=docker-entrypoint.service systemd-logind.service systemd-user-sessions.service
    27  EOF
    28  
    29  quoted_args="$(printf " %q" "${@}")"
    30  echo "${quoted_args}" > /etc/docker-entrypoint-cmd
    31  
    32  cat > /etc/systemd/system/docker-entrypoint.service << EOF
    33  [Unit]
    34  Description=docker-entrypoint.service
    35  
    36  [Service]
    37  ExecStart=/bin/bash -exc "source /etc/docker-entrypoint-cmd"
    38  # EXIT_STATUS is either an exit code integer or a signal name string, see systemd.exec(5)
    39  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"
    40  StandardInput=tty-force
    41  StandardOutput=inherit
    42  StandardError=inherit
    43  WorkingDirectory=$(pwd)
    44  EnvironmentFile=/etc/docker-entrypoint-env
    45  
    46  [Install]
    47  WantedBy=multi-user.target
    48  EOF
    49  
    50  systemctl mask systemd-firstboot.service systemd-udevd.service
    51  systemctl unmask systemd-logind
    52  systemctl enable docker-entrypoint.service
    53  
    54  systemd=
    55  if [ -x /lib/systemd/systemd ]; then
    56  	systemd=/lib/systemd/systemd
    57  elif [ -x /usr/lib/systemd/systemd ]; then
    58  	systemd=/usr/lib/systemd/systemd
    59  elif [ -x /sbin/init ]; then
    60  	systemd=/sbin/init
    61  else
    62  	echo >&2 'ERROR: systemd is not installed'
    63  	exit 1
    64  fi
    65  systemd_args="--show-status=false --unit=docker-entrypoint.target"
    66  echo "$0: starting $systemd $systemd_args"
    67  exec $systemd $systemd_args