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