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