|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +## This script can be used by generate-zbm to update a syslinux configuration |
| 4 | +## after a ZFSBootMenu initramfs and kernel pair is created. The file system |
| 5 | +## from which syslinux will run, which can be ext2/3/4 (make sure to disable |
| 6 | +## the `64bit` feature), NTFS, UFS/FFS, XFS or btrfs, must be mounted when this |
| 7 | +## hook runs. To ensure that syslinux can find the ZFSBootMenu kernel and |
| 8 | +## initramfs, make sure to set the `Components.ImageDir` option in your |
| 9 | +## ZFSBootMenu configuration to a path within the syslinux file system. |
| 10 | +## |
| 11 | +## Ensure the file is executable and place it in the directory specified by the |
| 12 | +## `Global.PostHooksDir` option in your ZFSBootMenu configuration file. |
| 13 | +## |
| 14 | +## This script uses the SYSLINUX_ROOT to refer to the mount point of the |
| 15 | +## syslinux filesystem and KERNEL_PATH to be the subdirectory, relative to |
| 16 | +## SYSLINUX_ROOT, where ZFSBootMenu will install initramfs and kernel pairs. |
| 17 | +## Kernels are expected to have file names of the form |
| 18 | +## |
| 19 | +## ${KERNEL_PREFIX}[-<version>] |
| 20 | +## |
| 21 | +## and a corresponding initramfs image |
| 22 | +## |
| 23 | +## initramfs[-<version>].img |
| 24 | +## |
| 25 | +## When creating syslinux entries, the variable ZBM_KCL_ARGS defines the |
| 26 | +## command-line arguments that will be passed to each ZFSBootMenu kernel. |
| 27 | +## |
| 28 | +## Customized headers and entries can be added before any automatic entries by |
| 29 | +## creating a directory and dumping syslinux configuration snippets to it. The |
| 30 | +## location of this directory is defined by the SYSLINUX_CONFD variable. |
| 31 | +## |
| 32 | +## Change the above variables as necessary to reflect your configuration. |
| 33 | + |
| 34 | +## By default, syslinux is on /boot/syslinux |
| 35 | +SYSLINUX_ROOT="/boot/syslinux" |
| 36 | +## By default, kernel and initramfs pairs will be in /boot/syslinux/zbm |
| 37 | +KERNEL_PATH="zbm" |
| 38 | +## By default, kernels are files starting with "vmlinuz" |
| 39 | +KERNEL_PREFIX="vmlinuz" |
| 40 | + |
| 41 | +# Set to a directory containing configuration snippets, if desired |
| 42 | +SYSLINUX_CONFD="" |
| 43 | + |
| 44 | +# By default, no arguments are passed to the kernel |
| 45 | +ZBM_KCL_ARGS="" |
| 46 | + |
| 47 | +if [ ! -d "${SYSLINUX_ROOT}" ]; then |
| 48 | + echo "ERROR: syslinux root '${SYSLINUX_ROOT}' does not exist" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +KERNEL_DIR="${SYSLINUX_ROOT}/${KERNEL_PATH}" |
| 53 | +KERNEL_DIR="${KERNEL_DIR%/}" |
| 54 | +if [ ! -d "${KERNEL_DIR}" ]; then |
| 55 | + echo "ERROR: kernel path '${KERNEL_DIR}' does not exist" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +SYSLINUX_CFG= |
| 60 | +cleanup() { |
| 61 | + [ -n "${SYSLINUX_CFG}" ] && rm -f "${SYSLINUX_CFG}" |
| 62 | + unset SYSLINUX_CFG |
| 63 | +} |
| 64 | + |
| 65 | +trap cleanup EXIT INT TERM QUIT |
| 66 | + |
| 67 | +if ! SYSLINUX_CFG="$(mktemp)"; then |
| 68 | + echo "ERROR: failed to make a temporary syslinux.cfg" |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +# Populate the header with configuration snippets |
| 73 | +FOUND_SNIPPETS= |
| 74 | +if [ -d "${SYSLINUX_CONFD}" ]; then |
| 75 | + readarray -t SNIPPETS < <(printf '%s\n' "${SYSLINUX_CONFD}"/* | sort) |
| 76 | + for snip in "${SNIPPETS[@]}"; do |
| 77 | + [ -r "${snip}" ] || continue |
| 78 | + |
| 79 | + FOUND_SNIPPETS="yes" |
| 80 | + cat < "${snip}" >> "${SYSLINUX_CFG}" |
| 81 | + done |
| 82 | +fi |
| 83 | + |
| 84 | +# Use a default header if no configuration snippets are defined |
| 85 | +if [ -z "${FOUND_SNIPPETS}" ]; then |
| 86 | + # Write the standard configuration header |
| 87 | + cat > "${SYSLINUX_CFG}" <<-EOF |
| 88 | + UI menu.c32 |
| 89 | + PROMPT 0 |
| 90 | +
|
| 91 | + MENU TITLE Choose a ZFSBootMenu image to boot |
| 92 | + TIMEOUT 50 |
| 93 | + EOF |
| 94 | +fi |
| 95 | + |
| 96 | +# Sort list of candidate kernels by version, newest first |
| 97 | +readarray -t KERNELS < <(printf '%s\n' "${KERNEL_DIR}/${KERNEL_PREFIX}"* | sort -V -r) |
| 98 | + |
| 99 | +# Identify each kernel/initramfs pair |
| 100 | +# The first in sort order will be the default |
| 101 | +DEFAULT_SET= |
| 102 | +for kern in "${KERNELS[@]}"; do |
| 103 | + # Make sure file exists and strip leading path |
| 104 | + [ -f "${kern}" ] || continue |
| 105 | + kern="${kern##*/}" |
| 106 | + |
| 107 | + # Strip the kernel prefix to look for matching initramfs |
| 108 | + version="${kern#"${KERNEL_PREFIX}"}" |
| 109 | + initramfs="initramfs${version}.img" |
| 110 | + [ -f "${KERNEL_DIR}/${initramfs}" ] || continue |
| 111 | + |
| 112 | + zbmlabel="zfsbootmenu${version//[[:space:]]/}" |
| 113 | + |
| 114 | + if [ -z "${DEFAULT_SET}" ]; then |
| 115 | + echo "DEFAULT ${zbmlabel}" >> "${SYSLINUX_CFG}" |
| 116 | + DEFAULT_SET="yes" |
| 117 | + fi |
| 118 | + |
| 119 | + cat >> "${SYSLINUX_CFG}" <<-EOF |
| 120 | +
|
| 121 | + LABEL ${zbmlabel} |
| 122 | + MENU LABEL ZFSBootMenu (${version#-}) |
| 123 | + LINUX ${KERNEL_PATH:+/${KERNEL_PATH}}/${kern} |
| 124 | + INITRD ${KERNEL_PATH:+/${KERNEL_PATH}}/${initramfs} |
| 125 | + EOF |
| 126 | + |
| 127 | + if [ -n "${ZBM_KCL_ARGS}" ]; then |
| 128 | + echo "APPEND ${ZBM_KCL_ARGS}" >> "${SYSLINUX_CFG}" |
| 129 | + fi |
| 130 | +done |
| 131 | + |
| 132 | +if [ -z "${DEFAULT_SET}" ]; then |
| 133 | + echo "ERROR: failed to find any kernels" |
| 134 | + exit 1 |
| 135 | +fi |
| 136 | + |
| 137 | +cp "${SYSLINUX_CFG}" "${SYSLINUX_ROOT}/syslinux.cfg" |
0 commit comments