install-extlinux.sh 6.0 KB

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