dind 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. set -e
  3. # DinD: a wrapper script which allows docker to be run inside a docker container.
  4. # Original version by Jerome Petazzoni <jerome@docker.com>
  5. # See the blog post: https://blog.docker.com/2013/09/docker-can-now-run-within-docker/
  6. #
  7. # This script should be executed inside a docker container in privilieged mode
  8. # ('docker run --privileged', introduced in docker 0.6).
  9. # Usage: dind CMD [ARG...]
  10. # apparmor sucks and Docker needs to know that it's in a container (c) @tianon
  11. export container=docker
  12. # First, make sure that cgroups are mounted correctly.
  13. CGROUP=/cgroup
  14. mkdir -p "$CGROUP"
  15. if ! mountpoint -q "$CGROUP"; then
  16. mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup $CGROUP || {
  17. echo >&2 'Could not make a tmpfs mount. Did you use --privileged?'
  18. exit 1
  19. }
  20. fi
  21. if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then
  22. mount -t securityfs none /sys/kernel/security || {
  23. echo >&2 'Could not mount /sys/kernel/security.'
  24. echo >&2 'AppArmor detection and -privileged mode might break.'
  25. }
  26. fi
  27. # Mount the cgroup hierarchies exactly as they are in the parent system.
  28. for HIER in $(cut -d: -f2 /proc/1/cgroup); do
  29. # The following sections address a bug which manifests itself
  30. # by a cryptic "lxc-start: no ns_cgroup option specified" when
  31. # trying to start containers within a container.
  32. # The bug seems to appear when the cgroup hierarchies are not
  33. # mounted on the exact same directories in the host, and in the
  34. # container.
  35. SUBSYSTEMS="${HIER%name=*}"
  36. # If cgroup hierarchy is named(mounted with "-o name=foo") we
  37. # need to mount it in $CGROUP/foo to create exect same
  38. # directoryes as on host. Else we need to mount it as is e.g.
  39. # "subsys1,subsys2" if it has two subsystems
  40. # Named, control-less cgroups are mounted with "-o name=foo"
  41. # (and appear as such under /proc/<pid>/cgroup) but are usually
  42. # mounted on a directory named "foo" (without the "name=" prefix).
  43. # Systemd and OpenRC (and possibly others) both create such a
  44. # cgroup. So just mount them on directory $CGROUP/foo.
  45. OHIER=$HIER
  46. HIER="${HIER#*name=}"
  47. mkdir -p "$CGROUP/$HIER"
  48. if ! mountpoint -q "$CGROUP/$HIER"; then
  49. mount -n -t cgroup -o "$OHIER" cgroup "$CGROUP/$HIER"
  50. fi
  51. # Likewise, on at least one system, it has been reported that
  52. # systemd would mount the CPU and CPU accounting controllers
  53. # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu"
  54. # but on a directory called "cpu,cpuacct" (note the inversion
  55. # in the order of the groups). This tries to work around it.
  56. if [ "$HIER" = 'cpuacct,cpu' ]; then
  57. ln -s "$HIER" "$CGROUP/cpu,cpuacct"
  58. fi
  59. # If hierarchy has multiple subsystems, in /proc/<pid>/cgroup
  60. # we will see ":subsys1,subsys2,subsys3,name=foo:" substring,
  61. # we need to mount it to "$CGROUP/foo" and if there were no
  62. # name to "$CGROUP/subsys1,subsys2,subsys3", so we must create
  63. # symlinks for docker daemon to find these subsystems:
  64. # ln -s $CGROUP/foo $CGROUP/subsys1
  65. # ln -s $CGROUP/subsys1,subsys2,subsys3 $CGROUP/subsys1
  66. if [ "$SUBSYSTEMS" != "${SUBSYSTEMS//,/ }" ]; then
  67. SUBSYSTEMS="${SUBSYSTEMS//,/ }"
  68. for SUBSYS in $SUBSYSTEMS
  69. do
  70. ln -s "$CGROUP/$HIER" "$CGROUP/$SUBSYS"
  71. done
  72. fi
  73. done
  74. # Note: as I write those lines, the LXC userland tools cannot setup
  75. # a "sub-container" properly if the "devices" cgroup is not in its
  76. # own hierarchy. Let's detect this and issue a warning.
  77. if ! grep -q :devices: /proc/1/cgroup; then
  78. echo >&2 'WARNING: the "devices" cgroup should be in its own hierarchy.'
  79. fi
  80. if ! grep -qw devices /proc/1/cgroup; then
  81. echo >&2 'WARNING: it looks like the "devices" cgroup is not mounted.'
  82. fi
  83. # Mount /tmp
  84. mount -t tmpfs none /tmp
  85. if [ $# -gt 0 ]; then
  86. exec "$@"
  87. fi
  88. echo >&2 'ERROR: No command specified.'
  89. echo >&2 'You probably want to run hack/make.sh, or maybe a shell?'