]>
code.delx.au - refind/blob - install.sh
3 # Linux/MacOS X script to install rEFInd
7 # ./install.sh [options]
10 # "--esp" to install to the ESP rather than to the system's root
11 # filesystem. This is the default on Linux
12 # "--usedefault {devicefile}" to install as default
13 # (/EFI/BOOT/BOOTX64.EFI and similar) to the specified device
14 # (/dev/sdd1 or whatever) without registering with the NVRAM.
15 # "--root {dir}" to specify installation using the specified directory
16 # as the system's root
17 # "--alldrivers" to install all drivers along with regular files
18 # "--nodrivers" to suppress driver installation (default in Linux is
19 # driver used on /boot; --nodrivers is OS X default)
20 # "--shim {shimfile}" to install a shim.efi file for Secure Boot
21 # "--preloader" is synonymous with "--shim"
22 # "--localkeys" to re-sign x86-64 binaries with a locally-generated key
23 # "--yes" to assume a "yes" response to all prompts
25 # The "esp" option is valid only on Mac OS X; it causes
26 # installation to the EFI System Partition (ESP) rather than
27 # to the current OS X boot partition. Under Linux, this script
28 # installs to the ESP by default.
30 # This program is copyright (c) 2012 by Roderick W. Smith
31 # It is released under the terms of the GNU GPL, version 3,
32 # a copy of which should be included in the file COPYING.txt.
36 # 0.7.0 -- Added support for the new Btrfs driver
37 # 0.6.12 -- Added support for PreLoader as well as for shim
38 # 0.6.11 -- Improvements in script's ability to handle directories with spaces
40 # 0.6.9 -- Install gptsync on Macs
41 # 0.6.8 -- Bug fix: ESP scan now uses "uniq".
42 # 0.6.6 -- Bug fix: Upgrade drivers when installed to EFI/BOOT. Also enable
43 # copying shim.efi and MokManager.efi over themselves.
44 # 0.6.4 -- Copies ext2 driver rather than ext4 driver for ext2/3fs
45 # 0.6.3 -- Support for detecting rEFInd in EFI/BOOT and EFI/Microsoft/Boot
46 # directories & for installing to EFI/BOOT in BIOS mode
47 # 0.6.2-1 -- Added --yes option & tweaked key-copying for use with RPM install script
48 # 0.6.1 -- Added --root option; minor bug fixes
49 # 0.6.0 -- Changed --drivers to --alldrivers and added --nodrivers option;
50 # changed default driver installation behavior in Linux to install
51 # the driver needed to read /boot (if available)
52 # 0.5.1.2 -- Fixed bug that caused failure to generate refind_linux.conf file
53 # 0.5.1.1 -- Fixed bug that caused script failure under OS X
54 # 0.5.1 -- Added --shim & --localkeys options & create sample refind_linux.conf
56 # 0.5.0 -- Added --usedefault & --drivers options & changed "esp" option to "--esp"
57 # 0.4.5 -- Fixed check for rEFItBlesser in OS X
58 # 0.4.2 -- Added notice about BIOS-based OSes & made NVRAM changes in Linux smarter
59 # 0.4.1 -- Added check for rEFItBlesser in OS X
60 # 0.3.3.1 -- Fixed OS X 10.7 bug; also works as make target
61 # 0.3.2.1 -- Check for presence of source files; aborts if not present
62 # 0.3.2 -- Initial version
64 # Note: install.sh version numbers match those of the rEFInd package
65 # with which they first appeared.
69 LocalKeysBase
="refind_local"
73 TargetX64
="refind_x64.efi"
74 TargetIA32
="refind_ia32.efi"
80 # Functions used by both OS X and Linux....
85 if [[ $OSName == "Linux" ]] ; then
86 # Install the driver required to read /boot, if it's available
91 while [[ $# -gt 0 ]]; do
93 --esp |
--ESP) InstallToEspOnMac
=1
95 --usedefault) TargetDir
=/EFI
/BOOT
97 TargetX64
="bootx64.efi"
98 TargetIA32
="bootia32.efi"
104 --localkeys) LocalKeys
=1
106 --shim |
--preloader) ShimSource
="$2"
107 ShimType
=`basename $ShimSource`
110 --drivers |
--alldrivers) InstallDrivers
="all"
112 --nodrivers) InstallDrivers
="none"
116 * ) echo "Usage: $0 [--esp | --usedefault {device-file} | --root {directory} ]"
117 echo " [--nodrivers | --alldrivers] [--shim {shim-filename}]"
118 echo " [--localkeys] [--yes]"
124 if [[ $InstallToEspOnMac == 1 && "$TargetDir" == '/EFI/BOOT' ]] ; then
125 echo "You may use --esp OR --usedefault, but not both! Aborting!"
128 if [[ "$RootDir" != '/' && "$TargetDir" == '/EFI/BOOT' ]] ; then
129 echo "You may use --usedefault OR --root, but not both! Aborting!"
132 if [[ "$RootDir" != '/' && $InstallToEspOnMac == 1 ]] ; then
133 echo "You may use --root OR --esp, but not both! Aborting!"
137 RLConfFile
="$RootDir/boot/refind_linux.conf"
138 EtcKeysDir
="$RootDir/etc/refind.d/keys"
141 # Get a yes/no response from the user and place it in the YesNo variable.
142 # If the AlwaysYes variable is set to 1, skip the user input and set "Y"
143 # in the YesNo variable.
145 if [[ $AlwaysYes == 1 ]] ; then
153 # Abort if the rEFInd files can't be found.
154 # Also sets $ConfFile to point to the configuration file,
155 # $IconsDir to point to the icons directory, and
156 # $ShimSource to the source of the shim.efi file (if necessary).
158 # Note: This check is satisfied if EITHER the 32- or the 64-bit version
159 # is found, even on the wrong platform. This is because the platform
160 # hasn't yet been determined. This could obviously be improved, but it
161 # would mean restructuring lots more code....
162 if [[ ! -f "$RefindDir/refind_ia32.efi" && ! -f "$RefindDir/refind_x64.efi" ]] ; then
163 echo "The rEFInd binary file is missing! Aborting installation!"
167 if [[ -f "$RefindDir/refind.conf-sample" ]] ; then
168 ConfFile
="$RefindDir/refind.conf-sample"
169 elif [[ -f "$ThisDir/refind.conf-sample" ]] ; then
170 ConfFile
="$ThisDir/refind.conf-sample"
172 echo "The sample configuration file is missing! Aborting installation!"
176 if [[ -d "$RefindDir/icons" ]] ; then
177 IconsDir
="$RefindDir/icons"
178 elif [[ -d "$ThisDir/icons" ]] ; then
179 IconsDir
="$ThisDir/icons"
181 echo "The icons directory is missing! Aborting installation!"
185 if [[ "$ShimSource" != "none" ]] ; then
186 if [[ -f "$ShimSource" ]] ; then
187 if [[ $ShimType == "shimx64.efi" ||
$ShimType == "shim.efi" ]] ; then
188 TargetX64
="grubx64.efi"
189 MokManagerSource
=`dirname "$ShimSource"`/MokManager.efi
190 elif [[ $ShimType == "preloader.efi" ||
$ShimType == "PreLoader.efi" ]] ; then
191 TargetX64
="loader.efi"
192 MokManagerSource
=`dirname "$ShimSource"`/HashTool.efi
194 echo "Unknown shim/PreBootloader filename: $ShimType!"
195 echo "Known filenames are shimx64.efi, shim.efi, and PreLoader.efi. Aborting!"
199 echo "The specified shim/PreBootloader file, $ShimSource, doesn't exist!"
200 echo "Aborting installation!"
206 # Helper for CopyRefindFiles; copies shim files (including MokManager, if it's
207 # available) to target.
209 cp -fb "$ShimSource" "$InstallDir/$TargetDir/$TargetShim"
210 if [[ $?
!= 0 ]] ; then
213 if [[ -f "$MokManagerSource" ]] ; then
214 cp -fb "$MokManagerSource" "$InstallDir/$TargetDir/"
216 if [[ $?
!= 0 ]] ; then
221 # Copy the public keys to the installation medium
223 if [[ $LocalKeys == 1 ]] ; then
224 mkdir
-p "$InstallDir/$TargetDir/keys/"
225 cp "$EtcKeysDir/$LocalKeysBase.cer" "$InstallDir/$TargetDir/keys/"
226 cp "$EtcKeysDir/$LocalKeysBase.crt" "$InstallDir/$TargetDir/keys/"
230 # Copy drivers from $RefindDir/drivers_$1 to $InstallDir/$TargetDir/drivers_$1,
231 # honoring the $InstallDrivers condition. Must be passed a suitable
232 # architecture code (ia32 or x64).
234 if [[ $InstallDrivers == "all" ]] ; then
235 mkdir
-p "$InstallDir/$TargetDir/drivers_$1"
236 cp "$ThisDir"/drivers_
$1/*_
$1.efi
"$InstallDir/$TargetDir/drivers_$1/" 2> /dev
/null
237 cp "$RefindDir"/drivers_
$1/*_
$1.efi
"$InstallDir/$TargetDir/drivers_$1/" 2> /dev
/null
238 elif [[ "$InstallDrivers" == "boot" && -x `which blkid` ]] ; then
239 BootPart
=`df /boot | grep dev | cut -f 1 -d " "`
240 BootFS
=`blkid -o export $BootPart 2> /dev/null | grep TYPE= | cut -f 2 -d =`
243 ext2 | ext3
) DriverType
="ext2"
244 # Could use ext4, but that can create unwanted entries from symbolic
245 # links in / to /boot/vmlinuz if a separate /boot partition is used.
247 ext4
) DriverType
="ext4"
249 reiserfs
) DriverType
="reiserfs"
251 btrfs
) DriverType
="btrfs"
253 hfsplus
) DriverType
="hfs"
257 if [[ -n $BootFS ]] ; then
258 echo "Installing driver for $BootFS (${DriverType}_$1.efi)"
259 mkdir
-p "$InstallDir/$TargetDir/drivers_$1"
260 cp "$ThisDir/drivers_$1/${DriverType}_$1.efi" "$InstallDir/$TargetDir/drivers_$1/" 2> /dev
/null
261 cp "$RefindDir/drivers_$1/${DriverType}_$1.efi" "$InstallDir/$TargetDir/drivers_$1"/ 2> /dev
/null
266 # Copy tools (currently only gptsync, and that only on Macs) to the EFI/tools
267 # directory on the ESP. Must be passed a suitable architecture code (ia32
270 mkdir
-p $InstallDir/EFI
/tools
271 if [[ $OSName == 'Darwin' ]] ; then
272 cp -f "$RefindDir/tools_$1/gptsync_$1.efi" "$InstallDir/EFI/tools/"
273 if [[ -f "$InstallDir/EFI/tools/gptsync.efi" ]] ; then
274 mv "$InstallDir/EFI/tools/gptsync.efi" "$InstallDir/EFI/tools/gptsync.efi-disabled"
275 echo "Found old gptsync.efi; disabling it by renaming it to gptsync.efi-disabled"
280 # Copy the rEFInd files to the ESP or OS X root partition.
281 # Sets Problems=1 if any critical commands fail.
283 mkdir
-p "$InstallDir/$TargetDir"
284 if [[ "$TargetDir" == '/EFI/BOOT' ]] ; then
285 cp "$RefindDir/refind_ia32.efi" "$InstallDir/$TargetDir/$TargetIA32" 2> /dev
/null
286 if [[ $?
!= 0 ]] ; then
287 echo "Note: IA32 (x86) binary not installed!"
289 cp "$RefindDir/refind_x64.efi" "$InstallDir/$TargetDir/$TargetX64" 2> /dev
/null
290 if [[ $?
!= 0 ]] ; then
293 if [[ "$ShimSource" != "none" ]] ; then
294 TargetShim
="bootx64.efi"
297 if [[ $InstallDrivers == "all" ]] ; then
298 cp -r "$RefindDir"/drivers_
* "$InstallDir/$TargetDir/" 2> /dev
/null
299 cp -r "$ThisDir"/drivers_
* "$InstallDir/$TargetDir/" 2> /dev
/null
300 elif [[ $Upgrade == 1 ]] ; then
301 if [[ $Platform == 'EFI64' ]] ; then
311 elif [[ $Platform == 'EFI64' ||
$TargetDir == "/EFI/Microsoft/Boot" ]] ; then
312 cp "$RefindDir/refind_x64.efi" "$InstallDir/$TargetDir/$TargetX64"
313 if [[ $?
!= 0 ]] ; then
318 Refind
="refind_x64.efi"
320 if [[ "$ShimSource" != "none" ]] ; then
321 if [[ "$TargetShim" == "default" ]] ; then
322 TargetShim
=`basename "$ShimSource"`
326 if [[ $LocalKeys == 0 ]] ; then
327 echo "Storing copies of rEFInd Secure Boot public keys in $EtcKeysDir"
328 mkdir
-p "$EtcKeysDir"
329 cp "$ThisDir/keys/refind.cer" "$EtcKeysDir" 2> /dev
/null
330 cp "$ThisDir/keys/refind.crt" "$EtcKeysDir" 2> /dev
/null
333 elif [[ $Platform == 'EFI32' ]] ; then
334 cp "$RefindDir/refind_ia32.efi" "$InstallDir/$TargetDir/$TargetIA32"
335 if [[ $?
!= 0 ]] ; then
340 Refind
="refind_ia32.efi"
342 echo "Unknown platform! Aborting!"
345 echo "Copied rEFInd binary files"
347 if [[ -d "$InstallDir/$TargetDir/icons" ]] ; then
348 rm -rf "$InstallDir/$TargetDir/icons-backup" &> /dev
/null
349 mv -f "$InstallDir/$TargetDir/icons" "$InstallDir/$TargetDir/icons-backup"
350 echo "Notice: Backed up existing icons directory as icons-backup."
352 cp -r "$IconsDir" "$InstallDir/$TargetDir"
353 if [[ $?
!= 0 ]] ; then
356 mkdir
-p "$InstallDir/$TargetDir/keys"
357 cp -rf "$ThisDir"/keys
/*.
[cd]er
"$InstallDir/$TargetDir/keys/" 2> /dev
/null
358 cp -rf "$EtcKeysDir"/*.
[cd]er
"$InstallDir/$TargetDir/keys/" 2> /dev
/null
359 if [[ -f "$InstallDir/$TargetDir/refind.conf" ]] ; then
360 echo "Existing refind.conf file found; copying sample file as refind.conf-sample"
361 echo "to avoid overwriting your customizations."
363 cp -f "$ConfFile" "$InstallDir/$TargetDir"
364 if [[ $?
!= 0 ]] ; then
368 echo "Copying sample configuration file as refind.conf; edit this file to configure"
371 cp -f "$ConfFile" "$InstallDir/$TargetDir/refind.conf"
372 if [[ $?
!= 0 ]] ; then
376 if [[ $DeleteRefindDir == 1 ]] ; then
377 echo "Deleting the temporary directory $RefindDir"
380 } # CopyRefindFiles()
382 # Mount the partition the user specified with the --usedefault option
383 MountDefaultTarget
() {
384 InstallDir
=/tmp
/refind_install
385 mkdir
-p "$InstallDir"
386 if [[ $OSName == 'Darwin' ]] ; then
387 mount
-t msdos
"$TargetPart" "$InstallDir"
388 elif [[ $OSName == 'Linux' ]] ; then
389 mount
-t vfat
"$TargetPart" "$InstallDir"
391 if [[ $?
!= 0 ]] ; then
392 echo "Couldn't mount $TargetPart ! Aborting!"
397 } # MountDefaultTarget()
400 # A series of OS X support functions....
403 # Mount the ESP at /Volumes/ESP or determine its current mount
405 # Sets InstallDir to the ESP mount point
406 # Sets UnmountEsp if we mounted it
408 # Identify the ESP. Note: This returns the FIRST ESP found;
409 # if the system has multiple disks, this could be wrong!
410 Temp
=`diskutil list | grep " EFI "`
411 Esp
=/dev
/`echo $Temp | cut -f 5 -d ' '`
412 # If the ESP is mounted, use its current mount point....
413 Temp
=`df | grep "$Esp"`
414 InstallDir
=`echo $Temp | cut -f 6 -d ' '`
415 if [[ "$InstallDir" == '' ]] ; then
416 mkdir
/Volumes
/ESP
&> /dev
/null
417 mount
-t msdos
"$Esp" /Volumes
/ESP
418 if [[ $?
!= 0 ]] ; then
419 echo "Unable to mount ESP! Aborting!\n"
423 InstallDir
="/Volumes/ESP"
427 # Control the OS X installation.
428 # Sets Problems=1 if problems found during the installation.
430 echo "Installing rEFInd on OS X...."
431 if [[ "$TargetDir" == "/EFI/BOOT" ]] ; then
433 elif [[ "$InstallToEspOnMac" == "1" ]] ; then
436 InstallDir
="$RootDir/"
438 echo "Installing rEFInd to the partition mounted at $InstallDir"
439 Platform
=`ioreg -l -p IODeviceTree | grep firmware-abi | cut -d "\"" -f 4`
441 if [[ $InstallToEspOnMac == "1" ]] ; then
442 bless
--mount "$InstallDir" --setBoot --file "$InstallDir/$TargetDir/$Refind"
443 elif [[ "$TargetDir" != "/EFI/BOOT" ]] ; then
444 bless
--setBoot --folder "$InstallDir/$TargetDir" --file "$InstallDir/$TargetDir/$Refind"
446 if [[ $?
!= 0 ]] ; then
449 if [[ -f /Library
/StartupItems
/rEFItBlesser ||
-d /Library
/StartupItems
/rEFItBlesser
]] ; then
451 echo "/Library/StartupItems/rEFItBlesser found!"
452 echo "This program is part of rEFIt, and will cause rEFInd to fail to work after"
453 echo -n "its first boot. Do you want to remove rEFItBlesser (Y/N)? "
455 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
456 echo "Deleting /Library/StartupItems/rEFItBlesser..."
457 rm -r /Library
/StartupItems
/rEFItBlesser
459 echo "Not deleting rEFItBlesser."
463 echo "WARNING: If you have an Advanced Format disk, *DO NOT* attempt to check the"
464 echo "bless status with 'bless --info', since this is known to cause disk corruption"
465 echo "on some systems!!"
471 # Now a series of Linux support functions....
474 # Check for evidence that we're running in Secure Boot mode. If so, and if
475 # appropriate options haven't been set, warn the user and offer to abort.
476 # If we're NOT in Secure Boot mode but the user HAS specified the --shim
477 # or --localkeys option, warn the user and offer to abort.
479 # FIXME: Although I checked the presence (and lack thereof) of the
480 # /sys/firmware/efi/vars/SecureBoot* files on my Secure Boot test system
481 # before releasing this script, I've since found that they are at least
482 # sometimes present when Secure Boot is absent. This means that the first
483 # test can produce false alarms. A better test is highly desirable.
485 VarFile
=`ls -d /sys/firmware/efi/vars/SecureBoot* 2> /dev/null`
486 if [[ -n "$VarFile" && "$TargetDir" != '/EFI/BOOT' && "$ShimSource" == "none" ]] ; then
488 echo "CAUTION: Your computer appears to support Secure Boot, but you haven't"
489 echo "specified a valid shim.efi file source. If you've disabled Secure Boot and"
490 echo "intend to leave it disabled, this is fine; but if Secure Boot is active, the"
491 echo "resulting installation won't boot. You can read more about this topic at"
492 echo "http://www.rodsbooks.com/refind/secureboot.html."
494 echo -n "Do you want to proceed with installation (Y/N)? "
496 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
497 echo "OK; continuing with the installation..."
503 if [[ "$ShimSource" != "none" && ! -n "$VarFile" ]] ; then
505 echo "You've specified installing using a shim.efi file, but your computer does not"
506 echo "appear to be running in Secure Boot mode. Although installing in this way"
507 echo "should work, it's unnecessarily complex. You may continue, but unless you"
508 echo "plan to enable Secure Boot, you should consider stopping and omitting the"
509 echo "--shim option. You can read more about this topic at"
510 echo "http://www.rodsbooks.com/refind/secureboot.html."
512 echo -n "Do you want to proceed with installation (Y/N)? "
514 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
515 echo "OK; continuing with the installation..."
521 if [[ $LocalKeys != 0 && ! -n "$VarFile" ]] ; then
523 echo "You've specified re-signing your rEFInd binaries with locally-generated keys,"
524 echo "but your computer does not appear to be running in Secure Boot mode. The"
525 echo "keys you generate will be useless unless you enable Secure Boot. You may"
526 echo "proceed with this installation, but before you do so, you may want to read"
527 echo "more about it at http://www.rodsbooks.com/refind/secureboot.html."
529 echo -n "Do you want to proceed with installation (Y/N)? "
531 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
532 echo "OK; continuing with the installation..."
538 } # CheckSecureBoot()
540 # Check for the presence of locally-generated keys from a previous installation in
541 # $EtcKeysDir (/etc/refind.d/keys). If they're not present, generate them using
544 PrivateKey
="$EtcKeysDir/$LocalKeysBase.key"
545 CertKey
="$EtcKeysDir/$LocalKeysBase.crt"
546 DerKey
="$EtcKeysDir/$LocalKeysBase.cer"
547 OpenSSL
=`which openssl 2> /dev/null`
549 # Do the work only if one or more of the necessary keys is missing
550 # TODO: Technically, we don't need the DerKey; but if it's missing and openssl
551 # is also missing, this will fail. This could be improved.
552 if [[ ! -f "$PrivateKey" ||
! -f "$CertKey" ||
! -f "$DerKey" ]] ; then
553 echo "Generating a fresh set of local keys...."
554 mkdir
-p "$EtcKeysDir"
555 chmod 0700 "$EtcKeysDir"
556 if [[ ! -x "$OpenSSL" ]] ; then
557 echo "Can't find openssl, which is required to create your private signing keys!"
561 if [[ -f "$PrivateKey" ]] ; then
562 echo "Backing up existing $PrivateKey"
563 cp -f "$PrivateKey" "$PrivateKey.backup" 2> /dev
/null
565 if [[ -f "$CertKey" ]] ; then
566 echo "Backing up existing $CertKey"
567 cp -f "$CertKey" "$CertKey.backup" 2> /dev
/null
569 if [[ -f "$DerKey" ]] ; then
570 echo "Backing up existing $DerKey"
571 cp -f "$DerKey" "$DerKey.backup" 2> /dev
/null
573 "$OpenSSL" req
-new -x509 -newkey rsa
:2048 -keyout "$PrivateKey" -out "$CertKey" \
574 -nodes -days 3650 -subj "/CN=Locally-generated rEFInd key/"
575 "$OpenSSL" x509
-in "$CertKey" -out "$DerKey" -outform DER
576 chmod 0600 "$PrivateKey"
578 echo "Using existing local keys...."
582 # Sign a single binary. Requires parameters:
584 # $2 = destination file
585 # Also assumes that the SBSign, PESign, UseSBSign, UsePESign, and various key variables are set
587 # Aborts script on error
589 $SBSign --key "$PrivateKey" --cert "$CertKey" --output "$2" "$1"
590 if [[ $?
!= 0 ]] ; then
591 echo "Problem signing the binary $1! Aborting!"
596 # Re-sign the x86-64 binaries with a locally-generated key, First look for appropriate
597 # key files in $EtcKeysDir. If they're present, use them to re-sign the binaries. If
598 # not, try to generate new keys and store them in $EtcKeysDir.
600 SBSign
=`which sbsign 2> /dev/null`
601 echo "Found sbsign at $SBSign"
602 TempDir
="/tmp/refind_local"
603 if [[ ! -x "$SBSign" ]] ; then
604 echo "Can't find sbsign, which is required to sign rEFInd with your own keys!"
609 mkdir
-p "$TempDir/drivers_x64"
610 cp "$RefindDir/refind.conf-sample $TempDir" 2> /dev
/null
611 cp "$ThisDir/refind.conf-sample $TempDir" 2> /dev
/null
612 cp "$RefindDir/refind_ia32.efi $TempDir" 2> /dev
/null
613 cp -a "$RefindDir/drivers_ia32 $TempDir" 2> /dev
/null
614 cp -a "$ThisDir/drivers_ia32 $TempDir" 2> /dev
/null
615 SignOneBinary
"$RefindDir/refind_x64.efi" "$TempDir/refind_x64.efi"
617 IFS
=$
(echo -en "\n\b")
618 for Driver
in `ls "$RefindDir"/drivers_x64/*.efi "$ThisDir"/drivers_x64/*.efi 2> /dev/null` ; do
619 TempName
=`basename "$Driver"`
620 SignOneBinary
"$Driver" "$TempDir/drivers_x64/$TempName"
627 # Identifies the ESP's location (/boot or /boot/efi, or these locations under
628 # the directory specified by --root); aborts if the ESP isn't mounted at
630 # Sets InstallDir to the ESP mount point.
632 EspLine
=`df "$RootDir/boot/efi" 2> /dev/null | grep boot/efi`
633 if [[ ! -n "$EspLine" ]] ; then
634 EspLine
=`df "$RootDir"/boot | grep boot`
636 InstallDir
=`echo $EspLine | cut -d " " -f 6`
637 if [[ -n "$InstallDir" ]] ; then
638 EspFilesystem
=`grep "$InstallDir" /etc/mtab | uniq | cut -d " " -f 3`
640 echo "EspFilesystem is '$EspFilesystem'"
641 if [[ $EspFilesystem != 'vfat' ]] ; then
642 echo "$RootDir/boot/efi doesn't seem to be on a VFAT filesystem. The ESP must be"
643 echo "mounted at $RootDir/boot or $RootDir/boot/efi and it must be VFAT! Aborting!"
646 echo "ESP was found at $InstallDir using $EspFilesystem"
649 # Uses efibootmgr to add an entry for rEFInd to the EFI's NVRAM.
650 # If this fails, sets Problems=1
653 Efibootmgr
=`which efibootmgr 2> /dev/null`
654 if [[ "$Efibootmgr" ]] ; then
655 InstallDisk
=`grep "$InstallDir" /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
656 PartNum
=`grep "$InstallDir" /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
657 EntryFilename
="$TargetDir/$Refind"
658 EfiEntryFilename
=`echo ${EntryFilename//\//\\\}`
659 EfiEntryFilename2
=`echo ${EfiEntryFilename} | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g`
660 ExistingEntry
=`"$Efibootmgr" -v | grep -i "$EfiEntryFilename2"`
662 if [[ "$ExistingEntry" ]] ; then
663 ExistingEntryBootNum
=`echo "$ExistingEntry" | cut -c 5-8`
664 FirstBoot
=`"$Efibootmgr" | grep BootOrder | cut -c 12-15`
665 if [[ "$ExistingEntryBootNum" != "$FirstBoot" ]] ; then
666 echo "An existing rEFInd boot entry exists, but isn't set as the default boot"
667 echo "manager. The boot order is being adjusted to make rEFInd the default boot"
668 echo "manager. If this is NOT what you want, you should use efibootmgr to"
669 echo "manually adjust your EFI's boot order."
670 "$Efibootmgr" -b $ExistingEntryBootNum -B &> /dev
/null
677 if [[ $InstallIt == "1" ]] ; then
678 echo "Installing it!"
679 "$Efibootmgr" -c -l "$EfiEntryFilename" -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum &> /dev
/null
680 if [[ $?
!= 0 ]] ; then
686 else # efibootmgr not found
691 if [[ $EfibootmgrProblems ]] ; then
693 echo "ALERT: There were problems running the efibootmgr program! You may need to"
694 echo "rename the $Refind binary to the default name (EFI/boot/bootx64.efi"
695 echo "on x86-64 systems or EFI/boot/bootia32.efi on x86 systems) to have it run!"
700 # Create a minimal/sample refind_linux.conf file in /boot.
701 GenerateRefindLinuxConf
() {
702 if [[ -f "$RLConfFile" ]] ; then
703 echo "Existing $RLConfFile found; not overwriting."
705 if [[ -f "$RootDir/etc/default/grub" ]] ; then
706 # We want the default options used by the distribution, stored here....
707 source "$RootDir/etc/default/grub"
709 RootFS
=`df "$RootDir" | grep dev | cut -f 1 -d " "`
710 StartOfDevname
=`echo "$RootFS" | cut -b 1-7`
711 if [[ "$StartOfDevname" == "/dev/sd" ||
"$StartOfDevName" == "/dev/hd" ]] ; then
712 # Identify root filesystem by UUID rather than by device node, if possible
713 Uuid
=`blkid -o export "$RootFS" 2> /dev/null | grep UUID=`
714 if [[ -n $Uuid ]] ; then
718 DefaultOptions
="$GRUB_CMDLINE_LINUX $GRUB_CMDLINE_LINUX_DEFAULT"
719 echo "\"Boot with standard options\" \"ro root=$RootFS $DefaultOptions \"" > $RLConfFile
720 echo "\"Boot to single-user mode\" \"ro root=$RootFS $DefaultOptions single\"" >> $RLConfFile
721 echo "\"Boot with minimal options\" \"ro root=$RootFS\"" >> $RLConfFile
725 # Set varaibles for installation in EFI/BOOT directory
727 TargetDir
="/EFI/BOOT"
728 if [[ $ShimSource == "none" ]] ; then
729 TargetX64
="bootx64.efi"
730 TargetIA32
="bootia32.efi"
732 if [[ $ShimType == "shim.efi" ||
$ShimType == "shimx64.efi" ]] ; then
733 TargetX64
="grubx64.efi"
734 elif [[ $ShimType == "preloader.efi" ||
$ShimType == "PreLoader.efi" ]] ; then
735 TargetX64
="loader.efi"
737 echo "Unknown shim/PreBootloader type: $ShimType"
741 TargetIA32
="bootia32.efi"
742 TargetShim
="bootx64.efi"
744 } # SetFilenamesForBoot()
746 # Set variables for installation in EFI/Microsoft/Boot directory
748 TargetDir
="/EFI/Microsoft/Boot"
749 if [[ $ShimSource == "none" ]] ; then
750 TargetX64
="bootmgfw.efi"
752 if [[ $ShimType == "shim.efi" ||
$ShimType == "shimx64.efi" ]] ; then
753 TargetX64
="grubx64.efi"
754 elif [[ $ShimType == "preloader.efi" ||
$ShimType == "PreLoader.efi" ]] ; then
755 TargetX64
="loader.efi"
757 echo "Unknown shim/PreBootloader type: $ShimType"
761 TargetShim
="bootmgfw.efi"
765 # TargetDir defaults to /EFI/refind; however, this function adjusts it as follows:
766 # - If an existing refind.conf is available in /EFI/BOOT or /EFI/Microsoft/Boot,
767 # install to that directory under the suitable name; but DO NOT do this if
768 # refind.conf is also in /EFI/refind.
769 # - If booted in BIOS mode and the ESP lacks any other EFI files, install to
771 # - If booted in BIOS mode and there's no refind.conf file and there is a
772 # /EFI/Microsoft/Boot/bootmgfw.efi file, move it down one level and
773 # install under that name, "hijacking" the Windows boot loader filename
774 DetermineTargetDir
() {
777 if [[ -f $InstallDir/EFI
/BOOT
/refind.conf
]] ; then
781 if [[ -f $InstallDir/EFI
/Microsoft
/Boot
/refind.conf
]] ; then
785 if [[ -f $InstallDir/EFI
/refind
/refind.conf
]] ; then
786 TargetDir
="/EFI/refind"
789 if [[ $Upgrade == 1 ]] ; then
790 echo "Found rEFInd installation in $InstallDir$TargetDir; upgrading it."
793 if [[ ! -d /sys
/firmware
/efi
&& $Upgrade == 0 ]] ; then # BIOS-mode
794 FoundEfiFiles
=`find "$InstallDir/EFI/BOOT" -name "*.efi" 2> /dev/null`
795 FoundConfFiles
=`find "$InstallDir" -name "refind\.conf" 2> /dev/null`
796 if [[ ! -n "$FoundConfFiles" && -f "$InstallDir/EFI/Microsoft/Boot/bootmgfw.efi" ]] ; then
797 mv -n "$InstallDir/EFI/Microsoft/Boot/bootmgfw.efi" "$InstallDir/EFI/Microsoft" &> /dev
/null
799 echo "Running in BIOS mode with a suspected Windows installation; moving boot loader"
800 echo "files so as to install to $InstallDir$TargetDir."
801 elif [[ ! -n "$FoundEfiFiles" ]] ; then # In BIOS mode and no default loader; install as default loader
803 echo "Running in BIOS mode with no existing default boot loader; installing to"
804 echo $InstallDir$TargetDir
806 echo "Running in BIOS mode with an existing default boot loader; backing it up and"
807 echo "installing rEFInd in its place."
808 if [[ -d "$InstallDir/EFI/BOOT-rEFIndBackup" ]] ; then
810 echo "Caution: An existing backup of a default boot loader exists! If the current"
811 echo "default boot loader and the backup are different boot loaders, the current"
812 echo "one will become inaccessible."
814 echo -n "Do you want to proceed with installation (Y/N)? "
816 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
817 echo "OK; continuing with the installation..."
822 mv -n "$InstallDir/EFI/BOOT" "$InstallDir/EFI/BOOT-rEFIndBackup"
826 } # DetermineTargetDir()
828 # Controls rEFInd installation under Linux.
829 # Sets Problems=1 if something goes wrong.
831 echo "Installing rEFInd on Linux...."
832 modprobe efivars
&> /dev
/null
833 if [[ $TargetDir == "/EFI/BOOT" ]] ; then
840 if [[ $CpuType == 'x86_64' ]] ; then
842 elif [[ ($CpuType == 'i386' ||
$CpuType == 'i486' ||
$CpuType == 'i586' ||
$CpuType == 'i686') ]] ; then
844 # If we're in EFI mode, do some sanity checks, and alert the user or even
845 # abort. Not in BIOS mode, though, since that could be used on an emergency
846 # disc to try to recover a troubled Linux installation.
847 if [[ -d /sys
/firmware
/efi
]] ; then
848 if [[ "$ShimSource" != "none" && "$TargetDir" != "/BOOT/EFI" ]] ; then
850 echo "CAUTION: shim does not currently supports 32-bit systems, so you should not"
851 echo "use the --shim option to install on such systems. Aborting!"
856 echo "CAUTION: This Linux installation uses a 32-bit kernel. 32-bit EFI-based"
857 echo "computers are VERY RARE. If you've installed a 32-bit version of Linux"
858 echo "on a 64-bit computer, you should manually install the 64-bit version of"
859 echo "rEFInd. If you're installing on a Mac, you should do so from OS X. If"
860 echo "you're positive you want to continue with this installation, answer 'Y'"
861 echo "to the following question..."
863 echo -n "Are you sure you want to continue (Y/N)? "
865 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
866 echo "OK; continuing with the installation..."
872 echo "Unknown CPU type '$CpuType'; aborting!"
876 if [[ $LocalKeys == 1 ]] ; then
882 if [[ "$TargetDir" != "/EFI/BOOT" && "$TargetDir" != "/EFI/Microsoft/Boot" ]] ; then
884 GenerateRefindLinuxConf
889 # The main part of the script. Sets a few environment variables,
890 # performs a few startup checks, and then calls functions to
891 # install under OS X or Linux, depending on the detected platform.
896 ThisDir
="$( cd -P "$
( dirname "${BASH_SOURCE[0]}" )" && pwd )"
897 RefindDir
="$ThisDir/refind"
898 ThisScript
="$ThisDir/`basename $0`"
899 if [[ `whoami` != "root" ]] ; then
900 echo "Not running as root; attempting to elevate privileges via sudo...."
901 sudo
"$ThisScript" "$@"
902 if [[ $?
!= 0 ]] ; then
903 echo "This script must be run as root (or using sudo). Exiting!"
910 if [[ $OSName == 'Darwin' ]] ; then
911 if [[ "$ShimSource" != "none" ]] ; then
912 echo "The --shim option is not supported on OS X! Exiting!"
915 if [[ "$LocalKeys" != 0 ]] ; then
916 echo "The --localkeys option is not supported on OS X! Exiting!"
920 elif [[ $OSName == 'Linux' ]] ; then
923 echo "Running on unknown OS; aborting!"
926 if [[ $Problems ]] ; then
929 echo "Installation has completed, but problems were detected. Review the output for"
930 echo "error messages and take corrective measures as necessary. You may need to"
931 echo "re-run this script or install manually before rEFInd will work."
935 echo "Installation has completed successfully."
939 if [[ $UnmountEsp ]] ; then
940 echo "Unmounting install dir"
944 if [[ "$InstallDir" == /tmp
/refind_install
]] ; then