Saturday, July 07, 2012

Converting Ubuntu Linux with GPT system disk back to MBR

GPT and EFI. Worst. Idea. Ever. At least with WTF-8 aka UTF-8 your system boots while looking funny, but with GPT all you get is black screen with blinking cursor at best. Sometimes even your cursor is taken away.

Recently I installed Ubuntu 12.04. Well actually it's Lubuntu since there's no other Ubuntu variant with usable GUI left and CentOS is competing hard with Slackware for most outdated packages in repository title. Anyway, this PC got EFI crap instead of usual reliable BIOS implementation.

All new PCs have EFI which means Ubuntu installer forces GPT partitions on you even with small system disk. In my case system disk is 160GB Intel G2 SSD. Using GPT with EFI is fine as long as it works. Did you know for example that adding second hard disk to such PC will corrupt your system disk and prevent system from booting? You didn't? Well yes it does so be careful out there.

You have two options. Either every time you make even smallest change to system - even restarting it with USB stick or USB HDD attached is enough - do reset-bios-to-defaults, boot-ubuntu-live-from-usb making sure you select EFI USB boot and not Legacy USB boot, re-install-grub cycle or you go back to MBR and forget GPT. Only time you need GPT is with boot disks over 2TB. That's over 2TB, disks sold as 2TB are 1,81TB and work just fine with MBR.

After spending too much time fighting to get EFI+GPT boot reliable I did only sensible thing there's to do: convert to MBR. But how do you do that without losing your existing system? Below how I think I did it, didn't actually take any notes as it was more of trial-and-error troubleshooting.

- Create USB stick with Lubuntu 12.04 on Windows. Make sure you use LinuxLive USB Creator as those are compatible with pretty much any PC unlike Universal USB Installer that might work or might not work. Naturally everyone is promoting Universal USB Installer and completely forgot that it's not only option. Especially when it's non-working option.

- Disable EFI boot sources on your BIOS. Enable Legacy boot with USB stick as primary.

- Boot from USB stick, make sure it's legacy USB booting, not EFI USB boot

- Open root shell

- Make sure EFI is disabled and /sys/firmware/efi directory is MISSING

- Install gdisk (apt-get install gdisk)

gdisk /dev/sdb
press "r"
press "g"
press "p"
press "o" and omit first partition (EFI MBR protective whatever, type EF00)
change root partition (0x07) to Linux (82) and swap to 83
press "w" to save changes

Check THIS for better explanation what's going on above.

- Shutdown. Pull power cord (yes you need to do that)

- Boot from same USB stick without using EFI

- Verify partition table is ok (fdisk /dev/sdb) and make sdb2 active if it isn't already

- Mount old root and chroot to it

mount /dev/sdb2 /mnt
mount -B /dev /mnt/dev
mount -B /dev/pts /mnt/dev/pts
mount -B /sys /mnt/sys
mount -B /proc /mnt/proc
cp /etc/resolv.conf /mnt/etc/resolv.conf # to make sure DNS resolution works
chroot /mnt

- Force grub and kernel reinstall

mv /boot/grub /boot/grub.old
mv /boot/efi /boot/efi.old
apt-get -y --reinstall install grub-efi-amd64 linux-image-generic

- Remove /boot/efi mount from /etc/fstab

- Shutdown. Pull power cable, yes, you need to do it again. After all this is EFI system. EFI is great!

- Remove USB stick

- Boot system, go to BIOS and make sure EFI is still disabled and legacy HDD boot is selected

- Done. Now it's working again and you can even add / remove disks without manually repairing boot sector and EFI config each time.

No comments:

Post a Comment

Got something to say?!