]> code.delx.au - refind/blob - install.sh
Fixed install.sh bug on OS X
[refind] / install.sh
1 #!/bin/bash
2 #
3 # Linux/MacOS X script to install rEFInd
4 #
5 # Usage:
6 #
7 # ./install.sh [options]
8 #
9 # options include:
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 # "--drivers" to install drivers along with regular files
16 # "--shim {shimfile}" to install a shim.efi file for Secure Boot
17 # "--localkeys" to re-sign x86-64 binaries with a locally-generated key
18 #
19 # The "esp" option is valid only on Mac OS X; it causes
20 # installation to the EFI System Partition (ESP) rather than
21 # to the current OS X boot partition. Under Linux, this script
22 # installs to the ESP by default.
23 #
24 # This program is copyright (c) 2012 by Roderick W. Smith
25 # It is released under the terms of the GNU GPL, version 3,
26 # a copy of which should be included in the file COPYING.txt.
27 #
28 # Revision history:
29 #
30 # 0.5.1.1 -- Fixed bug that caused script failure under OS X
31 # 0.5.1 -- Added --shim & --localkeys options & create sample refind_linux.conf
32 # in /boot
33 # 0.5.0 -- Added --usedefault & --drivers options & changed "esp" option to "--esp"
34 # 0.4.5 -- Fixed check for rEFItBlesser in OS X
35 # 0.4.2 -- Added notice about BIOS-based OSes & made NVRAM changes in Linux smarter
36 # 0.4.1 -- Added check for rEFItBlesser in OS X
37 # 0.3.3.1 -- Fixed OS X 10.7 bug; also works as make target
38 # 0.3.2.1 -- Check for presence of source files; aborts if not present
39 # 0.3.2 -- Initial version
40 #
41 # Note: install.sh version numbers match those of the rEFInd package
42 # with which they first appeared.
43
44 TargetDir=/EFI/refind
45 EtcKeysDir=/etc/refind.d/keys
46 LocalKeysBase="refind_local"
47 ShimSource="none"
48 TargetX64="refind_x64.efi"
49 TargetIA32="refind_ia32.efi"
50 LocalKeys=0
51 DeleteRefindDir=0
52
53 #
54 # Functions used by both OS X and Linux....
55 #
56
57 GetParams() {
58 InstallToEspOnMac=0
59 InstallDrivers=0
60 while [[ $# -gt 0 ]]; do
61 case $1 in
62 --esp | --ESP) InstallToEspOnMac=1
63 ;;
64 --usedefault) TargetDir=/EFI/BOOT
65 TargetPart=$2
66 TargetX64="bootx64.efi"
67 TargetIA32="bootia32.efi"
68 shift
69 ;;
70 --localkeys) LocalKeys=1
71 ;;
72 --shim) ShimSource=$2
73 shift
74 ;;
75 --drivers) InstallDrivers=1
76 ;;
77 * ) echo "Usage: $0 [--esp | --usedefault {device-file}] [--drivers] "
78 echo " [--shim {shim-filename}] [--localkeys]"
79 exit 1
80 esac
81 shift
82 done
83 if [[ $InstallToEspOnMac == 1 && $TargetDir == '/EFI/BOOT' ]] ; then
84 echo "You may use --esp OR --usedefault, but not both! Aborting!"
85 exit 1
86 fi
87 # exit 1
88 } # GetParams()
89
90 # Abort if the rEFInd files can't be found.
91 # Also sets $ConfFile to point to the configuration file,
92 # $IconsDir to point to the icons directory, and
93 # $ShimSource to the source of the shim.efi file (if necessary).
94 CheckForFiles() {
95 # Note: This check is satisfied if EITHER the 32- or the 64-bit version
96 # is found, even on the wrong platform. This is because the platform
97 # hasn't yet been determined. This could obviously be improved, but it
98 # would mean restructuring lots more code....
99 if [[ ! -f $RefindDir/refind_ia32.efi && ! -f $RefindDir/refind_x64.efi ]] ; then
100 echo "The rEFInd binary file is missing! Aborting installation!"
101 exit 1
102 fi
103
104 if [[ -f $RefindDir/refind.conf-sample ]] ; then
105 ConfFile=$RefindDir/refind.conf-sample
106 elif [[ -f $ThisDir/refind.conf-sample ]] ; then
107 ConfFile=$ThisDir/refind.conf-sample
108 else
109 echo "The sample configuration file is missing! Aborting installation!"
110 exit 1
111 fi
112
113 if [[ -d $RefindDir/icons ]] ; then
114 IconsDir=$RefindDir/icons
115 elif [[ -d $ThisDir/icons ]] ; then
116 IconsDir=$ThisDir/icons
117 else
118 echo "The icons directory is missing! Aborting installation!"
119 exit 1
120 fi
121
122 if [[ $ShimSource != "none" ]] ; then
123 if [[ -f $ShimSource ]] ; then
124 TargetX64="grubx64.efi"
125 MokManagerSource=`dirname $ShimSource`/MokManager.efi
126 else
127 echo "The specified shim file, $ShimSource, doesn't exist!"
128 echo "Aborting installation!"
129 exit 1
130 fi
131 fi
132 } # CheckForFiles()
133
134 # Helper for CopyRefindFiles; copies shim files (including MokManager, if it's
135 # available) to target.
136 CopyShimFiles() {
137 cp $ShimSource $InstallDir/$TargetDir/$TargetShim
138 if [[ $? != 0 ]] ; then
139 Problems=1
140 fi
141 if [[ -f $MokManagerSource ]] ; then
142 cp $MokManagerSource $InstallDir/$TargetDir/
143 fi
144 if [[ $? != 0 ]] ; then
145 Problems=1
146 fi
147 } # CopyShimFiles()
148
149 # Copy the public keys to the installation medium
150 CopyKeys() {
151 if [[ $LocalKeys == 1 ]] ; then
152 cp $EtcKeysDir/$LocalKeysBase.cer $InstallDir/$TargetDir
153 cp $EtcKeysDir/$LocalKeysBase.crt $InstallDir/$TargetDir
154 else
155 cp $ThisDir/refind.cer $InstallDir/$TargetDir
156 cp $ThisDir/refind.crt $InstallDir/$TargetDir
157 fi
158 } # CopyKeys()
159
160 # Copy the rEFInd files to the ESP or OS X root partition.
161 # Sets Problems=1 if any critical commands fail.
162 CopyRefindFiles() {
163 mkdir -p $InstallDir/$TargetDir &> /dev/null
164 if [[ $TargetDir == '/EFI/BOOT' ]] ; then
165 cp $RefindDir/refind_ia32.efi $InstallDir/$TargetDir/$TargetIA32 2> /dev/null
166 if [[ $? != 0 ]] ; then
167 echo "Note: IA32 (x86) binary not installed!"
168 fi
169 cp $RefindDir/refind_x64.efi $InstallDir/$TargetDir/$TargetX64 2> /dev/null
170 if [[ $? != 0 ]] ; then
171 Problems=1
172 fi
173 if [[ $ShimSource != "none" ]] ; then
174 TargetShim="bootx64.efi"
175 CopyShimFiles
176 fi
177 if [[ $InstallDrivers == 1 ]] ; then
178 cp -r $RefindDir/drivers_* $InstallDir/$TargetDir/ 2> /dev/null
179 cp -r $ThisDir/drivers_* $InstallDir/$TargetDir/ 2> /dev/null
180 fi
181 Refind=""
182 CopyKeys
183 elif [[ $Platform == 'EFI32' ]] ; then
184 cp $RefindDir/refind_ia32.efi $InstallDir/$TargetDir/$TargetIA32
185 if [[ $? != 0 ]] ; then
186 Problems=1
187 fi
188 if [[ $InstallDrivers == 1 ]] ; then
189 mkdir -p $InstallDir/$TargetDir/drivers_ia32
190 cp $RefindDir/drivers_ia32/*_ia32.efi $InstallDir/$TargetDir/drivers_ia32/ 2> /dev/null
191 cp $ThisDir/drivers_ia32/*_ia32.efi $InstallDir/$TargetDir/drivers_ia32/ 2> /dev/null
192 fi
193 Refind="refind_ia32.efi"
194 elif [[ $Platform == 'EFI64' ]] ; then
195 cp $RefindDir/refind_x64.efi $InstallDir/$TargetDir/$TargetX64
196 if [[ $? != 0 ]] ; then
197 Problems=1
198 fi
199 if [[ $InstallDrivers == 1 ]] ; then
200 mkdir -p $InstallDir/$TargetDir/drivers_x64
201 cp $RefindDir/drivers_x64/*_x64.efi $InstallDir/$TargetDir/drivers_x64/ 2> /dev/null
202 cp $ThisDir/drivers_x64/*_x64.efi $InstallDir/$TargetDir/drivers_x64/ 2> /dev/null
203 fi
204 Refind="refind_x64.efi"
205 CopyKeys
206 if [[ $ShimSource != "none" ]] ; then
207 TargetShim=`basename $ShimSource`
208 CopyShimFiles
209 Refind=$TargetShim
210 if [[ $LocalKeys == 0 ]] ; then
211 echo "Storing copies of rEFInd Secure Boot public keys in $EtcKeysDir"
212 mkdir -p $EtcKeysDir
213 cp $ThisDir/refind.cer $EtcKeysDir
214 cp $ThisDir/refind.crt $EtcKeysDir
215 fi
216 fi
217 else
218 echo "Unknown platform! Aborting!"
219 exit 1
220 fi
221 echo "Copied rEFInd binary files"
222 echo ""
223 if [[ -d $InstallDir/$TargetDir/icons ]] ; then
224 rm -rf $InstallDir/$TargetDir/icons-backup &> /dev/null
225 mv -f $InstallDir/$TargetDir/icons $InstallDir/$TargetDir/icons-backup
226 echo "Notice: Backed up existing icons directory as icons-backup."
227 fi
228 cp -r $IconsDir $InstallDir/$TargetDir
229 if [[ $? != 0 ]] ; then
230 Problems=1
231 fi
232 if [[ -f $InstallDir/$TargetDir/refind.conf ]] ; then
233 echo "Existing refind.conf file found; copying sample file as refind.conf-sample"
234 echo "to avoid overwriting your customizations."
235 echo ""
236 cp -f $ConfFile $InstallDir/$TargetDir
237 if [[ $? != 0 ]] ; then
238 Problems=1
239 fi
240 else
241 echo "Copying sample configuration file as refind.conf; edit this file to configure"
242 echo "rEFInd."
243 echo ""
244 cp -f $ConfFile $InstallDir/$TargetDir/refind.conf
245 if [[ $? != 0 ]] ; then
246 Problems=1
247 fi
248 fi
249 if [[ $DeleteRefindDir == 1 ]] ; then
250 echo "Deleting the temporary directory $RefindDir"
251 rm -r $RefindDir
252 fi
253 } # CopyRefindFiles()
254
255 # Mount the partition the user specified with the --usedefault option
256 MountDefaultTarget() {
257 InstallDir=/tmp/refind_install
258 mkdir -p $InstallDir
259 if [[ $OSName == 'Darwin' ]] ; then
260 mount -t msdos $TargetPart $InstallDir
261 elif [[ $OSName == 'Linux' ]] ; then
262 mount -t vfat $TargetPart $InstallDir
263 fi
264 if [[ $? != 0 ]] ; then
265 echo "Couldn't mount $TargetPart ! Aborting!"
266 rmdir $InstallDir
267 exit 1
268 fi
269 UnmountEsp=1
270 } # MountDefaultTarget()
271
272 #
273 # A series of OS X support functions....
274 #
275
276 # Mount the ESP at /Volumes/ESP or determine its current mount
277 # point.
278 # Sets InstallDir to the ESP mount point
279 # Sets UnmountEsp if we mounted it
280 MountOSXESP() {
281 # Identify the ESP. Note: This returns the FIRST ESP found;
282 # if the system has multiple disks, this could be wrong!
283 Temp=`diskutil list | grep " EFI "`
284 Esp=/dev/`echo $Temp | cut -f 5 -d ' '`
285 # If the ESP is mounted, use its current mount point....
286 Temp=`df | grep $Esp`
287 InstallDir=`echo $Temp | cut -f 6 -d ' '`
288 if [[ $InstallDir == '' ]] ; then
289 mkdir /Volumes/ESP &> /dev/null
290 mount -t msdos $Esp /Volumes/ESP
291 if [[ $? != 0 ]] ; then
292 echo "Unable to mount ESP! Aborting!\n"
293 exit 1
294 fi
295 UnmountEsp=1
296 InstallDir="/Volumes/ESP"
297 fi
298 } # MountOSXESP()
299
300 # Control the OS X installation.
301 # Sets Problems=1 if problems found during the installation.
302 InstallOnOSX() {
303 echo "Installing rEFInd on OS X...."
304 if [[ $TargetDir == "/EFI/BOOT" ]] ; then
305 MountDefaultTarget
306 elif [[ $InstallToEspOnMac == "1" ]] ; then
307 MountOSXESP
308 else
309 InstallDir="/"
310 fi
311 echo "Installing rEFInd to the partition mounted at '$InstallDir'"
312 Platform=`ioreg -l -p IODeviceTree | grep firmware-abi | cut -d "\"" -f 4`
313 CopyRefindFiles
314 if [[ $InstallToEspOnMac == "1" ]] ; then
315 bless --mount $InstallDir --setBoot --file $InstallDir/$TargetDir/$Refind
316 elif [[ $TargetDir != "/EFI/BOOT" ]] ; then
317 bless --setBoot --folder $InstallDir/$TargetDir --file $InstallDir/$TargetDir/$Refind
318 fi
319 if [[ $? != 0 ]] ; then
320 Problems=1
321 fi
322 if [[ -f /Library/StartupItems/rEFItBlesser || -d /Library/StartupItems/rEFItBlesser ]] ; then
323 echo
324 echo "/Library/StartupItems/rEFItBlesser found!"
325 echo "This program is part of rEFIt, and will cause rEFInd to fail to work after"
326 echo -n "its first boot. Do you want to remove rEFItBlesser (Y/N)? "
327 read YesNo
328 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
329 echo "Deleting /Library/StartupItems/rEFItBlesser..."
330 rm -r /Library/StartupItems/rEFItBlesser
331 else
332 echo "Not deleting rEFItBlesser."
333 fi
334 fi
335 echo
336 echo "WARNING: If you have an Advanced Format disk, *DO NOT* attempt to check the"
337 echo "bless status with 'bless --info', since this is known to cause disk corruption"
338 echo "on some systems!!"
339 echo
340 } # InstallOnOSX()
341
342
343 #
344 # Now a series of Linux support functions....
345 #
346
347 # Check for evidence that we're running in Secure Boot mode. If so, and if
348 # appropriate options haven't been set, warn the user and offer to abort.
349 # If we're NOT in Secure Boot mode but the user HAS specified the --shim
350 # or --localkeys option, warn the user and offer to abort.
351 CheckSecureBoot() {
352 VarFile=`ls -d /sys/firmware/efi/vars/SecureBoot* 2> /dev/null`
353 if [[ -n $VarFile && $TargetDir != '/EFI/BOOT' && $ShimSource == "none" ]] ; then
354 echo ""
355 echo "CAUTION: The computer seems to have been booted with Secure Boot active, but"
356 echo "you haven't specified a valid shim.efi file source. The resulting installation"
357 echo "will not boot unless you disable Secure Boot. You may continue, but you should"
358 echo "consider using --shim to specify a working shim.efi file. You can read more"
359 echo "about this topic at http://www.rodsbooks.com/refind/secureboot.html."
360 echo ""
361 echo -n "Do you want to proceed with installation (Y/N)? "
362 read ContYN
363 if [[ $ContYN == "Y" || $ContYN == "y" ]] ; then
364 echo "OK; continuing with the installation..."
365 else
366 exit 0
367 fi
368 fi
369
370 if [[ $ShimSource != "none" && ! -n $VarFile ]] ; then
371 echo ""
372 echo "You've specified installing using a shim.efi file, but your computer does not"
373 echo "appear to be running in Secure Boot mode. Although installing in this way"
374 echo "should work, it's unnecessarily complex. You may continue, but unless you"
375 echo "plan to enable Secure Boot, you should consider stopping and omitting the"
376 echo "--shim option. You can read more about this topic at"
377 echo "http://www.rodsbooks.com/refind/secureboot.html."
378 echo ""
379 echo -n "Do you want to proceed with installation (Y/N)? "
380 read ContYN
381 if [[ $ContYN == "Y" || $ContYN == "y" ]] ; then
382 echo "OK; continuing with the installation..."
383 else
384 exit 0
385 fi
386 fi
387
388 if [[ $LocalKeys != 0 && ! -n $VarFile ]] ; then
389 echo ""
390 echo "You've specified re-signing your rEFInd binaries with locally-generated keys,"
391 echo "but your computer does not appear to be running in Secure Boot mode. The"
392 echo "keys you generate will be useless unless you enable Secure Boot. You may"
393 echo "proceed with this installation, but before you do so, you may want to read"
394 echo "more about it at http://www.rodsbooks.com/refind/secureboot.html."
395 echo ""
396 echo -n "Do you want to proceed with installation (Y/N)? "
397 read ContYN
398 if [[ $ContYN == "Y" || $ContYN == "y" ]] ; then
399 echo "OK; continuing with the installation..."
400 else
401 exit 0
402 fi
403 fi
404
405 } # CheckSecureBoot()
406
407 # Check for the presence of locally-generated keys from a previous installation in
408 # $EtcKeysDir (/etc/refind.d/keys). If they're not present, generate them using
409 # openssl.
410 GenerateKeys() {
411 PrivateKey=$EtcKeysDir/$LocalKeysBase.key
412 CertKey=$EtcKeysDir/$LocalKeysBase.crt
413 DerKey=$EtcKeysDir/$LocalKeysBase.cer
414 OpenSSL=`which openssl 2> /dev/null`
415
416 # Do the work only if one or more of the necessary keys is missing
417 # TODO: Technically, we don't need the DerKey; but if it's missing and openssl
418 # is also missing, this will fail. This could be improved.
419 if [[ ! -f $PrivateKey || ! -f $CertKey || ! -f $DerKey ]] ; then
420 echo "Generating a fresh set of local keys...."
421 mkdir -p $EtcKeysDir
422 chmod 0700 $EtcKeysDir
423 if [[ ! -x $OpenSSL ]] ; then
424 echo "Can't find openssl, which is required to create your private signing keys!"
425 echo "Aborting!"
426 exit 1
427 fi
428 if [[ -f $PrivateKey ]] ; then
429 echo "Backing up existing $PrivateKey"
430 cp -f $PrivateKey $PrivateKey.backup 2> /dev/null
431 fi
432 if [[ -f $CertKey ]] ; then
433 echo "Backing up existing $CertKey"
434 cp -f $CertKey $CertKey.backup 2> /dev/null
435 fi
436 if [[ -f $DerKey ]] ; then
437 echo "Backing up existing $DerKey"
438 cp -f $DerKey $DerKey.backup 2> /dev/null
439 fi
440 $OpenSSL req -new -x509 -newkey rsa:2048 -keyout $PrivateKey -out $CertKey \
441 -nodes -days 3650 -subj "/CN=Locally-generated rEFInd key/"
442 $OpenSSL x509 -in $CertKey -out $DerKey -outform DER
443 chmod 0600 $PrivateKey
444 else
445 echo "Using existing local keys...."
446 fi
447 }
448
449 # Sign a single binary. Requires parameters:
450 # $1 = source file
451 # $2 = destination file
452 # Also assumes that the SBSign, PESign, UseSBSign, UsePESign, and various key variables are set
453 # appropriately.
454 # Aborts script on error
455 SignOneBinary() {
456 $SBSign --key $PrivateKey --cert $CertKey --output $2 $1
457 if [[ $? != 0 ]] ; then
458 echo "Problem signing the binary $1! Aborting!"
459 exit 1
460 fi
461 }
462
463 # Re-sign the x86-64 binaries with a locally-generated key, First look for appropriate
464 # key files in $EtcKeysDir. If they're present, use them to re-sign the binaries. If
465 # not, try to generate new keys and store them in $EtcKeysDir.
466 ReSignBinaries() {
467 SBSign=`which sbsign 2> /dev/null`
468 echo "Found sbsign at $SBSign"
469 TempDir="/tmp/refind_local"
470 if [[ ! -x $SBSign ]] ; then
471 echo "Can't find sbsign, which is required to sign rEFInd with your own keys!"
472 echo "Aborting!"
473 exit 1
474 fi
475 GenerateKeys
476 mkdir -p $TempDir/drivers_x64
477 cp $RefindDir/refind.conf-sample $TempDir 2> /dev/null
478 cp $ThisDir/refind.conf-sample $TempDir 2> /dev/null
479 cp $RefindDir/refind_ia32.efi $TempDir
480 cp -a $RefindDir/drivers_ia32 $TempDir 2> /dev/null
481 cp -a $ThisDir/drivers_ia32 $TempDir 2> /dev/null
482 SignOneBinary $RefindDir/refind_x64.efi $ThisDir/refind_x64.efi
483 for Driver in `ls $RefindDir/drivers_x64/*.efi $ThisDir/drivers_x64/*.efi 2> /dev/null` ; do
484 TempName=`basename $Driver`
485 SignOneBinary $Driver $TempDir/drivers_x64/$TempName
486 done
487 RefindDir=$TempDir
488 DeleteRefindDir=1
489 }
490
491 # Identifies the ESP's location (/boot or /boot/efi); aborts if
492 # the ESP isn't mounted at either location.
493 # Sets InstallDir to the ESP mount point.
494 FindLinuxESP() {
495 EspLine=`df /boot/efi | grep boot`
496 InstallDir=`echo $EspLine | cut -d " " -f 6`
497 EspFilesystem=`grep $InstallDir /etc/mtab | cut -d " " -f 3`
498 if [[ $EspFilesystem != 'vfat' ]] ; then
499 echo "/boot/efi doesn't seem to be on a VFAT filesystem. The ESP must be mounted at"
500 echo "/boot or /boot/efi and it must be VFAT! Aborting!"
501 exit 1
502 fi
503 echo "ESP was found at $InstallDir using $EspFilesystem"
504 } # MountLinuxESP
505
506 # Uses efibootmgr to add an entry for rEFInd to the EFI's NVRAM.
507 # If this fails, sets Problems=1
508 AddBootEntry() {
509 InstallIt="0"
510 Efibootmgr=`which efibootmgr 2> /dev/null`
511 if [[ $Efibootmgr ]] ; then
512 modprobe efivars &> /dev/null
513 InstallDisk=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
514 PartNum=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
515 EntryFilename=$TargetDir/$Refind
516 EfiEntryFilename=`echo ${EntryFilename//\//\\\}`
517 EfiEntryFilename2=`echo ${EfiEntryFilename} | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g`
518 ExistingEntry=`$Efibootmgr -v | grep $EfiEntryFilename2`
519 # NOTE: Below protects against duplicate entries, but only for non-Secure Boot
520 # installations.
521 # TODO: Improve to detect & protect against duplicating a Secure Boot entry.
522 if [[ $ExistingEntry && $ShimSource == "none" ]] ; then
523 ExistingEntryBootNum=`echo $ExistingEntry | cut -c 5-8`
524 FirstBoot=`$Efibootmgr | grep BootOrder | cut -c 12-15`
525 if [[ $ExistingEntryBootNum != $FirstBoot ]] ; then
526 echo "An existing rEFInd boot entry exists, but isn't set as the default boot"
527 echo "manager. The boot order is being adjusted to make rEFInd the default boot"
528 echo "manager. If this is NOT what you want, you should use efibootmgr to"
529 echo "manually adjust your EFI's boot order."
530 $Efibootmgr -b $ExistingEntryBootNum -B &> /dev/null
531 InstallIt="1"
532 fi
533 else
534 InstallIt="1"
535 fi
536 if [[ $InstallIt == "1" ]] ; then
537 echo "Installing it!"
538 $Efibootmgr -c -l $EfiEntryFilename -L rEFInd -d $InstallDisk -p $PartNum &> /dev/null
539 if [[ $? != 0 ]] ; then
540 EfibootmgrProblems=1
541 Problems=1
542 fi
543 fi
544 else
545 EfibootmgrProblems=1
546 Problems=1
547 fi
548 if [[ $EfibootmgrProblems ]] ; then
549 echo
550 echo "ALERT: There were problems running the efibootmgr program! You may need to"
551 echo "rename the $Refind binary to the default name (EFI/boot/bootx64.efi"
552 echo "on x86-64 systems or EFI/boot/bootia32.efi on x86 systems) to have it run!"
553 echo
554 fi
555 } # AddBootEntry()
556
557 # Create a minimal/sample refind_linux.conf file in /boot.
558 GenerateRefindLinuxConf() {
559 if [[ ! -f /boot/refind_linux.conf ]] ; then
560 if [[ -f /etc/default/grub ]] ; then
561 # We want the default options used by the distribution, stored here....
562 source /etc/default/grub
563 fi
564 RootFS=`df / | grep dev | cut -f 1 -d " "`
565 StartOfDevname=`echo $RootFS | cut -b 1-7`
566 if [[ $StartOfDevname == "/dev/sd" || $StartOfDevName == "/dev/hd" ]] ; then
567 # Identify root filesystem by UUID rather than by device node, if possible
568 Uuid=`blkid -o export $RootFS 2> /dev/null | grep UUID=`
569 if [[ -n $Uuid ]] ; then
570 RootFS=$Uuid
571 fi
572 fi
573 DefaultOptions="$GRUB_CMDLINE_LINUX $GRUB_CMDLINE_LINUX_DEFAULT"
574 echo "\"Boot with standard options\" \"ro root=$RootFS $DefaultOptions \"" > $RLConfFile
575 echo "\"Boot to single-user mode\" \"ro root=$RootFS $DefaultOptions single\"" >> $RLConfFile
576 fi
577 }
578
579 # Controls rEFInd installation under Linux.
580 # Sets Problems=1 if something goes wrong.
581 InstallOnLinux() {
582 echo "Installing rEFInd on Linux...."
583 if [[ $TargetDir == "/EFI/BOOT" ]] ; then
584 MountDefaultTarget
585 else
586 FindLinuxESP
587 fi
588 CpuType=`uname -m`
589 if [[ $CpuType == 'x86_64' ]] ; then
590 Platform="EFI64"
591 if [[ $LocalKeys == 1 ]] ; then
592 ReSignBinaries
593 fi
594 elif [[ $CpuType == 'i386' || $CpuType == 'i486' || $CpuType == 'i586' || $CpuType == 'i686' ]] ; then
595 if [[ $ShimSource != "none" && $TargetDir != "/BOOT/EFI" ]] ; then
596 echo ""
597 echo "CAUTION: Neither rEFInd nor shim currently supports 32-bit systems, so you"
598 echo "should not use the --shim option to install on such systems. Aborting!"
599 echo ""
600 exit 1
601 fi
602 Platform="EFI32"
603 echo
604 echo "CAUTION: This Linux installation uses a 32-bit kernel. 32-bit EFI-based"
605 echo "computers are VERY RARE. If you've installed a 32-bit version of Linux"
606 echo "on a 64-bit computer, you should manually install the 64-bit version of"
607 echo "rEFInd. If you're installing on a Mac, you should do so from OS X. If"
608 echo "you're positive you want to continue with this installation, answer 'Y'"
609 echo "to the following question..."
610 echo
611 echo -n "Are you sure you want to continue (Y/N)? "
612 read ContYN
613 if [[ $ContYN == "Y" || $ContYN == "y" ]] ; then
614 echo "OK; continuing with the installation..."
615 else
616 exit 0
617 fi
618 else
619 echo "Unknown CPU type '$CpuType'; aborting!"
620 exit 1
621 fi
622 CheckSecureBoot
623 CopyRefindFiles
624 if [[ $TargetDir != "/EFI/BOOT" ]] ; then
625 AddBootEntry
626 GenerateRefindLinuxConf
627 fi
628 } # InstallOnLinux()
629
630 #
631 # The main part of the script. Sets a few environment variables,
632 # performs a few startup checks, and then calls functions to
633 # install under OS X or Linux, depending on the detected platform.
634 #
635
636 GetParams $@
637 OSName=`uname -s`
638 ThisDir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
639 RefindDir="$ThisDir/refind"
640 ThisScript="$ThisDir/`basename $0`"
641 if [[ `whoami` != "root" ]] ; then
642 echo "Not running as root; attempting to elevate privileges via sudo...."
643 sudo $ThisScript "$@"
644 if [[ $? != 0 ]] ; then
645 echo "This script must be run as root (or using sudo). Exiting!"
646 exit 1
647 else
648 exit 0
649 fi
650 fi
651 CheckForFiles
652 if [[ $OSName == 'Darwin' ]] ; then
653 if [[ $ShimSource != "none" ]] ; then
654 echo "The --shim option is not supported on OS X! Exiting!"
655 exit 1
656 fi
657 if [[ $LocalKeys != 0 ]] ; then
658 echo "The --localkeys option is not supported on OS X! Exiting!"
659 exit 1
660 fi
661 InstallOnOSX $1
662 elif [[ $OSName == 'Linux' ]] ; then
663 InstallOnLinux
664 else
665 echo "Running on unknown OS; aborting!"
666 fi
667
668 if [[ $Problems ]] ; then
669 echo
670 echo "ALERT:"
671 echo "Installation has completed, but problems were detected. Review the output for"
672 echo "error messages and take corrective measures as necessary. You may need to"
673 echo "re-run this script or install manually before rEFInd will work."
674 echo
675 else
676 echo
677 echo "Installation has completed successfully."
678 echo
679 fi
680
681 if [[ $UnmountEsp ]] ; then
682 echo "Unmounting install dir"
683 umount $InstallDir
684 fi
685
686 if [[ $InstallDir == /tmp/refind_install ]] ; then
687 # sleep 5
688 rmdir $InstallDir
689 fi