setup-dchroot: fix SUITE_ARCH detection for jessie kfreebsd chroots
[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" "lts"
146                     genschrootconf "$suite" "$arch" "$target" "proposed-updates"
147                     genschrootconf "$suite" "$arch" "$target" "security"
148                     case "$arch" in
149                         kfreebsd-*)
150                             genschrootconf "$suite" "$arch" "$target" "kfreebsd"
151                             genschrootconf "$suite" "$arch" "$target" "kfreebsd-security"
152                             ;;
153                     esac
154                 fi
155             fi
156     esac
157     return 0
158 }
159
160 do_config() {
161     local tmpschrootconf=$(tempfile)
162     cleanup+=("rm -f $tmpschrootconf")
163     genschrootconf "$suite" "$arch" "$target" > "$tmpschrootconf"
164     if ! [ -e "$schrootconfig" ] || ! diff "$schrootconfig" "$tmpschrootconf" > /dev/null; then
165         mv "$tmpschrootconf" "$schrootconfig"
166         chmod 644 "$schrootconfig"
167     fi
168 }
169
170
171 set -e
172 set -u
173
174 arch="$THISARCH"
175 if [ -e /etc/schroot/dsa/default-mirror ]; then
176     mirror=$(cat /etc/schroot/dsa/default-mirror )
177 fi
178 mirror="${mirror:-http://ftp.debian.org/debian}"
179 configonly=""
180 force=""
181 basedir="/srv/chroot"
182 builddir=""
183 keyring=/usr/share/keyrings/debian-archive-keyring.gpg
184 personality="dsa"
185 sbuildnames=""
186 ubuntu=""
187 groupuser="Debian,guest,d-i"
188 grouproot=""
189 include="apt,fakeroot"
190 users=""
191 usersroot=""
192 bare=""
193 keep=""
194 suffix="dchroot"
195 declare -a cleanup
196 cleanup+=(":")
197 trap do_cleanup EXIT
198 buildd=""
199
200 while getopts "a:b:Bcd:Dfg:hI:Kk:m:o:O:p:r:sS:u" OPTION
201 do
202     case $OPTION in
203         a)
204             arch="$OPTARG"
205             ;;
206         b)
207             basedir="$OPTARG"
208             ;;
209         B)
210             bare="1"
211             ;;
212         c)
213             configonly="1"
214             ;;
215         D)
216             buildd="1"
217             sbuildnames="0"
218             bare="1"
219             groupuser=""
220             grouproot=""
221             users="buildd,buildd2"
222             usersroot="buildd,buildd2"
223             personality="buildd"
224             suffix="sbuild"
225             ;;
226         d)
227             builddir="$OPTARG"
228             ;;
229         f)
230             force="1"
231             ;;
232         g)
233             groupuser="$OPTARG"
234             ;;
235         h)
236             usage
237             exit 0
238             ;;
239         I)
240             include="$include,$OPTARG"
241             ;;
242         K)
243             keep="4"
244             ;;
245         k)
246             keyring="$OPTARG"
247             ;;
248         m)
249             mirror="$OPTARG"
250             ;;
251         o)
252             users="$OPTARG"
253             ;;
254         O)
255             usersroot="$OPTARG"
256             ;;
257         p)
258             personality="$OPTARG"
259             ;;
260         r)
261             grouproot="$OPTARG"
262             ;;
263         s)
264             sbuildnames="1"
265             ;;
266         S)
267             suffix="$OPTARG"
268             ;;
269         u)
270             ubuntu="1"
271             ;;
272         *)
273             usage >&2
274             exit 1
275             ;;
276     esac
277 done
278 shift $(($OPTIND - 1))
279
280 if [ "$#" != 1 ]; then
281     usage >&2
282     exit 1
283 fi
284 suite="$1"; shift
285 tuple="${suite}_${arch}"
286
287 if [ -z "${builddir:-}" ]; then
288   builddir=${builddir:-$basedir}
289   for u in schroot-unpack unpack; do
290     # if one of these directories exists and is on a different filesystem, prefer it.
291     candidate="$builddir/$u"
292     if [ -e "$candidate" ] &&
293        [ "$(stat -f --printf '%i\n' "$builddir" )" != "$(stat -f --printf '%i\n' "$candidate")" ]; then
294       builddir="$candidate"
295     fi
296   done
297 fi
298 [ -d "$basedir" ] || die "Error: $basedir does not exist (or is not a directory)."
299 [ -d "$builddir" ] || die "Error: $builddir does not exist (or is not a directory)."
300
301 target="$basedir/$tuple.tar.gz"
302 ! [ -e "$target" ] || [ -n "$force" ] || die "Error: $target already exists."
303
304 schrootconfig="/etc/schroot/chroot.d/${tuple}-$suffix"
305 ! [ -e "$schrootconfig" ] || [ -n "$force" ] || die "Error: $schrootconfig already exists."
306
307 suite_alias="$suite"
308 case "$arch" in
309     kfreebsd-*)
310         case "$suite" in
311             jessie)
312                 suite_alias="$suite-kfreebsd"
313                 ;;
314         esac
315         ;;
316 esac
317
318
319 #
320 # let's go
321 #
322
323 if [ -n "$configonly" ]; then
324     do_config
325     exit 0
326 fi
327
328 rootdir=$(mktemp -d "$builddir/create-$suite-XXXXXX")
329 cleanup+=("rm -r $rootdir")
330 cleanup+=("umount $rootdir/sys")
331 script=/usr/share/debootstrap/scripts/"$suite"
332 if ! [ -e "$script" ]; then
333     if [ -z "$ubuntu" ]; then
334         script=/usr/share/debootstrap/scripts/sid
335     else
336         script=/usr/share/debootstrap/scripts/gutsy
337     fi
338 fi
339
340 set -x
341 debootstrap \
342     --keyring "$keyring" \
343     --include="$include" \
344     --variant=buildd \
345     --arch="$arch" \
346     "$suite_alias" "$rootdir" "$mirror" "$script"
347 echo "$tuple" > "$rootdir/etc/debian_chroot"
348 echo "force-unsafe-io" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-unsafe-io"
349 echo "force-confnew" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-confnew"
350
351 cleanup+=("umount $rootdir/dev")
352 case "$(uname -s)" in
353   Linux)
354     ;;
355   GNU/kFreeBSD)
356     mount -t devfs none "$rootdir/dev"
357     ;;
358   *)
359     echo >&2 "Warning: Unexpected uname -s output."
360     ;;
361 esac
362
363
364 chroot "$rootdir" apt-get update
365 chroot "$rootdir" apt-get install -y --no-install-recommends policyrcd-script-zg2
366 cat > "$rootdir/usr/local/sbin/policy-rc.d" << 'EOF'
367 #!/bin/sh
368
369 # policy-rc.d script for chroots.
370 # Copyright (c) 2007 Peter Palfrader <peter@palfrader.org>
371
372 while true; do
373     case "$1" in
374         -*)      shift ;;
375         makedev) exit 0;;
376         *)
377             echo "Not running services in chroot."
378             exit 101
379             ;;
380     esac
381 done
382 EOF
383 chmod +x "$rootdir/usr/local/sbin/policy-rc.d"
384 [ -z "$bare" ] && [ -z "$ubuntu" ] && chroot "$rootdir" apt-get install -y --no-install-recommends locales-all
385 chroot "$rootdir" apt-get install -y --no-install-recommends build-essential
386 [ -z "$bare" ] && chroot "$rootdir" apt-get install -y --no-install-recommends zsh less vim fakeroot devscripts gdb
387 rm -f "$rootdir/etc/apt/sources.list" "$rootdir/etc/apt/sources.list.d/*"
388 chroot "$rootdir" apt-get clean
389 umount "$rootdir/dev" 2>/dev/null || true
390 umount "$rootdir/sys" 2>/dev/null || true
391
392 tartmp=$(tempfile --directory "$basedir" --suffix=".tar.gz")
393 cleanup+=("rm -f $tartmp")
394 (
395   cd "$rootdir"
396   chmod 0755 "$rootdir"
397   tar caf "$tartmp" .
398   if ! [ -z "$keep" ]; then
399     savelog -l -c 4 "$target"
400   fi
401   mv "$tartmp" "$target"
402 )
403
404 do_config