Note that exim contains tracker-specific configuration
[mirror/dsa-puppet.git] / modules / schroot / files / setup-dchroot
1 #!/bin/bash
2
3 ##
4 ## THIS FILE IS UNDER PUPPET CONTROL. DON'T EDIT IT HERE.
5 ## USE: git clone git+ssh://$USER@puppet.debian.org/srv/puppet.debian.org/git/dsa-puppet.git
6 ##
7
8 # Copyright (c) 2013 Peter Palfrader
9 #
10 # Permission is hereby granted, free of charge, to any person
11 # obtaining a copy of this software and associated documentation
12 # files (the "Software"), to deal in the Software without
13 # restriction, including without limitation the rights to use,
14 # copy, modify, merge, publish, distribute, sublicense, and/or sell
15 # copies of the Software, and to permit persons to whom the
16 # Software is furnished to do so, subject to the following
17 # conditions:
18 #
19 # The above copyright notice and this permission notice shall be
20 # included in all copies or substantial portions of the Software.
21 #
22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 # OTHER DEALINGS IN THE SOFTWARE.
30
31 THISARCH=$(dpkg --print-architecture)
32
33 usage()
34 {
35 cat << EOF
36 usage: $0 <suite>
37
38 OPTIONS:
39     -a ARCH    debootstrap arch [$arch]
40     -m MIRROR  http mirror to use [$mirror]
41     -b basedir place where to put the tarball [$basedir]
42     -B         install less stuff into chroot [$bare]
43     -c         write config only
44     -D         set up a buildd schroot - changes conffile name and
45                various defaults.  Run -D -h.  Also creates more aliases.
46     -d dir     place where to build the chroot [${builddir:-$basedir}]
47     -f         overwrite config and target tarball
48     -g GROUPS  groups that should have access to the schroot [$groupuser]
49     -K         keep old tarballs around for a while (4 iterations)
50     -k KEYRING use an alternate keyring [$keyring]
51     -o USER    users that should have access to the schroot [$users]
52     -O USER    users that should have root in the schroot [$usersroot]
53     -p PERS    use a different sbuild personality [$personality]
54     -r GROUPS  groups that should have root in the schroot [$grouproot]
55     -s         use sbuild compatible naming scheme [$sbuildnames]
56     -S SUFFIX  conffile suffix [$suffix]
57     -u         Ubuntu target
58     -h         this help
59 EOF
60 }
61
62 die() {
63     echo >&2 "$*"
64     exit 1
65 }
66
67 do_cleanup() {
68     local cnt
69     cnt=$((${#cleanup[*]}-1))
70     for i in $(seq ${cnt} -1 0); do
71         ${cleanup[$i]} || true
72     done
73 }
74
75 genname() {
76     local suite="$1"; shift
77     local arch="$1"; shift
78
79     if [ -n "$sbuildnames" ]; then
80         local name="${suite}-${arch}-sbuild"
81     else
82         local name="${suite}_${arch}-dchroot"
83     fi
84
85     echo "$name"
86 }
87
88
89 genschrootconf() {
90     local suite="$1"; shift
91     local arch="$1"; shift
92     local target="$1"; shift
93     local extra="${1:-}"; shift || true
94     local aliases=""
95
96     if [ -n "$extra" ]; then
97         local suite="${suite}-${extra}"
98     fi
99
100     local name="$(genname "$suite" "$arch")"
101
102     local fullname="$name"
103     case "$arch" in
104         kfreebsd-*)
105             aliases="$name"
106             # FreeBSD only allows relatively short paths to mountpoints
107             # therefore saving a few charakters making the chroot dir fit
108             name=${name/kfreebsd-amd64/k-a}
109             name=${name/kfreebsd-i386/k-i}
110         ;;
111     esac
112
113 cat << EOF
114 [${name}]
115 description=[${fullname}] Debian $suite chroot for $arch
116 type=file
117 file=$target
118 EOF
119 [ -n "$groupuser" ] && echo "groups=$groupuser"
120 [ -n "$grouproot" ] && echo "root-groups=$grouproot"
121 [ -n "$users" ] &&     echo "users=$users"
122 [ -n "$usersroot" ] && echo "root-users=$usersroot"
123
124     echo "profile=$personality"
125
126     if [ "$THISARCH" = "$arch" ] && [ -z "$buildd" ]; then
127         if [ -z "$aliases" ] ; then
128             aliases="$suite"
129         else
130             aliases="$aliases,$suite"
131         fi
132     fi
133     if [ -n "$aliases" ] ; then
134         echo "aliases=$aliases"
135     fi
136     case "$arch" in
137         armel|armhf|i386|mips|mipsel|powerpc|s390|sparc)
138             echo "personality=linux32"
139             ;;
140     esac
141     echo
142
143     case "$suite" in
144         sid)
145             genschrootconf "experimental" "$arch" "$target"
146             ;;
147         experimental)
148             :
149             ;;
150         *)
151             if [ -z "$extra" ] && [ -z "$ubuntu" ]; then
152                 genschrootconf "$suite" "$arch" "$target" "backports"
153                 if [ -n "$buildd" ] ; then
154                     genschrootconf "$suite" "$arch" "$target" "backports-sloppy"
155                     genschrootconf "$suite" "$arch" "$target" "proposed-updates"
156                     genschrootconf "$suite" "$arch" "$target" "security"
157                     case "$arch" in
158                         kfreebsd-*)
159                             genschrootconf "$suite" "$arch" "$target" "kfreebsd"
160                             genschrootconf "$suite" "$arch" "$target" "kfreebsd-security"
161                             ;;
162                     esac
163                 fi
164             fi
165     esac
166     return 0
167 }
168
169 do_config() {
170     local tmpschrootconf=$(tempfile)
171     cleanup+=("rm -f $tmpschrootconf")
172     genschrootconf "$suite" "$arch" "$target" > "$tmpschrootconf"
173     if ! [ -e "$schrootconfig" ] || ! diff "$schrootconfig" "$tmpschrootconf" > /dev/null; then
174         mv "$tmpschrootconf" "$schrootconfig"
175         chmod 644 "$schrootconfig"
176     fi
177 }
178
179
180 set -e
181 set -u
182
183 arch="$THISARCH"
184 if [ -e /etc/schroot/dsa/default-mirror ]; then
185     mirror=$(cat /etc/schroot/dsa/default-mirror )
186 fi
187 mirror="${mirror:-https://deb.debian.org/debian}"
188 smirror="https://deb.debian.org/debian-security"
189 configonly=""
190 force=""
191 basedir="/srv/chroot"
192 builddir=""
193 keyring=/usr/share/keyrings/debian-archive-keyring.gpg
194 personality="dsa"
195 sbuildnames=""
196 ubuntu=""
197 groupuser="Debian,guest,d-i"
198 grouproot=""
199 include="apt,fakeroot,ca-certificates"
200 users=""
201 usersroot=""
202 bare=""
203 keep=""
204 suffix="dchroot"
205 declare -a cleanup
206 cleanup+=(":")
207 trap do_cleanup EXIT
208 buildd=""
209
210 while getopts "a:b:Bcd:Dfg:hI:Kk:m:o:O:p:r:sS:u" OPTION
211 do
212     case $OPTION in
213         a)
214             arch="$OPTARG"
215             ;;
216         b)
217             basedir="$OPTARG"
218             ;;
219         B)
220             bare="1"
221             ;;
222         c)
223             configonly="1"
224             ;;
225         D)
226             buildd="1"
227             sbuildnames="0"
228             bare="1"
229             groupuser=""
230             grouproot=""
231             users="buildd,buildd2"
232             usersroot="buildd,buildd2"
233             personality="buildd"
234             suffix="sbuild"
235             ;;
236         d)
237             builddir="$OPTARG"
238             ;;
239         f)
240             force="1"
241             ;;
242         g)
243             groupuser="$OPTARG"
244             ;;
245         h)
246             usage
247             exit 0
248             ;;
249         I)
250             include="$include,$OPTARG"
251             ;;
252         K)
253             keep="4"
254             ;;
255         k)
256             keyring="$OPTARG"
257             ;;
258         m)
259             mirror="$OPTARG"
260             ;;
261         o)
262             users="$OPTARG"
263             ;;
264         O)
265             usersroot="$OPTARG"
266             ;;
267         p)
268             personality="$OPTARG"
269             ;;
270         r)
271             grouproot="$OPTARG"
272             ;;
273         s)
274             sbuildnames="1"
275             ;;
276         S)
277             suffix="$OPTARG"
278             ;;
279         u)
280             ubuntu="1"
281             ;;
282         *)
283             usage >&2
284             exit 1
285             ;;
286     esac
287 done
288 shift $(($OPTIND - 1))
289
290 if [ "$#" != 1 ]; then
291     usage >&2
292     exit 1
293 fi
294 suite="$1"; shift
295 tuple="${suite}_${arch}"
296
297 if [ -z "${builddir:-}" ]; then
298   builddir=${builddir:-$basedir}
299   for u in schroot-unpack unpack; do
300     # if one of these directories exists and is on a different filesystem, prefer it.
301     candidate="$builddir/$u"
302     if [ -e "$candidate" ] &&
303        [ "$(stat -f --printf '%i\n' "$builddir" )" != "$(stat -f --printf '%i\n' "$candidate")" ]; then
304       builddir="$candidate"
305     fi
306   done
307 fi
308 [ -d "$basedir" ] || die "Error: $basedir does not exist (or is not a directory)."
309 [ -d "$builddir" ] || die "Error: $builddir does not exist (or is not a directory)."
310
311 target="$basedir/$tuple.tar.gz"
312 ! [ -e "$target" ] || [ -n "$force" ] || die "Error: $target already exists."
313
314 schrootconfig="/etc/schroot/chroot.d/${tuple}-$suffix"
315 ! [ -e "$schrootconfig" ] || [ -n "$force" ] || die "Error: $schrootconfig already exists."
316
317 suite_alias="$suite"
318 case "$arch" in
319     kfreebsd-*)
320         case "$suite" in
321             jessie)
322                 suite_alias="$suite-kfreebsd"
323                 ;;
324         esac
325         ;;
326 esac
327
328
329 #
330 # let's go
331 #
332
333 if [ -n "$configonly" ]; then
334     do_config
335     exit 0
336 fi
337
338 rootdir=$(mktemp -d "$builddir/create-$suite-XXXXXX")
339 cleanup+=("rm -r $rootdir")
340 cleanup+=("umount $rootdir/sys")
341 script=/usr/share/debootstrap/scripts/"$suite"
342 if ! [ -e "$script" ]; then
343     if [ -z "$ubuntu" ]; then
344         script=/usr/share/debootstrap/scripts/sid
345     else
346         script=/usr/share/debootstrap/scripts/gutsy
347     fi
348 fi
349
350 case "$suite" in
351   stretch|jessie|precise|trusty|xenial)
352     include="$include,apt-transport-https"
353     ;;
354 esac
355
356 bindir=$(mktemp -d)
357 cleanup+=("rm -r $bindir")
358 cat > "$bindir/wget" << 'EOF'
359 #!/bin/sh
360 exec /usr/bin/wget --ca-directory=/etc/ssl/ca-global "$@"
361 EOF
362 chmod +x "$bindir/wget"
363
364 set -x
365 PATH="$bindir:$PATH" \
366   debootstrap \
367     --keyring "$keyring" \
368     --include="$include" \
369     --variant=buildd \
370     --arch="$arch" \
371     --no-merged-usr \
372     "$suite_alias" "$rootdir" "$mirror" "$script"
373 echo "$tuple" > "$rootdir/etc/debian_chroot"
374 echo "force-unsafe-io" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-unsafe-io"
375 echo "force-confnew" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-confnew"
376
377 cleanup+=("umount $rootdir/dev")
378 case "$(uname -s)" in
379   Linux)
380     ;;
381   GNU/kFreeBSD)
382     mount -t devfs none "$rootdir/dev"
383     ;;
384   *)
385     echo >&2 "Warning: Unexpected uname -s output."
386     ;;
387 esac
388
389
390 chroot "$rootdir" apt-get update
391 chroot "$rootdir" apt-get install -y --no-install-recommends policyrcd-script-zg2
392 cat > "$rootdir/usr/local/sbin/policy-rc.d" << 'EOF'
393 #!/bin/sh
394
395 # policy-rc.d script for chroots.
396 # Copyright (c) 2007 Peter Palfrader <peter@palfrader.org>
397
398 while true; do
399     case "$1" in
400         -*)      shift ;;
401         makedev) exit 0;;
402         *)
403             echo "Not running services in chroot."
404             exit 101
405             ;;
406     esac
407 done
408 EOF
409 chmod +x "$rootdir/usr/local/sbin/policy-rc.d"
410
411 case "$suite" in
412   jessie) # LTS updates
413     echo "deb ${smirror} ${suite}/updates main" >> "$rootdir/etc/apt/sources.list"
414     chroot "$rootdir" apt-get update
415     chroot "$rootdir" apt-get dist-upgrade -y
416     ;;
417 esac
418 if [ -n "$ubuntu" ]; then
419   echo "deb $mirror ${suite}-updates main" >> "$rootdir/etc/apt/sources.list"
420   echo "deb $mirror ${suite}-security main" >> "$rootdir/etc/apt/sources.list"
421   chroot "$rootdir" apt-get update
422   chroot "$rootdir" apt-get dist-upgrade -y
423 fi
424
425 [ -z "$bare" ] && [ -z "$ubuntu" ] && chroot "$rootdir" apt-get install -y --no-install-recommends locales-all
426 chroot "$rootdir" apt-get install -y --no-install-recommends build-essential
427 [ -z "$bare" ] && chroot "$rootdir" apt-get install -y --no-install-recommends zsh less vim fakeroot devscripts gdb
428 rm -f "$rootdir/etc/apt/sources.list" "$rootdir/etc/apt/sources.list.d/*"
429 chroot "$rootdir" apt-get clean
430 umount "$rootdir/dev" 2>/dev/null || true
431 umount "$rootdir/sys" 2>/dev/null || true
432
433 tartmp=$(tempfile --directory "$basedir" --suffix=".tar.gz")
434 cleanup+=("rm -f $tartmp")
435 (
436   cd "$rootdir"
437   chmod 0755 "$rootdir"
438   tar caf "$tartmp" .
439   if ! [ -z "$keep" ]; then
440     savelog -l -c 2 "$target"
441   fi
442   mv "$tartmp" "$target"
443 )
444
445 do_config