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