GRUB GPT HOWTO

This way you can boot a disk using Grub installed on a GPT partition table.

This guide assumes you want a boot filesystem and an LVM physical volume on your GPT partitioned disk.

You may need to recompile Linux. Select the “EFI partition system” in the Filesystems area. You may like to select RAID+LVM modules to use LVM also. You probably don’t need EFI BIOS support unless your machine has this, and therefore need to get a traditional os loader to function…

Normally, Grub does not understand GPT partition tables and needs to be tricked into starting from one. You need to create a very small partition at the start of your disk to hold the grub stage2, (or stage1.5 if you would like to start stage2 from /boot)

Parted

First thing. Create your GPT partition table on your device. I suggest allocating the smallest size possible that parted lets you get away with for the first partition, that is bigger than the stage2 image. The second partition will be your /boot and holds linux and its ramfs images. I suggest around a gigabyte for this. The rest of your hard disk is allocated to the third and final partition, which is your LVM volume.

mklabel gpt
mkpart non-fs 0 2
mkpart ext3 2 130
mkpart lvm 130 30401
GNU Parted 1.8.9
Using /dev/hda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit cyl
(parted) p
Model: SAMSUNG SP2514N (ide)
Disk /dev/hda: 30401cyl
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 30401,255,63.  Each cylinder is 8225kB.
Partition Table: gpt

Number  Start   End       Size      File system  Name    Flags
 1      0cyl    2cyl      1cyl                   non-fs
 2      2cyl    130cyl    128cyl    ext3         ext3
 3      130cyl  30401cyl  30271cyl               lvm

The filesystem types were non-fs, ext3, and lvm, respectively.

Gptsync

Now, as grub does not understand this, you need a fake MBR. Enter gptsync. You can get this program by installing refit and running it on the device you just set up, after exiting grub.

gptsync /dev/hda

gptsync sets up the MBR to point to the fake partitions, hovever the partition ID's will need correcting with fdisk next. Notice that the partition numbers are increased by one compared to GPT, and there is an extra partition as the first one.

Fixup with Fdisk

fdisk /dev/hda
/dev/hda1               1           1          16+  ee  EFI GPT
/dev/hda2   *           1           3       16048+  da  Non-FS data
/dev/hda3               3         131     1028160   83  Linux
/dev/hda4             131       30402   243154342   8e  Linux LVM

Set the first MBR partition type ee, as it is your GPT partition table.

The second partition in MBR is your GPT table's first partition. We use this to store the Grub stageloader, so set the type to da meaning this is not a filesystem.

The third partition is your GPT second, being the /boot partition and given the 83 type to say so.

The fourth is the LVM to-be, in GPT it is the 3rd, and has the type 8e

To linux, the GPT table takes precedence over MBR (but check you have only 3 partition devicenodes, not 4, to be sure), thus /dev/hda1 maps to the stageloader area (rather than the GPT partition table)

If you did forget to compile in GPT support into Linux you can probably get away with this if your LVM partition is not bigger than the maximum that the MBR partition table can support. Just access the grub partition through hda2 instead of hda1 until you get round to re-compiling Linux.

Filesystems

At this time you will want to format the second GPT partition with ext3 and install the /boot files in there

Also, format the LVM volume and add a volume group, say system and add logical volumes as desired, say root and swap.

GRUB 1

Now it is time to install grub

34 is the offset in sectors from the start of the device to the first GPT partition. Each sector is customarily 512 bytes long, so you can find the start of the first GPT partition at offset 0x4400 in hexedit.

The Grub stage1 in the first sector of your hard disk is to load the stage2 from this partition.

#!/bin/bash
# erase partition
dd if=/dev/zero of=/dev/hda1

# length in sectors of stage2
FILE=/boot/grub/stage2
S=$((  ( $(stat -c %s ${FILE}) + 511 ) / 512 ))

# put loader in partition
cat "${FILE}" > /dev/hda1

# install grub
grub --batch << EOF
root (hd1,2)
install (hd1,2)/grub/stage1 (hd1) (hd1)34+${S} (hd1,2)/grub/menu.lst
EOF

Or if you prefer to use a stage 1.5, makes starting up slower but grub can then be upgraded by replacing the stage2 file on /boot.

#!/bin/bash
# erase partition
dd if=/dev/zero of=/dev/hda1

# length in sectors of chosen stage1_5
FILE=/boot/grub/e2fs_stage1_5
S=$((  ( $(stat -c %s ${FILE}) + 511 ) / 512 ))

# put loader in partition
cat "${FILE}" > /dev/hda1

# install grub
grub --batch << EOF
root (hd1,2)
install (hd1,2)/grub/stage1 (hd1) (hd1)34+${S} (hd1,2)/grub/stage2 (hd1,2)/grub/menu.lst
EOF

GRUB 2

You may also use the non-fs partition to improve resilience when starting to use GRUB 2 in place of GRUB 1. GRUB 2 can load itself from the non-fs partition and therefore avoid using blocklists.

There is some info on marking a partition for the installation of GRUB.

In this situation you may not need to use gptsync any more as GRUB 2 understands the GPT tables. boot.img replaces the GRUB1 stage1 and goes in the MBR area. core.img replaces stage2 and will be copied into the non-fs partition.

parted /dev/hda
set 1 bios_grub on
quit

grub-install /dev/hda