2012-11-09 11:04:13 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2013-02-13 22:16:00 +00:00
|
|
|
# Configure grub. Note that the various conditionals here are to handle
|
|
|
|
# different distributions gracefully.
|
2012-11-09 11:04:13 +00:00
|
|
|
|
2014-09-04 04:56:29 +00:00
|
|
|
if [ ${DIB_DEBUG_TRACE:-1} -gt 0 ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
set -eu
|
2014-04-03 02:24:15 +00:00
|
|
|
set -o pipefail
|
2012-11-09 11:04:13 +00:00
|
|
|
|
2017-06-06 02:09:24 +00:00
|
|
|
BOOT_DEV=$IMAGE_BLOCK_DEVICE
|
|
|
|
|
|
|
|
# All available devices, handy for some bootloaders...
|
|
|
|
declare -A DEVICES
|
|
|
|
eval DEVICES=( $IMAGE_BLOCK_DEVICES )
|
2013-08-27 10:00:03 +00:00
|
|
|
|
|
|
|
function install_extlinux {
|
2016-01-14 02:01:14 +00:00
|
|
|
install-packages -m bootloader extlinux
|
2013-08-27 10:00:03 +00:00
|
|
|
|
|
|
|
echo "Installing Extlinux..."
|
|
|
|
|
2014-07-02 10:38:37 +00:00
|
|
|
# Find and install mbr.bin
|
|
|
|
for MBR in /usr/share/syslinux/mbr.bin /usr/lib/syslinux/mbr.bin \
|
|
|
|
/usr/lib/extlinux/mbr.bin /usr/lib/EXTLINUX/mbr.bin ; do
|
|
|
|
if [ -f $MBR ]; then
|
|
|
|
break
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
2014-07-02 10:38:37 +00:00
|
|
|
done
|
|
|
|
if [ ! -f $MBR ]; then
|
|
|
|
echo "mbr.bin (from EXT/SYSLINUX) not found."
|
|
|
|
exit 1
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
dd if=$MBR of=$BOOT_DEV
|
|
|
|
|
2014-07-02 10:38:37 +00:00
|
|
|
# Find any pre-created extlinux install directory
|
|
|
|
for EXTDIR in /boot/extlinux /boot/syslinux ; do
|
|
|
|
if [ -d $EXTDIR ] ; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ ! -d $EXTDIR ] ; then
|
|
|
|
# No install directory found so default to /boot/syslinux
|
|
|
|
EXTDIR=/boot/syslinux
|
|
|
|
mkdir -p $EXTDIR
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Finally install extlinux
|
|
|
|
extlinux --install $EXTDIR
|
2013-08-27 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function install_grub2 {
|
|
|
|
|
2013-09-14 18:06:08 +00:00
|
|
|
# Check for offline installation of grub
|
|
|
|
if [ -f "/tmp/grub/install" ] ; then
|
|
|
|
source /tmp/grub/install
|
2017-04-12 05:26:27 +00:00
|
|
|
# Right now we can't use pkg-map to branch by arch, so tag an architecture
|
|
|
|
# specific virtual package so we can install the rigth thing based on
|
|
|
|
# distribution.
|
2015-02-05 22:09:36 +00:00
|
|
|
elif [[ "$ARCH" =~ "ppc" ]]; then
|
2017-04-12 05:26:27 +00:00
|
|
|
install-packages -m bootloader grub-ppc64
|
2013-09-14 18:06:08 +00:00
|
|
|
else
|
2016-01-14 02:01:14 +00:00
|
|
|
install-packages -m bootloader grub-pc
|
2013-09-14 18:06:08 +00:00
|
|
|
fi
|
2013-08-27 10:00:03 +00:00
|
|
|
|
|
|
|
# XXX: grub-probe on the nbd0/loop0 device returns nothing - workaround, manually
|
|
|
|
# specify modules. https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1073731
|
2017-05-18 18:09:32 +00:00
|
|
|
GRUBNAME=$(type -p grub-install) || echo "trying grub2-install"
|
2013-08-27 10:00:03 +00:00
|
|
|
if [ -z "$GRUBNAME" ]; then
|
2017-05-18 18:09:32 +00:00
|
|
|
GRUBNAME=$(type -p grub2-install)
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If no GRUB2 is found, fallback to extlinux
|
|
|
|
if [ -z "$GRUBNAME" ] || [ $($GRUBNAME --version | grep "0.97" | wc -l) -ne 0 ]; then
|
|
|
|
echo "No GRUB2 found. Fallback to Extlinux..."
|
|
|
|
install_extlinux
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Installing GRUB2..."
|
|
|
|
|
2015-10-18 02:26:31 +00:00
|
|
|
# We need --force so grub does not fail due to being installed on the
|
|
|
|
# root partition of a block device.
|
|
|
|
GRUB_OPTS=${GRUB_OPTS:-"--force"}
|
2013-08-27 10:00:03 +00:00
|
|
|
# XXX: This is buggy:
|
|
|
|
# - --target=i386-pc is invalid for non-i386/amd64 architectures
|
|
|
|
# - and for UEFI too.
|
|
|
|
# GRUB_OPTS="$GRUB_OPTS --target=i386-pc"
|
2014-03-27 16:57:18 +00:00
|
|
|
if [[ ! $GRUB_OPTS == *--target* ]] && [[ $($GRUBNAME --version) =~ ' 2.' ]]; then
|
2014-05-02 17:02:38 +00:00
|
|
|
# /sys/ comes from the host machine. If the host machine is using EFI
|
|
|
|
# but the image being built doesn't have EFI boot-images installed we
|
|
|
|
# should set the --target to use a BIOS-based boot-image.
|
|
|
|
#
|
|
|
|
# * --target tells grub what's the target platform
|
|
|
|
# * the boot images are placed in /usr/lib/grub/<cpu>-<platform>
|
|
|
|
# * i386-pc is used for BIOS-based machines
|
|
|
|
# http://www.gnu.org/software/grub/manual/grub.html#Installation
|
|
|
|
#
|
|
|
|
if [ -d /sys/firmware/efi ]; then
|
|
|
|
if [ ! -d /usr/lib/grub/*-efi ]; then
|
|
|
|
case $ARCH in
|
|
|
|
"x86_64"|"amd64")
|
|
|
|
GRUB_OPTS="$GRUB_OPTS --target=i386-pc"
|
|
|
|
;;
|
|
|
|
"i386")
|
|
|
|
target=i386-pc
|
|
|
|
if [ -e /proc/device-tree ]; then
|
|
|
|
for x in /proc/device-tree/*; do
|
|
|
|
if [ -e "$x" ]; then
|
|
|
|
target="i386-ieee1275"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
GRUB_OPTS="$GRUB_OPTS --target=$target"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
2013-07-12 16:50:39 +00:00
|
|
|
fi
|
|
|
|
|
2015-02-05 22:09:36 +00:00
|
|
|
if [[ "$ARCH" =~ "ppc" ]] ; then
|
2017-04-12 05:26:27 +00:00
|
|
|
# For PPC (64-Bit regardless of Endian-ness), we use the "boot"
|
|
|
|
# partition as the one to point grub-install to, not the loopback
|
|
|
|
# device. ppc has a dedicated PReP boot partition.
|
|
|
|
# For grub2 < 2.02~beta3 this needs to be a /dev/mapper/... node after
|
|
|
|
# that a dev/loopXpN node will work fine.
|
2017-06-07 01:10:18 +00:00
|
|
|
$GRUBNAME --modules="part_msdos" $GRUB_OPTS ${DEVICES[boot]} --no-nvram
|
2015-02-05 22:09:36 +00:00
|
|
|
else
|
|
|
|
$GRUBNAME --modules="biosdisk part_msdos" $GRUB_OPTS $BOOT_DEV
|
|
|
|
fi
|
2013-07-12 16:50:39 +00:00
|
|
|
|
2013-08-27 10:00:03 +00:00
|
|
|
# This might be better factored out into a per-distro 'install-bootblock'
|
|
|
|
# helper.
|
2013-12-12 11:55:39 +00:00
|
|
|
if [ -d /boot/grub2 ]; then
|
2014-05-02 17:02:38 +00:00
|
|
|
GRUB_CFG=/boot/grub2/grub.cfg
|
2013-12-12 11:55:39 +00:00
|
|
|
elif [ -d /boot/grub ]; then
|
2014-05-02 17:02:38 +00:00
|
|
|
GRUB_CFG=/boot/grub/grub.cfg
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
2013-07-12 16:15:08 +00:00
|
|
|
|
2016-11-25 00:08:11 +00:00
|
|
|
# Override the root device to the default label, and disable uuid
|
|
|
|
# lookup.
|
|
|
|
echo "GRUB_DEVICE=LABEL=${DIB_ROOT_LABEL}" >> /etc/default/grub
|
|
|
|
echo 'GRUB_DISABLE_LINUX_UUID=true' >> /etc/default/grub
|
2016-12-02 03:08:06 +00:00
|
|
|
echo "GRUB_TIMEOUT=${DIB_GRUB_TIMEOUT:-5}" >>/etc/default/grub
|
2014-08-22 18:12:24 +00:00
|
|
|
echo 'GRUB_TERMINAL="serial console"' >>/etc/default/grub
|
2013-12-12 11:55:39 +00:00
|
|
|
echo 'GRUB_GFXPAYLOAD_LINUX=text' >>/etc/default/grub
|
2017-07-07 17:53:41 +00:00
|
|
|
|
|
|
|
# Serial console on Power is hvc0
|
2017-08-28 07:01:03 +00:00
|
|
|
if [[ "powerpc ppc64 ppc64le" =~ "$ARCH" ]]; then
|
2017-07-07 17:53:41 +00:00
|
|
|
SERIAL_CONSOLE="hvc0"
|
|
|
|
else
|
|
|
|
SERIAL_CONSOLE="ttyS0,115200"
|
|
|
|
fi
|
|
|
|
|
|
|
|
GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=${SERIAL_CONSOLE} no_timer_check"
|
|
|
|
|
|
|
|
echo "GRUB_CMDLINE_LINUX_DEFAULT=\"${GRUB_CMDLINE_LINUX_DEFAULT}\"" >>/etc/default/grub
|
2014-08-22 18:12:24 +00:00
|
|
|
echo 'GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >>/etc/default/grub
|
2017-05-18 18:09:32 +00:00
|
|
|
if type grub2-mkconfig >/dev/null; then
|
2016-08-22 12:09:57 +00:00
|
|
|
GRUB_MKCONFIG="grub2-mkconfig -o $GRUB_CFG"
|
|
|
|
else
|
|
|
|
GRUB_MKCONFIG="grub-mkconfig -o $GRUB_CFG"
|
|
|
|
fi
|
2014-12-04 17:35:21 +00:00
|
|
|
DISTRO_NAME=${DISTRO_NAME:-}
|
|
|
|
case $DISTRO_NAME in
|
|
|
|
'ubuntu'|'debian')
|
2017-03-08 13:39:55 +00:00
|
|
|
sed -i -e "s/\(^GRUB_CMDLINE_LINUX.*\)\"$/\1 ${DIB_BOOTLOADER_DEFAULT_CMDLINE}\"/" /etc/default/grub
|
2014-05-02 17:02:38 +00:00
|
|
|
GRUB_MKCONFIG=update-grub
|
2013-12-12 11:55:39 +00:00
|
|
|
;;
|
2015-03-22 17:45:07 +00:00
|
|
|
'fedora'|'centos7'|'centos')
|
2017-03-08 13:39:55 +00:00
|
|
|
echo "GRUB_CMDLINE_LINUX=\"${DIB_BOOTLOADER_DEFAULT_CMDLINE}\"" >>/etc/default/grub
|
2013-12-12 11:55:39 +00:00
|
|
|
;;
|
2014-12-04 17:35:21 +00:00
|
|
|
'opensuse')
|
2017-03-08 13:39:55 +00:00
|
|
|
sed -i -e "s/\(^GRUB_CMDLINE_LINUX.*\)\"$/\1 ${DIB_BOOTLOADER_DEFAULT_CMDLINE}\"/" /etc/default/grub
|
2013-12-12 11:55:39 +00:00
|
|
|
;;
|
|
|
|
esac
|
2014-07-17 09:52:34 +00:00
|
|
|
|
|
|
|
# os-prober leaks /dev/sda into config file in dual-boot host
|
|
|
|
# Disable grub-os-prober to avoid the issue while running
|
|
|
|
# grub-mkconfig
|
|
|
|
# Setting a flag to track whether the entry is already there in grub config
|
|
|
|
PROBER_DISABLED=
|
|
|
|
if ! grep -qe "^\s*GRUB_DISABLE_OS_PROBER=true" /etc/default/grub; then
|
|
|
|
PROBER_DISABLED=true
|
|
|
|
echo 'GRUB_DISABLE_OS_PROBER=true' >> /etc/default/grub
|
|
|
|
fi
|
|
|
|
|
2013-12-12 11:55:39 +00:00
|
|
|
$GRUB_MKCONFIG
|
|
|
|
|
2014-07-17 09:52:34 +00:00
|
|
|
# Remove the fix to disable os_prober
|
|
|
|
if [ -n "$PROBER_DISABLED" ]; then
|
|
|
|
sed -i '$d' /etc/default/grub
|
|
|
|
fi
|
|
|
|
|
2013-08-27 10:00:03 +00:00
|
|
|
# grub-mkconfig generates a config with the device in it,
|
|
|
|
# This shouldn't be needed, but old code has bugs
|
2014-12-04 17:35:21 +00:00
|
|
|
DIB_RELEASE=${DIB_RELEASE:-}
|
2017-05-25 22:25:26 +00:00
|
|
|
if [ "$DIB_RELEASE" = 'wheezy' ]; then
|
2014-05-02 17:02:38 +00:00
|
|
|
sed -i "s%search --no.*%%" $GRUB_CFG
|
|
|
|
sed -i "s%set root=.*%set root=(hd0,1)%" $GRUB_CFG
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
2016-11-25 00:08:11 +00:00
|
|
|
|
2015-02-11 14:19:06 +00:00
|
|
|
# Fix efi specific instructions in grub config file
|
|
|
|
if [ -d /sys/firmware/efi ]; then
|
|
|
|
sed -i 's%\(initrd\|linux\)efi /boot%\1 /boot%g' $GRUB_CFG
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
DIB_EXTLINUX=${DIB_EXTLINUX:-0}
|
|
|
|
if [ "$DIB_EXTLINUX" != "0" ]; then
|
|
|
|
install_extlinux
|
|
|
|
else
|
|
|
|
install_grub2
|
2013-05-10 02:59:35 +00:00
|
|
|
fi
|