]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/porterbox/files/setup-dchroot
Support install less crap in chroots, 2
[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     if dpkg --compare-versions "$(lsb_release --release --short)" '<' 7; then
98         echo "script-config=$personality/config"
99     else
100         echo "profile=$personality"
101     fi
102
103     if [ "$THISARCH" = "$arch" ]; then
104         echo "aliases=$suite"
105     fi
106     case "$arch" in
107         armel|armhf|i386|powerpc|s390|sparc)
108             echo "personality=linux32"
109             ;;
110     esac
111     echo
112
113     case "$suite" in
114         sid)
115             genschrootconf "experimental" "$arch" "$target"
116             ;;
117         experimental|jessie)
118             :
119             ;;
120         *)
121             if [ -z "$extra" ] && [ -z "$ubuntu" ]; then
122                 genschrootconf "$suite" "$arch" "$target" "backports"
123             fi
124     esac
125 }
126
127 do_config() {
128     local tmpschrootconf=$(tempfile)
129     cleanup+=("rm -f $tmpschrootconf")
130     genschrootconf "$suite" "$arch" "$target" > "$tmpschrootconf"
131     if ! [ -e "$schrootconfig" ] || ! diff "$schrootconfig" "$tmpschrootconf" > /dev/null; then
132         mv "$tmpschrootconf" "$schrootconfig"
133         chmod 644 "$schrootconfig"
134     fi
135 }
136
137
138 set -e
139 set -u
140
141 arch="$THISARCH"
142 if [ -e /etc/schroot/dsa/default-mirror ]; then
143     mirror=$(cat /etc/schroot/dsa/default-mirror )
144 fi
145 mirror="${mirror:-http://cdn.debian.net/debian}"
146 configonly=""
147 force=""
148 basedir="/srv/chroot"
149 builddir=""
150 keyring=/usr/share/keyrings/debian-archive-keyring.gpg
151 personality="dsa"
152 sbuildnames=""
153 ubuntu=""
154 groupuser="Debian,guest"
155 grouproot=""
156 bare=""
157 declare -a cleanup
158 trap do_cleanup EXIT
159
160 while getopts "a:b:Bcd:fg:hk:m:p:r:su" OPTION
161 do
162     case $OPTION in
163         a)
164             arch="$OPTARG"
165             ;;
166         b)
167             basedir="$OPTARG"
168             ;;
169         B)
170             bare="1"
171             ;;
172         c)
173             configonly="1"
174             ;;
175         d)
176             builddir="$OPTARG"
177             ;;
178         f)
179             force="1"
180             ;;
181         g)
182             groupuser="$OPTARG"
183             ;;
184         h)
185             usage
186             exit 0
187             ;;
188         k)
189             keyring="$OPTARG"
190             ;;
191         m)
192             mirror="$OPTARG"
193             ;;
194         p)
195             personality="$OPTARG"
196             ;;
197         r)
198             grouproot="$OPTARG"
199             ;;
200         s)
201             sbuildnames="1"
202             ;;
203         u)
204             ubuntu="1"
205             ;;
206         *)
207             usage >&2
208             exit 1
209             ;;
210     esac
211 done
212 shift $(($OPTIND - 1))
213
214 if [ "$#" != 1 ]; then
215     usage >&2
216     exit 1
217 fi
218 suite="$1"; shift
219 tuple="${suite}_${arch}"
220
221 builddir=${builddir:-$basedir}
222 [ -d "$basedir" ] || die "Error: $basedir does not exist (or is not a directory)."
223 [ -d "$builddir" ] || die "Error: $builddir does not exist (or is not a directory)."
224
225 target="$basedir/$tuple.tar.gz"
226 ! [ -e "$target" ] || [ -n "$force" ] || die "Error: $target already exists."
227
228 schrootconfig="/etc/schroot/chroot.d/${tuple}-dchroot"
229 ! [ -e "$schrootconfig" ] || [ -n "$force" ] || die "Error: $schrootconfig already exists."
230
231
232 #
233 # let's go
234 #
235
236 if [ -n "$configonly" ]; then
237     do_config
238     exit 0
239 fi
240
241 rootdir=$(mktemp -d "$builddir/create-$suite-XXXXXX")
242 cleanup+=("rm -r $rootdir")
243 cleanup+=("umount $rootdir/sys")
244 script=/usr/share/debootstrap/scripts/"$suite"
245 if ! [ -e "$script" ]; then
246     if [ -z "$ubuntu" ]; then
247         script=/usr/share/debootstrap/scripts/sid
248     else
249         script=/usr/share/debootstrap/scripts/gutsy
250     fi
251 fi
252
253 set -x
254 debootstrap \
255     --keyring "$keyring" \
256     --include="apt" \
257     --variant=buildd \
258     --arch="$arch" \
259     "$suite" "$rootdir" "$mirror" "$script"
260 echo "$tuple" > "$rootdir/etc/debian_chroot"
261 echo "force-unsafe-io" > "$rootdir/etc/dpkg/dpkg.cfg.d/force-unsafe-io"
262
263 chroot "$rootdir" apt-get update
264 chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends policyrcd-script-zg2
265 cat > "$rootdir/usr/local/sbin/policy-rc.d" << 'EOF'
266 #!/bin/sh
267
268 # policy-rc.d script for chroots.
269 # Copyright (c) 2007 Peter Palfrader <peter@palfrader.org>
270
271 while true; do
272     case "$1" in
273         -*)      shift ;;
274         makedev) exit 0;;
275         *)
276             echo "Not running services in chroot."
277             exit 101
278             ;;
279     esac
280 done
281 EOF
282 chmod +x "$rootdir/usr/local/sbin/policy-rc.d"
283 [ -z "$ubuntu" ] && chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends locales-all
284 chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends build-essential
285 [ -z "$bare" ] && chroot "$rootdir" apt-get install -y --force-yes --no-install-recommends zsh less vim fakeroot devscripts gdb
286 rm -f "$rootdir/etc/apt/sources.list" "$rootdir/etc/apt/sources.list.d/*"
287 chroot "$rootdir" apt-get clean
288 umount "$rootdir/sys" || true
289
290 tartmp=$(tempfile --directory "$basedir" --suffix=".tar.gz")
291 cleanup+=("rm -f $tartmp")
292 (cd "$rootdir" && tar caf "$tartmp" . && mv "$tartmp" "$target")
293
294 do_config