#!/bin/bash # # Copyright Amazon.com, Inc. or its affiliates. # SPDX-License-Identifier: GPL-3.0 # # Script for setting up grub2 on Amazon Linux systems # # This will install the BIOS grub on the BIOS boot partition if present # and will setup the stub config on the EFI partition for EFI grub set -e VERBOSE=false YES=false HAS_EFI=false HAS_BIOS=false EFI_MOUNT=/boot/efi BLKID_FILE=/dev/null UPDATECHECK=false log() { if "$VERBOSE" = true; then echo $@ fi } err() { echo $@ >&2 exit 1 } function ask() { if [ "$YES" = false ]; then echo "" while true; do read -p "$@" yn case $yn in [Yy]* ) break;; [Nn]* ) exit 0;; esac done fi } uppercase() { echo $@ | tr '[a-z]' '[A-Z]' } copy() { log "Copying $1 to $2" cp "$1" "$2" } function do_setup_bios() { # Nothing to do when just doing an update check if [ "$UPDATECHECK" = true ]; then return fi echo "Setting up grub2 for BIOS boot" ask "Do you want to overwrite grub on ${BOOT_DEV_PARENT} ? (y/n) " grub2-install --target=i386-pc ${BOOT_DEV_PARENT} } function do_setup_efi() { # Create the stub .cfg file in EFI/amzn that references the real one EFI_DIST="$EFI_MOUNT"/EFI/amzn STUB_CFG="$EFI_DIST/grub.cfg" # On update check, only do this if it doesn't already exist if [ "$UPDATECHECK" = true ]; then if [ -f $STUB_CFG ]; then return; fi fi echo "Creating $STUB_CFG..." # Get UUID of filesystem with /boot on it BOOT_UUID=$(grub2-probe /boot -t fs_uuid) # Make mountpoint-relative path to /boot/grub2 BOOT_RPATH=$(grub2-mkrelpath /boot/grub2) # Create /boot/efi/EFI/amzn in case it doesn't already mkdir -p -m 700 "$EFI_DIST" # Generate the "stub" config file that locates the real one and # loads it cat >"$STUB_CFG" <