Blob Blame History Raw
#!/bin/bash

# package name
PNAME="memtest86+"

# executable
ENAME="elf-memtest86+"

# GRUB 2 template
G2TEMPL="20_memtest86+"

if [ "$1" = "--help" -o "$1" = "-h" ]; then
    cat <<:EOF
usage: memtest-setup [OPTIONS]

This utility installs Memtest86+ into your GRUB boot loader menu.
It supports both GRUB 2 and GRUB Legacy (i.e. GRUB 0.9x).
In case of GRUB 2 it installs GRUB 2 template into /etc/grub.d and GRUB 2
config needs to be regenerated manually by running:

grub2-mkconfig -o /boot/grub2/grub.cfg

This is not done automatically because it could  overwrite any custom changes
in /boot/grub2/grub.cfg.

OPTIONS:
  -h , --help    show this help

:EOF
    exit 0
fi

if [ -d /sys/firmware/efi ]; then
    echo "memtest86+ does not support EFI platforms."
    exit 254
fi

if [ -f /boot/grub2/grub.cfg ]; then
    echo "GRUB 2 detected, installing template..."
    if [ ! -d /etc/grub.d ]; then
        echo "ERROR: unable to find /etc/grub.d"
        exit 253
    fi
    if [ -x  /etc/grub.d/$G2TEMPL ]; then
        echo "GRUB 2 template is already installed. Exiting..."
        exit 252
    fi
    if [ -f /etc/grub.d/$G2TEMPL ]; then
        echo "GRUB 2 template is already in place, only enabling..."
    else
        if [ ! -r /usr/share/memtest86+/$G2TEMPL ]; then
            echo "ERROR: unable to find GRUB 2 template."
            exit 251
        fi
        if ! cp /usr/share/memtest86+/$G2TEMPL /etc/grub.d; then
            echo "ERROR: unable to copy GRUB 2 template, do you have write permission to"
            echo "/etc/grub.d?"
            # EX_IOERR
            exit 74
        fi
    fi
    chmod a+x /etc/grub.d/$G2TEMPL
    echo "GRUB 2 template installed."
    echo "Do not forget to regenerate your grub.cfg by:"
    echo "  # grub2-mkconfig -o /boot/grub2/grub.cfg"
else
    MTVERSION=`rpm -q --qf '%{version}' $PNAME`
    MTPATH="/boot/$ENAME-$MTVERSION"

    MENT=`cat /boot/grub/grub.conf | grep "$ENAME-$MTVERSION"` &> /dev/null
    if [ "$MENT" != "" ]; then
        echo "$MTPATH is already configured. Exiting..."
        exit 0
    fi

    if [ ! -f $MTPATH ]; then
        echo "ERROR: $MTPATH does not exist."
        exit 255
    fi

    /sbin/new-memtest-pkg --install $MTVERSION --banner="Memtest86+"
    RETVAL="$?"

    if [ "$RETVAL" != "0" ]; then
        echo "ERROR: grubby failed to configure your bootloader for $MTPATH."
        exit $RETVAL
    else
        sed -i -e"s,kernel \(/boot\)\?/$ENAME,kernel --type=netbsd \1/$ENAME," /boot/grub/grub.conf
        sed -i -e"s,/$ENAME-$MTVERSION.*,/$ENAME-$MTVERSION," /boot/grub/grub.conf
    fi
fi

echo "Setup complete."