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