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