2014-05-01 17:38:44 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
|
2016-11-22 22:33:04 -05:00
|
|
|
mkimgdeb="$(basename "$0")"
|
|
|
|
mkimg="$(dirname "$0").sh"
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo >&2 "usage: $mkimgdeb rootfsDir suite [debootstrap-args]"
|
|
|
|
echo >&2 " note: $mkimgdeb meant to be used from $mkimg"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2014-05-01 17:38:44 -04:00
|
|
|
rootfsDir="$1"
|
2016-11-22 22:33:04 -05:00
|
|
|
if [ -z "$rootfsDir" ]; then
|
|
|
|
echo >&2 "error: rootfsDir is missing"
|
|
|
|
echo >&2
|
|
|
|
usage
|
|
|
|
fi
|
2014-05-01 17:38:44 -04:00
|
|
|
shift
|
|
|
|
|
|
|
|
# we have to do a little fancy footwork to make sure "rootfsDir" becomes the second non-option argument to debootstrap
|
|
|
|
|
|
|
|
before=()
|
|
|
|
while [ $# -gt 0 ] && [[ "$1" == -* ]]; do
|
|
|
|
before+=( "$1" )
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
suite="$1"
|
2016-11-22 22:33:04 -05:00
|
|
|
if [ -z "$suite" ]; then
|
|
|
|
echo >&2 "error: suite is missing"
|
|
|
|
echo >&2
|
|
|
|
usage
|
|
|
|
fi
|
2014-05-01 17:38:44 -04:00
|
|
|
shift
|
|
|
|
|
2015-02-28 02:11:55 -05:00
|
|
|
# get path to "chroot" in our current PATH
|
2016-11-22 22:33:04 -05:00
|
|
|
chrootPath="$(type -P chroot || :)"
|
|
|
|
if [ -z "$chrootPath" ]; then
|
|
|
|
echo >&2 "error: chroot not found. Are you root?"
|
|
|
|
echo >&2
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2015-02-28 02:11:55 -05:00
|
|
|
rootfs_chroot() {
|
|
|
|
# "chroot" doesn't set PATH, so we need to set it explicitly to something our new debootstrap chroot can use appropriately!
|
2015-03-25 13:38:17 -04:00
|
|
|
|
2015-02-28 02:11:55 -05:00
|
|
|
# set PATH and chroot away!
|
|
|
|
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' \
|
|
|
|
"$chrootPath" "$rootfsDir" "$@"
|
|
|
|
}
|
|
|
|
|
2014-11-18 17:13:35 -05:00
|
|
|
# allow for DEBOOTSTRAP=qemu-debootstrap ./mkimage.sh ...
|
|
|
|
: ${DEBOOTSTRAP:=debootstrap}
|
|
|
|
|
2014-05-01 17:38:44 -04:00
|
|
|
(
|
|
|
|
set -x
|
2014-11-18 17:13:35 -05:00
|
|
|
$DEBOOTSTRAP "${before[@]}" "$suite" "$rootfsDir" "$@"
|
2014-05-01 17:38:44 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# now for some Docker-specific tweaks
|
|
|
|
|
|
|
|
# prevent init scripts from running during install/update
|
2014-06-20 12:04:48 -04:00
|
|
|
echo >&2 "+ echo exit 101 > '$rootfsDir/usr/sbin/policy-rc.d'"
|
2015-03-25 10:53:24 -04:00
|
|
|
cat > "$rootfsDir/usr/sbin/policy-rc.d" <<-'EOF'
|
2015-03-02 20:33:38 -05:00
|
|
|
#!/bin/sh
|
2014-06-20 12:04:48 -04:00
|
|
|
|
2015-03-02 20:33:38 -05:00
|
|
|
# For most Docker users, "apt-get install" only happens during "docker build",
|
|
|
|
# where starting services doesn't work and often fails in humorous ways. This
|
|
|
|
# prevents those failures by stopping the services from attempting to start.
|
2014-06-20 12:04:48 -04:00
|
|
|
|
2015-03-02 20:33:38 -05:00
|
|
|
exit 101
|
2014-05-01 17:38:44 -04:00
|
|
|
EOF
|
|
|
|
chmod +x "$rootfsDir/usr/sbin/policy-rc.d"
|
|
|
|
|
|
|
|
# prevent upstart scripts from running during install/update
|
|
|
|
(
|
|
|
|
set -x
|
2015-02-28 02:11:55 -05:00
|
|
|
rootfs_chroot dpkg-divert --local --rename --add /sbin/initctl
|
2014-06-20 12:04:48 -04:00
|
|
|
cp -a "$rootfsDir/usr/sbin/policy-rc.d" "$rootfsDir/sbin/initctl"
|
|
|
|
sed -i 's/^exit.*/exit 0/' "$rootfsDir/sbin/initctl"
|
2014-05-01 17:38:44 -04:00
|
|
|
)
|
|
|
|
|
2014-06-20 12:04:48 -04:00
|
|
|
# shrink a little, since apt makes us cache-fat (wheezy: ~157.5MB vs ~120MB)
|
2015-02-28 02:11:55 -05:00
|
|
|
( set -x; rootfs_chroot apt-get clean )
|
2014-05-01 17:38:44 -04:00
|
|
|
|
2014-12-26 16:50:18 -05:00
|
|
|
# this file is one APT creates to make sure we don't "autoremove" our currently
|
|
|
|
# in-use kernel, which doesn't really apply to debootstraps/Docker images that
|
|
|
|
# don't even have kernels installed
|
|
|
|
rm -f "$rootfsDir/etc/apt/apt.conf.d/01autoremove-kernels"
|
|
|
|
|
2014-05-01 17:38:44 -04:00
|
|
|
# Ubuntu 10.04 sucks... :)
|
|
|
|
if strings "$rootfsDir/usr/bin/dpkg" | grep -q unsafe-io; then
|
|
|
|
# force dpkg not to call sync() after package extraction (speeding up installs)
|
|
|
|
echo >&2 "+ echo force-unsafe-io > '$rootfsDir/etc/dpkg/dpkg.cfg.d/docker-apt-speedup'"
|
2014-06-20 12:04:48 -04:00
|
|
|
cat > "$rootfsDir/etc/dpkg/dpkg.cfg.d/docker-apt-speedup" <<-'EOF'
|
2015-03-02 20:33:38 -05:00
|
|
|
# For most Docker users, package installs happen during "docker build", which
|
|
|
|
# doesn't survive power loss and gets restarted clean afterwards anyhow, so
|
|
|
|
# this minor tweak gives us a nice speedup (much nicer on spinning disks,
|
|
|
|
# obviously).
|
2014-06-20 12:04:48 -04:00
|
|
|
|
2015-03-02 20:33:38 -05:00
|
|
|
force-unsafe-io
|
2014-06-20 12:04:48 -04:00
|
|
|
EOF
|
2014-05-01 17:38:44 -04:00
|
|
|
fi
|
|
|
|
|
2014-05-25 23:59:43 -04:00
|
|
|
if [ -d "$rootfsDir/etc/apt/apt.conf.d" ]; then
|
2014-05-01 17:38:44 -04:00
|
|
|
# _keep_ us lean by effectively running "apt-get clean" after every install
|
|
|
|
aptGetClean='"rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true";'
|
|
|
|
echo >&2 "+ cat > '$rootfsDir/etc/apt/apt.conf.d/docker-clean'"
|
|
|
|
cat > "$rootfsDir/etc/apt/apt.conf.d/docker-clean" <<-EOF
|
2014-06-20 12:04:48 -04:00
|
|
|
# Since for most Docker users, package installs happen in "docker build" steps,
|
|
|
|
# they essentially become individual layers due to the way Docker handles
|
|
|
|
# layering, especially using CoW filesystems. What this means for us is that
|
|
|
|
# the caches that APT keeps end up just wasting space in those layers, making
|
|
|
|
# our layers unnecessarily large (especially since we'll normally never use
|
|
|
|
# these caches again and will instead just "docker build" again and make a brand
|
|
|
|
# new image).
|
|
|
|
|
|
|
|
# Ideally, these would just be invoking "apt-get clean", but in our testing,
|
|
|
|
# that ended up being cyclic and we got stuck on APT's lock, so we get this fun
|
|
|
|
# creation that's essentially just "apt-get clean".
|
2014-05-01 17:38:44 -04:00
|
|
|
DPkg::Post-Invoke { ${aptGetClean} };
|
|
|
|
APT::Update::Post-Invoke { ${aptGetClean} };
|
|
|
|
|
|
|
|
Dir::Cache::pkgcache "";
|
|
|
|
Dir::Cache::srcpkgcache "";
|
2014-06-20 12:04:48 -04:00
|
|
|
|
|
|
|
# Note that we do realize this isn't the ideal way to do this, and are always
|
2014-07-24 18:19:50 -04:00
|
|
|
# open to better suggestions (https://github.com/docker/docker/issues).
|
2014-05-01 17:38:44 -04:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# remove apt-cache translations for fast "apt-get update"
|
2014-06-20 12:04:48 -04:00
|
|
|
echo >&2 "+ echo Acquire::Languages 'none' > '$rootfsDir/etc/apt/apt.conf.d/docker-no-languages'"
|
|
|
|
cat > "$rootfsDir/etc/apt/apt.conf.d/docker-no-languages" <<-'EOF'
|
2015-03-02 20:33:38 -05:00
|
|
|
# In Docker, we don't often need the "Translations" files, so we're just wasting
|
|
|
|
# time and space by downloading them, and this inhibits that. For users that do
|
|
|
|
# need them, it's a simple matter to delete this file and "apt-get update". :)
|
2014-06-20 12:04:48 -04:00
|
|
|
|
2015-03-02 20:33:38 -05:00
|
|
|
Acquire::Languages "none";
|
2014-06-20 12:04:48 -04:00
|
|
|
EOF
|
2014-07-16 16:58:02 -04:00
|
|
|
|
|
|
|
echo >&2 "+ echo Acquire::GzipIndexes 'true' > '$rootfsDir/etc/apt/apt.conf.d/docker-gzip-indexes'"
|
|
|
|
cat > "$rootfsDir/etc/apt/apt.conf.d/docker-gzip-indexes" <<-'EOF'
|
2015-03-02 20:33:38 -05:00
|
|
|
# Since Docker users using "RUN apt-get update && apt-get install -y ..." in
|
|
|
|
# their Dockerfiles don't go delete the lists files afterwards, we want them to
|
|
|
|
# be as small as possible on-disk, so we explicitly request "gz" versions and
|
|
|
|
# tell Apt to keep them gzipped on-disk.
|
2014-07-16 16:58:02 -04:00
|
|
|
|
2015-03-02 20:33:38 -05:00
|
|
|
# For comparison, an "apt-get update" layer without this on a pristine
|
|
|
|
# "debian:wheezy" base image was "29.88 MB", where with this it was only
|
|
|
|
# "8.273 MB".
|
2014-07-16 16:58:02 -04:00
|
|
|
|
2015-03-02 20:33:38 -05:00
|
|
|
Acquire::GzipIndexes "true";
|
|
|
|
Acquire::CompressionTypes::Order:: "gz";
|
2014-07-16 16:58:02 -04:00
|
|
|
EOF
|
2015-03-02 20:42:26 -05:00
|
|
|
|
|
|
|
# update "autoremove" configuration to be aggressive about removing suggests deps that weren't manually installed
|
|
|
|
echo >&2 "+ echo Apt::AutoRemove::SuggestsImportant 'false' > '$rootfsDir/etc/apt/apt.conf.d/docker-autoremove-suggests'"
|
|
|
|
cat > "$rootfsDir/etc/apt/apt.conf.d/docker-autoremove-suggests" <<-'EOF'
|
|
|
|
# Since Docker users are looking for the smallest possible final images, the
|
|
|
|
# following emerges as a very common pattern:
|
|
|
|
|
|
|
|
# RUN apt-get update \
|
|
|
|
# && apt-get install -y <packages> \
|
|
|
|
# && <do some compilation work> \
|
|
|
|
# && apt-get purge -y --auto-remove <packages>
|
|
|
|
|
|
|
|
# By default, APT will actually _keep_ packages installed via Recommends or
|
|
|
|
# Depends if another package Suggests them, even and including if the package
|
|
|
|
# that originally caused them to be installed is removed. Setting this to
|
|
|
|
# "false" ensures that APT is appropriately aggressive about removing the
|
|
|
|
# packages it added.
|
|
|
|
|
|
|
|
# https://aptitude.alioth.debian.org/doc/en/ch02s05s05.html#configApt-AutoRemove-SuggestsImportant
|
|
|
|
Apt::AutoRemove::SuggestsImportant "false";
|
2014-07-16 16:58:02 -04:00
|
|
|
EOF
|
2014-05-01 17:38:44 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$DONT_TOUCH_SOURCES_LIST" ]; then
|
|
|
|
# tweak sources.list, where appropriate
|
|
|
|
lsbDist=
|
|
|
|
if [ -z "$lsbDist" -a -r "$rootfsDir/etc/os-release" ]; then
|
|
|
|
lsbDist="$(. "$rootfsDir/etc/os-release" && echo "$ID")"
|
|
|
|
fi
|
|
|
|
if [ -z "$lsbDist" -a -r "$rootfsDir/etc/lsb-release" ]; then
|
|
|
|
lsbDist="$(. "$rootfsDir/etc/lsb-release" && echo "$DISTRIB_ID")"
|
|
|
|
fi
|
|
|
|
if [ -z "$lsbDist" -a -r "$rootfsDir/etc/debian_version" ]; then
|
|
|
|
lsbDist='Debian'
|
|
|
|
fi
|
2014-06-20 12:04:48 -04:00
|
|
|
# normalize to lowercase for easier matching
|
|
|
|
lsbDist="$(echo "$lsbDist" | tr '[:upper:]' '[:lower:]')"
|
2014-05-01 17:38:44 -04:00
|
|
|
case "$lsbDist" in
|
2014-06-20 12:04:48 -04:00
|
|
|
debian)
|
2014-05-01 17:38:44 -04:00
|
|
|
# updates and security!
|
2018-12-05 13:35:17 -05:00
|
|
|
if curl -o /dev/null -s --head --location --fail "http://security.debian.org/dists/$suite/updates/main/binary-$(rootfs_chroot dpkg --print-architecture)/Packages.gz"; then
|
2014-05-01 17:38:44 -04:00
|
|
|
(
|
|
|
|
set -x
|
2014-06-20 12:04:48 -04:00
|
|
|
sed -i "
|
|
|
|
p;
|
|
|
|
s/ $suite / ${suite}-updates /
|
|
|
|
" "$rootfsDir/etc/apt/sources.list"
|
2014-05-01 17:38:44 -04:00
|
|
|
echo "deb http://security.debian.org $suite/updates main" >> "$rootfsDir/etc/apt/sources.list"
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
;;
|
2014-06-20 12:04:48 -04:00
|
|
|
ubuntu)
|
|
|
|
# add the updates and security repositories
|
2014-05-01 17:38:44 -04:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
sed -i "
|
2014-06-20 12:04:48 -04:00
|
|
|
p;
|
|
|
|
s/ $suite / ${suite}-updates /; p;
|
|
|
|
s/ $suite-updates / ${suite}-security /
|
2014-05-01 17:38:44 -04:00
|
|
|
" "$rootfsDir/etc/apt/sources.list"
|
|
|
|
)
|
|
|
|
;;
|
2014-06-20 12:04:48 -04:00
|
|
|
tanglu)
|
2014-05-01 17:38:44 -04:00
|
|
|
# add the updates repository
|
|
|
|
if [ "$suite" != 'devel' ]; then
|
|
|
|
(
|
|
|
|
set -x
|
2014-06-20 12:04:48 -04:00
|
|
|
sed -i "
|
|
|
|
p;
|
|
|
|
s/ $suite / ${suite}-updates /
|
|
|
|
" "$rootfsDir/etc/apt/sources.list"
|
2014-05-01 17:38:44 -04:00
|
|
|
)
|
|
|
|
fi
|
|
|
|
;;
|
2014-06-20 12:04:48 -04:00
|
|
|
steamos)
|
|
|
|
# add contrib and non-free if "main" is the only component
|
2014-05-01 17:38:44 -04:00
|
|
|
(
|
|
|
|
set -x
|
|
|
|
sed -i "s/ $suite main$/ $suite main contrib non-free/" "$rootfsDir/etc/apt/sources.list"
|
|
|
|
)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
|
|
|
(
|
|
|
|
set -x
|
2015-03-25 13:38:17 -04:00
|
|
|
|
2014-06-25 21:07:39 -04:00
|
|
|
# make sure we're fully up-to-date
|
2015-02-28 02:11:55 -05:00
|
|
|
rootfs_chroot sh -xc 'apt-get update && apt-get dist-upgrade -y'
|
2015-03-25 13:38:17 -04:00
|
|
|
|
2014-06-25 21:07:39 -04:00
|
|
|
# delete all the apt list files since they're big and get stale quickly
|
|
|
|
rm -rf "$rootfsDir/var/lib/apt/lists"/*
|
|
|
|
# this forces "apt-get update" in dependent images, which is also good
|
2015-03-25 13:38:17 -04:00
|
|
|
|
2014-08-12 17:37:42 -04:00
|
|
|
mkdir "$rootfsDir/var/lib/apt/lists/partial" # Lucid... "E: Lists directory /var/lib/apt/lists/partial is missing."
|
2014-05-01 17:38:44 -04:00
|
|
|
)
|