About Me

My photo
I know the last digit of PI

Friday, August 14, 2015

Updating to Windows 10 when having GRUB 2 bootloader installed in MBR.



Usually when having dualboot of Windows and Linux the GRUB 2 bootloader is installed in MBR. When trying to update to Windows 10 it fails with the following error "We Can't Tell if your PC has enough space to continue".
Well the solution for updating to Windows 10 is simple - restore the hardisk Master Boot Record (MBR) using Windows installation disc (it can be also created via Microsoft's MediaCreationTool - the same tool used for updating to Windows 10. Just on the first screen choose not to update, but "Create installation media for another PC" and write it to DVD/USB).
Boot from the Windows installation DVD/USB and follow instruction until screen that allows you to install windows or "Repair your computer". Choose to "Repair your computer" / "Troubleshoot" / "Advanced options" / "Command prompt"
Execute following command:
bootrec.exe /fixmbr
Reboot and start the update process again. Now the update should be successful.
Now Windows boots successfully but Linux is no longer accessible. One way to make Linux bootable again is to use Windows bootloader to load Linux. I prefer this method because I don't want to face any future problems with windows updates.
We need a copy of the GRUB2 exported as file. For that reason we will install GRUB on Partition Boot Record (PBR) and copy the boot sector to file. First we must access the installed Linux partitions. Create a "Live CD" on DVD/USB and boot from it. The from the shell mount your existing linux installation. Let's assume that /dev/sda3 is the linux boot partition, /dev/sda4 is the root partition. Now mount the volumes:
mount /dev/sda4 /mnt
mount /dev/sda3 /mnt/boot
mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
Optionally you may mount home directory
mount /dev/sdaX /mnt/home

Now do chroot:
chroot /mnt
Reinstall grup 2 on boot partition
grub2-install /dev/sda3
You may receive following error:
"warning: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. However, blocklists are UNRELIABLE and their use is discouraged.
error: will not proceed with blocklists"
Then just use --force
grub2-install --force /dev/sda3
Once the installation complets create a dump of the bootsector:
dd if=/dev/sda3 of=/tmp/linux.bootsector.bin bs=512 count=1
copy the /tmp/linux.bootsector.bin  to some partition accessible by Windows.
mkdir /mnt/c
mount /dev/sda2 /mnt/c
cp /tmp/linux.bootsector.bin /mnt/c
umount /mnt/c
Now reboot and start Windows and we need to create a new BCD entry for Linux
Open administrative command prompt (right click on "command prompt" and choose run as administrator)
bcdedit /create /d “Linux” /application BOOTSECTOR
The command will return GUID e.g. {5474794d-1fe8-4008-a0ae-d10210214f2a}
That GUID will be use in next commands.
Configure new BCD entry's partition and path to the location where linux.bootsector.bin is copied
bcdedit /set {5474794d-1fe8-4008-a0ae-d10210214f2a} device partition=C: 
bcdedit /set {5474794d-1fe8-4008-a0ae-d10210214f2a}  PATH \linux.bootsector.bin
Then configure the new entry to be shown last, and the OS selection menu timeout to be 5 seconds.
bcdedit /displayorder {5474794d-1fe8-4008-a0ae-d10210214f2a}  /addlast
bcdedit /timeout 5
Reboot and now you will be able to boot either Windows or Linux.

No comments: