REFInd

From ArchWiki
Revision as of 12:27, 23 February 2014 by Fengchao (talk | contribs) (Split from EFISTUB.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

rEFInd is a fork of rEFIt Boot Manager (used in Intel Macs) by Rod Smith (author of gdisk). rEFInd fixes many issues in rEFIt with respect to non-Mac UEFI booting and also has support for booting EFISTUB kernels and contains some features specific to them.

Boot EFISTUB

Tip: If you're new to EFISTUB and/or rEFInd, you need to read The rEFInd Boot Manager: Methods of Booting Linux before going any further. This section illustrates only one possible use-case which is not suitable for all configurations.
Note: refind-efi includes a install script from upstream at /usr/bin/refind-install which does the job of setting-up of rEFInd similar to below steps.
Note: For 32-bit aka IA32 EFI, replace x64 with ia32 (case-sensitive) in the below commands.
  • Mount efivarfs
# mount -t efivarfs efivarfs /sys/firmware/efi/efivars              # ignore if already mounted
  • Copy the following files from their source directory to their destination
# cp /usr/share/refind/refind_x64.efi $esp/EFI/refind/refind_x64.efi
# cp /usr/share/refind/refind.conf-sample  $esp/EFI/refind/refind.conf
# cp -r /usr/share/refind/icons $esp/EFI/refind/icons
# cp -r /usr/share/refind/drivers_x64 $esp/EFI/refind/drivers
  • Edit rEFInd's config file at $esp/EFI/refind/refind.conf. The file is well commented and self explanatory. Add a menu entry for Arch Linux.
refind.conf
menuentry "Arch Linux" {
        icon    EFI/refind/icons/os_arch.icns
        loader  vmlinuz-linux
        initrd  initramfs-linux.img
        options "root=PARTUUID=3518bb68-d01e-45c9-b973-0b5d918aae96 rw rootfstype=ext4 add_efi_memmap"
}
Note: Modify the loader and initrd paths if you did not place them in your ESP's root. Replace the string after PARTUUID with your root's PARTUUID. Please note in the example above that PARTUUID/PARTLABEL identifies a GPT partition, and differs from UUID/LABEL, which identifies a filesystem. Using the PARTUUID/PARTLABEL is advantageous because it is invariant if you reformat the partition with another filesystem. It's also useful if you don't have a filesystem on the partition (or use LUKS, which doesn't support LABELs).
  • Alternatively, you can create a refind_linux.conf file inside the directory where the kernel and initramfs files are located.
refind_linux.conf
"Boot with defaults" "root=PARTUUID=3518bb68-d01e-45c9-b973-0b5d918aae96 rw rootfstype=ext4 add_efi_memmap systemd.unit=graphical.target"
"Boot to Terminal"   "root=PARTUUID=3518bb68-d01e-45c9-b973-0b5d918aae96 rw rootfstype=ext4 add_efi_memmap systemd.unit=multi-user.target"
Tip: Each line of refind_linux.conf is displayed as a submenu by rEFInd. Access the submenu with "+" or "insert" or "F2" keys.
  • Create a boot entry in the UEFI boot menu using efibootmgr
# efibootmgr -c -d /dev/sdX -p Y -l /EFI/refind/refind_x64.efi -L "rEFInd"
Note: As of refind-efi 0.2.7, refind can auto-detect kernels in /boot, if there are UEFI drivers for the filesystem used by /boot partition (or / partition if no separate /boot is used) in the ESP, and are loaded by rEFInd. This is enabled in the default configuration in refind.conf (you may need to include the PATH to the drivers folders in the ESP). See [1] for more info.

Upgrading rEFInd

To automatically update rEFInd to the latest version, you could run refind-install (in cronjob) as alternative to the systemd script below. The command will be executed as follow:

/usr/bin/refind-install
Installing rEFInd on Linux....
ESP was found at /boot/efi using vfat
Found rEFInd installation in /boot/efi/EFI/refind; upgrading it.
Installing driver for ext4 (ext4_x64.efi)
Copied rEFInd binary files

Notice: Backed up existing icons directory as icons-backup.
Existing refind.conf file found; copying sample file as refind.conf-sample
to avoid overwriting your customizations.

rEFInd has been set as the default boot manager.
Existing //boot/refind_linux.conf found; not overwriting.

Installation has completed successfully.

Systemd Automation (Upgrading)

To automate the process of copying refind files and updating the nvram (if needed) use the following script.

Note: If you want to change the directory that refind is installed in the UEFISYS partition, just change the value of $refind_dir in the script
/usr/lib/systemd/scripts/refind_name_patchv2
#!/usr/bin/env bash
## COPYRIGHT 2013 : MARK E. LEE (BLUERIDER) : mlee24@binghamton.edu; mark@markelee.com

## LOG
## 1/17/2013 : Version 2 of refind_name_patch is released
##           : Supports long subdirectory location for refind
##           : Updates nvram when needed
##           : 10% speed boost
## 7/15/2013 : Changed arch to match 32-bit (ia32) and 64-bit (x64) naming scheme
##           : Changed directory copying in update-efi-dir to copy tools and drivers directories explicitly
##           : Changed efibootmgr writing code to be more concise and added (-w) to write the entry as per dusktreader's excellent guide : https://docs.google.com/document/d/1pvgm3BprpXoadsQi38FxqMOCUZhcSqFhZ26FZBkmn9I/edit
##           : Function to check if NVRAM boot entry was already listed was fixed to use awk and an if then clause
##           : ref_bin_escape was modified from : ref_bin_escape=${ref_bin//\//\\\\} to remove extra backslashes (error does not show up when using cmdline)
## 7/29/2013 : Changed location of tools,drivers, and binary directory to match capricious upstream move to /usr/share/refind

function main () {  ## main insertion function
  declare -r refind_dir="/boot/efi/EFI/refind"; ## set the refind directory
  arch=$(uname -m | awk -F'_' '{if ($1 == "x86") {print "x"$2} else if ($1 == "i686") {print "ia32"}}') &&  ## get bit architecture
  update-efi-dir;  ## updates or creates the refind directory
  update-efi-nvram;  ## updates nvram if needed
}

function update-efi-dir () {  ## setup the refind directory
  if [ ! -d $refind_dir ]; then  ## check if refind directory exists
    echo "Couldn't find $refind_dir";
    mkdir $refind_dir &&  ## make the refind directory if needed
    echo "Made $refind_dir";
  fi;
  if [ "$arch" ]; then  ## check if anything was stored in $arch
    cp -r /usr/share/refind/{refind_$arch.efi,keys,images,icons,fonts,docs,{tools,drivers}_$arch} $refind_dir/  && ## update the bins and dirs
    echo "Updated binaries and directory files for refind at $refind_dir";
   else
    echo "Failed to detect an x86 architecture";
    exit;
  fi;
}

function update-efi-nvram () { ## update the nvram with efibootmgr
  declare -r ref_bin=${refind_dir/\/boot\/efi}/refind_$arch.efi;  ## get path of refind binary (without /boot/efi)
  declare -r ref_bin_escape=${ref_bin//\//\\};  ## insert escape characters into $ref_bin
  [ "$(efibootmgr -v | awk "/${ref_bin_escape//\\/\\\\}/")" ] && ( ## check if boot entry is in nvram \
    echo "Found boot entry, no need to update nvram";
    ) || ( ## if boot entry is not in nvram; add it
    declare -r esp=$(mount -l | awk '/ESP/ {print $1}') &&  ## get ESP partition
    efibootmgr -cgw -d ${esp:0:8} -p ${esp:8} -L "rEFInd" -l $ref_bin_escape && ## update nvram
    echo "
    Updated nvram with entry rEFInd to boot $ref_bin
    Did not copy configuration files, please move refind.conf to $refind_dir/";
    )
}
main;  ## run the main insertion function
/usr/lib/systemd/system/refind_update.path
[Unit]
Description=Update rEFInd bootloader files

[Path]
PathChanged=/usr/share/refind/refind_<arch>.efi
Unit=refind_update.service

[Install]
WantedBy=multi-user.target
/usr/lib/systemd/system/refind_update.service
[Unit]
Description=Update rEFInd directories, binaries, and nvram

[Service]
Type=oneshot
ExecStart=/usr/bin/bash /usr/lib/systemd/scripts/refind_name_patchv2
RemainAfterExit=no

Enable the systemd path unit by running:

# systemctl enable refind_update.path

Apple Macs

In case of Apple Macs, try mactel-bootAUR for an experimental "bless" utility for Linux. If that does not work, use "bless" from within OSX to set rEFInd as default bootloader. Assuming UEFISYS partition is mounted at /mnt/efi within OSX, do

$ sudo bless --setBoot --folder /mnt/efi/EFI/refind --file /mnt/efi/EFI/refind/refind_x64.efi

VirtualBox

In case of VirtualBox, see VirtualBox#Using_Arch_under_Virtualbox_EFI_mode.