diff --git a/alpine/build-rootfs.sh b/alpine/build-rootfs.sh index f58c92f..c4ed860 100755 --- a/alpine/build-rootfs.sh +++ b/alpine/build-rootfs.sh @@ -16,7 +16,7 @@ chroot "$ROOTFS" /bin/sh -c "ln -s /var/cache/apk /etc/apk/cache" # chroot "$ROOTFS" /bin/sh -c "apk update" chroot "$ROOTFS" /bin/sh -c "apk add bash curl" cp "/install-packages.sh" "$ROOTFS/install-packages.sh" -chroot "$ROOTFS" /bin/bash /install-packages.sh +chroot "$ROOTFS" /bin/bash /install-packages.sh || exit 1 umount "$ROOTFS/var/cache/apk" umount "$ROOTFS/dev" @@ -26,4 +26,36 @@ umount "$ROOTFS/run" rm -r "$ROOTFS/build" -# Begin making full disk image for the device +### Begin making full disk image for the device +echo "=========================== RootFS "$( du -sh "$ROOTFS/" ) + +IMG=output.img +SIZE=512MB + +dd if=/dev/zero of="$IMG" bs=1 count=0 seek=$SIZE + +sgdisk -o "$IMG" \ + -n 1:2048:+64M -t 1:0700 \ + -n 2:0:-1M -t 2:8300 + +losetup -D +LOOP=$(losetup --find --show -P "$IMG") + +/sync-loop.sh "$LOOP" + +TMP_LOOP="/tmp$LOOP" +mkfs.vfat "${TMP_LOOP}p1" +mkfs.ext4 "${TMP_LOOP}p2" + +mkdir -p /mnt/img-root /mnt/img-boot + +mount ${TMP_LOOP}p1 /mnt/img-boot +mount ${TMP_LOOP}p2 /mnt/img-root + +cp -a "$ROOTFS"/. /mnt/img-root/ +cp /build/board.itb /mnt/img-boot + +umount /mnt/img-root +umount /mnt/img-boot + +losetup -d "$LOOP" diff --git a/alpine/install-packages.sh b/alpine/install-packages.sh index 500fa11..30f4c55 100755 --- a/alpine/install-packages.sh +++ b/alpine/install-packages.sh @@ -18,7 +18,7 @@ fi echo "--------------" sed -i "s/default_runtime = \"crun\"/\0\ncgroup_manager = \"cgroupfs\"/g" /etc/crio/crio.conf.d/10-crio.conf -cat /etc/crio/crio.conf.d/10-crio.conf +grep cgroup_manager /etc/crio/crio.conf.d/10-crio.conf || exit 1 mkdir -p /var/run/crio mkdir -p /var/lib/containers/storage diff --git a/alpine/sync-loop.sh b/alpine/sync-loop.sh new file mode 100755 index 0000000..c7baeaa --- /dev/null +++ b/alpine/sync-loop.sh @@ -0,0 +1,21 @@ +#!/bin/bash +DEVICE="$1" +FAKE_DEV="/tmp/dev" + +mkdir -p "$FAKE_DEV" + +echo "Refreshing partition table..." +partx -u "$DEVICE" 2>/dev/null || partx -a "$DEVICE" + +# Find partitions and their Major:Minor numbers +lsblk -rn -o NAME,MAJ:MIN "$DEVICE" | while read -r NAME MAJMIN; do + # Skip the parent loop0 + if [[ "$NAME" == "loop0" ]]; then continue; fi + + PART_PATH="$FAKE_DEV/$NAME" + MAJOR=$(echo $MAJMIN | cut -d: -f1) + MINOR=$(echo $MAJMIN | cut -d: -f2) + + echo "Creating node: $PART_PATH (b $MAJOR $MINOR)" + mknod "$PART_PATH" b "$MAJOR" "$MINOR" +done diff --git a/docker/alpine.Dockerfile b/docker/alpine.Dockerfile index 5206969..42c326a 100644 --- a/docker/alpine.Dockerfile +++ b/docker/alpine.Dockerfile @@ -1,6 +1,7 @@ ARG TAG=dev ARG DOCKER_IMAGE_ROOT=monok8s -FROM --platform=linux/amd64 ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build-base + +FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build-base ARG ALPINE_ARCH ARG ALPINE_VER @@ -10,6 +11,7 @@ RUN mkdir -p "/out/rootfs" COPY packages/alpine-minirootfs-${ALPINE_VER}-${ALPINE_ARCH}.tar.gz ./alpine.tar.gz COPY packages/${CRIO_VERSION}.tar.gz ./crio.tar.gz +COPY out/board.itb ./ RUN tar -xf alpine.tar.gz -C "/out/rootfs" diff --git a/docker/build-base.Dockerfile b/docker/build-base.Dockerfile index 945994e..9a2119c 100644 --- a/docker/build-base.Dockerfile +++ b/docker/build-base.Dockerfile @@ -1,4 +1,4 @@ -FROM --platform=linux/amd64 debian:bookworm-slim AS kernel-build +FROM --platform=$BUILDPLATFORM debian:bookworm-slim AS kernel-build ENV DEBIAN_FRONTEND=noninteractive @@ -11,8 +11,10 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ cpio \ curl \ + dosfstools \ file \ fdisk \ + gdisk \ flex \ git \ gettext-base \ @@ -21,11 +23,13 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ libssl-dev \ make \ pahole \ + parted \ perl \ python3 \ qemu-user-static \ rsync \ tar \ + udev \ xz-utils \ dwarves \ gcc-aarch64-linux-gnu \ diff --git a/docker/fit-build.Dockerfile b/docker/initramfs.Dockerfile similarity index 83% rename from docker/fit-build.Dockerfile rename to docker/initramfs.Dockerfile index 27663f4..e1f47d9 100644 --- a/docker/fit-build.Dockerfile +++ b/docker/initramfs.Dockerfile @@ -1,13 +1,13 @@ ARG TAG=dev ARG DOCKER_IMAGE_ROOT=monok8s -FROM --platform=linux/amd64 ${DOCKER_IMAGE_ROOT}/kernel-build:${TAG} AS kernel +FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/kernel-build:${TAG} AS kernel # Selective mods RUN mkdir /out/selected-mods COPY initramfs/selective-mods.sh / RUN /selective-mods.sh /out/rootfs/lib/modules/$(ls /out/rootfs/lib/modules/) /out/selected-mods -FROM --platform=linux/amd64 ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build +FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build ARG BUSYBOX_VERSION ARG ARCH @@ -38,3 +38,6 @@ RUN mkdir -p bin sbin etc proc sys dev lib usr/bin usr/sbin COPY --from=kernel /out/selected-mods/* ./lib/ RUN find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz + +FROM scratch +COPY --from=build /out/initramfs.cpio.gz /initramfs.cpio.gz diff --git a/docker/itb.Dockerfile b/docker/itb.Dockerfile index 912f6bc..a04dce0 100644 --- a/docker/itb.Dockerfile +++ b/docker/itb.Dockerfile @@ -3,7 +3,7 @@ ARG DOCKER_IMAGE_ROOT=monok8s FROM ${DOCKER_IMAGE_ROOT}/kernel-build:${TAG} AS kernel FROM ${DOCKER_IMAGE_ROOT}/fit-build:${TAG} AS fit -FROM --platform=linux/amd64 ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build +FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/build-base:${TAG} AS build ARG DEVICE_TREE_TARGET diff --git a/docker/kernel-build.Dockerfile b/docker/kernel-build.Dockerfile index 43be2b9..b48ae37 100644 --- a/docker/kernel-build.Dockerfile +++ b/docker/kernel-build.Dockerfile @@ -1,6 +1,6 @@ ARG TAG=dev ARG DOCKER_IMAGE_ROOT=monok8s -FROM --platform=linux/amd64 ${DOCKER_IMAGE_ROOT}/build-base:${TAG} +FROM --platform=$BUILDPLATFORM ${DOCKER_IMAGE_ROOT}/build-base:${TAG} ARG NXP_VERSION ARG ARCH diff --git a/docker/release.Dockerfile b/docker/release.Dockerfile new file mode 100644 index 0000000..810a462 --- /dev/null +++ b/docker/release.Dockerfile @@ -0,0 +1,4 @@ +ARG RELEASE_IMAGE +FROM scratch + +COPY --from artifacts $RELEASE_IMAGE /$RELEASE_IMAGE diff --git a/makefile b/makefile index a482495..6fc1ea4 100644 --- a/makefile +++ b/makefile @@ -10,6 +10,12 @@ ALPINE_TAR := $(PACKAGES_DIR)/alpine-minirootfs-$(ALPINE_VER)-$(ALPINE_ARCH).ta NXP_TAR := $(PACKAGES_DIR)/$(NXP_VERSION).tar.gz CRIO_TAR := $(PACKAGES_DIR)/$(CRIO_VERSION).tar.gz +BOARD_ITB := out/board.itb +INITRAMFS := out/initramfs.cpio.gz +RELEASE_IMAGE := out/monok8s-$(TAG).img + +ALPINE_SERIES := $(word 1,$(subst ., ,${ALPINE_VER})).$(word 2,$(subst ., ,${ALPINE_VER})) + $(BUSYBOX_TAR): @mkdir -p $(PACKAGES_DIR) curl -L -o $@ "https://github.com/mirror/busybox/archive/refs/tags/$(BUSYBOX_VERSION).tar.gz" @@ -32,7 +38,7 @@ build-base: --build-arg TAG=$(TAG) \ -t $(DOCKER_IMAGE_ROOT)/build-base:$(TAG) . -kernel-build: build-base $(NXP_TAR) +$(KERNEL_IMAGE): build-base $(NXP_TAR) docker build \ -f docker/kernel-build.Dockerfile \ --build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \ @@ -41,29 +47,34 @@ kernel-build: build-base $(NXP_TAR) --build-arg CROSS_COMPILE=$(CROSS_COMPILE) \ --build-arg NXP_VERSION=$(NXP_VERSION) \ --build-arg DEVICE_TREE_TARGET=$(DEVICE_TREE_TARGET) \ - -t $(DOCKER_IMAGE_ROOT)/kernel-build:$(TAG) . + --output type=local,dest=./out . -fit-build: kernel-build $(BUSYBOX_TAR) + @test -f $(KERNEL_IMAGE) + +$(INITRAMFS): $(KERNEL_IMAGE) $(BUSYBOX_TAR) docker build \ - -f docker/fit-build.Dockerfile \ + -f docker/initramfs.Dockerfile \ --build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \ --build-arg TAG=$(TAG) \ --build-arg ARCH=$(ARCH) \ --build-arg CROSS_COMPILE=$(CROSS_COMPILE) \ --build-arg BUSYBOX_VERSION=$(BUSYBOX_VERSION) \ - -t $(DOCKER_IMAGE_ROOT)/fit-build:$(TAG) . + --output type=local,dest=./out . -itb: fit-build + @test -f $(INITRAMFS) + +$(BOARD_ITB): $(INITRAMFS) docker build \ -f docker/itb.Dockerfile \ --build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \ --build-arg TAG=$(TAG) \ --build-arg ARCH=$(ARCH) \ --build-arg DEVICE_TREE_TARGET=$(DEVICE_TREE_TARGET) \ - --output type=local,dest=./out \ - -t $(DOCKER_IMAGE_ROOT)/itb:$(TAG) . + --output type=local,dest=./out . -buildenv-alpine: build-base $(ALPINE_TAR) $(CRIO_TAR) + @test -f $(BOARD_ITB) + +release: $(BOARD_ITB) build-base docker build \ -f docker/alpine.Dockerfile \ --build-arg DOCKER_IMAGE_ROOT=$(DOCKER_IMAGE_ROOT) \ @@ -72,13 +83,17 @@ buildenv-alpine: build-base $(ALPINE_TAR) $(CRIO_TAR) --build-arg ALPINE_VER=$(ALPINE_VER) \ --build-arg CRIO_VERSION=$(CRIO_VERSION) \ -t $(DOCKER_IMAGE_ROOT)/buildenv-alpine:$(TAG) . - -alpine-rootfs: buildenv-alpine kernel-build - docker run --rm -it \ + @cid=$$(docker create \ --privileged \ -v /cache/apk:/var/cache/apk \ - -v /cache/artifacts:/artifacts \ + --device=/dev/loop0:/dev/loop0 \ -e ROOTFS=/out/rootfs \ -e ARTIFACT_DIR=/artifacts \ + -e RELEASE_IMAGE=$(RELEASE_IMAGE) \ $(DOCKER_IMAGE_ROOT)/buildenv-alpine:$(TAG) \ - bash -lc '/build-rootfs.sh' + bash -lc '/build-rootfs.sh'); \ + docker start -a $$cid; \ + docker cp $$cid:/build/output.img $(RELEASE_IMAGE); \ + docker rm $$cid + + @test -f $(RELEASE_IMAGE)