Ubuntu – How to uninstall GRUB

grub2uninstall

A hard drive that I use only for data storage still has GRUB from past Ubuntu installations.

How can I remove GRUB from it without harming the rest of the drive's data?

Background

I occasionally move the data drive between computers with various boot order configurations, so I would like it to be non-bootable in order to avoid having to accommodate it in each computer's BIOS settings.

When I power on a computer while only the data drive is attached, the following appears:

error: no such device: fdf38dd4-9e9d-479d-b830-2a6989958503.
grub rescue> 

I can confirm from old backups of /etc/fstab that this was the UUID of a root partition that I recently reformatted and which no longer exists. Here's the the data drive's partition table and raw master boot record.

Please note that I'm not interested in workarounds that don't answer my primary question. I can think of several ways to work around this issue, but it bothers me on principle that I don't know how to directly resolve it. Every installation procedure should have a counterpart uninstallation procedure.

Best Answer

You can render the device not bootable simply by making the first few bytes of the disk 0x00.

Typically (and this is true for both grub, grub2 and ntldr iirc) the very first byte of your drive is going to be an x86 jmp instruction. This occurs before even the disklabel, because when passing execution to the device to bootstrap it, it simply sets the CPU to suck in the device information as code. If it has invalid code it triggers an interrupt and the BIOS handles the exception and goes to the next bootable device.

For instance, the beginning of my disk starts with:

00000000  eb 63 90 d0 bc 00 7c fb  50 07 50 1f fc be 1b 7c  |.c....|.P.P....||

The first part is eb 63 which is Jump to offset 0x63 from the current IP (so to 0x65).

00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|

Execution continues from here.

The end of the sector looks like this:

000001b0  cd 10 ac 3c 00 75 f4 c3  ed db 96 d6 00 00 80 01  |...<.u..........|
000001c0  01 00 83 fe ff ff 3f 00  00 00 c1 07 a6 0e 00 fe  |......?.........|
000001d0  ff ff 83 fe ff ff 00 60  00 11 00 00 38 29 00 fe  |.......`....8)..|
000001e0  ff ff 82 fe ff ff 00 08  a6 0e 00 58 5a 02 00 00  |...........XZ...|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|

If your disk is formatted as an MBR partition table then it only needs two things to be present, the partition table which is at offset 0x1be and the MBR signature, 55aa which occurs at the very end of the sector at offset 0x1fe. 0x1be is decimal 446.

The following will (of course) make the device unbootable. But this is what you want. If you don't want to make your device unable to be booted then don't do this, mmm-kay? I'm assuming your device is /dev/sdz, simply because not many people have a /dev/sdz, and this lowers the risk of some idiot newbie blindly copy pasting commands.

First, copy the MBR to a file for a backup.

sudo dd if=/dev/sdz of=/some/where/safe/preferably/not/on/dev/sdz/backup.mbr bs=512 count=1

Next, make a copy of that file:

cp backup.mbr backup.mbr.test

Next, we have to create a loopback device (so that the contents don't get truncated.) And apply the changes on our fake sector 0 as a test:

sudo losetup /dev/loop7 backup.mbr.test
sudo dd if=/dev/zero of=/dev/loop7 bs=446 count=1
sudo losetup -d /dev/loop7

hexdump the file and make sure that the entire partition table is intact:

sudo hexdump -C backup.mbr.test

You should see something like:

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 01  |................|
000001c0  01 00 83 fe ff ff 3f 00  00 00 c1 07 a6 0e 00 fe  |......?.........|
000001d0  ff ff 83 fe ff ff 00 60  00 11 00 00 38 29 00 fe  |.......`....8)..|
000001e0  ff ff 82 fe ff ff 00 08  a6 0e 00 58 5a 02 00 00  |...........XZ...|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

Now, 0x1be is where you see 80 on the hexdumped output, this can also be 00 and still be valid. (It's the "bootable" flag in the partition table, you can leave it alone, because it's completely ignored by most modern BIOSes...) The byte at 0x1bf though will almost never be 0x00 (it's most commonly 0x01 but it can take other values) you can compare this against your backup.mbr to make sure that nothing past 0x1be is changed.

Once you're satisfied that you applied the change correctly then you can directly copy the file over the first part of the disk. The reason why you want to do the file rather than /dev/zero again is for safety against typos. If you accidentally omit count=1 you're gonna have a bad time, copying a file on the other hand will never run past the EOF, ever. So it's safer.

sudo dd if=backup.mbr.test of=/dev/sdz

Next hexdump your disk to make sure that the changes took as expected.

hexdump -C /dev/sdz | head

Compare up to 0x200 against backup.mbr.test to make sure it's what you want.

Finally, if anything screws up for whatever reason you can simply copy the backup of the MBR back onto the drive via:

sudo dd if=backup.mbr of=/dev/sdz

Hope this helps.

Related Question