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