udoprog.github.io

Dual Booting: Debian Wheezy & Windows 8.1

So I recently got myself in a situation where I had to convert a system that booted Windows 8.1 through EFI, and Debian through MBR legacy into grub.

Of course I had been using my Debian partition for a while and was unkeen to get rid of the rather large amount of changes I’ve already done.

What got me into this mess

I recently acquired a new computer, and being the cheap bastard I am I only purchased one new SSD for it. So to get my priorities straight: at home I mostly game. So this disk was bound to run Windows.

After a pretty happy installation I sadly discovered that my old hard drive was starting to fail. It was also causing boot times into windows to increase so I had to get rid of it.

After about a month of struggling to get a workable Windows environment I’ve had enough and ordered another SSD for work.

Installing Debian the Naive way

Grab an installer image from https://www.debian.org/distrib/

Hopefully you’ll end up with an ISO (I like torrents), now burn it onto a USB stick.

#> sudo dd if=image.iso of=/dev/<usb> bs=4M
#> sync
  • Boot into the newly created USB by messing around with your BIOS.
  • Install it on one of your newly purchased SDDs.
  • Hapilly start using your newly installed system.

By now you should have a working system and are wondering what all the EFI fuzz is about.

Growing tired of entering BIOS every day

Eventually you should be growing tired of having to go into BIOS to switch which system to boot into. In my particular case my shitty-yet-awesome keyboard decides to roll a dice if it should work pre-boot or not.

So lets figure out how to get a Windows entry into GRUB instead.

Chainloading Windows 8.1

So, lets chainload our Windows 8.1 partition from within GRUB.

What does it look like?

#> fdisk -l /dev/sdb

WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.

Ah right, GPT. We’re gonna have to use gdisk.

#> gdisk -l /dev/sdb
...
   1            2048          206847   100.0 MiB   EF00  EFI system partition
   2          206848          468991   128.0 MiB   0C01  Microsoft reserved ...
   3          468992         1083391   300.0 MiB   2700  Basic data partition
   4         1083392      1000214527   476.4 GiB   0700  Basic data partition

Nice, but this is pretty far fetched from plain old MBR partition tables.

Time to mount the EFI system partition and find the windows OS loader.

#> sudo mkdir /boot/efi
#> mount /dev/sdb1 /boot/efi
#> find /boot/efi -name bootmgfw.efi

We’ll also need to find the UUID of the EFI partition so that grub knows where to load the OS Loader from.

#> ls -l /dev/disk/by-uuid | grep sdb1
lrwxrwxrwx 1 root root 10 Oct  4 17:10 52A9-DDD7 -> ../../sdb1

Using this information, lets setup a custom boot entry for Windows 8.1.

#> cat /etc/grub.d/40_custom
... snip

menuentry "Windows 8.1" {
    insmod part_gpt
    insmod chain
    search --fs-uuid --no-floppy --set=root 52A9-DDD7
    chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}

Let’s reboot and try it out!

GRUB - Error: Invalid Signature

So trying out the meny item was unsuccesful. Grub gives is an error saying Error: Invalid Signature, what the hell?

After some headscratching we remember the Naive debian installation we recently performed. The GRUB we are using uses MBR and legacy boot. None of the fancy EFI stuff is available to us, least of all the ability to verify OS Loader signatures.

Crap. Time to catch up.

Luckily Ubuntu is trying very hard to become EFI compliant and have done a ton of investigation. All of it documented on their wiki.

After reading through the available information, the gist of it is.

  • You must boot into an EFI ‘environment’ in order to modify it’s variables in NVRAM (what the hell?).
    How this is done depends on your BIOS. (Typically the boot entries that can perform this are prefixed with UEFI:)
  • You must either have enabled ‘Secure Boot’ (correctly signed EFI OS Loader), or disable it in whatever manner is available to your BIOS (if possible).
  • Lastly, GRUB can only chainload an EFI OS Loader if itself is started through an EFI OS Loader.
    It can however also load legacy MBR systems in this mode.

So our goal is: Install GRUB into the EFI system partition so it can boot both our legacy MBR disk, and Windows 8.1 through it’s EFI OS Loader

Installing GRUB into the EFI system partition

We will try to do the following.

  • Boot into a linux system through EFI.
  • chroot into our previously installed Debian system.
  • re-install grub into the EFI system partition.

The best live cd I could find to accomplish this was Ubuntu (>= 12.04.2) 64bit. As per recommendation on their wiki page, this contains the necessary partitions and EFI loader. You can find one here, (the torrent is the fastest option). take care to download a amd64 version.

Burn it to a USB in a similar manner as before and boot.

When messing around in BIOS, make sure that when you boot into it that it indicates that it is trying to boot into UEFI.

Check for the UEFI: prefix or some other indicator.

After boot, check that you have access to the EFI firmware.

#> stat /sys/firmware/efi
  File: ‘/sys/firmware/efi’
  Size: 0               Blocks: 0          IO Block: 4096   directory
...

Cool, lets setup our chroot (assuming /dev/sda1 is your debian partition).

#> sudo mkdir /debian
#> sudo mount /dev/sda1 /debian
#> sudo mount --bind /sys /debian/sys
#> sudo mount --bind /proc /debian/proc
#> sudo mount --bind /dev /debian/dev
#> sudo mount --bind /dev/pts /debian/dev/pts
#> sudo chroot /debian

Now lets reconfigure grub.

#> sudo apt-get install --reinstall grub-efi-amd64

And finally, install grub on the Windows disk with the GPT table.

#> sudo grub-install /dev/sdb

Time to reboot!

You should now be able to find another UEFI: partition in BIOS, this time called Debian.

Make this your default.

Summary

We now have two disks, sda and sdb.

  • sda contains Debian, and is partitioned using MBR.
  • sdb contains Windows 8.1, and is partitioned using GPT to boot through EFI. The EFI system partition contains OS Loaders for GRUB and Windows. GRUB is capable of reading the partition table of sda and its configuration file.

Good luck with your new dual boot system!