mkimage-debootstrap.sh 9.0 KB

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