#!/bin/bash ## COPYRIGHT 2013 : MARK E. LEE (BLUERIDER) : mlee24@binghamton.edu; mark@markelee.com ## LOG ## 1/16/2013 : updates nvram and specfied efi directory ($efi_dir); can also generate $efi_dir ## 1/16/2013 : depends on awk and bash ## 1/16/2013 : $efi_dir must possessess only 4 directories ## 1/16/2013 : currently doesn't copy config files ## 1/16/2013 : fixed copying /usr/share/refind instead of /usr/share/refind/* in update-efi-dir ## 1/16/2013 : added support for detecting if an nvram update is necessary function main () { ## main insertion function declare -r efi_dir="/boot/efi/EFI/refind"; ## set the refind directory ## convert efi_dir to efibootmgr format declare -r refind_efi=$(echo ${efi_dir/\/boot\/efi/} | awk -F'/' '{OFS= "\\"; print ($1,$2,$3)}'); declare -r arch=$(uname -m | awk -F'_' '{if ($1 == "x86"){print $2}}') && ## get bit architecture update-efi-dir; ## updates or creates the refind directory declare -r ref_bin=$(ls $efi_dir | grep $arch*.efi); ## get the name of the refind binary update-efi-nvram; ## updates nvram if needed } function update-efi-dir () { ## setup the refind directory if [ ! -d $efi_dir ]; then ## check if refind directory exists echo "Couldn't find $efi_dir"; sudo mkdir $efi_dir && ## make the refind directory if needed echo "Made $efi_dir"; fi; if [ "$arch" ]; then ## check if anything was stored in $arch sudo cp -r /usr/{share/refind/*,lib/refind/*$arch*} $efi_dir/ && ## update bin and dirs echo "Updated binaries and directory files for refind at $efi_dir"; else echo "Failed to detect an x86 architecture"; exit; fi; } function update-efi-nvram () { ## update the nvram with efibootmgr ## insert escape characters into $refind_efi\$ref_bin declare -r awk_efi_patch=$(echo "$refind_efi\\$ref_bin" | awk -F'\' '{OFS = "\\\\"; print ($1,$2,$3,$4)}'); declare -r boot_entry=$(sudo efibootmgr -v | grep $awk_efi_patch); ## find the boot entry sudo modprobe efivars && ## grab the efi variables for efibootmgr if [ -z "$boot_entry" ]; then declare -r esp=$(mount -l | awk '/ESP/ {print $1}') && ## get ESP partition declare -r efiDisk=${esp:0:8} && ## get ESP disk declare -r efiPart=${esp:8} && ## get ESP partition number ## update the nvram with entry "rEFInd" sudo efibootmgr -c -g -d $efiDisk -p $efiPart -w -L "rEFInd" -l "$refind_efi\\$ref_bin" && echo "Updated nvram with entry rEFInd to boot $refind_efi\\$ref_bin"; echo "Did not copy configuration files, please move refind.conf to $efi_dir/"; else echo "Found boot entry, no need to update nvram"; fi; } main; ## run the main insertion function