#!/bin/bash # Install the kernel built by build-kernel.sh into an existing root filesystem # and (re)generate /boot/extlinux/extlinux.conf for mainline U-Boot. # Nothing else in the root filesystem is touched: no initramfs, no Armbian # boot.scr/armbianEnv.txt changes (they stay as they are, unused by our U-Boot, # which tries extlinux.conf first). # # Usage: # sudo ./install-kernel.sh /mnt/card-root card's root partition mounted # ./install-kernel.sh ssh:root@radio.lo running board, over ssh # Installs out/opi0-kernel-$RELEASE.tar.gz; RELEASE defaults to the last # build (out/kernel-release). modules.dep is already in the tarball (depmod # runs at build time), so nothing is run on the target. # Every kernel under /boot/opi0/ gets a menu entry, highest version first # (sort -V on the release name, not file dates); the highest is the default. # An older one can be picked on the serial console if a new one fails. # Settings (config.sh / environment): PARTUUID, BOOT_DTB, BOOT_OVERLAYS, KERNEL_CONSOLE, # KERNEL_CMDLINE_EXTRA, RELEASE. set -euo pipefail . "$(dirname "$0")/config.sh" [ $# -eq 1 ] || die "usage: $0 /path/to/mounted/root | ssh:user@host" TARGET="$1" RELEASE="${RELEASE:-$(cat "$OUT/kernel-release" 2>/dev/null || true)}" [ -n "$RELEASE" ] || die "no kernel built yet, run ./build-kernel.sh" TARBALL="$OUT/opi0-kernel-$RELEASE.tar.gz" [ -f "$TARBALL" ] || die "$TARBALL not found" # run CMD on the target root (as shell code; paths relative to its /) case "$TARGET" in ssh:*) HOST="${TARGET#ssh:}" need_tools ssh trun() { ssh "$HOST" "cd / && $1"; } tput_file() { ssh "$HOST" "cat > '/$1.new' && mv '/$1.new' '/$1'"; } ;; *) ROOT="$(cd "$TARGET" && pwd -P)" [ "$ROOT" != / ] || die "refusing to install into this PC's own /" [ -e "$ROOT/etc/os-release" ] || die "$ROOT does not look like a root filesystem" [ -d "$ROOT/boot" ] || die "$ROOT has no /boot" trun() { (cd "$ROOT" && bash -c "$1"); } tput_file() { cat > "$ROOT/$1.new" && mv "$ROOT/$1.new" "$ROOT/$1"; } ;; esac # Root partition by PARTUUID: no initramfs, so root=UUID= cannot work. # PARTUUID=... in the environment overrides the detection. partuuid="${PARTUUID:-}" if [ -n "${ROOT:-}" ]; then if [ -z "$partuuid" ]; then [ "$(findmnt -no TARGET --target "$ROOT")" = "$ROOT" ] || die "$ROOT is not a mount point; mount the card's root partition there (or set PARTUUID)" partuuid="$(blkid -s PARTUUID -o value "$(findmnt -no SOURCE --target "$ROOT")" || true)" fi fstype="$(findmnt -no FSTYPE --target "$ROOT" || true)" else [ -n "$partuuid" ] || partuuid="$(trun 'blkid -s PARTUUID -o value "$(findmnt -no SOURCE /)"' || true)" fstype="$(trun 'findmnt -no FSTYPE /' || true)" fi [ -n "$partuuid" ] || die "could not find the PARTUUID of the root partition" case "$fstype" in ext[234]|btrfs|f2fs|xfs) ;; *) fstype=ext4 ;; esac info "root: PARTUUID=$partuuid ($fstype)" # Writing below follows the target's own symlinks (tar # --keep-directory-symlink, cp, mv). A relative link such as Debian's # lib -> usr/lib stays inside the target, but an absolute one (or ../..) # would resolve on this PC and write into its own /usr/lib or /boot. if [ -n "${ROOT:-}" ]; then for p in boot boot/opi0 "boot/opi0/$RELEASE" boot/extlinux \ boot/extlinux/extlinux.conf boot/extlinux/extlinux.conf.bak \ boot/extlinux/extlinux.conf.new lib lib/modules "lib/modules/$RELEASE"; do r="$(realpath -m -- "$ROOT/$p")" case "$r" in "$ROOT"/*) ;; *) die "/$p on the target resolves to $r, outside $ROOT (absolute symlink?); not writing there" ;; esac done for p in "boot/opi0/$RELEASE" "lib/modules/$RELEASE"; do [ -d "$ROOT/$p" ] || continue r="$(find "$ROOT/$p" -type l -lname '/*' -print -quit)" [ -z "$r" ] || die "absolute symlink $r in the target; not extracting over it" done fi # Armbian packages that can undo this setup on an apt upgrade. Warnings only. check_target_packages() { local st unheld st="$(trun 'cat var/lib/dpkg/status 2>/dev/null' | awk 'BEGIN { RS = ""; FS = "\n" } { p = s = "" for (i = 1; i <= NF; i++) { if ($i ~ /^Package: /) p = substr($i, 10) else if ($i ~ /^Status: /) s = substr($i, 9) } if (s ~ / installed$/) print p, s }' || true)" [ -n "$st" ] || return 0 unheld="$(awk '$1 ~ /^linux-(image|dtb|u-boot)-/ && $2 != "hold" { print $1 }' <<<"$st" | tr '\n' ' ')" [ -z "$unheld" ] || echo "WARNING: not on hold: ${unheld% }; an upgrade may rewrite Armbian's boot files or the bootloader. On the target: apt-mark hold ${unheld% }" >&2 ! grep -q '^u-boot-menu ' <<<"$st" || echo "WARNING: u-boot-menu is installed; it rewrites /boot/extlinux/extlinux.conf on kernel changes. Remove it." >&2 trun 'test -e lib/firmware/xr819/fw_xr819.bin' || echo "WARNING: /lib/firmware/xr819/ is missing (package armbian-firmware); the XR819 Wi-Fi will not work" >&2 local alg alg="$(trun 'cat etc/default/armbian-zram-config 2>/dev/null' | sed -n "s/^[[:space:]]*SWAP_ALGORITHM=[\"']*\([a-z0-9-]*\).*/\1/p" | tail -1 || true)" case "$alg" in ""|lz4|zstd|lzo|lzo-rle) ;; *) echo "WARNING: armbian-zram-config uses SWAP_ALGORITHM=$alg, which files/kernel.config does not enable" >&2 ;; esac } check_target_packages info "installing $RELEASE to $TARGET" # --keep-directory-symlink: /lib is a symlink to usr/lib on merged-/usr systems case "$TARGET" in ssh:*) ssh "$HOST" "tar --keep-directory-symlink --no-same-owner -xzf - -C /" < "$TARBALL" ;; *) tar --keep-directory-symlink -xzf "$TARBALL" -C "$ROOT" ;; esac # Releases on the target, highest version first (by name, not by date). releases="$(trun 'ls -1 boot/opi0 2>/dev/null' | sort -rV)" [ -n "$releases" ] || die "no kernels in /boot/opi0 on the target" # The last console= becomes /dev/console (emergency shell, init output). Keep # the serial console there even if KERNEL_CONSOLE is overridden in a different # order; the relative order of everything else is kept. console_args() { local a serial="" rest="" for a in $KERNEL_CONSOLE; do case "$a" in console=ttyS*) serial="${serial:+$serial }$a" ;; *) rest="${rest:+$rest }$a" ;; esac done echo "$rest${rest:+${serial:+ }}$serial" } cmdline_console="$(console_args)" case "$cmdline_console" in *console=ttyS*) ;; *) echo "WARNING: no console=ttyS* in KERNEL_CONSOLE; no serial emergency console" >&2 ;; esac gen_extlinux() { local r ov echo "# Generated by install-kernel.sh (OPi0 build) - rerun it to update." echo "# Booted by mainline U-Boot; no initramfs." echo "menu title Orange Pi Zero" echo "timeout 20" echo "default $(head -1 <<<"$releases")" for r in $releases; do echo echo "label $r" echo " kernel /boot/opi0/$r/zImage" echo " fdt /boot/opi0/$r/$BOOT_DTB" if [ -n "$BOOT_OVERLAYS" ]; then echo -n " fdtoverlays" for ov in $BOOT_OVERLAYS; do echo -n " /boot/opi0/$r/overlay/$ov.dtbo"; done echo fi echo " append root=PARTUUID=$partuuid rootfstype=$fstype rootwait rw $cmdline_console panic=10 consoleblank=0 $KERNEL_CMDLINE_EXTRA" done } trun 'mkdir -p boot/extlinux && { [ ! -f boot/extlinux/extlinux.conf ] || cp boot/extlinux/extlinux.conf boot/extlinux/extlinux.conf.bak; }' gen_extlinux | tput_file boot/extlinux/extlinux.conf trun 'sync' info "installed; /boot/extlinux/extlinux.conf:" trun 'cat boot/extlinux/extlinux.conf'