opnsense-deps/mk-image.sh
Jeremy Karst ccebd54083 board package: build per series, harden kernel install for U-Boot
- Makefile: read the FreeBSD release from tools' config/<series>/build.conf
  and derive the compiler target and package ABI from it, version the
  package <release>_<commit count>, take libxml2 from our own packages
  set instead of pkg.FreeBSD.org, clone the NXP sources from the Forgejo
  mirrors, and drop the image target (release/build.sh replaces it)
- +MANIFEST: ABI from the series, new maintainer and www
- GATEWAY.conf: annotate mono-gateway as coming from the OPNsense
  repository; fail the image build if the package is missing
- mk-image.sh: remove the amd64 qemu binary setup_xtools leaves behind
- 20-kernel-update: drop the 26.1.3 guard; check room on the FAT
  partition, copy and verify the new kernel before replacing the old
  one, keep kernel.old, restore it on failure, retry at next shutdown
- 01-mono-modules: log modules that fail to load instead of hiding it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:56:54 -05:00

131 lines
4.1 KiB
Bash

#!/bin/sh
#
# mk-image.sh -- Build a ZFS OPNsense ARM image directly
#
# This script replaces arm.sh's UFS image creation with ZFS.
# It sources opnsense-build's common.sh for all setup_* functions,
# so it must be run from ${TOOLSDIR}/build/ with the same arguments
# that the opnsense-build Makefile passes to build scripts.
#
# Usage (invoked by opnsense-deps Makefile):
# cd ${TOOLSDIR}/build && sh /usr/deps/mk-image.sh [common.sh args] 5G
#
# Copyright 2026 Mono Technologies Inc.
# SPDX-License-Identifier: BSD-2-Clause
set -e
SELF=arm
. ./common.sh
if [ ${PRODUCT_ARCH} != aarch64 ]; then
echo ">>> Cannot build arm image with arch ${PRODUCT_ARCH}"
exit 1
fi
check_image ${SELF} ${@}
IMGSIZE="5G"
if [ -n "${1}" ]; then
IMGSIZE=${1}
fi
ZPOOL=pool0
ARMIMG="${IMAGESDIR}/${PRODUCT_RELEASE}-arm-${PRODUCT_ARCH}${PRODUCT_DEVICE+"-${PRODUCT_DEVICE}"}.img"
sh ./clean.sh ${SELF}
setup_stage ${STAGEDIR}
# ---------------------------------------------------------------
# Check for stale ZFS pool
# ---------------------------------------------------------------
for IMPORT in $(zpool import 2>/dev/null | awk '$1 == "pool:" { print $2 }'); do
if [ "${IMPORT}" = "${ZPOOL}" ]; then
echo ">>> ERROR: ZFS pool '${ZPOOL}' already exists" >&2
exit 1
fi
done
# ---------------------------------------------------------------
# Create GPT image with FAT boot + ZFS root
# ---------------------------------------------------------------
echo ">>> Creating ZFS image (${IMGSIZE})..."
truncate -s ${IMGSIZE} ${ARMIMG}
DEV=$(mdconfig -a -t vnode -f ${ARMIMG})
DISK_SIZE=$(diskinfo /dev/${DEV} | awk '{ print $3 }')
python3 ./mkgpt.py /dev/${DEV} 80 ${DISK_SIZE}
newfs_msdos -L BOOT -F 16 /dev/${DEV}p1
echo ">>> Creating ZFS pool..."
zpool create -o cachefile=none -R ${STAGEDIR} ${ZPOOL} /dev/${DEV}p2
# Dataset layout (matches FreeBSD standard / OPNsense vm.sh)
zfs create -o mountpoint=none ${ZPOOL}/ROOT
zfs create -o mountpoint=/ ${ZPOOL}/ROOT/default
zfs create -o mountpoint=/tmp -o exec=on -o setuid=off ${ZPOOL}/tmp
zfs create -o mountpoint=/usr -o canmount=off ${ZPOOL}/usr
zfs create ${ZPOOL}/usr/home
zfs create -o mountpoint=/var -o canmount=off ${ZPOOL}/var
zfs create -o exec=off -o setuid=off ${ZPOOL}/var/audit
zfs create -o exec=off -o setuid=off ${ZPOOL}/var/crash
zfs create -o exec=off -o setuid=off ${ZPOOL}/var/log
zfs create -o atime=on ${ZPOOL}/var/mail
zfs create -o setuid=off ${ZPOOL}/var/tmp
zpool set bootfs=${ZPOOL}/ROOT/default ${ZPOOL}
# ---------------------------------------------------------------
# Populate rootfs (same sequence as arm.sh)
# ---------------------------------------------------------------
setup_base ${STAGEDIR}
setup_kernel ${STAGEDIR}
setup_xtools ${STAGEDIR}
setup_packages ${STAGEDIR}
setup_extras ${STAGEDIR} ${SELF}
setup_entropy ${STAGEDIR}
setup_xbase ${STAGEDIR}
# setup_xtools copies the host's qemu emulator to /usr/local/bin but
# setup_xbase only removes it from /usr/bin: drop the amd64 binary here.
# (release/build.sh also moves the xtools set aside before building the
# image so no other amd64 toolchain files are overlaid onto the image.)
rm -f ${STAGEDIR}/usr/local/bin/qemu-*-static
# ZFS fstab: only the FAT boot partition
cat > ${STAGEDIR}/etc/fstab << EOF
# Device Mountpoint FStype Options Dump Pass#
/dev/gpt/boot /boot/msdos msdosfs rw,noatime 0 0
EOF
# ---------------------------------------------------------------
# Populate FAT boot partition
# ---------------------------------------------------------------
mkdir -p ${STAGEDIR}/boot/msdos
mount_msdosfs /dev/${DEV}p1 ${STAGEDIR}/boot/msdos
arm_install_uboot
# EFI boot not used (U-Boot booti), but keep the check for completeness
if [ -n "${PRODUCT_UEFI}" -a -z "${PRODUCT_UEFI%%*"${SELF}"*}" ]; then
setup_efiboot ${STAGEDIR}/efiboot.img ${STAGEDIR}/boot/loader.efi
cp -r ${STAGEDIR}/efiboot.img.d/efi ${STAGEDIR}/boot/msdos/efi
fi
# ---------------------------------------------------------------
# Finalize
# ---------------------------------------------------------------
echo -n ">>> Building arm image... "
sync
umount ${STAGEDIR}/boot/msdos
zpool export ${ZPOOL}
mdconfig -d -u ${DEV}
echo "done"
sign_image ${ARMIMG}