]> code.delx.au - refind/blob - install.sh
Version 0.7.0 release with misc. filesystem driver improvements.
[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 # "--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
24 #
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.
29 #
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.
33 #
34 # Revision history:
35 #
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
39 # in their names
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
55 # in /boot
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
63 #
64 # Note: install.sh version numbers match those of the rEFInd package
65 # with which they first appeared.
66
67 RootDir="/"
68 TargetDir=/EFI/refind
69 LocalKeysBase="refind_local"
70 ShimSource="none"
71 ShimType="none"
72 TargetShim="default"
73 TargetX64="refind_x64.efi"
74 TargetIA32="refind_ia32.efi"
75 LocalKeys=0
76 DeleteRefindDir=0
77 AlwaysYes=0
78
79 #
80 # Functions used by both OS X and Linux....
81 #
82
83 GetParams() {
84 InstallToEspOnMac=0
85 if [[ $OSName == "Linux" ]] ; then
86 # Install the driver required to read /boot, if it's available
87 InstallDrivers="boot"
88 else
89 InstallDrivers="none"
90 fi
91 while [[ $# -gt 0 ]]; do
92 case $1 in
93 --esp | --ESP) InstallToEspOnMac=1
94 ;;
95 --usedefault) TargetDir=/EFI/BOOT
96 TargetPart="$2"
97 TargetX64="bootx64.efi"
98 TargetIA32="bootia32.efi"
99 shift
100 ;;
101 --root) RootDir="$2"
102 shift
103 ;;
104 --localkeys) LocalKeys=1
105 ;;
106 --shim | --preloader) ShimSource="$2"
107 ShimType=`basename $ShimSource`
108 shift
109 ;;
110 --drivers | --alldrivers) InstallDrivers="all"
111 ;;
112 --nodrivers) InstallDrivers="none"
113 ;;
114 --yes) AlwaysYes=1
115 ;;
116 * ) echo "Usage: $0 [--esp | --usedefault {device-file} | --root {directory} ]"
117 echo " [--nodrivers | --alldrivers] [--shim {shim-filename}]"
118 echo " [--localkeys] [--yes]"
119 exit 1
120 esac
121 shift
122 done
123
124 if [[ $InstallToEspOnMac == 1 && "$TargetDir" == '/EFI/BOOT' ]] ; then
125 echo "You may use --esp OR --usedefault, but not both! Aborting!"
126 exit 1
127 fi
128 if [[ "$RootDir" != '/' && "$TargetDir" == '/EFI/BOOT' ]] ; then
129 echo "You may use --usedefault OR --root, but not both! Aborting!"
130 exit 1
131 fi
132 if [[ "$RootDir" != '/' && $InstallToEspOnMac == 1 ]] ; then
133 echo "You may use --root OR --esp, but not both! Aborting!"
134 exit 1
135 fi
136
137 RLConfFile="$RootDir/boot/refind_linux.conf"
138 EtcKeysDir="$RootDir/etc/refind.d/keys"
139 } # GetParams()
140
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.
144 ReadYesNo() {
145 if [[ $AlwaysYes == 1 ]] ; then
146 YesNo="Y"
147 echo "Y"
148 else
149 read YesNo
150 fi
151 }
152
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).
157 CheckForFiles() {
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!"
164 exit 1
165 fi
166
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"
171 else
172 echo "The sample configuration file is missing! Aborting installation!"
173 exit 1
174 fi
175
176 if [[ -d "$RefindDir/icons" ]] ; then
177 IconsDir="$RefindDir/icons"
178 elif [[ -d "$ThisDir/icons" ]] ; then
179 IconsDir="$ThisDir/icons"
180 else
181 echo "The icons directory is missing! Aborting installation!"
182 exit 1
183 fi
184
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
193 else
194 echo "Unknown shim/PreBootloader filename: $ShimType!"
195 echo "Known filenames are shimx64.efi, shim.efi, and PreLoader.efi. Aborting!"
196 exit 1
197 fi
198 else
199 echo "The specified shim/PreBootloader file, $ShimSource, doesn't exist!"
200 echo "Aborting installation!"
201 exit 1
202 fi
203 fi
204 } # CheckForFiles()
205
206 # Helper for CopyRefindFiles; copies shim files (including MokManager, if it's
207 # available) to target.
208 CopyShimFiles() {
209 cp -fb "$ShimSource" "$InstallDir/$TargetDir/$TargetShim"
210 if [[ $? != 0 ]] ; then
211 Problems=1
212 fi
213 if [[ -f "$MokManagerSource" ]] ; then
214 cp -fb "$MokManagerSource" "$InstallDir/$TargetDir/"
215 fi
216 if [[ $? != 0 ]] ; then
217 Problems=1
218 fi
219 } # CopyShimFiles()
220
221 # Copy the public keys to the installation medium
222 CopyKeys() {
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/"
227 fi
228 } # CopyKeys()
229
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).
233 CopyDrivers() {
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 =`
241 DriverType=""
242 case $BootFS in
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.
246 ;;
247 ext4) DriverType="ext4"
248 ;;
249 reiserfs) DriverType="reiserfs"
250 ;;
251 btrfs) DriverType="btrfs"
252 ;;
253 hfsplus) DriverType="hfs"
254 ;;
255 *) BootFS=""
256 esac
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
262 fi
263 fi
264 }
265
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
268 # or x64).
269 CopyTools() {
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"
276 fi
277 fi
278 } # CopyTools()
279
280 # Copy the rEFInd files to the ESP or OS X root partition.
281 # Sets Problems=1 if any critical commands fail.
282 CopyRefindFiles() {
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!"
288 fi
289 cp "$RefindDir/refind_x64.efi" "$InstallDir/$TargetDir/$TargetX64" 2> /dev/null
290 if [[ $? != 0 ]] ; then
291 Problems=1
292 fi
293 if [[ "$ShimSource" != "none" ]] ; then
294 TargetShim="bootx64.efi"
295 CopyShimFiles
296 fi
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
302 CopyDrivers x64
303 CopyTools x64
304 else
305 CopyDrivers ia32
306 CopyTools ia32
307 fi
308 fi
309 Refind=""
310 CopyKeys
311 elif [[ $Platform == 'EFI64' || $TargetDir == "/EFI/Microsoft/Boot" ]] ; then
312 cp "$RefindDir/refind_x64.efi" "$InstallDir/$TargetDir/$TargetX64"
313 if [[ $? != 0 ]] ; then
314 Problems=1
315 fi
316 CopyDrivers x64
317 CopyTools x64
318 Refind="refind_x64.efi"
319 CopyKeys
320 if [[ "$ShimSource" != "none" ]] ; then
321 if [[ "$TargetShim" == "default" ]] ; then
322 TargetShim=`basename "$ShimSource"`
323 fi
324 CopyShimFiles
325 Refind="$TargetShim"
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
331 fi
332 fi
333 elif [[ $Platform == 'EFI32' ]] ; then
334 cp "$RefindDir/refind_ia32.efi" "$InstallDir/$TargetDir/$TargetIA32"
335 if [[ $? != 0 ]] ; then
336 Problems=1
337 fi
338 CopyDrivers ia32
339 CopyTools ia32
340 Refind="refind_ia32.efi"
341 else
342 echo "Unknown platform! Aborting!"
343 exit 1
344 fi
345 echo "Copied rEFInd binary files"
346 echo ""
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."
351 fi
352 cp -r "$IconsDir" "$InstallDir/$TargetDir"
353 if [[ $? != 0 ]] ; then
354 Problems=1
355 fi
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."
362 echo ""
363 cp -f "$ConfFile" "$InstallDir/$TargetDir"
364 if [[ $? != 0 ]] ; then
365 Problems=1
366 fi
367 else
368 echo "Copying sample configuration file as refind.conf; edit this file to configure"
369 echo "rEFInd."
370 echo ""
371 cp -f "$ConfFile" "$InstallDir/$TargetDir/refind.conf"
372 if [[ $? != 0 ]] ; then
373 Problems=1
374 fi
375 fi
376 if [[ $DeleteRefindDir == 1 ]] ; then
377 echo "Deleting the temporary directory $RefindDir"
378 rm -r "$RefindDir"
379 fi
380 } # CopyRefindFiles()
381
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"
390 fi
391 if [[ $? != 0 ]] ; then
392 echo "Couldn't mount $TargetPart ! Aborting!"
393 rmdir "$InstallDir"
394 exit 1
395 fi
396 UnmountEsp=1
397 } # MountDefaultTarget()
398
399 #
400 # A series of OS X support functions....
401 #
402
403 # Mount the ESP at /Volumes/ESP or determine its current mount
404 # point.
405 # Sets InstallDir to the ESP mount point
406 # Sets UnmountEsp if we mounted it
407 MountOSXESP() {
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"
420 exit 1
421 fi
422 UnmountEsp=1
423 InstallDir="/Volumes/ESP"
424 fi
425 } # MountOSXESP()
426
427 # Control the OS X installation.
428 # Sets Problems=1 if problems found during the installation.
429 InstallOnOSX() {
430 echo "Installing rEFInd on OS X...."
431 if [[ "$TargetDir" == "/EFI/BOOT" ]] ; then
432 MountDefaultTarget
433 elif [[ "$InstallToEspOnMac" == "1" ]] ; then
434 MountOSXESP
435 else
436 InstallDir="$RootDir/"
437 fi
438 echo "Installing rEFInd to the partition mounted at $InstallDir"
439 Platform=`ioreg -l -p IODeviceTree | grep firmware-abi | cut -d "\"" -f 4`
440 CopyRefindFiles
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"
445 fi
446 if [[ $? != 0 ]] ; then
447 Problems=1
448 fi
449 if [[ -f /Library/StartupItems/rEFItBlesser || -d /Library/StartupItems/rEFItBlesser ]] ; then
450 echo
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)? "
454 ReadYesNo
455 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
456 echo "Deleting /Library/StartupItems/rEFItBlesser..."
457 rm -r /Library/StartupItems/rEFItBlesser
458 else
459 echo "Not deleting rEFItBlesser."
460 fi
461 fi
462 echo
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!!"
466 echo
467 } # InstallOnOSX()
468
469
470 #
471 # Now a series of Linux support functions....
472 #
473
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.
478 #
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.
484 CheckSecureBoot() {
485 VarFile=`ls -d /sys/firmware/efi/vars/SecureBoot* 2> /dev/null`
486 if [[ -n "$VarFile" && "$TargetDir" != '/EFI/BOOT' && "$ShimSource" == "none" ]] ; then
487 echo ""
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."
493 echo ""
494 echo -n "Do you want to proceed with installation (Y/N)? "
495 ReadYesNo
496 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
497 echo "OK; continuing with the installation..."
498 else
499 exit 0
500 fi
501 fi
502
503 if [[ "$ShimSource" != "none" && ! -n "$VarFile" ]] ; then
504 echo ""
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."
511 echo ""
512 echo -n "Do you want to proceed with installation (Y/N)? "
513 ReadYesNo
514 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
515 echo "OK; continuing with the installation..."
516 else
517 exit 0
518 fi
519 fi
520
521 if [[ $LocalKeys != 0 && ! -n "$VarFile" ]] ; then
522 echo ""
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."
528 echo ""
529 echo -n "Do you want to proceed with installation (Y/N)? "
530 ReadYesNo
531 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
532 echo "OK; continuing with the installation..."
533 else
534 exit 0
535 fi
536 fi
537
538 } # CheckSecureBoot()
539
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
542 # openssl.
543 GenerateKeys() {
544 PrivateKey="$EtcKeysDir/$LocalKeysBase.key"
545 CertKey="$EtcKeysDir/$LocalKeysBase.crt"
546 DerKey="$EtcKeysDir/$LocalKeysBase.cer"
547 OpenSSL=`which openssl 2> /dev/null`
548
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!"
558 echo "Aborting!"
559 exit 1
560 fi
561 if [[ -f "$PrivateKey" ]] ; then
562 echo "Backing up existing $PrivateKey"
563 cp -f "$PrivateKey" "$PrivateKey.backup" 2> /dev/null
564 fi
565 if [[ -f "$CertKey" ]] ; then
566 echo "Backing up existing $CertKey"
567 cp -f "$CertKey" "$CertKey.backup" 2> /dev/null
568 fi
569 if [[ -f "$DerKey" ]] ; then
570 echo "Backing up existing $DerKey"
571 cp -f "$DerKey" "$DerKey.backup" 2> /dev/null
572 fi
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"
577 else
578 echo "Using existing local keys...."
579 fi
580 }
581
582 # Sign a single binary. Requires parameters:
583 # $1 = source file
584 # $2 = destination file
585 # Also assumes that the SBSign, PESign, UseSBSign, UsePESign, and various key variables are set
586 # appropriately.
587 # Aborts script on error
588 SignOneBinary() {
589 $SBSign --key "$PrivateKey" --cert "$CertKey" --output "$2" "$1"
590 if [[ $? != 0 ]] ; then
591 echo "Problem signing the binary $1! Aborting!"
592 exit 1
593 fi
594 }
595
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.
599 ReSignBinaries() {
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!"
605 echo "Aborting!"
606 exit 1
607 fi
608 GenerateKeys
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"
616 SaveIFS=$IFS
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"
621 done
622 IFS=$SaveIFS
623 RefindDir="$TempDir"
624 DeleteRefindDir=1
625 }
626
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
629 # either location.
630 # Sets InstallDir to the ESP mount point.
631 FindLinuxESP() {
632 EspLine=`df "$RootDir/boot/efi" 2> /dev/null | grep boot/efi`
633 if [[ ! -n "$EspLine" ]] ; then
634 EspLine=`df "$RootDir"/boot | grep boot`
635 fi
636 InstallDir=`echo $EspLine | cut -d " " -f 6`
637 if [[ -n "$InstallDir" ]] ; then
638 EspFilesystem=`grep "$InstallDir" /etc/mtab | uniq | cut -d " " -f 3`
639 fi
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!"
644 exit 1
645 fi
646 echo "ESP was found at $InstallDir using $EspFilesystem"
647 } # FindLinuxESP
648
649 # Uses efibootmgr to add an entry for rEFInd to the EFI's NVRAM.
650 # If this fails, sets Problems=1
651 AddBootEntry() {
652 InstallIt="0"
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"`
661
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
671 InstallIt="1"
672 fi
673 else
674 InstallIt="1"
675 fi
676
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
681 EfibootmgrProblems=1
682 Problems=1
683 fi
684 fi
685
686 else # efibootmgr not found
687 EfibootmgrProblems=1
688 Problems=1
689 fi
690
691 if [[ $EfibootmgrProblems ]] ; then
692 echo
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!"
696 echo
697 fi
698 } # AddBootEntry()
699
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."
704 else
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"
708 fi
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
715 RootFS="$Uuid"
716 fi
717 fi
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
722 fi
723 }
724
725 # Set varaibles for installation in EFI/BOOT directory
726 SetVarsForBoot() {
727 TargetDir="/EFI/BOOT"
728 if [[ $ShimSource == "none" ]] ; then
729 TargetX64="bootx64.efi"
730 TargetIA32="bootia32.efi"
731 else
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"
736 else
737 echo "Unknown shim/PreBootloader type: $ShimType"
738 echo "Aborting!"
739 exit 1
740 fi
741 TargetIA32="bootia32.efi"
742 TargetShim="bootx64.efi"
743 fi
744 } # SetFilenamesForBoot()
745
746 # Set variables for installation in EFI/Microsoft/Boot directory
747 SetVarsForMsBoot() {
748 TargetDir="/EFI/Microsoft/Boot"
749 if [[ $ShimSource == "none" ]] ; then
750 TargetX64="bootmgfw.efi"
751 else
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"
756 else
757 echo "Unknown shim/PreBootloader type: $ShimType"
758 echo "Aborting!"
759 exit 1
760 fi
761 TargetShim="bootmgfw.efi"
762 fi
763 }
764
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
770 # /EFI/BOOT
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() {
775 Upgrade=0
776
777 if [[ -f $InstallDir/EFI/BOOT/refind.conf ]] ; then
778 SetVarsForBoot
779 Upgrade=1
780 fi
781 if [[ -f $InstallDir/EFI/Microsoft/Boot/refind.conf ]] ; then
782 SetVarsForMsBoot
783 Upgrade=1
784 fi
785 if [[ -f $InstallDir/EFI/refind/refind.conf ]] ; then
786 TargetDir="/EFI/refind"
787 Upgrade=1
788 fi
789 if [[ $Upgrade == 1 ]] ; then
790 echo "Found rEFInd installation in $InstallDir$TargetDir; upgrading it."
791 fi
792
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
798 SetVarsForMsBoot
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
802 SetVarsForBoot
803 echo "Running in BIOS mode with no existing default boot loader; installing to"
804 echo $InstallDir$TargetDir
805 else
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
809 echo ""
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."
813 echo ""
814 echo -n "Do you want to proceed with installation (Y/N)? "
815 ReadYesNo
816 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
817 echo "OK; continuing with the installation..."
818 else
819 exit 0
820 fi
821 fi
822 mv -n "$InstallDir/EFI/BOOT" "$InstallDir/EFI/BOOT-rEFIndBackup"
823 SetVarsForBoot
824 fi
825 fi # BIOS-mode
826 } # DetermineTargetDir()
827
828 # Controls rEFInd installation under Linux.
829 # Sets Problems=1 if something goes wrong.
830 InstallOnLinux() {
831 echo "Installing rEFInd on Linux...."
832 modprobe efivars &> /dev/null
833 if [[ $TargetDir == "/EFI/BOOT" ]] ; then
834 MountDefaultTarget
835 else
836 FindLinuxESP
837 DetermineTargetDir
838 fi
839 CpuType=`uname -m`
840 if [[ $CpuType == 'x86_64' ]] ; then
841 Platform="EFI64"
842 elif [[ ($CpuType == 'i386' || $CpuType == 'i486' || $CpuType == 'i586' || $CpuType == 'i686') ]] ; then
843 Platform="EFI32"
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
849 echo ""
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!"
852 echo ""
853 exit 1
854 fi
855 echo
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..."
862 echo
863 echo -n "Are you sure you want to continue (Y/N)? "
864 ReadYesNo
865 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
866 echo "OK; continuing with the installation..."
867 else
868 exit 0
869 fi
870 fi # in EFI mode
871 else
872 echo "Unknown CPU type '$CpuType'; aborting!"
873 exit 1
874 fi
875
876 if [[ $LocalKeys == 1 ]] ; then
877 ReSignBinaries
878 fi
879
880 CheckSecureBoot
881 CopyRefindFiles
882 if [[ "$TargetDir" != "/EFI/BOOT" && "$TargetDir" != "/EFI/Microsoft/Boot" ]] ; then
883 AddBootEntry
884 GenerateRefindLinuxConf
885 fi
886 } # InstallOnLinux()
887
888 #
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.
892 #
893
894 OSName=`uname -s`
895 GetParams "$@"
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!"
904 exit 1
905 else
906 exit 0
907 fi
908 fi
909 CheckForFiles
910 if [[ $OSName == 'Darwin' ]] ; then
911 if [[ "$ShimSource" != "none" ]] ; then
912 echo "The --shim option is not supported on OS X! Exiting!"
913 exit 1
914 fi
915 if [[ "$LocalKeys" != 0 ]] ; then
916 echo "The --localkeys option is not supported on OS X! Exiting!"
917 exit 1
918 fi
919 InstallOnOSX $1
920 elif [[ $OSName == 'Linux' ]] ; then
921 InstallOnLinux
922 else
923 echo "Running on unknown OS; aborting!"
924 fi
925
926 if [[ $Problems ]] ; then
927 echo
928 echo "ALERT:"
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."
932 echo
933 else
934 echo
935 echo "Installation has completed successfully."
936 echo
937 fi
938
939 if [[ $UnmountEsp ]] ; then
940 echo "Unmounting install dir"
941 umount $InstallDir
942 fi
943
944 if [[ "$InstallDir" == /tmp/refind_install ]] ; then
945 # sleep 5
946 rmdir "$InstallDir"
947 fi