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