]> code.delx.au - refind/blob - install.sh
0.4.1 release
[refind] / install.sh
1 #!/bin/bash
2 #
3 # Linux/MacOS X script to install rEFInd
4 #
5 # Usage:
6 #
7 # ./install.sh [esp]
8 #
9 # The "esp" option is valid only on Mac OS X; it causes
10 # installation to the EFI System Partition (ESP) rather than
11 # to the current OS X boot partition. Under Linux, this script
12 # installs to the ESP by default.
13 #
14 # This program is copyright (c) 2012 by Roderick W. Smith
15 # It is released under the terms of the GNU GPL, version 3,
16 # a copy of which should be included in the file COPYING.txt.
17 #
18 # Revision history:
19 #
20 # 0.4.1 -- Added check for rEFItBlesser in OS X
21 # 0.3.3.1 -- Fixed OS X 10.7 bug; also works as make target
22 # 0.3.2.1 -- Check for presence of source files; aborts if not present
23 # 0.3.2 -- Initial version
24 #
25 # Note: install.sh version numbers match those of the rEFInd package
26 # with which they first appeared.
27
28 TargetDir=/EFI/refind
29
30 #
31 # Functions used by both OS X and Linux....
32 #
33
34 # Abort if the rEFInd files can't be found.
35 # Also sets $ConfFile to point to the configuration file, and
36 # $IconsDir to point to the icons directory
37 CheckForFiles() {
38 # Note: This check is satisfied if EITHER the 32- or the 64-bit version
39 # is found, even on the wrong platform. This is because the platform
40 # hasn't yet been determined. This could obviously be improved, but it
41 # would mean restructuring lots more code....
42 if [[ ! -f $RefindDir/refind_ia32.efi && ! -f $RefindDir/refind_x64.efi ]] ; then
43 echo "The rEFInd binary file is missing! Aborting installation!"
44 exit 1
45 fi
46
47 if [[ -f $RefindDir/refind.conf-sample ]] ; then
48 ConfFile=$RefindDir/refind.conf-sample
49 elif [[ -f $ThisDir/refind.conf-sample ]] ; then
50 ConfFile=$ThisDir/refind.conf-sample
51 else
52 echo "The sample configuration file is missing! Aborting installation!"
53 exit 1
54 fi
55
56 if [[ -d $RefindDir/icons ]] ; then
57 IconsDir=$RefindDir/icons
58 elif [[ -d $ThisDir/icons ]] ; then
59 IconsDir=$ThisDir/icons
60 else
61 echo "The icons directory is missing! Aborting installation!"
62 fi
63 } # CheckForFiles()
64
65 # Copy the rEFInd files to the ESP or OS X root partition.
66 # Sets Problems=1 if any critical commands fail.
67 CopyRefindFiles() {
68 mkdir -p $InstallPart/$TargetDir &> /dev/null
69 if [[ $Platform == 'EFI32' ]] ; then
70 cp $RefindDir/refind_ia32.efi $InstallPart/$TargetDir
71 if [[ $? != 0 ]] ; then
72 Problems=1
73 fi
74 Refind="refind_ia32.efi"
75 elif [[ $Platform == 'EFI64' ]] ; then
76 cp $RefindDir/refind_x64.efi $InstallPart/$TargetDir
77 if [[ $? != 0 ]] ; then
78 Problems=1
79 fi
80 Refind="refind_x64.efi"
81 else
82 echo "Unknown platform! Aborting!"
83 exit 1
84 fi
85 echo "Copied rEFInd binary file $Refind"
86 echo ""
87 if [[ -d $InstallPart/$TargetDir/icons ]] ; then
88 rm -rf $InstallPart/$TargetDir/icons-backup &> /dev/null
89 mv -f $InstallPart/$TargetDir/icons $InstallPart/$TargetDir/icons-backup
90 echo "Notice: Backed up existing icons directory as icons-backup."
91 fi
92 cp -r $IconsDir $InstallPart/$TargetDir
93 if [[ $? != 0 ]] ; then
94 Problems=1
95 fi
96 if [[ -f $InstallPart/$TargetDir/refind.conf ]] ; then
97 echo "Existing refind.conf file found; copying sample file as refind.conf-sample"
98 echo "to avoid collision."
99 echo ""
100 cp -f $ConfFile $InstallPart/$TargetDir
101 if [[ $? != 0 ]] ; then
102 Problems=1
103 fi
104 else
105 echo "Copying sample configuration file as refind.conf; edit this file to configure"
106 echo "rEFInd."
107 echo ""
108 cp -f $ConfFile $InstallPart/$TargetDir/refind.conf
109 if [[ $? != 0 ]] ; then
110 Problems=1
111 fi
112 fi
113 } # CopyRefindFiles()
114
115
116 #
117 # A series of OS X support functions....
118 #
119
120 # Mount the ESP at /Volumes/ESP or determine its current mount
121 # point.
122 # Sets InstallPart to the ESP mount point
123 # Sets UnmountEsp if we mounted it
124 MountOSXESP() {
125 # Identify the ESP. Note: This returns the FIRST ESP found;
126 # if the system has multiple disks, this could be wrong!
127 Temp=`diskutil list | grep EFI`
128 Esp=/dev/`echo $Temp | cut -f 5 -d ' '`
129 # If the ESP is mounted, use its current mount point....
130 Temp=`df | grep $Esp`
131 InstallPart=`echo $Temp | cut -f 6 -d ' '`
132 if [[ $InstallPart == '' ]] ; then
133 mkdir /Volumes/ESP &> /dev/null
134 mount -t msdos $Esp /Volumes/ESP
135 if [[ $? != 0 ]] ; then
136 echo "Unable to mount ESP! Aborting!\n"
137 exit 1
138 fi
139 UnmountEsp=1
140 InstallPart="/Volumes/ESP"
141 fi
142 } # MountOSXESP()
143
144 # Control the OS X installation.
145 # Sets Problems=1 if problems found during the installation.
146 InstallOnOSX() {
147 echo "Installing rEFInd on OS X...."
148 if [[ $1 == 'esp' || $1 == 'ESP' ]] ; then
149 MountOSXESP
150 else
151 InstallPart="/"
152 fi
153 echo "Installing rEFInd to the partition mounted at '$InstallPart'"
154 Platform=`ioreg -l -p IODeviceTree | grep firmware-abi | cut -d "\"" -f 4`
155 CopyRefindFiles
156 if [[ $1 == 'esp' || $1 == 'ESP' ]] ; then
157 bless --mount $InstallPart --setBoot --file $InstallPart/$TargetDir/$Refind
158 else
159 bless --setBoot --folder $InstallPart/$TargetDir --file $InstallPart/$TargetDir/$Refind
160 fi
161 if [[ $? != 0 ]] ; then
162 Problems=1
163 fi
164 if [[ -f /Library/StartupItems/rEFItBlesser ]] ; then
165 echo
166 echo "/Library/StartupItems/rEFItBlesser file found!"
167 echo "This program is part of rEFIt, and will cause rEFInd to fail to work after"
168 echo -n "its first boot. Do you want to remove rEFItBlesser (Y/N)? "
169 read YesNo
170 if [[ $YesNo == "Y" || $YesNo == "y" ]] ; then
171 echo "Deleting /Library/StartupItems/rEFItBlesser..."
172 rm /Library/StartupItems/rEFItBlesser
173 else
174 echo "Not deleting rEFItBlesser."
175 fi
176 fi
177 echo
178 echo "WARNING: If you have an Advanced Format disk, *DO NOT* attempt to check the"
179 echo "bless status with 'bless --info', since this is known to cause disk corruption"
180 echo "on some systems!!"
181 echo
182 } # InstallOnOSX()
183
184
185 #
186 # Now a series of Linux support functions....
187 #
188
189 # Identifies the ESP's location (/boot or /boot/efi); aborts if
190 # the ESP isn't mounted at either location.
191 # Sets InstallPart to the ESP mount point.
192 FindLinuxESP() {
193 EspLine=`df /boot/efi | grep boot`
194 InstallPart=`echo $EspLine | cut -d " " -f 6`
195 EspFilesystem=`grep $InstallPart /etc/mtab | cut -d " " -f 3`
196 if [[ $EspFilesystem != 'vfat' ]] ; then
197 echo "/boot/efi doesn't seem to be on a VFAT filesystem. The ESP must be mounted at"
198 echo "/boot or /boot/efi and it must be VFAT! Aborting!"
199 exit 1
200 fi
201 echo "ESP was found at $InstallPart using $EspFilesystem"
202 } # MountLinuxESP
203
204 # Uses efibootmgr to add an entry for rEFInd to the EFI's NVRAM.
205 # If this fails, sets Problems=1
206 AddBootEntry() {
207 Efibootmgr=`which efibootmgr 2> /dev/null`
208 if [[ $Efibootmgr ]] ; then
209 modprobe efivars &> /dev/null
210 InstallDisk=`grep $InstallPart /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
211 PartNum=`grep $InstallPart /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
212 EntryFilename=$TargetDir/$Refind
213 EfiEntryFilename=`echo ${EntryFilename//\//\\\}`
214 ExistingEntry=`$Efibootmgr -v | grep $Refind`
215 if [[ $ExistingEntry ]] ; then
216 echo "An existing EFI boot manager entry for rEFInd seems to exist:"
217 echo
218 echo "$ExistingEntry"
219 echo
220 echo "This entry is NOT being modified, and no new entry is being created."
221 else
222 $Efibootmgr -c -l $EfiEntryFilename -L rEFInd -d $InstallDisk -p $PartNum &> /dev/null
223 if [[ $? != 0 ]] ; then
224 EfibootmgrProblems=1
225 Problems=1
226 fi
227 fi
228 else
229 EfibootmgrProblems=1
230 Problems=1
231 fi
232 if [[ $EfibootmgrProblems ]] ; then
233 echo
234 echo "ALERT: There were problems running the efibootmgr program! You may need to"
235 echo "rename the $Refind binary to the default name (EFI/boot/bootx64.efi"
236 echo "on x86-64 systems or EFI/boot/bootia32.efi on x86 systems) to have it run!"
237 echo
238 fi
239 } # AddBootEntry()
240
241 # Controls rEFInd installation under Linux.
242 # Sets Problems=1 if something goes wrong.
243 InstallOnLinux() {
244 echo "Installing rEFInd on Linux...."
245 FindLinuxESP
246 CpuType=`uname -m`
247 if [[ $CpuType == 'x86_64' ]] ; then
248 Platform="EFI64"
249 elif [[ $CpuType == 'i386' || $CpuType == 'i486' || $CpuType == 'i586' || $CpuType == 'i686' ]] ; then
250 Platform="EFI32"
251 echo
252 echo "CAUTION: This Linux installation uses a 32-bit kernel. 32-bit EFI-based"
253 echo "computers are VERY RARE. If you've installed a 32-bit version of Linux"
254 echo "on a 64-bit computer, you should manually install the 64-bit version of"
255 echo "rEFInd. If you're installing on a Mac, you should do so from OS X. If"
256 echo "you're positive you want to continue with this installation, answer 'Y'"
257 echo "to the following question..."
258 echo
259 echo -n "Are you sure you want to continue (Y/N)? "
260 read ContYN
261 if [[ $ContYN == "Y" || $ContYN == "y" ]] ; then
262 echo "OK; continuing with the installation..."
263 else
264 exit 0
265 fi
266 else
267 echo "Unknown CPU type '$CpuType'; aborting!"
268 exit 1
269 fi
270 CopyRefindFiles
271 AddBootEntry
272 } # InstallOnLinux()
273
274 #
275 # The main part of the script. Sets a few environment variables,
276 # performs a few startup checks, and then calls functions to
277 # install under OS X or Linux, depending on the detected platform.
278 #
279
280 OSName=`uname -s`
281 ThisDir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
282 RefindDir="$ThisDir/refind"
283 ThisScript="$ThisDir/`basename $0`"
284 CheckForFiles
285 if [[ `whoami` != "root" ]] ; then
286 echo "Not running as root; attempting to elevate privileges via sudo...."
287 sudo $ThisScript $1
288 if [[ $? != 0 ]] ; then
289 echo "This script must be run as root (or using sudo). Exiting!"
290 exit 1
291 else
292 exit 0
293 fi
294 fi
295 if [[ $OSName == 'Darwin' ]] ; then
296 InstallOnOSX $1
297 elif [[ $OSName == 'Linux' ]] ; then
298 InstallOnLinux
299 else
300 echo "Running on unknown OS; aborting!"
301 fi
302
303 if [[ $Problems ]] ; then
304 echo
305 echo "ALERT:"
306 echo "Installation has completed, but problems were detected. Review the output for"
307 echo "error messages and take corrective measures as necessary. You may need to"
308 echo "re-run this script or install manually before rEFInd will work."
309 echo
310 else
311 echo
312 echo "Installation has completed successfully."
313 echo
314 fi
315
316 if [[ $UnmountEsp ]] ; then
317 umount $InstallPart
318 fi