Skip to content

Commit aac68fb

Browse files
committed
refactor: central chroot + qemu prep
Gets rid of a lot of repeated code
1 parent a345be3 commit aac68fb

File tree

3 files changed

+41
-76
lines changed

3 files changed

+41
-76
lines changed

src/chroot.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
# based on CustomPiOS by Guy Sheffer <guy at gmail dot com>
3+
4+
## chroot helpers
5+
6+
function prepare_chroot_environment() {
7+
# figure out which qemu to use - if any - and ensure its availability & functionality inside the chroot
8+
if [ "$(arch)" != "armv7l" ] && [ "$(arch)" != "aarch64" ] && [ "$(arch)" != "arm64" ] ; then
9+
if [ "$EDITBASE_ARCH" == "armv7l" ]; then
10+
# cross-compile for arm 32bit
11+
update-binfmts --enable qemu-arm
12+
cp `which qemu-arm-static` usr/bin/qemu-arm-static
13+
export QEMU=usr/bin/qemu-arm-static
14+
15+
elif [ "$EDITBASE_ARCH" == "aarch64" ] || [ "$EDITBASE_ARCH" == "arm64" ]; then
16+
# cross-compile for arm 64bit
17+
update-binfmts --enable qemu-aarch64
18+
cp `which qemu-aarch64-static` usr/bin/qemu-aarch64-static
19+
export QEMU=usr/bin/qemu-aarch64-static
20+
fi
21+
22+
(mount | grep -q -v "type binfmt_misc") || mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
23+
24+
else
25+
# arch native
26+
export QEMU=
27+
fi
28+
29+
# mount /proc if configured to do so
30+
if [ "$EDITBASE_MOUNT_PROC" == "1" ]; then
31+
echo "Mounting /proc of host..."
32+
mount -t proc /proc proc/
33+
fi
34+
}

src/customize

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ echo "DIST_PATH=$DIST_PATH"
2020

2121
source $DIR/config
2222
source $DIR/common.sh
23+
source $DIR/chroot.sh
2324

2425
IMAGE=$1
2526
if [ "$IMAGE" == "" ]; then
@@ -41,32 +42,6 @@ if [ ! -d "$CUSTOMIZE_SCRIPT_PATH" ]; then
4142
exit 1
4243
fi
4344

44-
function prepare_environment() {
45-
# In docker, these extra commands are required to enable this black-magic
46-
if [ -f /.dockerenv ] && [ "$(arch)" != "armv7l" ] && [ "$(arch)" != "aarch64" ] ; then
47-
if [ "$EDITBASE_ARCH" == "armv7l" ]; then
48-
update-binfmts --enable qemu-arm
49-
elif [ "$EDITBASE_ARCH" == "aarch64" ] || [ "$EDITBASE_ARCH" == "arm64" ]; then
50-
update-binfmts --enable qemu-aarch64
51-
fi
52-
(mount | grep -q -v "type binfmt_misc") || mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
53-
fi
54-
55-
# black magic of qemu-arm-static
56-
if [ "$(arch)" != "armv7l" ] && [ "$(arch)" != "aarch64" ] ; then
57-
if [ "$EDITBASE_ARCH" == "armv7l" ]; then
58-
cp `which qemu-arm-static` usr/bin/qemu-arm-static
59-
elif [ "$EDITBASE_ARCH" == "aarch64" ] || [ "$EDITBASE_ARCH" == "arm64" ]; then
60-
cp `which qemu-aarch64-static` usr/bin/qemu-aarch64-static
61-
fi
62-
fi
63-
64-
if [ "$EDITBASE_MOUNT_PROC" == "1" ]; then
65-
echo "Mounting /proc of host..."
66-
mount -t proc /proc proc/
67-
fi
68-
}
69-
7045
function copy_files() {
7146
#move filesystem files
7247
if [ -d "$1" ]; then
@@ -84,18 +59,7 @@ function execute_chroot_script() {
8459

8560
echo "::group::Running $1 in chroot..."
8661

87-
if [ "$(arch)" != "armv7l" ] && [ "$(arch)" != "aarch64" ] && [ "$(arch)" != "arm64" ] ; then
88-
if [ "$EDITBASE_ARCH" == "armv7l" ]; then
89-
echo "Building an armv7l system on a non-ARM host, using qemu-arm-static"
90-
chroot . usr/bin/qemu-arm-static /bin/bash /chroot_script
91-
elif [ "$EDITBASE_ARCH" == "aarch64" ] || [ "$EDITBASE_ARCH" == "arm64" ]; then
92-
echo "Building an aarch64/arm64 system on a non-ARM host, using qemu-aarch64-static"
93-
chroot . usr/bin/qemu-aarch64-static /bin/bash /chroot_script
94-
fi
95-
else
96-
echo "Building an armv7l/aarch64/arm64 system on an ARM host, not using qemu"
97-
chroot . /bin/bash /chroot_script
98-
fi
62+
chroot . $QEMU /bin/bash /chroot_script
9963

10064
echo "::endgroup::"
10165

@@ -151,7 +115,7 @@ pushd $EDITBASE_WORKSPACE
151115
fixLd
152116
fi
153117

154-
prepare_environment
118+
prepare_chroot_environment
155119

156120
### Execute chroot scripts ###
157121

src/enter_image

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,7 @@ echo "DIST_PATH=$DIST_PATH"
2323

2424
source $DIR/config
2525
source $DIR/common.sh
26-
27-
function execute_chroot_bash() {
28-
29-
# In docker, these extra commands are required to enable this black-magic
30-
if [ -f /.dockerenv ] && [ "$(arch)" != "armv7l" ] && [ "$(arch)" != "aarch64" ] ; then
31-
if [ "$EDITBASE_ARCH" == "armv7l" ]; then
32-
update-binfmts --enable qemu-arm
33-
elif [ "$EDITBASE_ARCH" == "aarch64" ] || [ "$EDITBASE_ARCH" == "arm64" ]; then
34-
update-binfmts --enable qemu-aarch64
35-
fi
36-
mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc || true
37-
fi
38-
39-
# black magic of qemu-arm-static
40-
if [ "$(arch)" != "armv7l" ] && [ "$(arch)" != "aarch64" ] ; then
41-
if [ "$EDITBASE_ARCH" == "armv7l" ]; then
42-
cp `which qemu-arm-static` usr/bin/qemu-arm-static
43-
elif [ "$EDITBASE_ARCH" == "aarch64" ] || [ "$EDITBASE_ARCH" == "arm64" ]; then
44-
cp `which qemu-aarch64-static` usr/bin/qemu-aarch64-static
45-
fi
46-
fi
47-
48-
if [ "$(arch)" != "armv7l" ] && [ "$(arch)" != "aarch64" ] && [ "$(arch)" != "arm64" ] ; then
49-
if [ "$EDITBASE_ARCH" == "armv7l" ]; then
50-
echo "Building on non-ARM device a armv7l system, using qemu-arm-static"
51-
chroot . usr/bin/qemu-arm-static /bin/bash
52-
elif [ "$EDITBASE_ARCH" == "aarch64" ] || [ "$EDITBASE_ARCH" == "arm64" ]; then
53-
echo "Building on non-ARM device a aarch64/arm64 system, using qemu-aarch64-static"
54-
chroot . usr/bin/qemu-aarch64-static /bin/bash
55-
fi
56-
else
57-
echo "Building on ARM device a armv7l/aarch64/arm64 system, not using qemu"
58-
chroot . /bin/bash
59-
fi
60-
}
26+
source $DIR/chroot.sh
6127

6228
mkdir -p $EDITBASE_WORKSPACE
6329
mkdir -p $EDITBASE_MOUNT_PATH
@@ -83,8 +49,9 @@ pushd $EDITBASE_WORKSPACE
8349
fixLd
8450
fi
8551

86-
### Execute bash ###
87-
execute_chroot_bash
52+
### Execute shell ###
53+
prepare_chroot_environment
54+
chroot . $QEMU ${EDITBASE_SHELL:-/bin/bash}
8855

8956
if [ "$EDITBASE_DISTRO" == "raspbian" ]; then
9057
restoreLd

0 commit comments

Comments
 (0)