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