-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate-chroot
More file actions
executable file
·524 lines (443 loc) · 10.1 KB
/
create-chroot
File metadata and controls
executable file
·524 lines (443 loc) · 10.1 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/bin/bash -eu
#
# Create a (kernel build) chroot
#
# -----------------------------------------------------------------------------
# Include the following packages
# The base DEB packages
DEB_INCLUDE=(
fakeroot
less
perl-openssl-defaults # [arm64] required due to dependency problems
sudo
tzdata
)
# Additional DEB packages (primarily for kernel builds)
DEB_PACKAGES=(
asciidoc
autoconf
automake
bc
binutils-dev
bison
build-essential
debhelper
devscripts
dh-systemd
docbook-utils
dpkg-dev
flex
gawk
gcc-aarch64-linux-gnu
gcc-arm-linux-gnueabihf
gcc-i686-linux-gnu
gcc-powerpc64le-linux-gnu
gcc-s390x-linux-gnu gcc
ghostscript
git
kernel-wedge
kmod
kpatch-build
libaudit-dev
libbabeltrace-devel
libdistro-info-perl
libdw-dev
libelf-dev
libiberty-dev
liblzma-dev
libmnl-devel
libnewt-dev
libnuma-dev
libpci-dev
libpng12-dev
libssl-dev
libtool
libudev-dev
libunwind8-dev
liburi-perl
makedumpfile
openssl
pkg-config
python-dev
python3-apt
python3-launchpadlib
python3-requests
rsync
sbsigntool
sharutils
transfig
uuid-dev
wget
xmlto
)
# The base RPM packages
RPM_INCLUDE=(
dnf-plugins-core
fakeroot
less
sudo
tzdata
)
# Additional RPM packages
RPM_PACKAGES=(
fedora-packager
rpmdevtools
wget
# Packages for kernel builds
asciidoc
audit-libs-devel
bc
binutils-devel
bison
bpftool
dwarves
elfutils-devel
flex
gcc
git
hostname
java-devel
kabi-dw
libbpf-devel
libcap-devel
libcap-ng-devel
llvm-toolset
m4
make
ncurses-devel
net-tools
newt-devel
nss-tools
numactl-devel
openssl-devel
pciutils-devel
perl-ExtUtils-Embed
perl-devel
perl-generators
pesign
python3-devel
python3-docutils
rsync
xmlto
xz-devel
zlib-devel
)
# -----------------------------------------------------------------------------
# Exit handler
function out()
{
local rc=${?}
trap - EXIT INT HUP TERM
# Unmount bound directories in case debootstrap failed
if mount | grep -q " on ${BASEDIR}/" ; then
mount | grep " on ${BASEDIR}/" | awk '{ print $3 }' | xargs umount
fi
# Emit a status message
if [ "${rc}" -eq 0 ] ; then
echo -e "\e[92mChroot created successfully\e[0m"
else
# Purge the chroot and config file
if [ "${KEEP}" -eq 0 ] ; then
rm -rf "${BASEDIR}" "/etc/schroot/chroot.d/${NAME}"
fi
echo -e "\e[91mError: Failed to create chroot!\e[0m" >&2
fi
exit "${rc}"
}
# -----------------------------------------------------------------------------
# Print variables
function print_vars()
{
local var
for var in "${@}" ; do
printf "%-8s: %s\n" "${var}" "${!var}"
done
}
# -----------------------------------------------------------------------------
# Create a Debian chroot
function debian_create_chroot()
{
local components opts tz package
case "${VENDOR}" in
ubuntu)
components="main,universe"
;;
*)
components="main"
;;
esac
if [ "${NO_CHECK_GPG}" -eq 1 ] ; then
opts=("--no-check-gpg")
fi
if [ "${RELEASE}" = "precise" ] ; then
# Debootstrap fails for Precise. Don't bail out in this case so we can
# fix up the chroot later.
set +e
fi
# Create the base chroot
debootstrap \
"${opts[@]}" \
--include="$(IFS=, ; echo "${DEB_INCLUDE[*]}")" \
--components="${components}" \
--arch="${ARCH}" \
"${RELEASE}" \
"${BASEDIR}"
if [ "${RELEASE}" = "precise" ] ; then
# Fix the Precise chroot
${SCHROOT} dpkg-divert --local --rename --add /sbin/initctl
${SCHROOT} ln -s /bin/true /sbin/initctl
set -e
fi
# Refresh the chroot
${SCHROOT} apt-get -y update
${SCHROOT} apt-get -y upgrade
${SCHROOT} apt-get -y dist-upgrade
# Set the chroot timezone to match the host
tz=$(cat /etc/timezone)
ln -fs "/usr/share/zoneinfo/${tz}" "${BASEDIR}/etc/localtime"
${SCHROOT} dpkg-reconfigure -f noninteractive tzdata
# Preseed kexec-tools
${SCHROOT} bash -c "echo kexec-tools kexec-tools/load_kexec boolean false | debconf-set-selections"
# Install the additional packages
if [ "${BASE}" -eq 0 ] ; then
for package in "${DEB_PACKAGES[@]}" ; do
${SCHROOT} apt-get -y install "${package}" 2>/dev/null || true
done
fi
# Clean the cache
${SCHROOT} apt-get clean
}
# -----------------------------------------------------------------------------
# Create a Fedora chroot
function fedora_create_chroot()
{
local pkg_manager powertools
cat <<EOF >/tmp/repo.config
[centos-7]
name=centos-7
baseurl=http://mirror.centos.org/centos/7/os/${ARCH}
gpgcheck=0
[centos-8]
name=centos-8
baseurl=http://mirror.centos.org/centos/8-stream/BaseOS/${ARCH}/os
gpgcheck=0
[centos-9]
name=centos-9
baseurl=http://mirror.stream.centos.org/9-stream/BaseOS/${ARCH}/os
gpgcheck=0
[fedora-${RELEASE}]
name=fedora-${RELEASE}
baseurl=https://dl.fedoraproject.org/pub/archive/fedora/linux/releases/${RELEASE}/Everything/${ARCH}/os
gpgcheck=0
[opensuse-${RELEASE}]
name=opensuse-${RELEASE}
baseurl=http://download.opensuse.org/distribution/leap/${RELEASE}/repo/oss
gpgcheck=0
EOF
pkg_manager=dnf
powertools=0
case "${DIST}" in
centos-7)
pkg_manager=yum
;;
centos-8)
powertools=1
;;
esac
# Install the base system
mkdir -p "${BASEDIR}"
dnf --config=/tmp/repo.config --assumeyes --nogpgcheck \
--releasever="${RELEASE}" \
--disablerepo=* --enablerepo="${DIST}" \
--installroot="${BASEDIR}" install "${pkg_manager}"
# We need to reinstall the base system because the original installation
# put the RPM database in ~/.rpmdb
${SCHROOT} "${pkg_manager}" --releasever="${RELEASE}" --assumeyes \
--nogpgcheck install "${pkg_manager}"
# Install the base packages
${SCHROOT} "${pkg_manager}" --assumeyes install epel-release || true
for package in "${RPM_INCLUDE[@]}" ; do
${SCHROOT} "${pkg_manager}" --assumeyes install "${package}" || true
done
# Install the additional packages
if [ "${BASE}" -eq 0 ] ; then
if [ ${powertools} -eq 1 ] ; then
${SCHROOT} dnf config-manager --set-enabled powertools
fi
for package in "${RPM_PACKAGES[@]}" ; do
${SCHROOT} "${pkg_manager}" --assumeyes install "${package}" || true
done
fi
# Clean the cache
${SCHROOT} "${pkg_manager}" clean all
# TODO: Enable the debuginfo repos
}
# -----------------------------------------------------------------------------
# Usage
function usage()
{
cat <<EOF
Usage: create-chroot [-a ARCH] [-b] [-d] [-f] [-h] [-k] [-n] DIST [NAME]
Create a chroot for kernel package builds.
Positional arguments:
DIST A recognized distribution, like bionic, stretch, fedora-26, ...
NAME The name of the chroot, defaults to <DIST>-<ARCH> if not specified.
Optional arguments:
-a, --arch ARCH Architecture of the chroot (defaults to the host
architecture).
-b, --base Create a base chroot without additional packages.
-d, --debug Enable debugging (set -x).
-f, --force Force the recreation of an existing chroot.
-h, --help Show this help text.
-k, --keep Keep the chroot and config file after a build failure.
-n, --no-check-gpg Don't check GPG signatures of retrieved Release files.
EOF
}
# -----------------------------------------------------------------------------
# Main entry point
ARCH=
BASE=0
FORCE=0
DIST=
KEEP=0
NAME=
NO_CHECK_GPG=0
while [ ${#} -gt 0 ] ; do
case ${1} in
-a|--arch)
ARCH="${2}"
shift
;;
-b|--base)
BASE=1
;;
-d|--debug)
set -x
;;
-f|--force)
FORCE=1
;;
-h|--help)
usage
exit
;;
-k|--keep)
KEEP=1
;;
-n|--no-check-gpg)
NO_CHECK_GPG=1
;;
-*)
echo "Error: Unexpected option: ${1}" >&2
exit 2
;;
*)
if [ -z "${DIST}" ] ; then
DIST="${1}"
elif [ -z "${NAME}" ] ; then
NAME="${1}"
else
echo "Error: Unexpected argument: ${1}" >&2
exit 2
fi
;;
esac
shift
done
if [ -z "${DIST}" ] ; then
usage
exit 2
fi
case "${DIST}" in
fedora-*|centos-*|opensuse-*)
BASE_NAME=${DIST/-/}
BASE_DIST=fedora
VENDOR=${DIST%%-*}
if [ -z "${ARCH}" ] ; then
ARCH=x86_64
fi
;;
debian-*|ubuntu-*)
BASE_NAME=${DIST#*-}
BASE_DIST=debian
VENDOR=${DIST%%-*}
;;
stretch|buster|bullseye|bookworm|sid)
BASE_NAME=${DIST}
BASE_DIST=debian
VENDOR=debian
;;
*)
BASE_NAME=${DIST}
BASE_DIST=debian
VENDOR=ubuntu
;;
esac
if [ "${BASE_DIST}" = "debian" ] ; then
echo "Deprecated for Debian/Ubuntu! Use 'run-chroot'."
exit 1
fi
RELEASE=${DIST#*-}
if [ -z "${ARCH}" ] ; then
ARCH=amd64
fi
if [ -z "${NAME}" ] ; then
NAME="${BASE_NAME}-${ARCH}"
fi
SCHROOT="schroot -c ${NAME} --"
print_vars DIST VENDOR RELEASE ARCH NAME
# -----------------------------------------------------------------------------
# Verify that we're root
if [ "$(id -u)" -ne 0 ] ; then
echo "Error: This script needs to be run as root" >&2
exit 1
fi
# -----------------------------------------------------------------------------
# Sanity checks
# Check if the chroot is currently in use
if mount | grep -E -q "/run/schroot/mount/${NAME}-[0-9a-f]{8}-" ; then
echo "Error: The chroot is currently mounted and in use" >&2
exit 1
fi
BASEDIR=/var/lib/schroot/chroots/${NAME}
# Verify the chroot directory
if ! [ -d "${BASEDIR%/*}" ] ; then
echo "Error: No such directory: ${BASEDIR%/*}"
exit 1
fi
# Check if the chroot directory exists already
if [ -d "${BASEDIR}" ] ; then
if [ "${FORCE}" -eq 0 ] ; then
echo "Error: Chroot directory exists already" >&2
echo " Use -f, --force to recreate it" >&2
exit 1
else
rm -rf "${BASEDIR}"
fi
fi
trap out EXIT INT HUP TERM
# -----------------------------------------------------------------------------
# Add a config file to /etc/schroot/chroot.d/
cat <<EOF >"/etc/schroot/chroot.d/${NAME}"
[${NAME}]
type=directory
directory=${BASEDIR}
root-users=${SUDO_USER}
preserve-environment=true
EOF
# -----------------------------------------------------------------------------
# Create the chroot
"${BASE_DIST}_create_chroot"
# -----------------------------------------------------------------------------
# Add a sudo user to the chroot
sudoer="${BASEDIR}/etc/sudoers.d/${SUDO_USER}"
cat <<EOF >"${sudoer}"
${SUDO_USER} ALL = NOPASSWD: ALL
EOF
chmod 440 "${sudoer}"
# -----------------------------------------------------------------------------
# Create /etc/hosts
cat <<EOF >"${BASEDIR}"/etc/hosts
127.0.0.1 localhost
127.0.1.1 $(hostname)
EOF