• Boot help script for UEFI systems


    Hi all!
    There have been so many booting problems lately that I wrote a small bash script that might help people in UEFI systems.
    The script can also be used on a working system for generating additional boot entries that boot directly to the selected operating system.

    This script only creates a new file /boot/grub/custom.cfg to your system, so nothing disruptive is done. And no need to run ‘grub-mkconfig’ afterwards. You’ll just see new boot entries like ‘custom_antergos’, ‘custom_Microsoft’ etc.

    Copy the script below to a text file. You can make it executable:

    chmod +x script_name
    

    and run it like any program, or run it using bash:

    bash script_name
    

    Change the script_name above to your liking.

    Disclaimer: you may use it at your own risk!

    #!/bin/bash
    #
    # Generate /boot/grub/custom.cfg (see TARGET below) for UEFI systems.
    # Possible existing TARGET file is backed up.
    # Either use this script in your daily linux installation, or
    # chroot to a non-booting linux and run this script.
    
    TARGET=/boot/grub/custom.cfg
    
    Generate()
    {
        echo "$*" >> "$TARGET"
    }
    
    StartHere()
    {
        if [ "$(id -u)" != "0" ] ; then
            echo "Root permissions needed to run this program!"
            return 1
        fi
        local xx yy
        local dev
        local mountpoint=/root/mountpoint.$$
        local linux_efis windows_efis
        local efi
        local name_prefix="custom_"
    
        # look for .efi files
        for xx in $(lsblk -lo NAME,FSTYPE | grep vfat | awk '{print $1}')
        do
            dev="/dev/$xx"
            mkdir -p "$mountpoint"
            mount "$dev" "$mountpoint"
    
            # look for linux efi files
            for yy in grubx64.efi
            do
                efi=$(find "$mountpoint" -type f -iname "$yy" | sed 's|'$mountpoint'|'$dev'@|')
                if [ "$efi" != "" ] ; then
                    linux_efis+="$efi "
                fi
            done
    
            # look for windows efi files
            for yy in bootmgfw.efi bootx64.efi
            do
                efi=$(find "$mountpoint" -type f -iname "$yy" | sed 's|'$mountpoint'|'$dev'@|')
                if [ "$efi" != "" ] ; then
                    windows_efis+="$efi "
                    break   # the first is enough!
                fi
            done
            umount "$mountpoint"
        done
    
        if [ -d "$mountpoint" ] ; then
            rmdir "$mountpoint"
        fi
    
        echo "Found (Linux):"
        for xx in $linux_efis
        do
            echo "  $xx"
        done
        echo "Found (Windows):"
        for xx in $windows_efis
        do
            echo "  $xx"
        done
    
        if [ "$linux_efis" = "" ] && [ "$windows_efis" = "" ] ; then
            echo "Nothing to generate!"
            return 1
        fi
    
        # Generate menu entries.
        # Backup the old file if it exists.
    
        if [ -e "$TARGET" ] ; then
            mv "$TARGET" "$TARGET".$(date +%Y%m%d-%H%M%S)
        fi
        Generate "# Generated by $0 at $(date)"
        Generate "#"
        for xx in $linux_efis $windows_efis
        do
            yy="$name_prefix""$(echo "$xx" | sed 's|^.*/[eE][fF][iI]/\([^/]*\)/.*$|\1|')"
            Generate "menuentry \"$yy\" {"
            Generate "  savedefault"
            yy=$(echo "$xx" | sed 's|^.*\@||')
            Generate "  set file=\"$yy\""
            Generate "  search --set --file \${file}"
            Generate "  chainloader \${file}"
            Generate "}"
        done
    
        # Show where the resulting file resides.
        ls -l "$TARGET"
    }
    
    StartHere
    
    
  • may package this one after some testers ?

    Also something wewhat should be nice for a antergos-rescue-ISO @keegan

    [updates once a week] = [90% less problems]
    [Li{u}n//u//{i}x] since 1988 - overcoming failure means success
    howto-install-antergos
    how to add system logs
    i3 GNOME

  • It could be packaged, but as you know, I’m quite slow to do that. I’d appreciate any help on this, maybe @Keegan or anybody else with UEFI computers?

    Of course some testing should be done first, the more testers the better. This particular script should not break anything since it writes only one file (and creates a backup if the file already exists). But I have tested it only on one machine, so it really needs more testing.

  • i think only that this will help mostly users with not so much experience, and so it will be nice to simple install it over packagemanager and run it… instead of copy paste and the need of some more knowledge tomake it run…

    [updates once a week] = [90% less problems]
    [Li{u}n//u//{i}x] since 1988 - overcoming failure means success
    howto-install-antergos
    how to add system logs
    i3 GNOME

  • @joekamprad
    Yes, people new to Antergos/Arch/Linux would benefit the most out of this script probably, and sure, the best would be to have it as an already existing package. Best would be in the ISO installer as one of the helper tools.

  • @joekamprad said in Boot help script for UEFI systems:

    Also something wewhat should be nice for a antergos-rescue-ISO @keegan

    Good idea:grinning:. Sadly, I am too busy right now to do that, but hopefully life slows down soon!

    I’d appreciate any help on this, maybe @Keegan or anybody else with UEFI computers?

    I’m really sorry, but again, I’m just swamped with work:disappointed:. Perhaps some other time though!

    Keep trying, never give up. In the end, you will find that it was all worth it

    Community ISO: https://antergoscommunityeditions.wordpress.com
    Linux Basics: https://linuxbasicssite.wordpress.com

uefi37 systems2 boot175 Posts 6Views 134
Log in to reply