Happy time with recent GRUB2 update with Ubuntu 12.04 LTS

My short story of dealing with failed grub2 install which rendered my server unbootable.

It was all good till recent update of grub2. Apparently it no longer fits into MBR and I missed related warnings. Or may be I had grub-legacy. I do not know honestly, but in result I had un-bootable remote server.

Using rescue mode (which is like Live-CD, but instead of GUI gives you SSH access so you could restore your server) I was able to get into server and try to update grub. Here is what I got for grub-install /dev/sda:

# grub-install /dev/sda
/usr/sbin/grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding won't be possible!.
/usr/sbin/grub-setup: warn: Embedding is not possible.  GRUB can only be installed in this setup by using blocklists.  However, blocklists are UNRELIABLE and their use is discouraged..
/usr/sbin/grub-setup: error: will not proceed with blocklists.

Basically, it is saying that it needs to put its installer somewhere and can’t find place for it. For grub2 there is relatively small part of code which get executed after code in MBR finish. This code run different modules to detect file systems, software raids, etc. It is small, few kilobytes. There are two places where it can be placed on MBR partitioned disk – either right before first partition (called embedding) or placing all code in the regular files system and store specific physical position of these files on the disk in the MBR (called blocklists.) Blocklists approach is not reliable due to the fact that files might move and if physical location in MBR will not update – grub code will never get a chance to start.

After I figured out all of the above (which took a while – I have problem like this for the first time, so if I explained something wrong – please let me know,) I decided to find a place before first partition to use embedding option. I’ve found there is about 700Kb of free space between 1st and 2nd partition:

Number  Start   End     Size    Type     File system     Flags
        0.00B   511B    512B             Free Space
 1      512B    512MB   512MB   primary  ext2            raid
        512MB   513MB   753kB            Free Space
 2      513MB   2560MB  2047MB  primary  linux-swap(v1)
 3      2560MB  954GB   951GB   primary  ext4            raid
        954GB   1000GB  46.3GB           Free Space

Somehow my server was setup with first partition starting with sector 1 while usually first partition starts with sector 63 specifically for purposes to give enough space for all kind of pre-boot software. So in theory I can move 1st partition closer to 2nd and get these necessary few kilobytes to put grub2 code. Easiest for me was to mount /boot partition (that’s what first partition used for currently,) copy all files in a safe location, delete this partition and recreate it on a new place.

To do precise positioning on the disk, I switched to “sector” units in “parted”:

# parted
(parted) unit s
(parted) print free                                                      
Model: ATA WDC WD10EARS-003 (scsi)
Disk /dev/sda: 1953525168s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start        End          Size         Type     File system     Flags
        0s           0s           1s                    Free Space
 1      1s           1000000s     1000000s     primary  ext2            raid
        1000001s     1001471s     1471s                 Free Space
 2      1001472s     4999167s     3997696s     primary  linux-swap(v1)
 3      4999168s     1863024639s  1858025472s  primary  ext4            raid
        1863024640s  1953525167s  90500528s             Free Space

So now I remove old partition, create new one, format it with same file system, mount and copy all files over:

(parted) mkpart primary 1471s 1001471s
Warning: The resulting partition is not properly aligned for best
Ignore/Cancel? Ignore
(parted) set 1 raid on                                                   
(parted) quit                                                            
# mke2fs /dev/sda1

At this point I have empty ext2 partition at /dev/sda1 which starts at sector 1471. Now copy all files back over to new boot partition and run grub-install to finally get no errors!

# mount /dev/md1 /mnt
# mount /dev/md0 /mnt/boot
# mount --bind /proc /mnt/proc
# mount --bind /dev /mnt/dev
# mount --bind /sys /mnt/sys
# grub-install --boot-directory=/mnt/boot /dev/sda

Reboot and after that it likely will boot :)