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