]> code.delx.au - refind/blob - install.sh
Small menu improvements; bug fixes & improvements to install.sh
[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 #|| ! -f $RefindDir/refind.conf-sample || ! -d $RefindDir/icons
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 echo
165 echo "WARNING: If you have an Advanced Format disk, *DO NOT* attempt to check the"
166 echo "bless status with 'bless --info', since this is known to cause disk corruption"
167 echo "on some systems!!"
168 echo
169 } # InstallOnOSX()
170
171
172 #
173 # Now a series of Linux support functions....
174 #
175
176 # Identifies the ESP's location (/boot or /boot/efi); aborts if
177 # the ESP isn't mounted at either location.
178 # Sets InstallPart to the ESP mount point.
179 FindLinuxESP() {
180 EspLine=`df /boot/efi | grep boot`
181 InstallPart=`echo $EspLine | cut -d " " -f 6`
182 EspFilesystem=`grep $InstallPart /etc/mtab | cut -d " " -f 3`
183 if [[ $EspFilesystem != 'vfat' ]] ; then
184 echo "/boot/efi doesn't seem to be on a VFAT filesystem. The ESP must be mounted at"
185 echo "/boot or /boot/efi and it must be VFAT! Aborting!"
186 exit 1
187 fi
188 echo "ESP was found at $InstallPart using $EspFilesystem"
189 } # MountLinuxESP
190
191 # Uses efibootmgr to add an entry for rEFInd to the EFI's NVRAM.
192 # If this fails, sets Problems=1
193 AddBootEntry() {
194 Efibootmgr=`which efibootmgr 2> /dev/null`
195 if [[ $Efibootmgr ]] ; then
196 modprobe efivars &> /dev/null
197 InstallDisk=`grep $InstallPart /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
198 PartNum=`grep $InstallPart /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
199 EntryFilename=$TargetDir/$Refind
200 EfiEntryFilename=`echo ${EntryFilename//\//\\\}`
201 ExistingEntry=`$Efibootmgr -v | grep $Refind`
202 if [[ $ExistingEntry ]] ; then
203 echo "An existing EFI boot manager entry for rEFInd seems to exist:"
204 echo
205 echo "$ExistingEntry"
206 echo
207 echo "This entry is NOT being modified, and no new entry is being created."
208 else
209 $Efibootmgr -c -l $EfiEntryFilename -L rEFInd -d $InstallDisk -p $PartNum &> /dev/null
210 if [[ $? != 0 ]] ; then
211 EfibootmgrProblems=1
212 Problems=1
213 fi
214 fi
215 else
216 EfibootmgrProblems=1
217 Problems=1
218 fi
219 if [[ $EfibootmgrProblems ]] ; then
220 echo
221 echo "ALERT: There were problems running the efibootmgr program! You may need to"
222 echo "rename the $Refind binary to the default name (EFI/boot/bootx64.efi"
223 echo "on x86-64 systems or EFI/boot/bootia32.efi on x86 systems) to have it run!"
224 echo
225 fi
226 } # AddBootEntry()
227
228 # Controls rEFInd installation under Linux.
229 # Sets Problems=1 if something goes wrong.
230 InstallOnLinux() {
231 echo "Installing rEFInd on Linux...."
232 FindLinuxESP
233 CpuType=`uname -m`
234 if [[ $CpuType == 'x86_64' ]] ; then
235 Platform="EFI64"
236 elif [[ $CpuType == 'i386' || $CpuType == 'i486' || $CpuType == 'i586' || $CpuType == 'i686' ]] ; then
237 Platform="EFI32"
238 echo
239 echo "CAUTION: This Linux installation uses a 32-bit kernel. 32-bit EFI-based"
240 echo "computers are VERY RARE. If you've installed a 32-bit version of Linux"
241 echo "on a 64-bit computer, you should manually install the 64-bit version of"
242 echo "rEFInd. If you're installing on a Mac, you should do so from OS X. If"
243 echo "you're positive you want to continue with this installation, answer 'Y'"
244 echo "to the following question..."
245 echo
246 echo -n "Are you sure you want to continue (Y/N)? "
247 read ContYN
248 if [[ $ContYN == "Y" || $ContYN == "y" ]] ; then
249 echo "OK; continuing with the installation..."
250 else
251 exit 0
252 fi
253 else
254 echo "Unknown CPU type '$CpuType'; aborting!"
255 exit 1
256 fi
257 CopyRefindFiles
258 AddBootEntry
259 } # InstallOnLinux()
260
261 #
262 # The main part of the script. Sets a few environment variables,
263 # performs a few startup checks, and then calls functions to
264 # install under OS X or Linux, depending on the detected platform.
265 #
266
267 OSName=`uname -s`
268 ThisDir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
269 RefindDir="$ThisDir/refind"
270 ThisScript="$ThisDir/`basename $0`"
271 CheckForFiles
272 if [[ `whoami` != "root" ]] ; then
273 echo "Not running as root; attempting to elevate privileges via sudo...."
274 sudo $ThisScript $1
275 if [[ $? != 0 ]] ; then
276 echo "This script must be run as root (or using sudo). Exiting!"
277 exit 1
278 else
279 exit 0
280 fi
281 fi
282 if [[ $OSName == 'Darwin' ]] ; then
283 InstallOnOSX $1
284 elif [[ $OSName == 'Linux' ]] ; then
285 InstallOnLinux
286 else
287 echo "Running on unknown OS; aborting!"
288 fi
289
290 if [[ $Problems ]] ; then
291 echo
292 echo "ALERT:"
293 echo "Installation has completed, but problems were detected. Review the output for"
294 echo "error messages and take corrective measures as necessary. You may need to"
295 echo "re-run this script or install manually before rEFInd will work."
296 echo
297 else
298 echo
299 echo "Installation has completed successfully."
300 echo
301 fi
302
303 if [[ $UnmountEsp ]] ; then
304 umount $InstallPart
305 fi