setup-dchroot: Request unmerged /usr
[mirror/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 genname() {
76     local suite="$1"; shift
77     local arch="$1"; shift
78
79     if [ -n "$sbuildnames" ]; then
80         local name="${suite}-${arch}-sbuild"
81     else
82         local name="${suite}_${arch}-dchroot"
83     fi
84
85     echo "$name"
86 }
87
88
89 genschrootconf() {
90     local suite="$1"; shift
91     local arch="$1"; shift
92     local target="$1"; shift
93     local extra="${1:-}"; shift || true
94     local aliases=""
95
96     if [ -n "$extra" ]; then
97         local suite="${suite}-${extra}"
98     fi
99
100     local name="$(genname "$suite" "$arch")"
101
102     local fullname="$name"
103     case "$arch" in
104         kfreebsd-*)
105             aliases="$name"
106             # FreeBSD only allows relatively short paths to mountpoints
107             # therefore saving a few charakters making the chroot dir fit
108             name=${name/kfreebsd-amd64/k-a}
109             name=${name/kfreebsd-i386/k-i}
110         ;;
111     esac
112
113 cat << EOF
114 [${name}]
115 description=[${fullname}] Debian $suite chroot for $arch
116 type=file
117 file=$target
118 EOF
119 [ -n "$groupuser" ] && echo "groups=$groupuser"
120 [ -n "$grouproot" ] && echo "root-groups=$grouproot"
121 [ -n "$users" ] &&     echo "users=$users"
122 [ -n "$usersroot" ] && echo "root-users=$usersroot"
123
124     echo "profile=$personality"
125
126     if [ "$THISARCH" = "$arch" ] && [ -z "$buildd" ]; then
127         if [ -z "$aliases" ] ; then
128             aliases="$suite"
129         else
130             aliases="$aliases,$suite"
131         fi
132     fi
133     if [ -n "$aliases" ] ; then
134         echo "aliases=$aliases"
135     fi
136     case "$arch" in
137         armel|armhf|i386|mips|mipsel|powerpc|s390|sparc)
138             echo "personality=linux32"
139             ;;
140     esac
141     echo
142
143     case "$suite" in
144         sid)
145             genschrootconf "experimental" "$arch" "$target"
146             ;;
147         experimental)
148             :
149             ;;
150         *)
151             if [ -z "$extra" ] && [ -z "$ubuntu" ]; then
152                 genschrootconf "$suite" "$arch" "$target" "backports"
153                 if [ -n "$buildd" ] ; then
154                     genschrootconf "$suite" "$arch" "$target" "backports-sloppy"
155                     genschrootconf "$suite" "$arch" "$target" "proposed-updates"
156                     genschrootconf "$suite" "$arch" "$target" "security"
157                     case "$arch" in
158                         kfreebsd-*)
159                             genschrootconf "$suite" "$arch" "$target" "kfreebsd"
160                             genschrootconf "$suite" "$arch" "$target" "kfreebsd-security"
161                             ;;
162                     esac
163                 fi
164             fi
165     esac
166     return 0
167 }
168
169 do_config() {
170     local tmpschrootconf=$(tempfile)
171     cleanup+=("rm -f $tmpschrootconf")
172     genschrootconf "$suite" "$arch" "$target" > "$tmpschrootconf"
173     if ! [ -e "$schrootconfig" ] || ! diff "$schrootconfig" "$tmpschrootconf" > /dev/null; then
174         mv "$tmpschrootconf" "$schrootconfig"
175         chmod 644 "$schrootconfig"
176     fi
177 }
178
179
180 set -e
181 set -u
182
183 arch="$THISARCH"
184 if [ -e /etc/schroot/dsa/default-mirror ]; then
185     mirror=$(cat /etc/schroot/dsa/default-mirror )
186 fi
187 mirror="${mirror:-http://ftp.debian.org/debian}"
188 configonly=""
189 force=""
190 basedir="/srv/chroot"
191 builddir=""
192 keyring=/usr/share/keyrings/debian-archive-keyring.gpg
193 personality="dsa"
194 sbuildnames=""
195 ubuntu=""
196 groupuser="Debian,guest,d-i"
197 grouproot=""
198 include="apt,fakeroot"
199 users=""
200 usersroot=""
201 bare=""
202 keep=""
203 suffix="dchroot"
204 declare -a cleanup
205 cleanup+=(":")
206 trap do_cleanup EXIT
207 buildd=""
208
209 while getopts "a:b:Bcd:Dfg:hI:Kk:m:o:O:p:r:sS:u" OPTION
210 do
211     case $OPTION in
212         a)
213             arch="$OPTARG"
214             ;;
215         b)
216             basedir="$OPTARG"
217             ;;
218         B)
219             bare="1"
220             ;;
221         c)
222             configonly="1"
223             ;;
224         D)
225             buildd="1"
226             sbuildnames="0"
227             bare="1"
228             groupuser=""
229             grouproot=""
230             users="buildd,buildd2"
231             usersroot="buildd,buildd2"
232             personality="buildd"
233             suffix="sbuild"
234             ;;
235         d)
236             builddir="$OPTARG"
237             ;;
238         f)
239             force="1"
240             ;;
241         g)
242             groupuser="$OPTARG"
243             ;;
244         h)
245             usage
246             exit 0
247             ;;
248         I)
249             include="$include,$OPTARG"
250             ;;
251         K)
252             keep="4"
253             ;;
254         k)
255             keyring="$OPTARG"
256             ;;
257         m)
258             mirror="$OPTARG"
259             ;;
260         o)
261             users="$OPTARG"
262             ;;
263         O)
264             usersroot="$OPTARG"
265             ;;
266         p)
267             personality="$OPTARG"
268             ;;
269         r)
270             grouproot="$OPTARG"
271             ;;
272         s)
273             sbuildnames="1"
274             ;;
275         S)
276             suffix="$OPTARG"
277             ;;
278         u)
279             ubuntu="1"
280             ;;
281         *)
282             usage >&2
283             exit 1
284             ;;
285     esac
286 done
287 shift $(($OPTIND - 1))
288
289 if [ "$#" != 1 ]; then
290     usage >&2
291     exit 1
292 fi
293 suite="$1"; shift
294 tuple="${suite}_${arch}"
295
296 if [ -z "${builddir:-}" ]; then
297   builddir=${builddir:-$basedir}
298   for u in schroot-unpack unpack; do
299     # if one of these directories exists and is on a different filesystem, prefer it.
300     candidate="$builddir/$u"
301     if [ -e "$candidate" ] &&
302        [ "$(stat -f --printf '%i\n' "$builddir" )" != "$(stat -f --printf '%i\n' "$candidate")" ]; then
303       builddir="$candidate"
304     fi
305   done
306 fi
307 [ -d "$basedir" ] || die "Error: $basedir does not exist (or is not a directory)."
308 [ -d "$builddir" ] || die "Error: $builddir does not exist (or is not a directory)."
309
310 target="$basedir/$tuple.tar.gz"
311 ! [ -e "$target" ] || [ -n "$force" ] || die "Error: $target already exists."
312
313 schrootconfig="/etc/schroot/chroot.d/${tuple}-$suffix"
314 ! [ -e "$schrootconfig" ] || [ -n "$force" ] || die "Error: $schrootconfig already exists."
315
316 suite_alias="$suite"
317 case "$arch" in
318     kfreebsd-*)
319         case "$suite" in
320             jessie)
321                 suite_alias="$suite-kfreebsd"
322                 ;;
323         esac
324         ;;
325 esac
326
327
328 #
329 # let's go
330 #
331
332 if [ -n "$configonly" ]; then
333     do_config
334     exit 0
335 fi
336
337 rootdir=$(mktemp -d "$builddir/create-$suite-XXXXXX")
338 cleanup+=("rm -r $rootdir")
339 cleanup+=("umount $rootdir/sys")
340 script=/usr/share/debootstrap/scripts/"$suite"
341 if ! [ -e "$script" ]; then
342     if [ -z "$ubuntu" ]; then
343         script=/usr/share/debootstrap/scripts/sid
344     else
345         script=/usr/share/debootstrap/scripts/gutsy
346     fi
347 fi
348
349 set -x
350 debootstrap \
351     --keyring "$keyring" \
352     --include="$include" \
353     --variant=buildd \
354     --arch="$arch" \
355     --no-merged-usr \
356     "$suite_alias" "$rootdir" "$mirror" "$script"
357 echo "$tuple" > "$rootdir/etc/debian_chroot"
358 echo "force-unsafe-io" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-unsafe-io"
359 echo "force-confnew" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-confnew"
360
361 cleanup+=("umount $rootdir/dev")
362 case "$(uname -s)" in
363   Linux)
364     ;;
365   GNU/kFreeBSD)
366     mount -t devfs none "$rootdir/dev"
367     ;;
368   *)
369     echo >&2 "Warning: Unexpected uname -s output."
370     ;;
371 esac
372
373
374 chroot "$rootdir" apt-get update
375 chroot "$rootdir" apt-get install -y --no-install-recommends policyrcd-script-zg2
376 cat > "$rootdir/usr/local/sbin/policy-rc.d" << 'EOF'
377 #!/bin/sh
378
379 # policy-rc.d script for chroots.
380 # Copyright (c) 2007 Peter Palfrader <peter@palfrader.org>
381
382 while true; do
383     case "$1" in
384         -*)      shift ;;
385         makedev) exit 0;;
386         *)
387             echo "Not running services in chroot."
388             exit 101
389             ;;
390     esac
391 done
392 EOF
393 chmod +x "$rootdir/usr/local/sbin/policy-rc.d"
394 [ -z "$bare" ] && [ -z "$ubuntu" ] && chroot "$rootdir" apt-get install -y --no-install-recommends locales-all
395 chroot "$rootdir" apt-get install -y --no-install-recommends build-essential
396 [ -z "$bare" ] && chroot "$rootdir" apt-get install -y --no-install-recommends zsh less vim fakeroot devscripts gdb
397 if [ -n "$buildd" ] ; then
398     case "$suite" in
399       wheezy|jessie|stretch)
400         chroot "$rootdir" apt-get install -y --no-install-recommends apt-transport-https ca-certificates
401         ;;
402       *)
403         chroot "$rootdir" apt-get install -y --no-install-recommends ca-certificates
404         ;;
405     esac
406 fi
407 if [ -n "$ubuntu" ]; then
408   echo "deb $mirror ${suite}-updates main" >> "$rootdir/etc/apt/sources.list"
409   echo "deb $mirror ${suite}-security main" >> "$rootdir/etc/apt/sources.list"
410   chroot "$rootdir" apt-get update
411   chroot "$rootdir" apt-get dist-upgrade -y
412 fi
413 rm -f "$rootdir/etc/apt/sources.list" "$rootdir/etc/apt/sources.list.d/*"
414 chroot "$rootdir" apt-get clean
415 umount "$rootdir/dev" 2>/dev/null || true
416 umount "$rootdir/sys" 2>/dev/null || true
417
418 tartmp=$(tempfile --directory "$basedir" --suffix=".tar.gz")
419 cleanup+=("rm -f $tartmp")
420 (
421   cd "$rootdir"
422   chmod 0755 "$rootdir"
423   tar caf "$tartmp" .
424   if ! [ -z "$keep" ]; then
425     savelog -l -c 2 "$target"
426   fi
427   mv "$tartmp" "$target"
428 )
429
430 do_config