github.com/gunjan5/docker@v1.8.2/hack/dind (about)

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