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