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