install-kernel.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  2. # Install the kernel built by build-kernel.sh into an existing root filesystem
  3. # and (re)generate /boot/extlinux/extlinux.conf for mainline U-Boot.
  4. # Nothing else in the root filesystem is touched: no initramfs, no Armbian
  5. # boot.scr/armbianEnv.txt changes (they stay as they are, unused by our U-Boot,
  6. # which tries extlinux.conf first).
  7. #
  8. # Usage:
  9. # sudo ./install-kernel.sh /mnt/card-root card's root partition mounted
  10. # ./install-kernel.sh ssh:root@radio.lo running board, over ssh
  11. # Installs out/opi0-kernel-$RELEASE.tar.gz; RELEASE defaults to the last
  12. # build (out/kernel-release). modules.dep is already in the tarball (depmod
  13. # runs at build time), so nothing is run on the target.
  14. # Every kernel under /boot/opi0/ gets a menu entry, highest version first
  15. # (sort -V on the release name, not file dates); the highest is the default.
  16. # An older one can be picked on the serial console if a new one fails.
  17. # Settings (config.sh / environment): PARTUUID, BOOT_DTB, BOOT_OVERLAYS, KERNEL_CONSOLE,
  18. # KERNEL_CMDLINE_EXTRA, RELEASE.
  19. set -euo pipefail
  20. . "$(dirname "$0")/config.sh"
  21. [ $# -eq 1 ] || die "usage: $0 /path/to/mounted/root | ssh:user@host"
  22. TARGET="$1"
  23. RELEASE="${RELEASE:-$(cat "$OUT/kernel-release" 2>/dev/null || true)}"
  24. [ -n "$RELEASE" ] || die "no kernel built yet, run ./build-kernel.sh"
  25. TARBALL="$OUT/opi0-kernel-$RELEASE.tar.gz"
  26. [ -f "$TARBALL" ] || die "$TARBALL not found"
  27. # run CMD on the target root (as shell code; paths relative to its /)
  28. case "$TARGET" in
  29. ssh:*)
  30. HOST="${TARGET#ssh:}"
  31. need_tools ssh
  32. trun() { ssh "$HOST" "cd / && $1"; }
  33. tput_file() { ssh "$HOST" "cat > '/$1.new' && mv '/$1.new' '/$1'"; }
  34. ;;
  35. *)
  36. ROOT="$(cd "$TARGET" && pwd -P)"
  37. [ "$ROOT" != / ] || die "refusing to install into this PC's own /"
  38. [ -e "$ROOT/etc/os-release" ] || die "$ROOT does not look like a root filesystem"
  39. [ -d "$ROOT/boot" ] || die "$ROOT has no /boot"
  40. trun() { (cd "$ROOT" && bash -c "$1"); }
  41. tput_file() { cat > "$ROOT/$1.new" && mv "$ROOT/$1.new" "$ROOT/$1"; }
  42. ;;
  43. esac
  44. # Root partition by PARTUUID: no initramfs, so root=UUID= cannot work.
  45. # PARTUUID=... in the environment overrides the detection.
  46. partuuid="${PARTUUID:-}"
  47. if [ -n "${ROOT:-}" ]; then
  48. if [ -z "$partuuid" ]; then
  49. [ "$(findmnt -no TARGET --target "$ROOT")" = "$ROOT" ] ||
  50. die "$ROOT is not a mount point; mount the card's root partition there (or set PARTUUID)"
  51. partuuid="$(blkid -s PARTUUID -o value "$(findmnt -no SOURCE --target "$ROOT")" || true)"
  52. fi
  53. fstype="$(findmnt -no FSTYPE --target "$ROOT" || true)"
  54. else
  55. [ -n "$partuuid" ] || partuuid="$(trun 'blkid -s PARTUUID -o value "$(findmnt -no SOURCE /)"' || true)"
  56. fstype="$(trun 'findmnt -no FSTYPE /' || true)"
  57. fi
  58. [ -n "$partuuid" ] || die "could not find the PARTUUID of the root partition"
  59. case "$fstype" in ext[234]|btrfs|f2fs|xfs) ;; *) fstype=ext4 ;; esac
  60. info "root: PARTUUID=$partuuid ($fstype)"
  61. # Writing below follows the target's own symlinks (tar
  62. # --keep-directory-symlink, cp, mv). A relative link such as Debian's
  63. # lib -> usr/lib stays inside the target, but an absolute one (or ../..)
  64. # would resolve on this PC and write into its own /usr/lib or /boot.
  65. if [ -n "${ROOT:-}" ]; then
  66. for p in boot boot/opi0 "boot/opi0/$RELEASE" boot/extlinux \
  67. boot/extlinux/extlinux.conf boot/extlinux/extlinux.conf.bak \
  68. boot/extlinux/extlinux.conf.new lib lib/modules "lib/modules/$RELEASE"; do
  69. r="$(realpath -m -- "$ROOT/$p")"
  70. case "$r" in
  71. "$ROOT"/*) ;;
  72. *) die "/$p on the target resolves to $r, outside $ROOT (absolute symlink?); not writing there" ;;
  73. esac
  74. done
  75. for p in "boot/opi0/$RELEASE" "lib/modules/$RELEASE"; do
  76. [ -d "$ROOT/$p" ] || continue
  77. r="$(find "$ROOT/$p" -type l -lname '/*' -print -quit)"
  78. [ -z "$r" ] || die "absolute symlink $r in the target; not extracting over it"
  79. done
  80. fi
  81. # Armbian packages that can undo this setup on an apt upgrade. Warnings only.
  82. check_target_packages() {
  83. local st unheld
  84. st="$(trun 'cat var/lib/dpkg/status 2>/dev/null' | awk 'BEGIN { RS = ""; FS = "\n" }
  85. { p = s = ""
  86. for (i = 1; i <= NF; i++) {
  87. if ($i ~ /^Package: /) p = substr($i, 10)
  88. else if ($i ~ /^Status: /) s = substr($i, 9)
  89. }
  90. if (s ~ / installed$/) print p, s }' || true)"
  91. [ -n "$st" ] || return 0
  92. unheld="$(awk '$1 ~ /^linux-(image|dtb|u-boot)-/ && $2 != "hold" { print $1 }' <<<"$st" | tr '\n' ' ')"
  93. [ -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
  94. ! grep -q '^u-boot-menu ' <<<"$st" ||
  95. echo "WARNING: u-boot-menu is installed; it rewrites /boot/extlinux/extlinux.conf on kernel changes. Remove it." >&2
  96. trun 'test -e lib/firmware/xr819/fw_xr819.bin' ||
  97. echo "WARNING: /lib/firmware/xr819/ is missing (package armbian-firmware); the XR819 Wi-Fi will not work" >&2
  98. local alg
  99. alg="$(trun 'cat etc/default/armbian-zram-config 2>/dev/null' |
  100. sed -n "s/^[[:space:]]*SWAP_ALGORITHM=[\"']*\([a-z0-9-]*\).*/\1/p" | tail -1 || true)"
  101. case "$alg" in
  102. ""|lz4|zstd|lzo|lzo-rle) ;;
  103. *) echo "WARNING: armbian-zram-config uses SWAP_ALGORITHM=$alg, which files/kernel.config does not enable" >&2 ;;
  104. esac
  105. }
  106. check_target_packages
  107. info "installing $RELEASE to $TARGET"
  108. # --keep-directory-symlink: /lib is a symlink to usr/lib on merged-/usr systems
  109. case "$TARGET" in
  110. ssh:*) ssh "$HOST" "tar --keep-directory-symlink --no-same-owner -xzf - -C /" < "$TARBALL" ;;
  111. *) tar --keep-directory-symlink -xzf "$TARBALL" -C "$ROOT" ;;
  112. esac
  113. # Releases on the target, highest version first (by name, not by date).
  114. releases="$(trun 'ls -1 boot/opi0 2>/dev/null' | sort -rV)"
  115. [ -n "$releases" ] || die "no kernels in /boot/opi0 on the target"
  116. # The last console= becomes /dev/console (emergency shell, init output). Keep
  117. # the serial console there even if KERNEL_CONSOLE is overridden in a different
  118. # order; the relative order of everything else is kept.
  119. console_args() {
  120. local a serial="" rest=""
  121. for a in $KERNEL_CONSOLE; do
  122. case "$a" in
  123. console=ttyS*) serial="${serial:+$serial }$a" ;;
  124. *) rest="${rest:+$rest }$a" ;;
  125. esac
  126. done
  127. echo "$rest${rest:+${serial:+ }}$serial"
  128. }
  129. cmdline_console="$(console_args)"
  130. case "$cmdline_console" in
  131. *console=ttyS*) ;;
  132. *) echo "WARNING: no console=ttyS* in KERNEL_CONSOLE; no serial emergency console" >&2 ;;
  133. esac
  134. gen_extlinux() {
  135. local r ov
  136. echo "# Generated by install-kernel.sh (OPi0 build) - rerun it to update."
  137. echo "# Booted by mainline U-Boot; no initramfs."
  138. echo "menu title Orange Pi Zero"
  139. echo "timeout 20"
  140. echo "default $(head -1 <<<"$releases")"
  141. for r in $releases; do
  142. echo
  143. echo "label $r"
  144. echo " kernel /boot/opi0/$r/zImage"
  145. echo " fdt /boot/opi0/$r/$BOOT_DTB"
  146. if [ -n "$BOOT_OVERLAYS" ]; then
  147. echo -n " fdtoverlays"
  148. for ov in $BOOT_OVERLAYS; do echo -n " /boot/opi0/$r/overlay/$ov.dtbo"; done
  149. echo
  150. fi
  151. echo " append root=PARTUUID=$partuuid rootfstype=$fstype rootwait rw $cmdline_console panic=10 consoleblank=0 $KERNEL_CMDLINE_EXTRA"
  152. done
  153. }
  154. trun 'mkdir -p boot/extlinux && { [ ! -f boot/extlinux/extlinux.conf ] || cp boot/extlinux/extlinux.conf boot/extlinux/extlinux.conf.bak; }'
  155. gen_extlinux | tput_file boot/extlinux/extlinux.conf
  156. trun 'sync'
  157. info "installed; /boot/extlinux/extlinux.conf:"
  158. trun 'cat boot/extlinux/extlinux.conf'