github.com/reds/docker@v1.11.2-rc1/contrib/mkimage-debootstrap.sh (about)

     1  #!/usr/bin/env bash
     2  set -e
     3  
     4  echo >&2
     5  echo >&2 'warning: this script is deprecated - see mkimage.sh and mkimage/debootstrap'
     6  echo >&2
     7  
     8  variant='minbase'
     9  include='iproute,iputils-ping'
    10  arch='amd64' # intentionally undocumented for now
    11  skipDetection=
    12  strictDebootstrap=
    13  justTar=
    14  
    15  usage() {
    16  	echo >&2
    17  
    18  	echo >&2 "usage: $0 [options] repo suite [mirror]"
    19  
    20  	echo >&2
    21  	echo >&2 'options: (not recommended)'
    22  	echo >&2 "  -p set an http_proxy for debootstrap"
    23  	echo >&2 "  -v $variant # change default debootstrap variant"
    24  	echo >&2 "  -i $include # change default package includes"
    25  	echo >&2 "  -d # strict debootstrap (do not apply any docker-specific tweaks)"
    26  	echo >&2 "  -s # skip version detection and tagging (ie, precise also tagged as 12.04)"
    27  	echo >&2 "     # note that this will also skip adding universe and/or security/updates to sources.list"
    28  	echo >&2 "  -t # just create a tarball, especially for dockerbrew (uses repo as tarball name)"
    29  
    30  	echo >&2
    31  	echo >&2 "   ie: $0 username/debian squeeze"
    32  	echo >&2 "       $0 username/debian squeeze http://ftp.uk.debian.org/debian/"
    33  
    34  	echo >&2
    35  	echo >&2 "   ie: $0 username/ubuntu precise"
    36  	echo >&2 "       $0 username/ubuntu precise http://mirrors.melbourne.co.uk/ubuntu/"
    37  
    38  	echo >&2
    39  	echo >&2 "   ie: $0 -t precise.tar.bz2 precise"
    40  	echo >&2 "       $0 -t wheezy.tgz wheezy"
    41  	echo >&2 "       $0 -t wheezy-uk.tar.xz wheezy http://ftp.uk.debian.org/debian/"
    42  
    43  	echo >&2
    44  }
    45  
    46  # these should match the names found at http://www.debian.org/releases/
    47  debianStable=wheezy
    48  debianUnstable=sid
    49  # this should match the name found at http://releases.ubuntu.com/
    50  ubuntuLatestLTS=trusty
    51  # this should match the name found at http://releases.tanglu.org/
    52  tangluLatest=aequorea
    53  
    54  while getopts v:i:a:p:dst name; do
    55  	case "$name" in
    56  		p)
    57  			http_proxy="$OPTARG"
    58  			;;
    59  		v)
    60  			variant="$OPTARG"
    61  			;;
    62  		i)
    63  			include="$OPTARG"
    64  			;;
    65  		a)
    66  			arch="$OPTARG"
    67  			;;
    68  		d)
    69  			strictDebootstrap=1
    70  			;;
    71  		s)
    72  			skipDetection=1
    73  			;;
    74  		t)
    75  			justTar=1
    76  			;;
    77  		?)
    78  			usage
    79  			exit 0
    80  			;;
    81  	esac
    82  done
    83  shift $(($OPTIND - 1))
    84  
    85  repo="$1"
    86  suite="$2"
    87  mirror="${3:-}" # stick to the default debootstrap mirror if one is not provided
    88  
    89  if [ ! "$repo" ] || [ ! "$suite" ]; then
    90  	usage
    91  	exit 1
    92  fi
    93  
    94  # some rudimentary detection for whether we need to "sudo" our docker calls
    95  docker=''
    96  if docker version > /dev/null 2>&1; then
    97  	docker='docker'
    98  elif sudo docker version > /dev/null 2>&1; then
    99  	docker='sudo docker'
   100  elif command -v docker > /dev/null 2>&1; then
   101  	docker='docker'
   102  else
   103  	echo >&2 "warning: either docker isn't installed, or your current user cannot run it;"
   104  	echo >&2 "         this script is not likely to work as expected"
   105  	sleep 3
   106  	docker='docker' # give us a command-not-found later
   107  fi
   108  
   109  # make sure we have an absolute path to our final tarball so we can still reference it properly after we change directory
   110  if [ "$justTar" ]; then
   111  	if [ ! -d "$(dirname "$repo")" ]; then
   112  		echo >&2 "error: $(dirname "$repo") does not exist"
   113  		exit 1
   114  	fi
   115  	repo="$(cd "$(dirname "$repo")" && pwd -P)/$(basename "$repo")"
   116  fi
   117  
   118  # will be filled in later, if [ -z "$skipDetection" ]
   119  lsbDist=''
   120  
   121  target="${TMPDIR:-/var/tmp}/docker-rootfs-debootstrap-$suite-$$-$RANDOM"
   122  
   123  cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
   124  returnTo="$(pwd -P)"
   125  
   126  if [ "$suite" = 'lucid' ]; then
   127  	# lucid fails and doesn't include gpgv in minbase; "apt-get update" fails
   128  	include+=',gpgv'
   129  fi
   130  
   131  set -x
   132  
   133  # bootstrap
   134  mkdir -p "$target"
   135  sudo http_proxy=$http_proxy debootstrap --verbose --variant="$variant" --include="$include" --arch="$arch" "$suite" "$target" "$mirror"
   136  
   137  cd "$target"
   138  
   139  if [ -z "$strictDebootstrap" ]; then
   140  	# prevent init scripts from running during install/update
   141  	#  policy-rc.d (for most scripts)
   142  	echo $'#!/bin/sh\nexit 101' | sudo tee usr/sbin/policy-rc.d > /dev/null
   143  	sudo chmod +x usr/sbin/policy-rc.d
   144  	#  initctl (for some pesky upstart scripts)
   145  	sudo chroot . dpkg-divert --local --rename --add /sbin/initctl
   146  	sudo ln -sf /bin/true sbin/initctl
   147  	# see https://github.com/docker/docker/issues/446#issuecomment-16953173
   148  
   149  	# shrink the image, since apt makes us fat (wheezy: ~157.5MB vs ~120MB)
   150  	sudo chroot . apt-get clean
   151  
   152  	if strings usr/bin/dpkg | grep -q unsafe-io; then
   153  		# while we're at it, apt is unnecessarily slow inside containers
   154  		#  this forces dpkg not to call sync() after package extraction and speeds up install
   155  		#    the benefit is huge on spinning disks, and the penalty is nonexistent on SSD or decent server virtualization
   156  		echo 'force-unsafe-io' | sudo tee etc/dpkg/dpkg.cfg.d/02apt-speedup > /dev/null
   157  		# we have this wrapped up in an "if" because the "force-unsafe-io"
   158  		# option was added in dpkg 1.15.8.6
   159  		# (see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=584254#82),
   160  		# and ubuntu lucid/10.04 only has 1.15.5.6
   161  	fi
   162  
   163  	# we want to effectively run "apt-get clean" after every install to keep images small (see output of "apt-get clean -s" for context)
   164  	{
   165  		aptGetClean='"rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true";'
   166  		echo "DPkg::Post-Invoke { ${aptGetClean} };"
   167  		echo "APT::Update::Post-Invoke { ${aptGetClean} };"
   168  		echo 'Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";'
   169  	} | sudo tee etc/apt/apt.conf.d/no-cache > /dev/null
   170  
   171  	# and remove the translations, too
   172  	echo 'Acquire::Languages "none";' | sudo tee etc/apt/apt.conf.d/no-languages > /dev/null
   173  
   174  	# helpful undo lines for each the above tweaks (for lack of a better home to keep track of them):
   175  	#  rm /usr/sbin/policy-rc.d
   176  	#  rm /sbin/initctl; dpkg-divert --rename --remove /sbin/initctl
   177  	#  rm /etc/dpkg/dpkg.cfg.d/02apt-speedup
   178  	#  rm /etc/apt/apt.conf.d/no-cache
   179  	#  rm /etc/apt/apt.conf.d/no-languages
   180  
   181  	if [ -z "$skipDetection" ]; then
   182  		# see also rudimentary platform detection in hack/install.sh
   183  		lsbDist=''
   184  		if [ -r etc/lsb-release ]; then
   185  			lsbDist="$(. etc/lsb-release && echo "$DISTRIB_ID")"
   186  		fi
   187  		if [ -z "$lsbDist" ] && [ -r etc/debian_version ]; then
   188  			lsbDist='Debian'
   189  		fi
   190  
   191  		case "$lsbDist" in
   192  			Debian)
   193  				# add the updates and security repositories
   194  				if [ "$suite" != "$debianUnstable" -a "$suite" != 'unstable' ]; then
   195  					# ${suite}-updates only applies to non-unstable
   196  					sudo sed -i "p; s/ $suite main$/ ${suite}-updates main/" etc/apt/sources.list
   197  
   198  					# same for security updates
   199  					echo "deb http://security.debian.org/ $suite/updates main" | sudo tee -a etc/apt/sources.list > /dev/null
   200  				fi
   201  				;;
   202  			Ubuntu)
   203  				# add the universe, updates, and security repositories
   204  				sudo sed -i "
   205  					s/ $suite main$/ $suite main universe/; p;
   206  					s/ $suite main/ ${suite}-updates main/; p;
   207  					s/ $suite-updates main/ ${suite}-security main/
   208  				" etc/apt/sources.list
   209  				;;
   210  			Tanglu)
   211  				# add the updates repository
   212  				if [ "$suite" = "$tangluLatest" ]; then
   213  					# ${suite}-updates only applies to stable Tanglu versions
   214  					sudo sed -i "p; s/ $suite main$/ ${suite}-updates main/" etc/apt/sources.list
   215  				fi
   216  				;;
   217  			SteamOS)
   218  				# add contrib and non-free
   219  				sudo sed -i "s/ $suite main$/ $suite main contrib non-free/" etc/apt/sources.list
   220  				;;
   221  		esac
   222  	fi
   223  
   224  	# make sure our packages lists are as up to date as we can get them
   225  	sudo chroot . apt-get update
   226  	sudo chroot . apt-get dist-upgrade -y
   227  fi
   228  
   229  if [ "$justTar" ]; then
   230  	# create the tarball file so it has the right permissions (ie, not root)
   231  	touch "$repo"
   232  
   233  	# fill the tarball
   234  	sudo tar --numeric-owner -caf "$repo" .
   235  else
   236  	# create the image (and tag $repo:$suite)
   237  	sudo tar --numeric-owner -c . | $docker import - $repo:$suite
   238  
   239  	# test the image
   240  	$docker run -i -t $repo:$suite echo success
   241  
   242  	if [ -z "$skipDetection" ]; then
   243  		case "$lsbDist" in
   244  			Debian)
   245  				if [ "$suite" = "$debianStable" -o "$suite" = 'stable' ] && [ -r etc/debian_version ]; then
   246  					# tag latest
   247  					$docker tag $repo:$suite $repo:latest
   248  
   249  					if [ -r etc/debian_version ]; then
   250  						# tag the specific debian release version (which is only reasonable to tag on debian stable)
   251  						ver=$(cat etc/debian_version)
   252  						$docker tag $repo:$suite $repo:$ver
   253  					fi
   254  				fi
   255  				;;
   256  			Ubuntu)
   257  				if [ "$suite" = "$ubuntuLatestLTS" ]; then
   258  					# tag latest
   259  					$docker tag $repo:$suite $repo:latest
   260  				fi
   261  				if [ -r etc/lsb-release ]; then
   262  					lsbRelease="$(. etc/lsb-release && echo "$DISTRIB_RELEASE")"
   263  					if [ "$lsbRelease" ]; then
   264  						# tag specific Ubuntu version number, if available (12.04, etc.)
   265  						$docker tag $repo:$suite $repo:$lsbRelease
   266  					fi
   267  				fi
   268  				;;
   269  			Tanglu)
   270  				if [ "$suite" = "$tangluLatest" ]; then
   271  					# tag latest
   272  					$docker tag $repo:$suite $repo:latest
   273  				fi
   274  				if [ -r etc/lsb-release ]; then
   275  					lsbRelease="$(. etc/lsb-release && echo "$DISTRIB_RELEASE")"
   276  					if [ "$lsbRelease" ]; then
   277  						# tag specific Tanglu version number, if available (1.0, 2.0, etc.)
   278  						$docker tag $repo:$suite $repo:$lsbRelease
   279  					fi
   280  				fi
   281  				;;
   282  			SteamOS)
   283  				if [ -r etc/lsb-release ]; then
   284  					lsbRelease="$(. etc/lsb-release && echo "$DISTRIB_RELEASE")"
   285  					if [ "$lsbRelease" ]; then
   286  						# tag specific SteamOS version number, if available (1.0, 2.0, etc.)
   287  						$docker tag $repo:$suite $repo:$lsbRelease
   288  					fi
   289  				fi
   290  				;;
   291  		esac
   292  	fi
   293  fi
   294  
   295  # cleanup
   296  cd "$returnTo"
   297  sudo rm -rf "$target"