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:

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.

Just curious - if you delete the files in /boot/grub (which I assumed you did), does the mbr code really matter? I don't think it will get used by anything else, will it? I could be wrong, but I wouldn't think it would be used, and I'd hate to screw around with something at such a low level if I cared about the data.
–
Marty FriedMay 4 '12 at 2:12

Can you dump the raw MBR data and post it here? You should be able to do something like sfdisk -d /dev/sdb > sdb.out.
–
BreakthroughMay 5 '12 at 16:39

5 Answers
5

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.

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.

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:

Those byte counts look uncomfortably arbitrary. Do you know if they're the same for GRUB2?
–
ændrükMay 4 '12 at 2:08

1

The byte count is because the partition table lies between 446 and 512. Of course, this begs the question of why you want to remove the grub MBR... it isn't hurting anything just sitting there unused. If you want another boot loader instead, just install it and it will replace grub.
–
psusiMay 4 '12 at 2:39

3

Wow, this type of answer should have "WARNING: EXTREMELY DANGEROUS" written in big red letters all over it. I'm sure the OP is capable of doing this but I'd hate to see some newbie user copy-pasting the first command into the terminal without even knowing what "partition table" is
–
SergeyMay 4 '12 at 2:47

1

Do not do this. The first command will wipe out the partition table (as the OP mentioned), but the second command will cause undefined behaviour if the MBR is not configured properly.
–
BreakthroughMay 5 '12 at 16:36

1

Umm.. I don't know why you guys are freaking out, the commands that tachyons pasted don't do anything at all. You can test with touch testfile, dd if=/dev/urandom of=testfile bs=512 count=1, sudo losetup /dev/loop7 testfile, sudo dd if=/dev/null of=/dev/loop7 bs=446 count=1, sudo hexdump -Cv /dev/loop7. As you can see /dev/null is not a 0 source, it's a EOF source. dd cannot and will not copy anything from /dev/null you need to use /dev/zero. Second @Breakthrough, no undefined behavior is possible if the first byte of sector 0 is 0x00. I don't know why you think that.
–
OmnipotentEntityJul 26 '12 at 17:47

is, that it successfully un-installed grub2 from /dev/sda (where my Windows 7 is installed), so the first part of the question "How do I remove grub from /dev/sda?" has been answered.

However, the 2nd part of the question, which is "How do I restore the MBR of /dev/sda?" has not been answered since the install-mbr command failed to restore the MBR. As a result, Windows does not boot any more and the Windows boot manager reports an error about a damaged MBR and asks the user to repair from a windows repair CD.

After reading the Wikipedia article on the subject I'd like to propose a few additional solutions:

Change boot order in BIOS :)

The best and the safest one: use fdisk to remove "bootable" flag from any partitions on that drive. Most MBRs look for a "bootable" partition to chain-load from, so I would expect GRUB to just do nothing if there are no such partitions. Haven't tested though.

If the above does not help, try installing a free clone of standard MBR code:

From reading the Wikipedia article, I have an impression that the only thing which identifies the MBR is its signature which is at the very end of the sector (bytes 510 and 511). The first 446 bytes of MBR supposed to contain machine instructions. The BIOS is supposed to transfer control to the bootloader regardless of the actual contents of the first 446 bytes, provided that MBR signature is present:

On IBM PC-compatible computers, the bootstrapping firmware contained
within the ROM BIOS loads and executes the master boot record.[14]...
Thus, the beginning of the MBR is expected to contain real mode
machine language instructions.[14] The BIOS reads the MBR from the
storage device into physical memory, and then directs the
microprocessor to the start of the boot code.

Due to the restricted size of the MBR's code section, it typically
contains only a small program that copies additional code (such as a
boot loader) from the storage device into memory. Control is then
passed to this code, which is responsible for loading the actual
operating system.

...

The bootstrap sequence in the BIOS will load the first valid MBR that
it finds into the computer's physical memory at address 0x7C00. The
last instruction executed in the BIOS code will be a "jump" to that
address, to direct execution to the beginning of the MBR copy. The
primary validation for most BIOSes is the 0xAA55 signature on the end,
although a BIOS implementor may choose to include other checks, such
verifying that the MBR contains a valid partition table without
entries referring to sectors beyond the reported capacity of the disk.

So my understanding is that MBR is always supposed to contain a bootloader, and zeroing the first 446 bytes of it would not stop BIOS from trying to boot from the disk - but it is likely to make the computer hang while trying to execute invalid code.

UPDATE: Also, this article suggests that to make the disk to look "un-bootable" for BIOS you should actually edit the MBR signature at the and of the sector (using any disk editor). I'm not sure if it's going to affect OS seeing the partition table on the disk though... but at least you can always modify those bytes back...

In my case i had Debian linux but wanted to use Mandriva, will work for others too

Turn off your pc, then remove the disk that boots that you dont want to boot (that has grub)

Simply put in a bootable usb made from the mandriva iso or other variant you like to install there are tools for making bootable usb sticks from iso files use google
(or you could us a burned installer from cd rom)

Now most linux installers give you a choice of what to do, try and play / use for evaluation or portable linux or run setup to install it. At this point we just wait (move up down cursor so the screen will wait but dont press enter or click with the mouse).

Just remind at this point your USB / or / CDRom has started and is running.
now its time to plugin back the harddisk that we temporaly removed
wait a minute (some bios do require a small wait a minute is more then enough)

Continue to setup process since most installers contain partitions tools you can do whatever you want. well its a simple solution, i got rid of an old linux setup simply as a beginner