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