Date Sun 28 January 2018 Tags devops linux debian virtualbox

Have you ever wanted to boot your Macbook into Linux but don't want to risk attempting a dual boot install? I did. I didn't want to use a Live Boot system. I wanted a proper install. This is how I did it.

The Prologue

When I got my Macbook Pro a few years ago I set it up to dual boot with Debian. I never used it much as my desktop machine runs Debian, but it was nice to know that it could. However when I attempted an update from Sierra to High Sierra I ended up in a world of pain. I won't go into the details but suffice to say the Time Machine backup saved me, even if it did take me two days, several attempts and a complete reinstall to get the machine back to the state it was before the High Sierra upgrade...but without the dual boot capability.

So I decided not to try that again, but I still wanted to be able to boot my Mac into Debian. The Mac can boot off of USB devices so I got a USB external drive and went about trying to figure out how to install Debian to it in such a way that the Mac would boot it.

What I didn't want to do was boot the Mac off a Debian install drive and attempt to install onto the USB drive. I didn't want to risk the installer messing with the boot loader or the NVRAM of the Mac.

Enter VirtualBox

VirtualBox has two things that help in this scenario. The first is the ability to pass-thru a USB device to the virtual machine. This would allow the VM to see the USB device directly. The second is the ability to set a VM to have EFI which is what the Mac uses to boot.

I created a VM with no hard drive.

I left everything else as the defaults. Then I went to the settings of the VM and in the System tab ticked the Enable EFI (special OSes only) option.

Under Storage I set the CD image to a downloaded Debian 9.3 installer. I started the VM and whilst it was on the boot menu I went to the Devices menu, then to USB and clicked on my USB hard drive.

Install and go

I ran the installer as if I were installing to any other VM. I got a screen that says that the VM may already have an existing OS using BIOS compatibility mode. I just selected the option to Force UEFI installation.

The important bit

My limited understanding of UEFI is that the computer's NVRAM contains details of bootable OSes. When you install a new OS, this NVRAM gets updated. However I had installed onto a USB device in a VM so it was the VM's NVRAM has been updated, not the Mac's.

This is why I didn't want to just boot the Mac into a Debian installer and install onto a USB drive from there. I didn't want to mess with the Mac at all. But as a result of doing the install in VirtualBox the Mac will not be aware of the Debian install as it's NVRAM doesn't know about it.

UEFI can boot from removable devices. When Debian is installed in normal (non-expert) mode the Grub bootloader is installed in the non-removable device way. When I installed Debian using the expert mode I was asked about this during the installation of Grub, so I just needed to tell the installer to install in the "removable media path".

The message is something like

Some EFI-based systems are buggy and do not handle new bootloaders correctly. If you force an extra installation of GRUB to the EFI removable media path, this should ensure that this system will boot Debian correctly despite such a problem. However, it may remove the ability to boot any other operating systems that also depend on this path. If so, you will need to make sure that GRUB is configured successfully to be able to boot any other OS installations correctly.

So the easy way to fix this was to install Debian in expert mode. However I also tried without using expert mode to see what would happen. Sure enough the Mac didn't recognise the USB drive as bootable.

To fix this required reconfiguring Grub. I tried again and at the end of the installation in VirtualBox the VM reboots. It didn't boot automatically into Debian but dropped into the UEFI Interactive Shell so I had to reattach the USB drive to the VM through the Devices menu, then USB and select the USB drive, and reboot the VM.

A helpful hint at this point. If I restarted VirtualBox then attempted to boot the VM off my USB device I would get dropped into the UEFI shell. It turns out that VirtualBox's boot menu is DVD/CD drive, UEFI shell, then USB device in that order. To get it to boot the USB drive I typed exit in the UEFI shell, went to the Boot Manager menu and selected the EFI USB Device

Once I was back in the Debian VM I logged in and - as the root user - ran

dpkg-reconfigure grub-efi-amd64

and, when prompted with this screen

I chose Yes. When prompted if the NVRAM should be updated I chose No. This should prevent updates to grub from altering the Mac's NVRAM when you are running from Linux. However I haven't tested that yet so if you are trying this please beware when upgrading Grub.

From a technical point of view the difference is down to where the bootloader is installed. For a fixed drive it ends up in /boot/efi/EFI/Debian/grubx64.efi . For a removable drive it is /boor/efi/EFI/BOOT/BOOTX64.EFI It is the latter one that the Mac will look for to boot a removable device.

Boot it up

I rebooted the Mac and held down Option (aka Alt) whilst it booted. I was presented with a choice of two drives to boot from; the internal drive that booted into the Mac OS, and the USB drive that will show as "EFI Boot" and booted into Debian.

To change the icon on the boot menu I grabbed a Debian icon from https://github.com/nanoant/efi/tree/master/boot/themes/mac/icons and, whilst in Debian, copied it to /boot/efi/.VolumeIcon.icns

And there you have it. A bootable USB drive with a Debian installation on and no changes to the Mac.

Happy Linuxing.

Darren @ Æ