]>
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.1 -- Generalized to support ARM64 (aka AARCH64, aa64)
24 # 0.10.0 -- Renamed from mvrefind.sh to mvrefind
25 # 0.6.3 -- Initial release
27 # Note: mvrefind version numbers match those of the rEFInd package
28 # with which they first appeared.
32 TargetShim
=$SourceShim
33 SourceDir
=`readlink -f ${1}`
34 TargetDir
=`readlink -f ${2}`
36 # Identifies the ESP's location (/boot or /boot/efi); aborts if the ESP isn't
37 # mounted at either location. Also splits the ESP location from SourceDir and
38 # TargetDir, leaving them intact but creating new EspSourceDir and EspTargetDir
39 # variables containing only the ESP components thereof. These new variables
40 # are also converted to all-lowercase and any trailing slash is stripped, to
41 # assist in comparisons. (This is reasonable because FAT is case-insensitive.)
42 # Sets InstallDir to the ESP mount point.
44 EspLine
=`df $RootDir/boot/efi 2> /dev/null | grep boot/efi`
45 if [[ ! -n $EspLine ]] ; then
46 EspLine
=`df $RootDir/boot | grep boot`
48 InstallDir
=`echo $EspLine | cut -d " " -f 6`
49 if [[ -n $InstallDir ]] ; then
50 EspFilesystem
=`grep $InstallDir /etc/mtab | cut -d " " -f 3`
52 if [[ $EspFilesystem != 'vfat' ]] ; then
53 echo "$RootDir/boot/efi doesn't seem to be on a VFAT filesystem. The ESP must be"
54 echo "mounted at $RootDir/boot or $RootDir/boot/efi and it must be VFAT! Aborting!"
58 # Sanity check on source & target....
59 EspPathLength
=`expr length $InstallDir`
60 Temp
=`echo $SourceDir | cut -c 1-$EspPathLength`
61 if [[ $Temp != $InstallDir ]] ; then
62 echo "$SourceDir isn't on the ESP ($InstallDir)! Aborting!"
65 Temp
=`echo $TargetDir | cut -c 1-$EspPathLength`
66 if [[ $Temp != $InstallDir ]] ; then
67 echo "$TargetDir isn't on the ESP ($InstallDir)! Aborting!"
71 # Temporarily replace "/" in pathnames with ",", so as to enable sed to
73 TempInstallDir
=`echo $InstallDir | tr '/' ','`
74 Temp
=`echo $SourceDir | tr '/' ',' | sed s/${TempInstallDir}//g | tr ',' '/' | tr '[A-Z]' '[a-z]'`
75 EspSourceDir
=`dirname $Temp`/`basename $Temp`
76 Temp
=`echo $TargetDir | tr '/' ',' | sed s/${TempInstallDir}//g | tr ',' '/' | tr '[A-Z]' '[a-z]'`
77 EspTargetDir
=`dirname $Temp`/`basename $Temp`
78 if [[ $EspSourceDir == $EspTargetDir ]] ; then
79 echo "$SourceDir is the same as $TargetDir! Aborting!"
97 echo "Unsupported CPU type; aborting!"
100 Source
="refind_$Platform.efi"
104 # Adjust filename variables appropriately for their locations and detected
105 # presence (or lack thereof) of shim installation
107 if [[ -f $SourceDir/grub
$Platform.efi
]] ; then
108 Source
="grub$Platform.efi"
110 if [[ $EspSourceDir == "/efi/boot" ]] ; then
111 SourceShim
="boot$Platform.efi"
112 elif [[ $EspSourceDir == "/efi/microsoft/boot" ]] ; then
113 SourceShim
="bootmgfw.efi"
118 if [[ $EspSourceDir == "/efi/boot" ]] ; then
119 Source
="boot$Platform.efi"
120 elif [[ $EspSourceDir == "/efi/microsoft/boot" ]] ; then
121 Source
="bootmgfw.efi"
125 if [[ $EspTargetDir == "/efi/boot" ]] ; then
126 if [[ $TargetShim == "none" ]] ; then
127 Target
="boot$Platform.efi"
129 TargetShim
="boot$Platform.efi"
131 elif [[ $EspTargetDir == "/efi/microsoft/boot" ]] ; then
132 if [[ $TargetShim == "none" ]] ; then
133 Target
="bootmgfw.efi"
135 TargetShim
="bootmgfw.efi"
138 } # AdjustFilenames()
140 # Checks for the presence of necessary files, including both boot loaders
141 # and support utilities (efibootmgr, etc.)
143 if [[ (! -f $SourceDir/$Source) ||
144 ($SourceShim != "none" && ! -f $SourceDir/SourceShim
) ||
145 ! -f $SourceDir/refind.conf
]] ; then
146 echo "There doesn't seem to be a rEFInd installation at $SourceDir!"
150 if [[ $EspTargetDir != "/efi/boot" && $EspTargetDir != "/efi/microsoft/boot" ]] ; then
151 Efibootmgr
=`which efibootmgr 2> /dev/null`
152 if [[ ! -f $Efibootmgr ]] ; then
153 echo "Moving to a non-default directory requires a working efibootmgr utility, but"
154 echo "one can't be found! Aborting!"
156 elif [[ ! -d "/sys/firmware/efi" ]] ; then
157 echo "Moving to a non-default directory requires a boot into EFI mode, but we seem"
158 echo "to be running in BIOS mode. (Perhaps typing 'modprobe efivars' will fix this."
164 # Do final checks & then move the files!
166 ExistingFiles
=`find $TargetDir -name "*.efi" 2> /dev/null`
167 if [[ -n $ExistingFiles && $EspTargetDir != "/efi/boot" && $EspTargetDir != "/efi/microsoft/boot" ]] ; then
168 echo "$TargetDir isn't empty! Aborting!"
172 if [[ $EspTargetDir == "/efi/boot" && -d $TargetDir ]] ; then
173 if [[ -d $InstallDir/EFI
/BOOT-rEFIndBackup
]] ; then
175 echo "Caution: An existing backup of a default boot loader exists! If the current"
176 echo "default boot loader and the backup are different boot loaders, the current"
177 echo "one will become inaccessible."
179 echo -n "Do you want to proceed with moving (Y/N)? "
181 if [[ $YesNo == "Y" ||
$YesNo == "y" ]] ; then
182 echo "OK; continuing with the move..."
187 mv $TargetDir $InstallDir/EFI
/BOOT-refindBackup
&> /dev
/null
191 if [[ $EspTargetDir == "/efi/microsoft/boot" && -d $TargetDir ]] ; then
192 mv -n $EspTargetDir/bootmgfw.efi
$InstallDir/EFI
/Microsoft
/
196 mv $SourceDir/icons
$TargetDir/ 2> /dev
/null
197 mv $SourceDir/icons-backup
$TargetDir/ 2> /dev
/null
198 mv $SourceDir/drivers_
* $TargetDir/ 2> /dev
/null
199 mv $SourceDir/keys
$TargetDir 2> /dev
/null
200 mv $SourceDir/$Source $TargetDir/$Target 2> /dev
/null
201 mv $SourceDir/$SourceShim $TargetDir/$TargetShim 2> /dev
/null
202 mv $SourceDir/refind.conf
* $TargetDir/ 2> /dev
/null
203 rmdir $SourceDir 2> /dev
/null
206 # Clean up after moving files -- mainly restoring old backed-up files, if present
208 if [[ $EfiSourceDir == "/efi/boot" && -d $InstallDir/EFI
/BOOT-rEFIndBackup
&& ! -d $SourceDir ]] ; then
209 mv $InstallDir/EFI
/BOOT-rEFIndBackup
$SourceDir 2> /dev
/null
211 if [[ $EfiSourceDir == "/efi/microsoft/boot" && -f $InstallDir/EFI
/Microsoft
/bootmgfw.efi
]] ; then
212 mv -n $InstallDir/EFI
/Microsoft
/bootmgfw.efi
$SourceDir/bootmgfw.efi
214 } # PostMoveCleanup()
216 # If necessary, create a new NVRAM entry for the new location
219 Efibootmgr
=`which efibootmgr 2> /dev/null`
220 InstallDisk
=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 1-8`
221 PartNum
=`grep $InstallDir /etc/mtab | cut -d " " -f 1 | cut -c 9-10`
223 if [[ $TargetShim != "none" ]] ; then
224 EntryFilename
=$EspTargetDir/$TargetShim
226 EntryFilename
=$EspTargetDir/$Target
229 EfiEntryFilename
=`echo ${EntryFilename//\//\\\}`
230 EfiEntryFilename2
=`echo ${EfiEntryFilename} | sed s/\\\\\\\\/\\\\\\\\\\\\\\\\/g`
231 ExistingEntry
=`$Efibootmgr -v | grep -i $EfiEntryFilename2`
233 if [[ $ExistingEntry ]] ; then
234 ExistingEntryBootNum
=`echo $ExistingEntry | cut -c 5-8`
235 FirstBoot
=`$Efibootmgr | grep BootOrder | cut -c 12-15`
236 if [[ $ExistingEntryBootNum != $FirstBoot ]] ; then
237 $Efibootmgr -b $ExistingEntryBootNum -B &> /dev
/null
244 if [[ $InstallIt == "1" ]] ; then
245 if [[ $EfiTargetDir == "/efi/microsoft/boot" ]] ; then
246 # Name it the way some firmware expects -- see http://mjg59.dreamwidth.org/20187.html
247 $Efibootmgr -c -l $EfiEntryFilename -L "Windows Boot Manager" -d $InstallDisk -p $PartNum &> /dev
/null
249 $Efibootmgr -c -l $EfiEntryFilename -L "rEFInd Boot Manager" -d $InstallDisk -p $PartNum &> /dev
/null
251 if [[ $?
!= 0 ]] ; then
256 if [[ $EfibootmgrProblems ]] ; then
258 echo "ALERT: There were problems running the efibootmgr program! Your moved rEFInd"
259 echo "might not run!"
265 # Main body of script
268 if [[ $# != 2 ]] ; then
269 echo "Usage: $0 {source-directory} {target-directory}"
272 if [[ `whoami` != "root" ]] ; then
273 echo "Not running as root! Aborting!"