1040212ab2864b79396921aec78ca418e8676c2d
[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 genschrootconf() {
76     local suite="$1"; shift
77     local arch="$1"; shift
78     local target="$1"; shift
79     local extra="${1:-}"; shift || true
80     local aliases=""
81
82     if [ -n "$extra" ]; then
83         local suite="${suite}-${extra}"
84     fi
85
86     if [ -n "$sbuildnames" ]; then
87         local name="${suite}-${arch}-sbuild"
88     else
89         local name="${suite}_${arch}-dchroot"
90     fi
91
92     local fullname="$name"
93     case "$arch" in
94         kfreebsd-*)
95             aliases="$name"
96             # FreeBSD only allows relatively short paths to mountpoints
97             # therefore saving a few charakters making the chroot dir fit
98             name=${name/kfreebsd-amd64/k-a}
99             name=${name/kfreebsd-i386/k-i}
100         ;;
101     esac
102
103 cat << EOF
104 [${name}]
105 description=[${fullname}] Debian $suite chroot for $arch
106 type=file
107 file=$target
108 EOF
109 [ -n "$groupuser" ] && echo "groups=$groupuser"
110 [ -n "$grouproot" ] && echo "root-groups=$grouproot"
111 [ -n "$users" ] &&     echo "users=$users"
112 [ -n "$usersroot" ] && echo "root-users=$usersroot"
113
114     echo "profile=$personality"
115
116     if [ "$THISARCH" = "$arch" ] && [ -z "$buildd" ]; then
117         if [ -z "$aliases" ] ; then
118             aliases="$suite"
119         else
120             aliases="$aliases,$suite"
121         fi
122     fi
123     if [ -n "$aliases" ] ; then
124         echo "aliases=$aliases"
125     fi
126     case "$arch" in
127         armel|armhf|i386|mips|mipsel|powerpc|s390|sparc)
128             echo "personality=linux32"
129             ;;
130     esac
131     echo
132
133     case "$suite" in
134         sid)
135             genschrootconf "experimental" "$arch" "$target"
136             ;;
137         experimental)
138             :
139             ;;
140         *)
141             if [ -z "$extra" ] && [ -z "$ubuntu" ]; then
142                 genschrootconf "$suite" "$arch" "$target" "backports"
143                 if [ -n "$buildd" ] ; then
144                     genschrootconf "$suite" "$arch" "$target" "backports-sloppy"
145                     genschrootconf "$suite" "$arch" "$target" "proposed-updates"
146                     genschrootconf "$suite" "$arch" "$target" "security"
147                     case "$arch" in
148                         kfreebsd-*)
149                             genschrootconf "$suite" "$arch" "$target" "kfreebsd"
150                             genschrootconf "$suite" "$arch" "$target" "kfreebsd-security"
151                             ;;
152                     esac
153                 fi
154             fi
155     esac
156     return 0
157 }
158
159 do_config() {
160     local tmpschrootconf=$(tempfile)
161     cleanup+=("rm -f $tmpschrootconf")
162     genschrootconf "$suite" "$arch" "$target" > "$tmpschrootconf"
163     if ! [ -e "$schrootconfig" ] || ! diff "$schrootconfig" "$tmpschrootconf" > /dev/null; then
164         mv "$tmpschrootconf" "$schrootconfig"
165         chmod 644 "$schrootconfig"
166     fi
167 }
168
169
170 set -e
171 set -u
172
173 arch="$THISARCH"
174 if [ -e /etc/schroot/dsa/default-mirror ]; then
175     mirror=$(cat /etc/schroot/dsa/default-mirror )
176 fi
177 mirror="${mirror:-http://ftp.debian.org/debian}"
178 configonly=""
179 force=""
180 basedir="/srv/chroot"
181 builddir=""
182 keyring=/usr/share/keyrings/debian-archive-keyring.gpg
183 personality="dsa"
184 sbuildnames=""
185 ubuntu=""
186 groupuser="Debian,guest,d-i"
187 grouproot=""
188 include="apt,fakeroot"
189 users=""
190 usersroot=""
191 bare=""
192 keep=""
193 suffix="dchroot"
194 declare -a cleanup
195 cleanup+=(":")
196 trap do_cleanup EXIT
197 buildd=""
198
199 while getopts "a:b:Bcd:Dfg:hI:Kk:m:o:O:p:r:sS:u" OPTION
200 do
201     case $OPTION in
202         a)
203             arch="$OPTARG"
204             ;;
205         b)
206             basedir="$OPTARG"
207             ;;
208         B)
209             bare="1"
210             ;;
211         c)
212             configonly="1"
213             ;;
214         D)
215             buildd="1"
216             sbuildnames="0"
217             bare="1"
218             groupuser=""
219             grouproot=""
220             users="buildd,buildd2"
221             usersroot="buildd,buildd2"
222             personality="buildd"
223             suffix="sbuild"
224             ;;
225         d)
226             builddir="$OPTARG"
227             ;;
228         f)
229             force="1"
230             ;;
231         g)
232             groupuser="$OPTARG"
233             ;;
234         h)
235             usage
236             exit 0
237             ;;
238         I)
239             include="$include,$OPTARG"
240             ;;
241         K)
242             keep="4"
243             ;;
244         k)
245             keyring="$OPTARG"
246             ;;
247         m)
248             mirror="$OPTARG"
249             ;;
250         o)
251             users="$OPTARG"
252             ;;
253         O)
254             usersroot="$OPTARG"
255             ;;
256         p)
257             personality="$OPTARG"
258             ;;
259         r)
260             grouproot="$OPTARG"
261             ;;
262         s)
263             sbuildnames="1"
264             ;;
265         S)
266             suffix="$OPTARG"
267             ;;
268         u)
269             ubuntu="1"
270             ;;
271         *)
272             usage >&2
273             exit 1
274             ;;
275     esac
276 done
277 shift $(($OPTIND - 1))
278
279 if [ "$#" != 1 ]; then
280     usage >&2
281     exit 1
282 fi
283 suite="$1"; shift
284 tuple="${suite}_${arch}"
285
286 if [ -z "${builddir:-}" ]; then
287   builddir=${builddir:-$basedir}
288   for u in schroot-unpack unpack; do
289     # if one of these directories exists and is on a different filesystem, prefer it.
290     candidate="$builddir/$u"
291     if [ -e "$candidate" ] &&
292        [ "$(stat -f --printf '%i\n' "$builddir" )" != "$(stat -f --printf '%i\n' "$candidate")" ]; then
293       builddir="$candidate"
294     fi
295   done
296 fi
297 [ -d "$basedir" ] || die "Error: $basedir does not exist (or is not a directory)."
298 [ -d "$builddir" ] || die "Error: $builddir does not exist (or is not a directory)."
299
300 target="$basedir/$tuple.tar.gz"
301 ! [ -e "$target" ] || [ -n "$force" ] || die "Error: $target already exists."
302
303 schrootconfig="/etc/schroot/chroot.d/${tuple}-$suffix"
304 ! [ -e "$schrootconfig" ] || [ -n "$force" ] || die "Error: $schrootconfig already exists."
305
306 suite_alias="$suite"
307 case "$arch" in
308     kfreebsd-*)
309         case "$suite" in
310             jessie)
311                 suite_alias="$suite-kfreebsd"
312                 ;;
313         esac
314         ;;
315 esac
316
317
318 #
319 # let's go
320 #
321
322 if [ -n "$configonly" ]; then
323     do_config
324     exit 0
325 fi
326
327 rootdir=$(mktemp -d "$builddir/create-$suite-XXXXXX")
328 cleanup+=("rm -r $rootdir")
329 cleanup+=("umount $rootdir/sys")
330 script=/usr/share/debootstrap/scripts/"$suite"
331 if ! [ -e "$script" ]; then
332     if [ -z "$ubuntu" ]; then
333         script=/usr/share/debootstrap/scripts/sid
334     else
335         script=/usr/share/debootstrap/scripts/gutsy
336     fi
337 fi
338
339 set -x
340 debootstrap \
341     --keyring "$keyring" \
342     --include="$include" \
343     --variant=buildd \
344     --arch="$arch" \
345     "$suite_alias" "$rootdir" "$mirror" "$script"
346 echo "$tuple" > "$rootdir/etc/debian_chroot"
347 echo "force-unsafe-io" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-unsafe-io"
348 echo "force-confnew" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-confnew"
349
350 cleanup+=("umount $rootdir/dev")
351 case "$(uname -s)" in
352   Linux)
353     ;;
354   GNU/kFreeBSD)
355     mount -t devfs none "$rootdir/dev"
356     ;;
357   *)
358     echo >&2 "Warning: Unexpected uname -s output."
359     ;;
360 esac
361
362
363 chroot "$rootdir" apt-get update
364 chroot "$rootdir" apt-get install -y --no-install-recommends policyrcd-script-zg2
365 cat > "$rootdir/usr/local/sbin/policy-rc.d" << 'EOF'
366 #!/bin/sh
367
368 # policy-rc.d script for chroots.
369 # Copyright (c) 2007 Peter Palfrader <peter@palfrader.org>
370
371 while true; do
372     case "$1" in
373         -*)      shift ;;
374         makedev) exit 0;;
375         *)
376             echo "Not running services in chroot."
377             exit 101
378             ;;
379     esac
380 done
381 EOF
382 chmod +x "$rootdir/usr/local/sbin/policy-rc.d"
383 [ -z "$bare" ] && [ -z "$ubuntu" ] && chroot "$rootdir" apt-get install -y --no-install-recommends locales-all
384 chroot "$rootdir" apt-get install -y --no-install-recommends build-essential
385 [ -z "$bare" ] && chroot "$rootdir" apt-get install -y --no-install-recommends zsh less vim fakeroot devscripts gdb
386 if [ -n "$buildd" ] ; then
387     case "$suite" in
388       wheezy|jessie|stretch)
389         chroot "$rootdir" apt-get install -y --no-install-recommends apt-transport-https ca-certificates
390         ;;
391       *)
392         chroot "$rootdir" apt-get install -y --no-install-recommends ca-certificates
393         ;;
394     esac
395 fi
396 rm -f "$rootdir/etc/apt/sources.list" "$rootdir/etc/apt/sources.list.d/*"
397 chroot "$rootdir" apt-get clean
398 umount "$rootdir/dev" 2>/dev/null || true
399 umount "$rootdir/sys" 2>/dev/null || true
400
401 tartmp=$(tempfile --directory "$basedir" --suffix=".tar.gz")
402 cleanup+=("rm -f $tartmp")
403 (
404   cd "$rootdir"
405   chmod 0755 "$rootdir"
406   tar caf "$tartmp" .
407   if ! [ -z "$keep" ]; then
408     savelog -l -c 2 "$target"
409   fi
410   mv "$tartmp" "$target"
411 )
412
413 do_config