]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/porterbox/files/setup-dchroot
Allow supplying a different schroot personality
[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 build the chroot/tarball [$basedir]
42     -c         write config only
43     -f         overwrite config and target tarball
44     -k KEYRING use an alternate keyring [$keyring]
45     -p PERS    use a different sbuild personality [$personality]
46     -s         use sbuild compatible naming scheme
47     -h         this help
48 EOF
49 }
50
51 die() {
52     echo >&2 "$*"
53     exit 1
54 }
55
56 do_cleanup() {
57     local cnt
58     cnt=$((${#cleanup[*]}-1))
59     for i in $(seq ${cnt} -1 0); do
60         ${cleanup[$i]} || true
61     done
62 }
63
64 genschrootconf() {
65     local suite="$1"; shift
66     local arch="$1"; shift
67     local target="$1"; shift
68     local extra="${1:-}"; shift || true
69
70     if [ -n "$extra" ]; then
71         local suite="${suite}-${extra}"
72     fi
73
74     if [ -n "$sbuildnames" ]; then
75         local name="${suite}-${arch}-sbuild"
76     else
77         local name="${suite}_${arch}-dchroot"
78     fi
79
80
81 cat << EOF
82 [${name}]
83 description=[${name}] Debian $suite chroot for $arch
84 type=file
85 file=$target
86 groups=Debian,guest
87 root-groups=adm
88 source-groups=adm
89 source-root-groups=adm
90 EOF
91
92     if dpkg --compare-versions "$(lsb_release --release --short)" '<' 7; then
93         echo "script-config=$personality/config"
94     else
95         echo "profile=$personality"
96     fi
97
98     if [ "$THISARCH" = "$arch" ]; then
99         echo "aliases=$suite"
100     fi
101     case "$arch" in
102         armel|armhf|i386|powerpc|s390|sparc)
103             echo "personality=linux32"
104             ;;
105     esac
106     echo
107
108     case "$suite" in
109         sid)
110             genschrootconf "experimental" "$arch" "$target"
111             ;;
112         experimental|jessie)
113             :
114             ;;
115         *)
116             if [ -z "$extra" ]; then
117                 genschrootconf "$suite" "$arch" "$target" "backports"
118             fi
119     esac
120 }
121
122
123 set -e
124 set -u
125
126 arch="$THISARCH"
127 if [ -e /etc/schroot/dsa/default-mirror ]; then
128     mirror=$(cat /etc/schroot/dsa/default-mirror )
129 fi
130 mirror="${mirror:-http://cdn.debian.net/debian}"
131 configonly=""
132 force=""
133 basedir="/srv/chroot"
134 keyring=/usr/share/keyrings/debian-archive-keyring.gpg
135 personality="dsa"
136 sbuildnames=""
137 declare -a cleanup
138 trap do_cleanup EXIT
139
140 while getopts "a:b:cfhk:m:p:s" OPTION
141 do
142     case $OPTION in
143         a)
144             arch="$OPTARG"
145             ;;
146         b)
147             basedir="$OPTARG"
148             ;;
149         c)
150             configonly="1"
151             ;;
152         f)
153             force="1"
154             ;;
155         h)
156             help
157             exit 0
158             ;;
159         k)
160             keyring="$OPTARG"
161             ;;
162         m)
163             mirror="$OPTARG"
164             ;;
165         p)
166             personality="$OPTARG"
167             ;;
168         s)
169             sbuildnames="1"
170             ;;
171         *)
172             usage >&2
173             exit 1
174             ;;
175     esac
176 done
177 shift $(($OPTIND - 1))
178
179 if [ "$#" != 1 ]; then
180     usage >&2
181     exit 1
182 fi
183 suite="$1"; shift
184 tuple="${suite}_${arch}"
185
186 [ -d "$basedir" ] || die "Error: $basedir does not exist (or is not a directory)."
187
188 target="$basedir/$tuple.tar.gz"
189 ! [ -e "$target" ] || [ -n "$force" ] || die "Error: $target already exists."
190
191 schrootconfig="/etc/schroot/chroot.d/${tuple}-dchroot"
192 ! [ -e "$schrootconfig" ] || [ -n "$force" ] || die "Error: $schrootconfig already exists."
193
194
195 #
196 # let's go
197 #
198 genschrootconf "$suite" "$arch" "$target" | tee "$schrootconfig"
199
200 if [ -n "$configonly" ]; then exit 0; fi
201
202 rootdir=$(mktemp -d "$basedir/create-$suite-XXXXXX")
203 cleanup+=("rm -r $rootdir")
204 cleanup+=("umount $rootdir/sys")
205
206 set -x
207 debootstrap \
208     --keyring "$keyring" \
209     --include="apt" \
210     --variant=buildd \
211     --arch="$arch" \
212     "$suite" "$rootdir" "$mirror"
213 echo "$tuple" > $rootdir/etc/debian_chroot
214
215 chroot "$rootdir" apt-get update
216 chroot "$rootdir" apt-get install -y --no-install-recommends policyrcd-script-zg2
217 cat > "$rootdir/usr/local/sbin/policy-rc.d" << 'EOF'
218 #!/bin/sh
219
220 # policy-rc.d script for chroots.
221 # Copyright (c) 2007 Peter Palfrader <peter@palfrader.org>
222
223 while true; do
224     case "$1" in
225         -*)      shift ;;
226         makedev) exit 0;;
227         *)
228             echo "Not running services in chroot."
229             exit 101
230             ;;
231     esac
232 done
233 EOF
234 chmod +x "$rootdir/usr/local/sbin/policy-rc.d"
235 chroot "$rootdir" apt-get install -y --no-install-recommends zsh locales-all build-essential less vim fakeroot devscripts gdb
236 rm -f "$rootdir/etc/apt/sources.list" "$rootdir/etc/apt/sources.list.d/*"
237 umount "$rootdir/sys" || true
238
239 tartmp=$(tempfile --directory "$basedir" --suffix=".tar.gz")
240 cleanup+=("rm -f $tartmp")
241 (cd "$rootdir" && tar caf "$tartmp" . && mv "$tartmp" "$target")