41d91a7
#!/bin/bash
41d91a7
947ffdf
# GRUB 2 template
fd66414
G2TEMPL="20_memtest86+"
e7f3321
05c6bab
# GRUB 2 environment file
05c6bab
CONF_FILE="/etc/memtest86+.conf"
05c6bab
ab89f38
# GRUB2 configuration file
ab89f38
GRUB2CFG="/boot/grub2/grub.cfg"
ab89f38
05c6bab
# GRUB2 environment variable to control image type
05c6bab
CONF_VAR="INSTALL_ELF"
05c6bab
05c6bab
# whether to install ELF image
05c6bab
ELF=1
05c6bab
947ffdf
if [ "$1" = "--help" -o "$1" = "-h" ]; then
947ffdf
    cat <<:EOF
48b4c45
Usage:
48b4c45
  memtest-setup [-b|-e|-h]
48b4c45
  memtest-setup [--bin|--elf|--help]
48b4c45
48b4c45
The memtest-setup utility installs Memtest86+ into GRUB 2 boot loader menu.
48b4c45
It installs GRUB 2 template into /etc/grub.d directory.
07291e5
48b4c45
GRUB 2 configuration file needs to be regenerated manually by running:
947ffdf
48b4c45
  grub2-mkconfig -o $GRUB2CFG
947ffdf
48b4c45
This is not done automatically because it could overwrite any custom changes
48b4c45
in GRUB 2 configuration file.
07291e5
48b4c45
Options:
48b4c45
  -b, --bin     Install a binary Memtest86+ image.
48b4c45
  -e, --elf     Install an ELF Memtest86+ image (the default).
48b4c45
  -h, --help    Print a help message and exit.
07291e5
947ffdf
:EOF
947ffdf
    exit 0
947ffdf
fi
947ffdf
05c6bab
[ "$1" = "-b" -o "$1" = "--bin" ] && ELF=0
05c6bab
[ "$1" = "-e" -o "$1" = "--elf" ] && ELF=1
05c6bab
41d91a7
if [ -d /sys/firmware/efi ]; then
ab89f38
    echo "ERROR: memtest86+ does not support EFI platforms."
41d91a7
    exit 254
41d91a7
fi
41d91a7
48b4c45
if [ ! -r "$GRUB2CFG" ]; then
ab89f38
   echo "ERROR: unable to read grub configuration file. Do you have enough permissions?"
ab89f38
   echo "Try to run as root."
ab89f38
   exit 249
ab89f38
fi
ab89f38
48b4c45
# install GRUB 2 template
48b4c45
if [ ! -d /etc/grub.d ]; then
48b4c45
    echo "ERROR: unable to find /etc/grub.d"
48b4c45
    exit 253
48b4c45
fi
48b4c45
if [ ! -r /usr/share/memtest86+/$G2TEMPL ]; then
48b4c45
    echo "ERROR: unable to find GRUB 2 template."
48b4c45
    exit 251
48b4c45
fi
48b4c45
if ! cp /usr/share/memtest86+/$G2TEMPL /etc/grub.d; then
48b4c45
    echo "ERROR: unable to copy GRUB 2 template, do you have write permission to"
48b4c45
    echo "/etc/grub.d?"
48b4c45
    # EX_IOERR
48b4c45
    exit 74
48b4c45
fi
48b4c45
if [ ! -w "$CONF_FILE" ]
48b4c45
then
48b4c45
    echo "ERROR: file '$CONF_FILE' is not writable."
48b4c45
    exit 250
41d91a7
fi
48b4c45
chmod a+x /etc/grub.d/$G2TEMPL
48b4c45
echo "GRUB 2 template installed."
48b4c45
echo "Do not forget to regenerate your grub.cfg by:"
48b4c45
echo "  # grub2-mkconfig -o $GRUB2CFG"
41d91a7
05c6bab
# update/add configuration variable to the configuration file
05c6bab
if grep -q "^\s*$CONF_VAR\s*=" "$CONF_FILE"
05c6bab
then
05c6bab
    sed -i "/^\s*$CONF_VAR\s*=/ s/\(\s*$CONF_VAR\s*=[\"']\?\)[^\"']*\([\"']\?\s*\)/\1${ELF}\2/g" "$CONF_FILE"
05c6bab
else
05c6bab
    echo "$CONF_VAR=\"$ELF\"" >> "$CONF_FILE"
05c6bab
fi
05c6bab
41d91a7
echo "Setup complete."