summaryrefslogtreecommitdiffstats
path: root/bin/bfh-chroot
blob: a611cd612b0358d419b48723797cbf13a8d86184 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/sh

set -e

COMMAND="${1:-enter}"

if [ -n "${2}" ]
then
	DISTRIBUTION="$(echo ${2} | awk -F- '{ print $1 }')"
else
	DISTRIBUTION="${2:-sid}"
fi

if [ -n "${3}" ]
then
	ARCHITECTURE="${3:-$(dpkg --print-architecture)}"
else
	ARCHITECTURE="$(echo ${2} | awk -F- '{ print $2 }')"
	ARCHITECTURE="${ARCHITECTURE:-amd64}"
fi

case "${DISTRIBUTION}" in
	*)
		MIRROR="debian.ethz.ch"
		;;
esac

CHROOT="/srv/chroots/${DISTRIBUTION}-${ARCHITECTURE}"

Chroot ()
{
	if [ "${ARCHITECTURE}" = "$(dpkg --print-architecture)" ]
	then
		chroot "${CHROOT}" /usr/bin/env -i HOME="/root" PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" TERM="${TERM}" ${@}
	else
		linux32 chroot "${CHROOT}" /usr/bin/env -i HOME="/root" PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" TERM="${TERM}" ${@}
	fi
}

case "${COMMAND}" in
	create)
		if [ ! -d "${CHROOT}" ]
		then
			debootstrap --arch=${ARCHITECTURE} --include=progress-linux ${DISTRIBUTION} "${CHROOT}" http://${MIRROR}/debian
		else
			echo "${CHROOT}: file or directory already exists."
			exit 1
		fi

		# Configure apt

cat > "${CHROOT}/etc/apt/apt.conf.d/chroot.conf" << EOF
Acquire::PDiffs "false";
APT::Clean-Installed "true";
APT::Install-Recommends "false";
APT::Install-Suggests "false";
EOF

cat > "${CHROOT}/etc/apt/sources.list" << EOF
deb http://${MIRROR}/debian ${DISTRIBUTION} main contrib non-free
EOF

		case "${DISTRIBUTION}" in
			sid)

cat >> "${CHROOT}/etc/apt/sources.list" << EOF
deb http://${MIRROR}/debian experimental main contrib non-free
EOF

				;;

			buster)

cat > "${CHROOT}/etc/apt/sources.list.d/progress-linux.sources" << EOF
Types: deb
URIs: https://cdn.deb.progress-linux.org/packages
Suites: engywuck-backports
Components: main contrib non-free restricted
PDiffs: no
Signed-By: /usr/share/progress-linux/pgp-keys/apt.progress-linux.org.gpg
EOF

				;;
		esac

		# Configure bash

cat > "${CHROOT}/etc/profile.d/chroot.sh" << EOF
#!/bin/sh

alias ls="ls --color=auto"

export DEBEMAIL="maintainers@lists.progress-linux.org"
export DEBFULLNAME="Progress Linux Maintainers"

export LANG="en_US.UTF-8"
export LANGUAGE="en_US:en"
export LC_ALL="C.UTF-8"
EOF

		# Configure dpkg
		if [ -e "${CHROOT}/etc/dpkg" ]
		then

cat > "${CHROOT}/etc/dpkg/dpkg.cfg.d/chroot" << EOF
force-unsafe-io
EOF

		fi

		if [ -e "${CHROOT}/sbin/start-stop-daemon" ]
		then
			mv "${CHROOT}/sbin/start-stop-daemon" "${CHROOT}/sbin/start-stop-daemon.orig"
		fi

cat > "${CHROOT}/sbin/start-stop-daemon" << EOF
#!/bin/sh

exit 0
EOF

		chmod 0755 "${CHROOT}/sbin/start-stop-daemon"

		# Configure sysv-rc
		if [ -e "${CHROOT}/usr/sbin/policy-rc.d" ]
		then
			mv "${CHROOT}/usr/sbin/policy-rc.d" "${CHROOT}/usr/sbin/policy-rc.d.orig"
		fi

cat > "${CHROOT}/usr/sbin/policy-rc.d" << EOF
#!/bin/sh

echo "All runlevel operations denied by policy" >&2

exit 101
EOF

		chmod 0755 "${CHROOT}/usr/sbin/policy-rc.d"

		# Configure debian
		echo "${DISTRIBUTION}_${ARCHITECTURE}" > "${CHROOT}/etc/debian_chroot"

		Chroot apt update
		Chroot apt upgrade --yes
		Chroot apt dist-upgrade --yes

		Chroot apt install --yes \
			build-essential debhelper fakeroot

		Chroot apt install --yes \
			gnupg sudo wget

		Chroot apt install --yes \
			git rsync ca-certificates wget

		Chroot apt clean

		echo "Root password"

		Chroot "passwd"
		Chroot "adduser --home /build --ingroup sudo build"

		echo ". /etc/profile" >> "${CHROOT}/build/.bashrc"

		case $(cat /etc/hostname) in
			all*)
				BASH_HISTORY="indep"
				;;

			source*)
				BASH_HISTORY="source"
				;;

			*)
				BASH_HISTORY="arch"
				;;
		esac

		case "${BASH_HISTORY}" in
			arch)

cat > "${CHROOT}/build/.bash_history" << EOF
cd */ && sudo apt-get build-dep -y . && dpkg-buildpackage -B && cd .. && rm -rf */
sudo apt-get install -y -t engywuck-backports debhelper
clear && bbuild.sh -b binary-arch -d buster-backports -p 
rsync -aPv --progress --stats * apt.bfh.science::upload && rm -rf *
EOF

				;;

			indep)

cat > "${CHROOT}/build/.bash_history" << EOF
cd */ && sudo apt-get build-dep -y . && dpkg-buildpackage -A && cd .. && rm -rf */
sudo apt-get install -y -t engywuck-backports debhelper
clear && bbuild.sh -b binary-indep -d buster-backports -p 
rsync -aPv --progress --stats * apt.bfh.science::upload && rm -rf *
EOF

				;;

			source)

cat > "${CHROOT}/build/.bash_history" << EOF
cd */ && sudo apt-get build-dep -y . && dpkg-buildpackage -A && cd .. && rm -rf */
sudo apt-get install -y -t engywuck-backports debhelper
clear && bbuild.sh -b source -d buster-backports -p 
rsync -aPv --progress --stats * apt.bfh.science::upload && rm -rf *
EOF

				;;
		esac

		chown 1000:1000 "${CHROOT}/build/.bash_history"

		echo "build ALL=NOPASSWD: ALL" >> "${CHROOT}/etc/sudoers"
		;;

	chroot)
		Chroot "su - build"
		;;

	update)
		Chroot chmod 777 /dev/ptmx
		Chroot apt-get --force-yes --yes update
		Chroot apt-get --force-yes --yes upgrade
		Chroot apt-get --force-yes --yes dist-upgrade
		Chroot apt-get --force-yes --yes clean
		;;

	save)
		killall -9 syslogd > /dev/null 2>&1 || true
		killall -9 klogd > /dev/null 2>&1 || true

		for FILESYSTEM in /sys /proc /lib/init/rw /dev/shm /dev/pts /dev
		do
			umount -f "${CHROOT}/${FILESYSTEM}" > /dev/null 2>&1 || true
		done

		rm -f "${CHROOT}/root/.bash_history"
		rm -f "${CHROOT}/root/.viminfo"
		rm -f "${CHROOT}/build/.viminfo"
		rm -rf "${CHROOT}/var/tmp/vi.recover"
		rm -f "${CHROOT}/etc/machine-id"

		case $(cat /etc/hostname) in
			all*)
				BASH_HISTORY="indep"
				;;

			source*)
				BASH_HISTORY="source"
				;;

			*)
				BASH_HISTORY="arch"
				;;
		esac

		case "${BASH_HISTORY}" in
			arch)

cat > "${CHROOT}/build/.bash_history" << EOF
cd */ && sudo apt-get build-dep -y . && dpkg-buildpackage -B && cd .. && rm -rf */
sudo apt-get install -y -t engywuck-backports debhelper
clear && bbuild.sh -b binary-arch -d buster-backports -p 
rsync -aPv --progress --stats * apt.bfh.science::upload && rm -rf *
EOF

				;;

			indep)

cat > "${CHROOT}/build/.bash_history" << EOF
cd */ && sudo apt-get build-dep -y . && dpkg-buildpackage -A && cd .. && rm -rf */
sudo apt-get install -y -t engywuck-backports debhelper
clear && bbuild.sh -b binary-indep -d buster-backports -p 
rsync -aPv --progress --stats * apt.bfh.science::upload && rm -rf *
EOF

				;;

			source)

cat > "${CHROOT}/build/.bash_history" << EOF
cd */ && sudo apt-get build-dep -y . && dpkg-buildpackage -A && cd .. && rm -rf */
sudo apt-get install -y -t engywuck-backports debhelper
clear && bbuild.sh -b source -d buster-backports -p 
rsync -aPv --progress --stats * apt.bfh.science::upload && rm -rf *
EOF
				;;
		esac

		rsync -av --delete "${CHROOT}"/ "${CHROOT}".orig

		mount proc-$(basename ${CHROOT}) "${CHROOT}/proc" -t proc
		mount devpts-$(basename ${CHROOT}) "${CHROOT}/dev/pts" -t devpts
		mount sysfs-$(basename ${CHROOT}) "${CHROOT}/sys" -t sysfs
		mount /dev/shm -o bind "${CHROOT}/dev/shm"
		;;

	restore)
		killall -9 syslogd > /dev/null 2>&1 || true
		killall -9 klogd > /dev/null 2>&1 || true

		for FILESYSTEM in /sys /proc /lib/init/rw /dev/shm /dev/pts /dev
		do
			umount -f "${CHROOT}/${FILESYSTEM}" > /dev/null 2>&1 || true
		done

		rsync -av --delete "${CHROOT}".orig/ "${CHROOT}"

		mount proc-$(basename ${CHROOT}) "${CHROOT}/proc" -t proc
		mount devpts-$(basename ${CHROOT}) "${CHROOT}/dev/pts" -t devpts
		mount sysfs-$(basename ${CHROOT}) "${CHROOT}/sys" -t sysfs
		mount /dev/shm -o bind "${CHROOT}/dev/shm"
		;;

	reset)
		"${0}" restore ${DISTRIBUTION} ${ARCHITECTURE}
		"${0}" update ${DISTRIBUTION} ${ARCHITECTURE}
		"${0}" save ${DISTRIBUTION} ${ARCHITECTURE}
		;;

	*)
		echo "Wrong option."
		exit 1
esac