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