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