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