Monday, July 15, 2013

Dual booting on EFI systems

Some time ago, I chose to reinstall Windows on my home computer. I was using Server 2012 thanks to Dreamspark. I was most excited for trying ReFS, but it didn't feel any faster than ntfs. Of course, its main feature isn't speed, but integrity, specially if you mount it over a mirrored raid array. But what I didn't like the most about WinServer was that it took so long to boot up.

So ok, I spent some money and bought a copy of Windows 8. It sure was faster to boot up, and having superfetch back was a nice addition as well, even more if you have lots of ram. My motherboard right now is a Gigabyte Z77 UD3H. It allows booting using UEFI or the "legacy" bios style (quoted because it still is quite used). I chose to install Windows in EFI mode. It really doesn't feel much different than MBR mode, but I know that it made an EFI partition and the boot loader was there. Everything is cool so far.

Recently my coworkers convinced me to install a Linux flavor in my work laptop. It comes preloaded with Windows 7, but most of them replaced it with Linux. They wonder how could I bare Windows? Well, it does the job (actually the computer I use the most comes with Linux, with Android on top of it, and fits in a pocket). Since I work as a JVM programmer (ex-Java, nowadays Scala), it is really the same to me what OS I have running. But I decided to install Linux in order to relearn its console commands. I say relearn because I had to use Linux before, in a previous job. I chose Linux Mint to vary a bit from Ubuntu.

Anyway, I was able to shrink my Windows partition using the Disk Management console. This laptop was using MBR and its disk had already three primary partitions (four is the limit of primary partitions for a MBR disk). The system partition with Windows installed on it, a reserved partition, and a recovery partition. Now I added the four and last one, and partition to format with ext4 and install Mint on it. But I also needed a swap partition! So I ended up ditching the recovery partition and put the swap there.

Linux booted really fast compared to Windows, and somehow I felt it does a better use of this laptop resources. So I wanted to install some Linux flavor at home as well. This time I chose Ubuntu, version 13.04, for this task because it's well supported and there are pages talking about how to install it using UEFI. They are mostly related to new computers having Secure Boot enabled, but that was not my case.

First, I again shrunk the Windows partition on the SSD to make room for Ubuntu. I moved some stuff (like games) to a partition in a regular mechanical disk and made sure that disk also have free space. My idea was to install the root directory to the SSD, and swap and home to the HDD. Having 16gb of ram I was quite sure paging would barely occur, so I didn't want to waste SSD space installing it there.

Afterwards, I burnt Ubuntu's Live CD, boot it and installed Ubuntu. It automatically detected my EFI partition, and when asked where to install the boot loader, I chose my root partition /dev/sda5 instead of the entire device /dev/sda. Then the installation proceeded.

But what happened? I was asked to reboot, and then it booted straight to Windows! I was hoping for GRUB to appear. At least I was able to boot and open the browser to find out what the deal was.

After reading some blogs, I rebooted to the Live CD and went to /boot/efi. There it mounts the EFI partition I had on my SSD. There I had:

  • /boot/efi/EFI/Boot/bootx64.efi Microsoft's boot loader
  • /boot/efi/EFI/ubuntu/grubx64.efi Ubuntu's boot loader
  • /boot/efi/EFI/Microsoft/... all Windows stuff

I backed up bootx64 and copied grubx64 to bootx64 in Boot folder. When I rebooted it went straight to Ubuntu!

Now I made sure that Ubuntu was installed well (HDD's home mounted and such), updated to the latest version of its software, installed Chrome and the rest. It was handy that I had direct access to the EFI partition right there. Later I learnt that I could access the EFI partition within Windows with mountvol /s.

I knew that in the motherboard's firmware there was an entry for the Windows Boot Manager, which Windows added when it was installed. Now it was accessing GRUB, but there was no option to boot to Windows from GRUB. I read that I could add that using chainloading, but I didn't understand quite well how that was done. I wanted to see if it was possible to add a GRUB entry on the system's firmware and choose there if booting to Windows or to Ubuntu, so I rebooted and entered BIOS.

Now I learnt a few things:

1) This motherboard can boot the system in UEFI or BIOS mode. The UEFI options are (not literal):

  • Windows Boot Manager
  • UEFI: first disk (the ssd)
  • UEFI: dvd

and then the BIOS options were the ssd, the hdd and the dvd unit.

It turned out that the first option was UEFI: ssd and this one went to load bootx64 (grub now). If I chose Windows Boot Manager it booted Windows.

2) The BIOS support is called CSM for Compatibility Support Module. I tried disabling it, and then when I rebooted, the screen was all black and it went straight to Ubuntu. It was faster, maybe because it didn't have to load all the legacy BIOS stuff. But then again, I was unable not only to enter BIOS but also to choose the operating system. I ended up clearing the CMOS.

3) This motherboard has no EFI shell. I downloaded a shell and installed it as bootx64. Now when I booted I went straight to a shell. Fun...

Suddenly I stumbled upon what I was looking for: rEFInd. A GUI where I could choose what EFI system to load. I installed it following their instructions and it automatically detected my Windows and Ubuntu systems, with pretty icons and such. Sadly it seems its graphics support relies on the already established system, so when I tried disabling CSM again, nothing appeared, just a blank screen. I cleared the CMOS again and the system was back to normal.

Now rEFInd serves as my EFI Boot Manager, where I choose if I want to boot to Windows or to Ubuntu. I know I could have used GRUB for something similar, or maybe even the motherboard's boot selection to choose both operating systems, but I like how this manager looks, and I feel I learnt much about the EFI boot process. I've read many sites in my quest to gather enough knowledge as to avoid messing things up, and now I'm writing this post before this knowledge is gone.