dind-systemd 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. set -e
  3. # Set the container env-var, so that AppArmor is enabled in the daemon and
  4. # containerd when running docker-in-docker.
  5. #
  6. # see: https://github.com/containerd/containerd/blob/787943dc1027a67f3b52631e084db0d4a6be2ccc/pkg/apparmor/apparmor_linux.go#L29-L45
  7. # see: https://github.com/moby/moby/commit/de191e86321f7d3136ff42ff75826b8107399497
  8. container=docker
  9. export container
  10. if [ $# -eq 0 ]; then
  11. echo >&2 'ERROR: No command specified. You probably want to run `journalctl -f`, or maybe `bash`?'
  12. exit 1
  13. fi
  14. if [ ! -t 0 ]; then
  15. echo >&2 'ERROR: TTY needs to be enabled (`docker run -t ...`).'
  16. exit 1
  17. fi
  18. # Change mount propagation to shared, which SystemD PID 1 would normally do
  19. # itself when started by the kernel. SystemD skips that when it detects it is
  20. # running in a container.
  21. mount --make-rshared /
  22. # Allow AppArmor to work inside the container;
  23. #
  24. # aa-status
  25. # apparmor filesystem is not mounted.
  26. # apparmor module is loaded.
  27. #
  28. # mount -t securityfs none /sys/kernel/security
  29. #
  30. # aa-status
  31. # apparmor module is loaded.
  32. # 30 profiles are loaded.
  33. # 30 profiles are in enforce mode.
  34. # /snap/snapd/18357/usr/lib/snapd/snap-confine
  35. # ...
  36. #
  37. # Note: https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts#sys-kernel-security
  38. #
  39. # ## /sys/kernel/security
  40. #
  41. # In /sys/kernel/security mounted the securityfs interface, which allows
  42. # configuration of Linux Security Modules. This allows configuration of
  43. # AppArmor policies, and so access to this may allow a container to disable
  44. # its MAC system.
  45. #
  46. # Given that we're running privileged already, this should not be an issue.
  47. if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then
  48. mount -t securityfs none /sys/kernel/security || {
  49. echo >&2 'Could not mount /sys/kernel/security.'
  50. echo >&2 'AppArmor detection and --privileged mode might break.'
  51. }
  52. fi
  53. env > /etc/docker-entrypoint-env
  54. cat > /etc/systemd/system/docker-entrypoint.target << EOF
  55. [Unit]
  56. Description=the target for docker-entrypoint.service
  57. Requires=docker-entrypoint.service systemd-logind.service systemd-user-sessions.service
  58. EOF
  59. quoted_args="$(printf " %q" "${@}")"
  60. echo "${quoted_args}" > /etc/docker-entrypoint-cmd
  61. cat > /etc/systemd/system/docker-entrypoint.service << EOF
  62. [Unit]
  63. Description=docker-entrypoint.service
  64. [Service]
  65. ExecStart=/bin/bash -exc "source /etc/docker-entrypoint-cmd"
  66. # EXIT_STATUS is either an exit code integer or a signal name string, see systemd.exec(5)
  67. 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"
  68. StandardInput=tty-force
  69. StandardOutput=inherit
  70. StandardError=inherit
  71. WorkingDirectory=$(pwd)
  72. EnvironmentFile=/etc/docker-entrypoint-env
  73. [Install]
  74. WantedBy=multi-user.target
  75. EOF
  76. systemctl mask systemd-firstboot.service systemd-udevd.service
  77. systemctl unmask systemd-logind
  78. systemctl enable docker-entrypoint.service
  79. systemd=
  80. if [ -x /lib/systemd/systemd ]; then
  81. systemd=/lib/systemd/systemd
  82. elif [ -x /usr/lib/systemd/systemd ]; then
  83. systemd=/usr/lib/systemd/systemd
  84. elif [ -x /sbin/init ]; then
  85. systemd=/sbin/init
  86. else
  87. echo >&2 'ERROR: systemd is not installed'
  88. exit 1
  89. fi
  90. systemd_args="--show-status=false --unit=docker-entrypoint.target"
  91. echo "$0: starting $systemd $systemd_args"
  92. exec $systemd $systemd_args