Skip to content

Commit aa9c6a0

Browse files
committed
build: armbian: kernel: refactor Dockerfile with helper
- building the Armbian kernels would produce different hashes depending on the arch of the host - moving the affected code into the Dockerfile would lead to escaping pain; instead implement a docker.sh helper - in practice, all code in the Dockerfile is hashed, but the arch decision is now therein and hash won't change - also, allows for reuse, which is bound to come later Signed-off-by: Ricardo Pardini <[email protected]>
1 parent 0d47df1 commit aa9c6a0

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

bash/docker.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,37 @@ function pull_docker_image_from_remote_with_retries() {
7878
log error "Failed to pull ${image} after ${retries} retries."
7979
exit 1
8080
}
81+
82+
# Helper script, for common task of installing packages on a Debian Dockerfile
83+
# always includes curl and downloads ORAS binary too
84+
# takes the relative directory to write the helper to
85+
# sets outer scope dockerfile_helper_filename with the name of the file for the Dockerfile (does not include the directory)
86+
function produce_dockerfile_helper_apt_oras() {
87+
declare target_dir="$1"
88+
declare helper_name="apt-oras-helper.sh"
89+
dockerfile_helper_filename="Dockerfile.autogen.helper.${helper_name}" # this is negated in .dockerignore
90+
91+
declare fn="${target_dir}${dockerfile_helper_filename}"
92+
cat <<- 'DOWNLOAD_HELPER_SCRIPT' > "${fn}"
93+
#!/bin/bash
94+
set -e
95+
declare oras_version="1.2.2" # See https://github.com/oras-project/oras/releases
96+
# determine the arch to download from current arch
97+
declare oras_arch="unknown"
98+
case "$(uname -m)" in
99+
"x86_64") oras_arch="amd64" ;;
100+
"aarch64" | "arm64") oras_arch="arm64" ;;
101+
*) log error "ERROR: ARCH $(uname -m) not supported by ORAS? check https://github.com/oras-project/oras/releases" && exit 1 ;;
102+
esac
103+
declare oras_down_url="https://github.com/oras-project/oras/releases/download/v${oras_version}/oras_${oras_version}_linux_${oras_arch}.tar.gz"
104+
export DEBIAN_FRONTEND=noninteractive
105+
apt-get -qq -o "Dpkg::Use-Pty=0" update || apt-get -o "Dpkg::Use-Pty=0" update
106+
apt-get -qq install -o "Dpkg::Use-Pty=0" -q -y curl "${@}" || apt-get install -o "Dpkg::Use-Pty=0" -q -y curl "${@}"
107+
curl -sL -o /oras.tar.gz ${oras_down_url}
108+
tar -xvf /oras.tar.gz -C /usr/local/bin/ oras
109+
rm -rf /oras.tar.gz
110+
chmod +x /usr/local/bin/oras
111+
echo -n "ORAS version: " && oras version
112+
DOWNLOAD_HELPER_SCRIPT
113+
log debug "Created apt-oras helper script '${fn}'"
114+
}

bash/kernel/kernel_armbian.sh

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,19 @@ function calculate_kernel_version_armbian() {
2525
ARMBIAN_KERNEL_MAJOR_MINOR_POINT="$(echo -n "${ARMBIAN_KERNEL_VERSION}" | cut -d "-" -f 1)"
2626
log info "ARMBIAN_KERNEL_MAJOR_MINOR_POINT: ${ARMBIAN_KERNEL_MAJOR_MINOR_POINT}"
2727

28-
declare -g ARMBIAN_KERNEL_DOCKERFILE="kernel/Dockerfile.autogen.armbian.${inventory_id}"
2928

30-
declare oras_version="1.2.0-rc.1" # @TODO bump this once it's released; yes it's much better than 1.1.x's
31-
# determine the arch to download from current arch
32-
declare oras_arch="unknown"
33-
case "$(uname -m)" in
34-
"x86_64") oras_arch="amd64" ;;
35-
"aarch64" | "arm64") oras_arch="arm64" ;;
36-
*) log error "ERROR: ARCH $(uname -m) not supported by ORAS? check https://github.com/oras-project/oras/releases" && exit 1 ;;
37-
esac
38-
declare oras_down_url="https://github.com/oras-project/oras/releases/download/v${oras_version}/oras_${oras_version}_linux_${oras_arch}.tar.gz"
29+
# A helper script, as escaping bash into a RUN command in Dockerfile is a pain; included in input_hash later
30+
declare dockerfile_helper_filename="undefined.sh"
31+
produce_dockerfile_helper_apt_oras "kernel/" # will create the helper script in kernel/ directory; sets helper_name
3932

4033
# Lets create a Dockerfile that will be used to obtain the artifacts needed, using ORAS binary
34+
declare -g ARMBIAN_KERNEL_DOCKERFILE="kernel/Dockerfile.autogen.armbian.${inventory_id}"
4135
echo "Creating Dockerfile '${ARMBIAN_KERNEL_DOCKERFILE}'... "
4236
cat <<- ARMBIAN_ORAS_DOCKERFILE > "${ARMBIAN_KERNEL_DOCKERFILE}"
4337
FROM debian:stable AS downloader
44-
# Install ORAS binary tool from GitHub releases
45-
ENV DEBIAN_FRONTEND=noninteractive
46-
RUN apt -o "Dpkg::Use-Pty=0" update && apt install -o "Dpkg::Use-Pty=0" -y curl dpkg-dev && \
47-
curl -sL -o /oras.tar.gz ${oras_down_url} && \
48-
tar -xvf /oras.tar.gz -C /usr/local/bin/ oras && \
49-
chmod +x /usr/local/bin/oras && \
50-
oras version
38+
# Call the helper to install curl, oras, and dpkg-dev
39+
ADD ./${dockerfile_helper_filename} /apt-oras-helper.sh
40+
RUN bash /apt-oras-helper.sh dpkg-dev
5141
5242
FROM downloader AS downloaded
5343
@@ -76,11 +66,11 @@ function calculate_kernel_version_armbian() {
7666
# Important: this tarball needs to have permissions for the root directory included! Otherwise linuxkit rootfs will have the wrong permissions on / (root)
7767
WORKDIR /armbian/modules_only
7868
RUN mv /armbian/image/lib /armbian/modules_only/
79-
RUN echo "Before cleaning: " && du -h -d 10 -x . | sort -h | tail -n 20
69+
RUN echo "Before cleaning: " && du -h -d 10 -x lib/modules | sort -h | tail -n 20
8070
# Trim the kernel modules to save space; hopefully your required hardware is not included here
81-
RUN rm -rfv ./lib/modules/*/kernel/drivers/net/wireless ./lib/modules/*/kernel/sound ./lib/modules/*/kernel/drivers/media
82-
RUN rm -rfv ./lib/modules/*/kernel/drivers/infiniband
83-
RUN echo "After cleaning: " && du -h -d 10 -x . | sort -h | tail -n 20
71+
RUN rm -rf ./lib/modules/*/kernel/drivers/net/wireless ./lib/modules/*/kernel/sound ./lib/modules/*/kernel/drivers/media
72+
RUN rm -rf ./lib/modules/*/kernel/drivers/infiniband
73+
RUN echo "After cleaning: " && du -h -d 10 -x lib/modules | sort -h | tail -n 20
8474
RUN tar -cf /armbian/output/kernel.tar .
8575
8676
# Create a tarball with the dtbs in usr/lib/linux-image-*
@@ -96,8 +86,7 @@ function calculate_kernel_version_armbian() {
9686
ARMBIAN_ORAS_DOCKERFILE
9787

9888
declare input_hash="" short_input_hash=""
99-
# shellcheck disable=SC2002 # keep cat & hash stdin so we can easily add more factors to the hash one day
100-
input_hash="$(cat "${ARMBIAN_KERNEL_DOCKERFILE}" | sha256sum - | cut -d ' ' -f 1)"
89+
input_hash="$(cat "${ARMBIAN_KERNEL_DOCKERFILE}" "kernel/${dockerfile_helper_filename}" | sha256sum - | cut -d ' ' -f 1)"
10190
short_input_hash="${input_hash:0:8}"
10291
kernel_oci_version="${ARMBIAN_KERNEL_MAJOR_MINOR_POINT}-${short_input_hash}"
10392
armbian_type="${inventory_id#"armbian-"}" # remove the 'armbian-' prefix from inventory_id, but keep the rest. "uefi" has "current/edge" and "arm64/x86" variants.

kernel/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
Dockerfile*
44
Makefile
55
README.md
6+
!Dockerfile.autogen.helper*

0 commit comments

Comments
 (0)