debootstrap 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env bash
  2. set -e
  3. rootfsDir="$1"
  4. shift
  5. # we have to do a little fancy footwork to make sure "rootfsDir" becomes the second non-option argument to debootstrap
  6. before=()
  7. while [ $# -gt 0 ] && [[ "$1" == -* ]]; do
  8. before+=( "$1" )
  9. shift
  10. done
  11. suite="$1"
  12. shift
  13. # get path to "chroot" in our current PATH
  14. chrootPath="$(type -P chroot)"
  15. rootfs_chroot() {
  16. # "chroot" doesn't set PATH, so we need to set it explicitly to something our new debootstrap chroot can use appropriately!
  17. # set PATH and chroot away!
  18. PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' \
  19. "$chrootPath" "$rootfsDir" "$@"
  20. }
  21. # allow for DEBOOTSTRAP=qemu-debootstrap ./mkimage.sh ...
  22. : ${DEBOOTSTRAP:=debootstrap}
  23. (
  24. set -x
  25. $DEBOOTSTRAP "${before[@]}" "$suite" "$rootfsDir" "$@"
  26. )
  27. # now for some Docker-specific tweaks
  28. # prevent init scripts from running during install/update
  29. echo >&2 "+ echo exit 101 > '$rootfsDir/usr/sbin/policy-rc.d'"
  30. cat > "$rootfsDir/usr/sbin/policy-rc.d" <<-'EOF'
  31. #!/bin/sh
  32. # For most Docker users, "apt-get install" only happens during "docker build",
  33. # where starting services doesn't work and often fails in humorous ways. This
  34. # prevents those failures by stopping the services from attempting to start.
  35. exit 101
  36. EOF
  37. chmod +x "$rootfsDir/usr/sbin/policy-rc.d"
  38. # prevent upstart scripts from running during install/update
  39. (
  40. set -x
  41. rootfs_chroot dpkg-divert --local --rename --add /sbin/initctl
  42. cp -a "$rootfsDir/usr/sbin/policy-rc.d" "$rootfsDir/sbin/initctl"
  43. sed -i 's/^exit.*/exit 0/' "$rootfsDir/sbin/initctl"
  44. )
  45. # shrink a little, since apt makes us cache-fat (wheezy: ~157.5MB vs ~120MB)
  46. ( set -x; rootfs_chroot apt-get clean )
  47. # this file is one APT creates to make sure we don't "autoremove" our currently
  48. # in-use kernel, which doesn't really apply to debootstraps/Docker images that
  49. # don't even have kernels installed
  50. rm -f "$rootfsDir/etc/apt/apt.conf.d/01autoremove-kernels"
  51. # Ubuntu 10.04 sucks... :)
  52. if strings "$rootfsDir/usr/bin/dpkg" | grep -q unsafe-io; then
  53. # force dpkg not to call sync() after package extraction (speeding up installs)
  54. echo >&2 "+ echo force-unsafe-io > '$rootfsDir/etc/dpkg/dpkg.cfg.d/docker-apt-speedup'"
  55. cat > "$rootfsDir/etc/dpkg/dpkg.cfg.d/docker-apt-speedup" <<-'EOF'
  56. # For most Docker users, package installs happen during "docker build", which
  57. # doesn't survive power loss and gets restarted clean afterwards anyhow, so
  58. # this minor tweak gives us a nice speedup (much nicer on spinning disks,
  59. # obviously).
  60. force-unsafe-io
  61. EOF
  62. fi
  63. if [ -d "$rootfsDir/etc/apt/apt.conf.d" ]; then
  64. # _keep_ us lean by effectively running "apt-get clean" after every install
  65. aptGetClean='"rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true";'
  66. echo >&2 "+ cat > '$rootfsDir/etc/apt/apt.conf.d/docker-clean'"
  67. cat > "$rootfsDir/etc/apt/apt.conf.d/docker-clean" <<-EOF
  68. # Since for most Docker users, package installs happen in "docker build" steps,
  69. # they essentially become individual layers due to the way Docker handles
  70. # layering, especially using CoW filesystems. What this means for us is that
  71. # the caches that APT keeps end up just wasting space in those layers, making
  72. # our layers unnecessarily large (especially since we'll normally never use
  73. # these caches again and will instead just "docker build" again and make a brand
  74. # new image).
  75. # Ideally, these would just be invoking "apt-get clean", but in our testing,
  76. # that ended up being cyclic and we got stuck on APT's lock, so we get this fun
  77. # creation that's essentially just "apt-get clean".
  78. DPkg::Post-Invoke { ${aptGetClean} };
  79. APT::Update::Post-Invoke { ${aptGetClean} };
  80. Dir::Cache::pkgcache "";
  81. Dir::Cache::srcpkgcache "";
  82. # Note that we do realize this isn't the ideal way to do this, and are always
  83. # open to better suggestions (https://github.com/docker/docker/issues).
  84. EOF
  85. # remove apt-cache translations for fast "apt-get update"
  86. echo >&2 "+ echo Acquire::Languages 'none' > '$rootfsDir/etc/apt/apt.conf.d/docker-no-languages'"
  87. cat > "$rootfsDir/etc/apt/apt.conf.d/docker-no-languages" <<-'EOF'
  88. # In Docker, we don't often need the "Translations" files, so we're just wasting
  89. # time and space by downloading them, and this inhibits that. For users that do
  90. # need them, it's a simple matter to delete this file and "apt-get update". :)
  91. Acquire::Languages "none";
  92. EOF
  93. echo >&2 "+ echo Acquire::GzipIndexes 'true' > '$rootfsDir/etc/apt/apt.conf.d/docker-gzip-indexes'"
  94. cat > "$rootfsDir/etc/apt/apt.conf.d/docker-gzip-indexes" <<-'EOF'
  95. # Since Docker users using "RUN apt-get update && apt-get install -y ..." in
  96. # their Dockerfiles don't go delete the lists files afterwards, we want them to
  97. # be as small as possible on-disk, so we explicitly request "gz" versions and
  98. # tell Apt to keep them gzipped on-disk.
  99. # For comparison, an "apt-get update" layer without this on a pristine
  100. # "debian:wheezy" base image was "29.88 MB", where with this it was only
  101. # "8.273 MB".
  102. Acquire::GzipIndexes "true";
  103. Acquire::CompressionTypes::Order:: "gz";
  104. EOF
  105. # update "autoremove" configuration to be aggressive about removing suggests deps that weren't manually installed
  106. echo >&2 "+ echo Apt::AutoRemove::SuggestsImportant 'false' > '$rootfsDir/etc/apt/apt.conf.d/docker-autoremove-suggests'"
  107. cat > "$rootfsDir/etc/apt/apt.conf.d/docker-autoremove-suggests" <<-'EOF'
  108. # Since Docker users are looking for the smallest possible final images, the
  109. # following emerges as a very common pattern:
  110. # RUN apt-get update \
  111. # && apt-get install -y <packages> \
  112. # && <do some compilation work> \
  113. # && apt-get purge -y --auto-remove <packages>
  114. # By default, APT will actually _keep_ packages installed via Recommends or
  115. # Depends if another package Suggests them, even and including if the package
  116. # that originally caused them to be installed is removed. Setting this to
  117. # "false" ensures that APT is appropriately aggressive about removing the
  118. # packages it added.
  119. # https://aptitude.alioth.debian.org/doc/en/ch02s05s05.html#configApt-AutoRemove-SuggestsImportant
  120. Apt::AutoRemove::SuggestsImportant "false";
  121. EOF
  122. fi
  123. if [ -z "$DONT_TOUCH_SOURCES_LIST" ]; then
  124. # tweak sources.list, where appropriate
  125. lsbDist=
  126. if [ -z "$lsbDist" -a -r "$rootfsDir/etc/os-release" ]; then
  127. lsbDist="$(. "$rootfsDir/etc/os-release" && echo "$ID")"
  128. fi
  129. if [ -z "$lsbDist" -a -r "$rootfsDir/etc/lsb-release" ]; then
  130. lsbDist="$(. "$rootfsDir/etc/lsb-release" && echo "$DISTRIB_ID")"
  131. fi
  132. if [ -z "$lsbDist" -a -r "$rootfsDir/etc/debian_version" ]; then
  133. lsbDist='Debian'
  134. fi
  135. # normalize to lowercase for easier matching
  136. lsbDist="$(echo "$lsbDist" | tr '[:upper:]' '[:lower:]')"
  137. case "$lsbDist" in
  138. debian)
  139. # updates and security!
  140. if [ "$suite" != 'sid' -a "$suite" != 'unstable' ]; then
  141. (
  142. set -x
  143. sed -i "
  144. p;
  145. s/ $suite / ${suite}-updates /
  146. " "$rootfsDir/etc/apt/sources.list"
  147. echo "deb http://security.debian.org $suite/updates main" >> "$rootfsDir/etc/apt/sources.list"
  148. )
  149. fi
  150. ;;
  151. ubuntu)
  152. # add the updates and security repositories
  153. (
  154. set -x
  155. sed -i "
  156. p;
  157. s/ $suite / ${suite}-updates /; p;
  158. s/ $suite-updates / ${suite}-security /
  159. " "$rootfsDir/etc/apt/sources.list"
  160. )
  161. ;;
  162. tanglu)
  163. # add the updates repository
  164. if [ "$suite" != 'devel' ]; then
  165. (
  166. set -x
  167. sed -i "
  168. p;
  169. s/ $suite / ${suite}-updates /
  170. " "$rootfsDir/etc/apt/sources.list"
  171. )
  172. fi
  173. ;;
  174. steamos)
  175. # add contrib and non-free if "main" is the only component
  176. (
  177. set -x
  178. sed -i "s/ $suite main$/ $suite main contrib non-free/" "$rootfsDir/etc/apt/sources.list"
  179. )
  180. ;;
  181. esac
  182. fi
  183. (
  184. set -x
  185. # make sure we're fully up-to-date
  186. rootfs_chroot sh -xc 'apt-get update && apt-get dist-upgrade -y'
  187. # delete all the apt list files since they're big and get stale quickly
  188. rm -rf "$rootfsDir/var/lib/apt/lists"/*
  189. # this forces "apt-get update" in dependent images, which is also good
  190. mkdir "$rootfsDir/var/lib/apt/lists/partial" # Lucid... "E: Lists directory /var/lib/apt/lists/partial is missing."
  191. )