Skip to content

Instantly share code, notes, and snippets.

@fathonix
Last active September 16, 2023 07:58
Show Gist options
  • Save fathonix/4cba388b75b6b9c4b741da4b8eaa0e46 to your computer and use it in GitHub Desktop.
Save fathonix/4cba388b75b6b9c4b741da4b8eaa0e46 to your computer and use it in GitHub Desktop.
Modified update-extlinux script to update syslinux configuration
#!/bin/sh
# Taken from https://git.adelielinux.org/adelie/packages/-/blob/abefa433c8e2199520e5a763e415e7ed4d60f551/legacy/syslinux/update-extlinux
# Modified for Legacy & UEFI Syslinux
version=
default=0
timeout=5
verbose=0
conf=/boot/syslinux/syslinux.cfg
myconf=/etc/update-syslinux.conf
# read in syslinux settings
if [ -f "$myconf" ]; then
. $myconf
fi
everbose() {
if [ "$verbose" = "0" ]; then
return
fi
echo $*
}
ewarn() {
echo "WARNING:" $@ >&2
}
eerror() {
echo "ERROR:" $@ >&2
return 1
}
usage() {
echo "usage: $0 [-v|--verbose] [--warn-only]"
}
while [ $# -gt 0 ]; do
opt="$1"
shift
case "$opt" in
-v|--verbose)
verbose=1
;;
--warn-only)
warn_only=1
;;
--)
break
;;
-*)
usage
exit 1
;;
esac
done
everbose "Updating syslinux configuration."
if [ "x$root" = "x" ]; then
ewarn "Root device is not specified in $myconf."
blkid_export=$(findmnt -n -o SOURCE /)
if [ -n "$blkid_export" ]; then
export $blkid_export
fi
if [ -z "$UUID" ]; then
# try parse /proc/mount for mounted /
dev=$(awk '$2 == "/" {dev=$1} END {print dev}' /proc/mounts)
if [ -n "$dev" ]; then
blkid_export=$(blkid -o export $dev)
if [ -n "$blkid_export" ]; then
export "$blkid_export"
fi
fi
fi
if [ -z "$UUID" ]; then
if [ -z "$dev" ]; then
if [ -n "$warn_only" ]; then
ewarn "Failed to detect root device. syslinux.conf is not updated"
exit 0
else
eerror "Failed to detect root device"
exit 1
fi
else
root=$dev
fi
else
root=UUID=$UUID
fi
everbose "Root device is: $root"
fi
rtimeout=$(( ${timeout} * 10 ))
syslinux_menu=menu.c32
menu_hidden=
# vesa menu has been requested?
if [ "$vesa_menu" = "1" ]; then
syslinux_menu=vesamenu.c32
fi
umask 0022
rm -f $conf.new
echo "# Generated by update-syslinux $version" > $conf.new
if [ -n "$serial_port" ]; then
echo "SERIAL $serial_port ${serial_baud:-115200}" >> $conf.new
fi
echo "DEFAULT $syslinux_menu" >> $conf.new
echo "PROMPT 0" >> $conf.new
echo "MENU TITLE Adelie $(uname -s) Boot Menu" >> $conf.new
if [ "$hidden" = "1" ]; then
echo "MENU HIDDEN" >> $conf.new
fi
echo "MENU AUTOBOOT Adelie will be booted automatically in # seconds." >> $conf.new
echo "TIMEOUT $rtimeout" >> $conf.new
lst=0
if [ -f "/boot/xen.gz" ]; then
for kernel in $(find /boot -name "vmlinuz-*" -type f); do
tag=$(basename $kernel | cut -b9-)
everbose "Found Xen hypervisor: /boot/xen.gz, kernel: $kernel"
if [ -f "/boot/initramfs-$tag" ]; then
everbose "Found initramfs: /boot/initramfs-$tag"
initramfs="initramfs-$tag"
else
initramfs=
fi
label=xen-$(grep -w -l $tag /usr/share/kernel/*/kernel.release \
| cut -d/ -f5)
if [ "$label" = "xen-" ]; then
label=xen-$lst
fi
echo "LABEL $label" >> $conf.new
if [ "$label" = "$default" ]; then
echo " MENU DEFAULT" >> $conf.new
fi
echo " MENU LABEL Xen + Linux $tag" >> $conf.new
echo " COM32 mboot.c32" >> $conf.new
echo " APPEND xen.gz $xen_opts --- $(basename $kernel) root=$root modules=${modules}${TYPE:+,$TYPE} $default_kernel_opts --- $initramfs" >> $conf.new
echo "" >> $conf.new
lst=$(($lst + 1))
done
fi
for kernel in $(find /boot -name "vmlinuz*" -type f); do
case $kernel in
*vmlinuz) tag=vanilla;;
*vmlinuz-*) tag=$(basename $kernel | cut -b9-);;
*) continue;;
esac
everbose "Found kernel: $kernel"
label=$(grep -w -l $tag /usr/share/kernel/*/kernel.release | cut -d/ -f5)
if [ -z "$label" ]; then
if [ "$tag" = vanilla ]; then
label="vanilla"
else
label=$lst
fi
fi
echo "LABEL $label" >> $conf.new
if [ "$label" = "$default" ]; then
echo " MENU DEFAULT" >> $conf.new
fi
echo " MENU LABEL Linux $tag" >> $conf.new
echo " LINUX $(basename $kernel)" >> $conf.new
if [ -f "/boot/initramfs-$tag" ]; then
everbose "Found initramfs: /boot/initramfs-$tag"
echo " INITRD initramfs-$tag" >> $conf.new
fi
echo " APPEND root=$root modules=${modules}${TYPE:+,$TYPE} $default_kernel_opts" >> $conf.new
echo "" >> $conf.new
lst=$(($lst + 1))
done
if [ -n "$password" ]; then
echo "NOESCAPE 1" >> $conf.new
echo "MENU MASTER PASSWD $password" >> $conf.new
echo "" >> $conf.new
chmod o-r $conf.new
fi
everbose "$lst entries found."
for entry in /etc/update-syslinux.d/*; do
[ -f "$entry" ] && { cat $entry; echo ""; } >> $conf.new
done
echo "MENU SEPARATOR" >> $conf.new
echo "" >> $conf.new
if [ -f "/boot/hdt.c32" ]; then
everbose "Found Hardware Detection Tool: /boot/hdt.c32"
echo "LABEL hdt" >> $conf.new
echo " MENU LABEL Hardware info" >> $conf.new
if [ -n "$password" ]; then
echo " MENU PASSWD" >> $conf.new
fi
echo " COM32 hdt.c32" >> $conf.new
if [ -f "/boot/memtest" ]; then
everbose "Found memtest86+: /boot/memtest"
echo " APPEND memtest=memtest" >> $conf.new
fi
echo "" >> $conf.new
elif [ -f "/boot/memtest" ]; then
everbose "Found memtest86+: /boot/memtest"
echo "LABEL memtest" >> $conf.new
echo " MENU LABEL Memtest86+" >> $conf.new
echo " KERNEL memtest" >> $conf.new
echo "" >> $conf.new
fi
for i in reboot poweroff; do
[ -f "/boot/$i.c32" ] || continue
everbose "Found $i"
# make first char capital
cap=$( echo $i | awk '{sub(".", substr(toupper($0),1,1), $0); print}' )
echo "LABEL $i" >> $conf.new
echo " MENU LABEL $cap" >> $conf.new
echo " COM32 $i.c32" >> $conf.new
echo "" >> $conf.new
done
if cmp -s $conf.new $conf; then
everbose "Configuration unchanged."
rm $conf.new
fi
if [ "$overwrite" != "1" ]; then
exit 0
elif [ -f "$conf.new" ]; then
# keep a backup just in case
if [ -f "$conf" ]; then
mv $conf $conf.old
fi
mv $conf.new $conf
fi
# configuration for extlinux config builder
# overwrite
# Overwrite current /boot/extlinux.conf. If this is not '1' we will only
# write to /boot/extlinux.conf.new
overwrite=1
# vesa_menu
# use fancy vesa menu (vesamenu.c32) menus, won't work with serial
# vesa_menu=1
# default_kernel_opts
# default kernel options
default_kernel_opts=rw
# modules
# modules which should be loaded before pivot_root
modules=
#sd-mod,usb-storage,ext4
# root
# root device - if not specified, will be guessed using
# findmnt -n -o SOURCE /
root=
# verbose
# if set to non-zero, update-extlinux will be a lot more verbose.
verbose=0
# hidden
# if set to non-zero, the boot menu will be hidden by default.
hidden=1
# timeout
# number of seconds to wait before booting default
timeout=5
# default
# default kernel to boot
default=6.1.0-12-amd64
# serial_port
# serial port number - if not specified, serial console will be disabled
serial_port=
# serial_baud
# the baudrate for the serial port. Will use 115200 if unset
serial_baud=115200
# xen_opts
# options to hand to xen hypervisor, useful ones are:
# dom0_mem=256M (give domain-0 environment 256M ram)
xen_opts=dom0_mem=256M
# if you copy /usr/share/syslinux/reboot.c32 to /boot/, a menu entry
# will be auto-generated for it
# if you copy hdt.c32, libgpl.c32, and libmenu.c32 from /usr/share/syslinux/
# to /boot/, a menu entry will be auto-generated for HDT
# if you download and install /boot/memtest, then if HDT is present it
# will use it, else a separate menu entry will be auto-generated for
# memtest
# optional password
# you can generate a SHA512 password using: mkpasswd
#
# if you assign a password, you should make this file world-unreadable
#
# if a password is assigned, the menu entries can't be edited at boot
# time, and HDT if present is password-protected
#
# you can also include "MENU PASSWD" in any custom entries you have in
# /etc/update-extlinux.d/
password=''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment