wrapdocker 3.7 KB

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