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