9af91fc9e49c3cede43957ed779739c035c0483d
[mirror/dsa-puppet.git] / modules / porterbox / 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 various defaults.  Run -D -h)."
45     -d dir     place where to build the chroot [${builddir:-$basedir}]
46     -f         overwrite config and target tarball
47     -g GROUPS  groups that should have access to the schroot [$groupuser]
48     -K         keep old tarballs around for a while (4 iterations)
49     -k KEYRING use an alternate keyring [$keyring]
50     -o USER    users that should have access to the schroot [$users]
51     -O USER    users that should have root in the schroot [$usersroot]
52     -p PERS    use a different sbuild personality [$personality]
53     -r GROUPS  groups that should have root in the schroot [$grouproot]
54     -s         use sbuild compatible naming scheme [$sbuildnames]
55     -S SUFFIX  conffile suffix [$suffix]
56     -u         Ubuntu target
57     -h         this help
58 EOF
59 }
60
61 die() {
62     echo >&2 "$*"
63     exit 1
64 }
65
66 do_cleanup() {
67     local cnt
68     cnt=$((${#cleanup[*]}-1))
69     for i in $(seq ${cnt} -1 0); do
70         ${cleanup[$i]} || true
71     done
72 }
73
74 genschrootconf() {
75     local suite="$1"; shift
76     local arch="$1"; shift
77     local target="$1"; shift
78     local extra="${1:-}"; shift || true
79
80     if [ -n "$extra" ]; then
81         local suite="${suite}-${extra}"
82     fi
83
84     if [ -n "$sbuildnames" ]; then
85         local name="${suite}-${arch}-sbuild"
86     else
87         local name="${suite}_${arch}-dchroot"
88     fi
89
90
91 cat << EOF
92 [${name}]
93 description=[${name}] Debian $suite chroot for $arch
94 type=file
95 file=$target
96 EOF
97 [ -n "$groupuser" ] && echo "groups=$groupuser"
98 [ -n "$grouproot" ] && echo "root-groups=$grouproot"
99 [ -n "$users" ] &&     echo "users=$users"
100 [ -n "$usersroot" ] && echo "root-users=$usersroot"
101
102     echo "profile=$personality"
103
104     if [ "$THISARCH" = "$arch" ]; then
105         echo "aliases=$suite"
106     fi
107     case "$arch" in
108         armel|armhf|i386|powerpc|s390|sparc)
109             echo "personality=linux32"
110             ;;
111     esac
112     echo
113
114     case "$suite" in
115         sid)
116             genschrootconf "experimental" "$arch" "$target"
117             ;;
118         #experimental|jessie)
119         experimental)
120             :
121             ;;
122         *)
123             if [ -z "$extra" ] && [ -z "$ubuntu" ]; then
124                 genschrootconf "$suite" "$arch" "$target" "backports"
125             fi
126     esac
127 }
128
129 do_config() {
130     local tmpschrootconf=$(tempfile)
131     cleanup+=("rm -f $tmpschrootconf")
132     genschrootconf "$suite" "$arch" "$target" > "$tmpschrootconf"
133     if ! [ -e "$schrootconfig" ] || ! diff "$schrootconfig" "$tmpschrootconf" > /dev/null; then
134         mv "$tmpschrootconf" "$schrootconfig"
135         chmod 644 "$schrootconfig"
136     fi
137 }
138
139
140 set -e
141 set -u
142
143 arch="$THISARCH"
144 if [ -e /etc/schroot/dsa/default-mirror ]; then
145     mirror=$(cat /etc/schroot/dsa/default-mirror )
146 fi
147 mirror="${mirror:-http://cdn.debian.net/debian}"
148 configonly=""
149 force=""
150 basedir="/srv/chroot"
151 builddir=""
152 keyring=/usr/share/keyrings/debian-archive-keyring.gpg
153 personality="dsa"
154 sbuildnames=""
155 ubuntu=""
156 groupuser="Debian,guest"
157 grouproot=""
158 users=""
159 usersroot=""
160 bare=""
161 keep=""
162 suffix="dchroot"
163 declare -a cleanup
164 cleanup+=(":")
165 trap do_cleanup EXIT
166
167 while getopts "a:b:Bcd:Dfg:hKk:m:o:O:p:r:sS:u" OPTION
168 do
169     case $OPTION in
170         a)
171             arch="$OPTARG"
172             ;;
173         b)
174             basedir="$OPTARG"
175             ;;
176         B)
177             bare="1"
178             ;;
179         c)
180             configonly="1"
181             ;;
182         D)
183             sbuildnames="0"
184             bare="1"
185             groupuser=""
186             grouproot=""
187             users="buildd"
188             usersroot="buildd"
189             personality="buildd-dsa"
190             suffix="sbuild"
191             ;;
192         d)
193             builddir="$OPTARG"
194             ;;
195         f)
196             force="1"
197             ;;
198         g)
199             groupuser="$OPTARG"
200             ;;
201         h)
202             usage
203             exit 0
204             ;;
205         K)
206             keep="4"
207             ;;
208         k)
209             keyring="$OPTARG"
210             ;;
211         m)
212             mirror="$OPTARG"
213             ;;
214         o)
215             users="$OPTARG"
216             ;;
217         O)
218             usersroot="$OPTARG"
219             ;;
220         p)
221             personality="$OPTARG"
222             ;;
223         r)
224             grouproot="$OPTARG"
225             ;;
226         s)
227             sbuildnames="1"
228             ;;
229         S)
230             suffix="$OPTARG"
231             ;;
232         u)
233             ubuntu="1"
234             ;;
235         *)
236             usage >&2
237             exit 1
238             ;;
239     esac
240 done
241 shift $(($OPTIND - 1))
242
243 if [ "$#" != 1 ]; then
244     usage >&2
245     exit 1
246 fi
247 suite="$1"; shift
248 tuple="${suite}_${arch}"
249
250 builddir=${builddir:-$basedir}
251 [ -d "$basedir" ] || die "Error: $basedir does not exist (or is not a directory)."
252 [ -d "$builddir" ] || die "Error: $builddir does not exist (or is not a directory)."
253
254 target="$basedir/$tuple.tar.gz"
255 ! [ -e "$target" ] || [ -n "$force" ] || die "Error: $target already exists."
256
257 schrootconfig="/etc/schroot/chroot.d/${tuple}-$suffix"
258 ! [ -e "$schrootconfig" ] || [ -n "$force" ] || die "Error: $schrootconfig already exists."
259
260
261 #
262 # let's go
263 #
264
265 if [ -n "$configonly" ]; then
266     do_config
267     exit 0
268 fi
269
270 rootdir=$(mktemp -d "$builddir/create-$suite-XXXXXX")
271 cleanup+=("rm -r $rootdir")
272 cleanup+=("umount $rootdir/sys")
273 script=/usr/share/debootstrap/scripts/"$suite"
274 if ! [ -e "$script" ]; then
275     if [ -z "$ubuntu" ]; then
276         script=/usr/share/debootstrap/scripts/sid
277     else
278         script=/usr/share/debootstrap/scripts/gutsy
279     fi
280 fi
281
282 set -x
283 debootstrap \
284     --keyring "$keyring" \
285     --include="apt" \
286     --variant=buildd \
287     --arch="$arch" \
288     "$suite" "$rootdir" "$mirror" "$script"
289 echo "$tuple" > "$rootdir/etc/debian_chroot"
290 echo "force-unsafe-io" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-unsafe-io"
291 echo "force-confnew" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-confnew"
292
293 cleanup+=("umount $rootdir/dev")
294 case "$(uname -s)" in
295   Linux)
296     ;;
297   GNU/kFreeBSD)
298     mount -t devfs none "$rootdir/dev"
299     ;;
300   *)
301     echo >&2 "Warning: Unexpected uname -s output."
302     ;;
303 esac
304
305
306 chroot "$rootdir" apt-get update
307 chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends policyrcd-script-zg2
308 cat > "$rootdir/usr/local/sbin/policy-rc.d" << 'EOF'
309 #!/bin/sh
310
311 # policy-rc.d script for chroots.
312 # Copyright (c) 2007 Peter Palfrader <peter@palfrader.org>
313
314 while true; do
315     case "$1" in
316         -*)      shift ;;
317         makedev) exit 0;;
318         *)
319             echo "Not running services in chroot."
320             exit 101
321             ;;
322     esac
323 done
324 EOF
325 chmod +x "$rootdir/usr/local/sbin/policy-rc.d"
326 [ -z "$bare" ] && [ -z "$ubuntu" ] && chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends locales-all
327 chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends build-essential
328 [ -z "$bare" ] && chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends zsh less vim fakeroot devscripts gdb
329 rm -f "$rootdir/etc/apt/sources.list" "$rootdir/etc/apt/sources.list.d/*"
330 chroot "$rootdir" apt-get clean
331 umount "$rootdir/dev" 2>/dev/null || true
332 umount "$rootdir/sys" 2>/dev/null || true
333
334 tartmp=$(tempfile --directory "$basedir" --suffix=".tar.gz")
335 cleanup+=("rm -f $tartmp")
336 (
337   cd "$rootdir"
338   tar caf "$tartmp" .
339   if ! [ -z "$keep" ]; then
340     savelog -l -c 4 "$target"
341   fi
342   mv "$tartmp" "$target"
343 )
344
345 do_config