From 83df9af6f3fdf72d6dc7605b0a74a4c25cd96bfd Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Tue, 26 Jun 2018 10:11:43 +0800 Subject: Use syslinux as legacy BIOS bootloader - Install syslinux by default, fallback to GRUB - Generate syslinux.cfg --- buildlive | 34 +++++++++++++--------------------- functions.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ install_syslinux | 17 +++++++++++++++++ mksyslinux.sh | 31 +++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 21 deletions(-) create mode 100755 install_syslinux create mode 100755 mksyslinux.sh diff --git a/buildlive b/buildlive index aeec242..3de3b03 100755 --- a/buildlive +++ b/buildlive @@ -107,6 +107,8 @@ fi if [[ "$DOWNLOAD_ONLY" == 0 ]]; then UUID="$(findmnt -o UUID --raw --noheadings $ROOTPATH \ || fatalerror "UUID of $ROOTPATH not found, not a mountpoint?")" + BOOTPART="$(findmnt -o SOURCE --raw --noheadings $BOOTPATH \ + || fatalerror "$BOOTPATH seems not a mountpoint!")" # liveusb kernel and data should be placed on one disk # so get DEVNAME by the mountpoint of ROOTPATH is ok DEVNAME="/dev/$(getdiskbypart "/dev/disk/by-uuid/$UUID")" @@ -114,7 +116,8 @@ if [[ "$DOWNLOAD_ONLY" == 0 ]]; then KERNELDIR="$BOOTPATH/liveusb-kernel" DATADIR="$ROOTPATH/liveusb-data" GRUBCFG="$BOOTPATH/grub/grub.cfg" - install -d "$BOOTPATH/grub" + SYSLINUXCFG="$BOOTPATH/syslinux/syslinux.cfg" + install -d "$BOOTPATH/grub" "$BOOTPATH/syslinux" install -d "$KERNELDIR" "$DATADIR" msg "Files will be copy to $ROOTPATH" fi @@ -145,23 +148,8 @@ if [[ "$DOWNLOAD_ONLY" == 1 ]]; then exit 0 fi -echo '# The live USB grub.cfg file' > "$GRUBCFG" - -if [ -z "$TXTMODE" ]; then - cat >> "$GRUBCFG" << 'EOF' -if [ ${grub_platform} == efi ]; then - insmod all_video - insmod font - if loadfont /grub/fonts/unicode.pf2; then - insmod gfxterm - set gfxmode=auto - set gfxpayload=keep - terminal_output gfxterm - fi -fi - -EOF -fi +grubcfg_header > "$GRUBCFG" +syslinux_header > "$SYSLINUXCFG" for i in `seq 1 ${#DISTROLIST[@]}` do @@ -174,14 +162,18 @@ do install_live fi gen_grubcfg "$DISTRO" >> "$GRUBCFG" + gen_syslinux "$DISTRO" >> "$SYSLINUXCFG" done if [ "$GRUBCFG_ONLY" == 1 ]; then - msg 'Only generate grub.cfg, will not install GRUB.' + msg 'Only generate grub.cfg and syslinux.cfg, will not install bootloader.' elif [ "$INSTALL_GRUB" == 1 ]; then - read -p 'Answer Y to install GRUB.[Y/n] ' + read -p 'Answer Y to install bootloader.[Y/n] ' if [[ "$REPLY" =~ ^[Yy]|^$ ]]; then - as-root grub-install --force --boot-directory="$BOOTPATH" --target=i386-pc "$DEVNAME" + if ! as-root ./install_syslinux "$DEVNAME" "$BOOTPART" "$BOOTPATH"; then + msg 'Failed to install syslinux, try to install GRUB as legacy BIOS bootloader.' + as-root grub-install --force --boot-directory="$BOOTPATH" --target=i386-pc "$DEVNAME" + fi as-root grub-install --boot-directory="$BOOTPATH" --efi-directory="$BOOTPATH" --bootloader-id=grub --target=x86_64-efi --removable --no-nvram "$DEVNAME" fi fi diff --git a/functions.sh b/functions.sh index c077231..00e0e0c 100644 --- a/functions.sh +++ b/functions.sh @@ -59,6 +59,19 @@ gen_grubcfg() { fi } +gen_syslinux() { + local entry allentries count name + allentries=("distro/$1/entry"*) + name=$(echo $1|sed 's/\//_/g') + count=0 + for entry in "${allentries[@]}" + do + UUID="$UUID" ISOFILE="$ISOFILE" LABEL="$name$count" \ + ./mksyslinux.sh "$entry" + count=$(($count+1)) + done +} + download_iso() { mkdir -p "$ISOPATH" for url in ${mirrorlist[@]} @@ -119,3 +132,41 @@ as-root() { su -c "$*" fi } + +syslinux_header() { + cat << EOF +UI menu.c32 + +MENU TITLE Live USB +MENU COLOR border 30;44 #40ffffff #a0000000 std +MENU COLOR title 1;36;44 #9033ccff #a0000000 std +MENU COLOR sel 7;37;40 #e0ffffff #20ffffff all +MENU COLOR unsel 37;44 #50ffffff #a0000000 std +MENU COLOR help 37;40 #c0ffffff #a0000000 std +MENU COLOR timeout_msg 37;40 #80ffffff #00000000 std +MENU COLOR timeout 1;37;40 #c0ffffff #00000000 std +MENU COLOR msg07 37;40 #90ffffff #a0000000 std +MENU COLOR tabmsg 31;40 #30ffffff #00000000 std + +EOF +} + +grubcfg_header() { + echo '# The live USB grub.cfg file' + + if [ -z "$TXTMODE" ]; then + cat << 'EOF' +if [ ${grub_platform} == efi ]; then + insmod all_video + insmod font + if loadfont /grub/fonts/unicode.pf2; then + insmod gfxterm + set gfxmode=auto + set gfxpayload=keep + terminal_output gfxterm + fi +fi + +EOF + fi +} diff --git a/install_syslinux b/install_syslinux new file mode 100755 index 0000000..d4fdbf9 --- /dev/null +++ b/install_syslinux @@ -0,0 +1,17 @@ +#!/bin/bash +# Copyright (C) 2018 Iru Cai +# SPDX-License-Identifier: GPL-3.0-or-later + +# Example: ./install_syslinux /dev/sdb /dev/sdb1 /media/boot +set -e + +SYSLINUX_BIOS=/usr/lib/syslinux/bios +DEVNAME="$1" +BOOTPART="$2" +BOOTPATH="$3" + +test -d "$SYSLINUX_BIOS" +syslinux -i "$BOOTPART" +dd bs=440 count=1 conv=notrunc if="$SYSLINUX_BIOS/mbr.bin" of="$DEVNAME" +install -d "$BOOTPATH/syslinux" +cp "$SYSLINUX_BIOS"/*.c32 "$BOOTPATH/syslinux" diff --git a/mksyslinux.sh b/mksyslinux.sh new file mode 100755 index 0000000..f433fe8 --- /dev/null +++ b/mksyslinux.sh @@ -0,0 +1,31 @@ +# Copyright (C) 2018 Iru Cai +# SPDX-License-Identifier: GPL-3.0-or-later +# +# generate syslinux menuentry +# usage: UUID="$UUID" ISOFILE="$ISOFILE" LABEL="$LABEL" \ +# ./mkgrubcfg.sh +# +# variables in entryfile: +# - UUID: the UUID of the partition +# - ISOFILE: the file name of iso (needed to pass to the entry file) +# - LABEL: syslinux label +# +# parameters in entry file: +# - TITLE: GRUB menu entry title +# - KERNEL: path to kernel image +# - INITRD: path to initramfs/initrd image +# - OPTION: kernel command line +# - X64: y/n, indicates whether it's 64-bit + +source "$1" +_INITRD=$(echo ${INITRD[*]}|sed 's/ /,/g') + +cat << EOF +LABEL $LABEL +MENU LABEL $TITLE +LINUX $KERNEL +INITRD $_INITRD +APPEND $OPTION +EOF + +echo -- cgit v1.2.3