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