consmirror.c: the BSD notice is replaced (Makronix's own file). Files with changes of other sessions pending are left for their commit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
94 lines
4.4 KiB
Bash
94 lines
4.4 KiB
Bash
#!/bin/sh
|
|
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
#
|
|
# Make the root file system image for one device: the base's rootfs tar (from
|
|
# tools/mkrootfs-<base>.sh), the device's kernel modules, and what it needs to
|
|
# be reached over USB -- as one ext4 file system with the device's ROOT_UUID,
|
|
# to be written to ROOT_PARTITION (tools/flash-root.sh).
|
|
#
|
|
# Usage: mkrootimg.sh DEVICEDIR ROOTFS.tar KERNEL_TREE OUT.img ROOT_PASSWORD
|
|
#
|
|
# The image is a bare file system, not a disk image with partitions: it sits
|
|
# directly on userdata, and its resize_root_filesystem grows it to the
|
|
# partition with resize2fs on the first boot. /etc/fstab is not empty, so the
|
|
# old pmOS initramfs did not look for a pmOS boot partition (init_2nd.sh).
|
|
#
|
|
# Layout on the file system (docs/NEXT.md, task B; base/initramfs/init):
|
|
#
|
|
# /system/current/ the system: the tree this script assembles
|
|
# /data/ NOT in the image -- the initramfs creates it on the
|
|
# first boot, and it survives every update
|
|
#
|
|
# An image from before 2026-11-09 has the tree at the top instead; the
|
|
# initramfs still boots those (whole partition as root).
|
|
#
|
|
# No root: the tree is assembled inside a user namespace, where the files of
|
|
# the tar are owned by 0, and mke2fs -d copies that ownership into the image.
|
|
|
|
set -eu
|
|
. "$(dirname "$0")/lib.sh"
|
|
PATH=$PATH:/sbin:/usr/sbin
|
|
|
|
[ $# -eq 5 ] || { echo "usage: $0 DEVICEDIR ROOTFS.tar KERNEL_TREE OUT.img ROOT_PASSWORD" >&2; exit 2; }
|
|
load_device "$1"
|
|
abs() { echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"; }
|
|
tarball=$(abs "$2") tree=$(cd "$3" && pwd) out=$(abs "$4") password=$5
|
|
|
|
[ -n "${ROOT_UUID:-}" ] || die "$DEVDIR/device.conf: ROOT_UUID not set"
|
|
[ -f "$tarball" ] || die "no rootfs $tarball (make rootfs BASE=...)"
|
|
[ -n "$password" ] || die "empty ROOT_PASSWORD"
|
|
# the base, from the tar's name (rootfs-<base>-<device>.tar, Makefile)
|
|
base=$(basename "$tarball" .tar)
|
|
base=${base#rootfs-}
|
|
base=${base%-"$(basename "$DEVDIR")"}
|
|
[ -f "$ROOT/base/$base/base.conf" ] || die "cannot tell the base from $tarball"
|
|
|
|
work=$ROOT/build/rootimg-work.$$
|
|
mkdir -p "$work"
|
|
trap 'unshare --map-root-user --map-auto rm -rf "$work"' EXIT
|
|
|
|
# the modules of the kernel in the boot image: same tree, same release
|
|
release=$(cat "$tree/include/config/kernel.release")
|
|
kernel_make "$tree" -s \
|
|
INSTALL_MOD_PATH="$work/modules" INSTALL_MOD_STRIP=1 modules_install
|
|
[ -d "$work/modules/lib/modules/$release" ] || die "modules_install wrote no $release"
|
|
|
|
# reboot2 (base/reboot2.c): restart with a reboot mode (fastboot), for
|
|
# reboot-mode(1); static, so it does not depend on the host's glibc version
|
|
"${CROSS_COMPILE}gcc" -static -Os -Wall -Werror -o "$work/reboot2" \
|
|
"$ROOT/base/reboot2.c" || die "cannot build base/reboot2.c"
|
|
"${CROSS_COMPILE}strip" "$work/reboot2"
|
|
|
|
# the root password as a SHA-512 crypt hash, the scheme both bases' shadow uses
|
|
# the NetBSD userland, staged outside the namespace (tools/lib.sh)
|
|
userland_stage "$(basename "$DEVDIR")" "$work/userland"
|
|
|
|
pwhash=$(openssl passwd -6 "$password")
|
|
makronix_version
|
|
export MRI_WORK="$work" MRI_TAR="$tarball" MRI_OUT="$out" MRI_PWHASH="$pwhash" \
|
|
MRI_UUID="$ROOT_UUID" MRI_RELEASE="$release" \
|
|
MRI_MXRELEASE="Makronix $MX_RELEASE $MX_CODENAME ($MX_CHANNEL)" \
|
|
MRI_SOCDIR="$ROOT/soc/$SOC" MRI_BASE="$base" \
|
|
MRI_BASEDIR="$ROOT/base/$base" MRI_ASM="$ROOT/tools/assemble-rootfs.sh" \
|
|
MRI_USERLAND="$work/userland"
|
|
|
|
# the tree (tools/assemble-rootfs.sh) becomes /system/current; /data is not in
|
|
# the image (the initramfs creates it on the first boot and never touches it
|
|
# again); the file system is the tree plus a quarter plus 256 MiB, in 4 KiB
|
|
# blocks, and the initramfs grows it to the partition on the first boot
|
|
unshare --map-root-user --map-auto sh -eu -c '
|
|
img=$MRI_WORK/img
|
|
mkdir -p "$img/system/current"
|
|
sh "$MRI_ASM" "$img/system/current" "$MRI_TAR" \
|
|
"$MRI_WORK/modules/lib/modules" "$MRI_RELEASE" "$MRI_BASEDIR" \
|
|
"$MRI_SOCDIR" "$MRI_BASE" "$MRI_WORK/reboot2" "$MRI_PWHASH" \
|
|
"$MRI_UUID" "$MRI_MXRELEASE" "$MRI_USERLAND"
|
|
kb=$(du -sk "$img/system/current" | cut -f1)
|
|
blocks=$(( (kb + kb / 4 + 262144) / 4 ))
|
|
rm -f "$MRI_OUT"
|
|
mke2fs -q -t ext4 -b 4096 -U "$MRI_UUID" -L makronix_root \
|
|
-E root_owner=0:0 -d "$img" "$MRI_OUT" "$blocks"
|
|
'
|
|
e2fsck -fn "$out" >/dev/null || die "e2fsck reports errors in $out"
|
|
echo "$out: $(wc -c < "$out" | tr -d ' ') bytes, UUID $ROOT_UUID, modules $release"
|