]>
code.delx.au - refind/blob - mvrefind
3 # Linux script to move an existing rEFInd installation from one directory to
6 # copyright (c) 2013-2015 by Roderick W. Smith
8 # This program is licensed under the terms of the GNU GPL, version 3,
9 # or (at your option) any later version.
10 # You should have received a copy of the GNU General Public License
11 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # ./mvrefind /path/to/source /path/to/destination
17 # Typically used to "hijack" or "unhijack" a Windows boot loader location or
18 # to help convert a rEFInd installation made in BIOS mode to one that works
23 # 0.10.2 -- Fixed bug in moving bootmgfw.efi in some situations
24 # 0.10.1 -- Generalized to support ARM64 (aka AARCH64, aa64)
25 # 0.10.0 -- Renamed from mvrefind.sh to mvrefind
26 # 0.6.3 -- Initial release
28 # Note: mvrefind version numbers match those of the rEFInd package
29 # with which they first appeared.
33 TargetShim
=$SourceShim
34 SourceDir
=`readlink -f ${1}`
35 TargetDir
=`readlink -f ${2}`
37 # Identifies the ESP's location (/boot or /boot/efi); aborts if the ESP isn't
38 # mounted at either location. Also splits the ESP location from SourceDir and
39 # TargetDir, leaving them intact but creating new EspSourceDir and EspTargetDir
40 # variables containing only the ESP components thereof. These new variables
41 # are also converted to all-lowercase and any trailing slash is stripped, to
42 # assist in comparisons. (This is reasonable because FAT is case-insensitive.)
43 # Sets InstallDir to the ESP mount point.
45 EspLine
=`df $RootDir/boot/efi 2> /dev/null | grep boot/efi`
46 if [[ ! -n $EspLine ]] ; then
47 EspLine
=`df $RootDir/boot | grep boot`
49 InstallDir
=`echo $EspLine | cut -d " " -f 6`
50 if [[ -n $InstallDir ]] ; then
51 EspFilesystem
=`grep $InstallDir /etc/mtab | cut -d " " -f 3`
53 if [[ $EspFilesystem != 'vfat' ]] ; then
54 echo "$RootDir/boot/efi doesn't seem to be on a VFAT filesystem. The ESP must be"
55 echo "mounted at $RootDir/boot or $RootDir/boot/efi and it must be VFAT! Aborting!"
59 # Sanity check on source & target....
60 EspPathLength
=`expr length $InstallDir`
61 Temp
=`echo $SourceDir | cut -c 1-$EspPathLength`
62 if [[ $Temp != $InstallDir ]] ; then
63 echo "$SourceDir isn't on the ESP ($InstallDir)! Aborting!"
66 Temp
=`echo $TargetDir | cut -c 1-$EspPathLength`
67 if [[ $Temp != $InstallDir ]] ; then
68 echo "$TargetDir isn't on the ESP ($InstallDir)! Aborting!"
72 # Temporarily replace "/" in pathnames with ",", so as to enable sed to
74 TempInstallDir
=`echo $InstallDir | tr '/' ','`
75 Temp
=`echo $SourceDir | tr '/' ',' | sed s/${TempInstallDir}//g | tr ',' '/' | tr '[A-Z]' '[a-z]'`
76 EspSourceDir
=`dirname $Temp`/`basename $Temp`
77 Temp
=`echo $TargetDir | tr '/' ',' | sed s/${TempInstallDir}//g | tr ',' '/' | tr '[A-Z]' '[a-z]'`
78 EspTargetDir
=`dirname $Temp`/`basename $Temp`
79 if [[ $EspSourceDir == $EspTargetDir ]] ; then
80 echo "$SourceDir is the same as $TargetDir! Aborting!"
98 echo "Unsupported CPU type; aborting!"
101 Source
="refind_$Platform.efi"
105 # Adjust filename variables appropriately for their locations and detected
106 # presence (or lack thereof) of shim installation
108 if [[ -f $SourceDir/grub
$Platform.efi
]] ; then
109 Source
="grub$Platform.efi"
111 if [[ $EspSourceDir == "/efi/boot" ]] ; then
112 SourceShim
="boot$Platform.efi"
113 elif [[ $EspSourceDir == "/efi/microsoft/boot" ]] ; then
114 SourceShim
="bootmgfw.efi"
119 if [[ $EspSourceDir == "/efi/boot" ]] ; then
120 Source
="boot$Platform.efi"
121 elif [[ $EspSourceDir == "/efi/microsoft/boot" ]] ; then
122 Source
="bootmgfw.efi"
126 if [[ $EspTargetDir == "/efi/boot" ]] ; then
127 if [[ $TargetShim == "none" ]] ; then
128 Target
="boot$Platform.efi"
130 TargetShim
="boot$Platform.efi"
132 elif [[ $EspTargetDir == "/efi/microsoft/boot" ]] ; then
133 if [[ $TargetShim == "none" ]] ; then
134 Target
="bootmgfw.efi"
136 TargetShim
="bootmgfw.efi"
139 } # AdjustFilenames()
141 # Checks for the presence of necessary files, including both boot loaders
142 # and support utilities (efibootmgr, etc.)
144 if [[ (! -f $SourceDir/$Source) ||
145 ($SourceShim != "none" && ! -f $SourceDir/SourceShim
) ||
146 ! -f $SourceDir/refind.conf
]] ; then
147 echo "There doesn't seem to be a rEFInd installation at $SourceDir!"
151 if [[ $EspTargetDir != "/efi/boot" && $EspTargetDir != "/efi/microsoft/boot" ]] ; then
152 Efibootmgr
=`which efibootmgr 2> /dev/null`
153 if [[ ! -f $Efibootmgr ]] ; then
154 echo "Moving to a non-default directory requires a working efibootmgr utility, but"
155 echo "one can't be found! Aborting!"
157 elif [[ ! -d "/sys/firmware/efi" ]] ; then
158 echo "Moving to a non-default directory requires a boot into EFI mode, but we seem"
159 echo "to be running in BIOS mode. (Perhaps typing 'modprobe efivars' will fix this."
165 # Do final checks & then move the files!
167 ExistingFiles
=`find $TargetDir -name "*.efi" 2> /dev/null`
168 if [[ -n $ExistingFiles && $EspTargetDir != "/efi/boot" && $EspTargetDir != "/efi/microsoft/boot" ]] ; then
169 echo "$TargetDir isn't empty! Aborting!"
173 if [[ $EspTargetDir == "/efi/boot" && -d $TargetDir ]] ; then
174 if [[ -d $InstallDir/EFI
/BOOT-rEFIndBackup
]] ; then
176 echo "Caution: An existing backup of a default boot loader exists! If the current"
177 echo "default boot loader and the backup are different boot loaders, the current"
178 echo "one will become inaccessible."
180 echo -n "Do you want to proceed with moving (Y/N)? "
182 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
183 echo "OK; continuing with the move..."
188 mv $TargetDir $InstallDir/EFI
/BOOT-refindBackup
&> /dev
/null
192 if [[ $EspTargetDir == "/efi/microsoft/boot" && -d $TargetDir ]] ; then
193 mv -n $TargetDir/bootmgfw.efi
$InstallDir/EFI
/Microsoft
/
197 mv $SourceDir/icons
$TargetDir/ 2> /dev
/null
198 mv $SourceDir/icons-backup
$TargetDir/ 2> /dev
/null
199 mv $SourceDir/drivers_
* $TargetDir/ 2> /dev
/null
200 mv $SourceDir/keys
$TargetDir 2> /dev
/null
201 mv $SourceDir/$Source $TargetDir/$Target 2> /dev
/null
202 mv $SourceDir/$SourceShim $TargetDir/$TargetShim 2> /dev
/null
203 mv $SourceDir/refind.conf
* $TargetDir/ 2> /dev
/null
204 rmdir $SourceDir 2> /dev
/null
207 # Clean up after moving files -- mainly restoring old backed-up files, if present
209 if [[ $EfiSourceDir == "/efi/boot" && -d $InstallDir/EFI
/BOOT-rEFIndBackup
&& ! -d $SourceDir ]] ; then
210 mv $InstallDir/EFI
/BOOT-rEFIndBackup
$SourceDir 2> /dev
/null
212 if [[ $EfiSourceDir == "/efi/microsoft/boot" && -f $InstallDir/EFI
/Microsoft
/bootmgfw.efi
]] ; then
213 mv -n $InstallDir/EFI
/Microsoft
/bootmgfw.efi
$SourceDir/bootmgfw.efi
215 } # PostMoveCleanup()
217 # If necessary, create a new NVRAM entry for the new location
220 Efibootmgr
=`which efibootmgr 2> /dev/null`
221 InstallDisk
=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
222 PartNum
=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
224 if [[ $TargetShim != "none" ]] ; then
225 EntryFilename
=$EspTargetDir/$TargetShim
227 EntryFilename
=$EspTargetDir/$Target
230 EfiEntryFilename
=`echo ${EntryFilename//\//\\\}`
231 EfiEntryFilename2
=`echo ${EfiEntryFilename} | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g`
232 ExistingEntry
=`$Efibootmgr -v | grep -i $EfiEntryFilename2`
234 if [[ $ExistingEntry ]] ; then
235 ExistingEntryBootNum
=`echo $ExistingEntry | cut -c 5-8`
236 FirstBoot
=`$Efibootmgr | grep BootOrder | cut -c 12-15`
237 if [[ $ExistingEntryBootNum != $FirstBoot ]] ; then
238 $Efibootmgr -b $ExistingEntryBootNum -B &> /dev
/null
245 if [[ $InstallIt == "1" ]] ; then
246 if [[ $EspTargetDir == "/efi/microsoft/boot" ]] ; then
247 # Name it the way some firmware expects -- see http://mjg59.dreamwidth.org/20187.html
248 $Efibootmgr -c -l $EfiEntryFilename -L "Windows Boot Manager" -d $InstallDisk -p $PartNum &> /dev
/null
250 $Efibootmgr -c -l $EfiEntryFilename -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum &> /dev
/null
252 if [[ $?
!= 0 ]] ; then
257 if [[ $EfibootmgrProblems ]] ; then
259 echo "ALERT: There were problems running the efibootmgr program! Your moved rEFInd"
260 echo "might not run!"
266 # Main body of script
269 if [[ $# != 2 ]] ; then
270 echo "Usage: $0 {source-directory} {target-directory}"
273 if [[ `whoami` != "root" ]] ; then
274 echo "Not running as root! Aborting!"