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