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