]>
code.delx.au - refind/blob - mvrefind
3 # Linux script to move an existing rEFInd installation from one directory to another
7 # ./mvrefind /path/to/source /path/to/destination
9 # Typically used to "hijack" or "unhijack" a Windows boot loader location or
10 # to help convert a rEFInd installation made in BIOS mode to one that works
15 # 0.10.0 -- Renamed from mvrefind.sh to mvrefind
16 # 0.6.3 -- Initial release
18 # Note: mvrefind version numbers match those of the rEFInd package
19 # with which they first appeared.
22 SourceX64
="refind_x64.efi"
24 SourceIA32
="refind_ia32.efi"
25 TargetIA32
=$SourceIA32
27 TargetShim
=$SourceShim
28 SourceDir
=`readlink -f ${1}`
29 TargetDir
=`readlink -f ${2}`
31 # Identifies the ESP's location (/boot or /boot/efi); aborts if the ESP isn't
32 # mounted at either location. Also splits the ESP location from SourceDir and
33 # TargetDir, leaving them intact but creating new EspSourceDir and EspTargetDir
34 # variables containing only the ESP components thereof. These new variables
35 # are also converted to all-lowercase and any trailing slash is stripped, to
36 # assist in comparisons. (This is reasonable because FAT is case-insensitive.)
37 # Sets InstallDir to the ESP mount point.
39 EspLine
=`df $RootDir/boot/efi 2> /dev/null | grep boot/efi`
40 if [[ ! -n $EspLine ]] ; then
41 EspLine
=`df $RootDir/boot | grep boot`
43 InstallDir
=`echo $EspLine | cut -d " " -f 6`
44 if [[ -n $InstallDir ]] ; then
45 EspFilesystem
=`grep $InstallDir /etc/mtab | cut -d " " -f 3`
47 if [[ $EspFilesystem != 'vfat' ]] ; then
48 echo "$RootDir/boot/efi doesn't seem to be on a VFAT filesystem. The ESP must be"
49 echo "mounted at $RootDir/boot or $RootDir/boot/efi and it must be VFAT! Aborting!"
53 # Sanity check on source & target....
54 EspPathLength
=`expr length $InstallDir`
55 Temp
=`echo $SourceDir | cut -c 1-$EspPathLength`
56 if [[ $Temp != $InstallDir ]] ; then
57 echo "$SourceDir isn't on the ESP ($InstallDir)! Aborting!"
60 Temp
=`echo $TargetDir | cut -c 1-$EspPathLength`
61 if [[ $Temp != $InstallDir ]] ; then
62 echo "$TargetDir isn't on the ESP ($InstallDir)! Aborting!"
66 # Temporarily replace "/" in pathnames with ",", so as to enable sed to
68 TempInstallDir
=`echo $InstallDir | tr '/' ','`
69 Temp
=`echo $SourceDir | tr '/' ',' | sed s/${TempInstallDir}//g | tr ',' '/' | tr '[A-Z]' '[a-z]'`
70 EspSourceDir
=`dirname $Temp`/`basename $Temp`
71 Temp
=`echo $TargetDir | tr '/' ',' | sed s/${TempInstallDir}//g | tr ',' '/' | tr '[A-Z]' '[a-z]'`
72 EspTargetDir
=`dirname $Temp`/`basename $Temp`
73 if [[ $EspSourceDir == $EspTargetDir ]] ; then
74 echo "$SourceDir is the same as $TargetDir! Aborting!"
79 # Adjust filename variables appropriately for their locations and detected
80 # presence (or lack thereof) of shim installation
82 if [[ -f $SourceDir/grubx64.efi
]] ; then
83 SourceX64
="grubx64.efi"
85 if [[ $EspSourceDir == "/efi/boot" ]] ; then
86 SourceShim
="bootx64.efi"
87 elif [[ $EspSourceDir == "/efi/microsoft/boot" ]] ; then
88 SourceShim
="bootmgfw.efi"
93 if [[ $EspSourceDir == "/efi/boot" ]] ; then
94 SourceX64
="bootx64.efi"
95 SourceIA32
="bootia32.efi"
96 elif [[ $EspSourceDir == "/efi/microsoft/boot" ]] ; then
97 SourceX64
="bootmgfw.efi"
101 if [[ $EspTargetDir == "/efi/boot" ]] ; then
102 if [[ $TargetShim == "none" ]] ; then
103 TargetX64
="bootx64.efi"
104 TargetIA32
="bootia32.efi"
106 TargetShim
="bootx64.efi"
108 elif [[ $EspTargetDir == "/efi/microsoft/boot" ]] ; then
109 if [[ $TargetShim == "none" ]] ; then
110 TargetX64
="bootmgfw.efi"
112 TargetShim
="bootmgfw.efi"
115 } # AdjustFilenames()
117 # Checks for the presence of necessary files, including both boot loaders
118 # and support utilities (efibootmgr, etc.)
120 if [[ (! -f $SourceDir/$SourceX64 && ! -f $SourceDir/$SourceIA32) ||
121 ($SourceShim != "none" && ! -f $SourceDir/SourceShim
) ||
122 ! -f $SourceDir/refind.conf
]] ; then
123 echo "There doesn't seem to be a rEFInd installation at $SourceDir!"
127 if [[ $EspTargetDir != "/efi/boot" && $EspTargetDir != "/efi/microsoft/boot" ]] ; then
128 Efibootmgr
=`which efibootmgr 2> /dev/null`
129 if [[ ! -f $Efibootmgr ]] ; then
130 echo "Moving to a non-default directory requires a working efibootmgr utility, but"
131 echo "one can't be found! Aborting!"
133 elif [[ ! -d "/sys/firmware/efi" ]] ; then
134 echo "Moving to a non-default directory requires a boot into EFI mode, but we seem"
135 echo "to be running in BIOS mode. (Perhaps typing 'modprobe efivars' will fix this."
141 # Do final checks & then move the files!
143 ExistingFiles
=`find $TargetDir -name "*.efi" 2> /dev/null`
144 if [[ -n $ExistingFiles && $EspTargetDir != "/efi/boot" && $EspTargetDir != "/efi/microsoft/boot" ]] ; then
145 echo "$TargetDir isn't empty! Aborting!"
149 if [[ $EspTargetDir == "/efi/boot" && -d $TargetDir ]] ; then
150 if [[ -d $InstallDir/EFI
/BOOT-rEFIndBackup
]] ; then
152 echo "Caution: An existing backup of a default boot loader exists! If the current"
153 echo "default boot loader and the backup are different boot loaders, the current"
154 echo "one will become inaccessible."
156 echo -n "Do you want to proceed with moving (Y/N)? "
158 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
159 echo "OK; continuing with the move..."
164 mv $TargetDir $InstallDir/EFI
/BOOT-refindBackup
&> /dev
/null
168 if [[ $EspTargetDir == "/efi/microsoft/boot" && -d $TargetDir ]] ; then
169 mv -n $EspTargetDir/bootmgfw.efi
$InstallDir/EFI
/Microsoft
/
173 mv $SourceDir/icons
$TargetDir/ 2> /dev
/null
174 mv $SourceDir/icons-backup
$TargetDir/ 2> /dev
/null
175 mv $SourceDir/drivers_
* $TargetDir/ 2> /dev
/null
176 mv $SourceDir/keys
$TargetDir 2> /dev
/null
177 mv $SourceDir/$SourceX64 $TargetDir/$TargetX64 2> /dev
/null
178 mv $SourceDir/$SourceIA32 $TargetDir/$TargetIA32 2> /dev
/null
179 mv $SourceDir/$SourceShim $TargetDir/$TargetShim 2> /dev
/null
180 mv $SourceDir/refind.conf
* $TargetDir/ 2> /dev
/null
181 rmdir $SourceDir 2> /dev
/null
184 # Clean up after moving files -- mainly restoring old backed-up files, if present
186 if [[ $EfiSourceDir == "/efi/boot" && -d $InstallDir/EFI
/BOOT-rEFIndBackup
&& ! -d $SourceDir ]] ; then
187 mv $InstallDir/EFI
/BOOT-rEFIndBackup
$SourceDir 2> /dev
/null
189 if [[ $EfiSourceDir == "/efi/microsoft/boot" && -f $InstallDir/EFI
/Microsoft
/bootmgfw.efi
]] ; then
190 mv -n $InstallDir/EFI
/Microsoft
/bootmgfw.efi
$SourceDir/bootmgfw.efi
192 } # PostMoveCleanup()
194 # If necessary, create a new NVRAM entry for the new location
197 Efibootmgr
=`which efibootmgr 2> /dev/null`
198 InstallDisk
=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
199 PartNum
=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
201 if [[ $TargetShim != "none" ]] ; then
202 EntryFilename
=$EspTargetDir/$TargetShim
205 if [[ $CpuType == 'x86_64' ]] ; then
206 EntryFilename
=$EspTargetDir/$TargetX64
208 EntryFilename
=$EspTargetDir/$TargetIA32
212 EfiEntryFilename
=`echo ${EntryFilename//\//\\\}`
213 EfiEntryFilename2
=`echo ${EfiEntryFilename} | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g`
214 ExistingEntry
=`$Efibootmgr -v | grep -i $EfiEntryFilename2`
216 if [[ $ExistingEntry ]] ; then
217 ExistingEntryBootNum
=`echo $ExistingEntry | cut -c 5-8`
218 FirstBoot
=`$Efibootmgr | grep BootOrder | cut -c 12-15`
219 if [[ $ExistingEntryBootNum != $FirstBoot ]] ; then
220 $Efibootmgr -b $ExistingEntryBootNum -B &> /dev
/null
227 if [[ $InstallIt == "1" ]] ; then
228 if [[ $EfiTargetDir == "/efi/microsoft/boot" ]] ; then
229 # Name it the way some firmware expects -- see http://mjg59.dreamwidth.org/20187.html
230 $Efibootmgr -c -l $EfiEntryFilename -L "Windows Boot Manager" -d $InstallDisk -p $PartNum &> /dev
/null
232 $Efibootmgr -c -l $EfiEntryFilename -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum &> /dev
/null
234 if [[ $?
!= 0 ]] ; then
239 if [[ $EfibootmgrProblems ]] ; then
241 echo "ALERT: There were problems running the efibootmgr program! Your moved rEFInd"
242 echo "might not run!"
248 # Main body of script
251 if [[ $# != 2 ]] ; then
252 echo "Usage: $0 {source-directory} {target-directory}"
255 if [[ `whoami` != "root" ]] ; then
256 echo "Not running as root! Aborting!"