Wednesday, January 14, 2015

Creating a GPT partition with GRUB

Here are the steps to create a GPT partition and install GRUB on it. There are a few different ways of doing this, but these are the general steps.

First find the path to the device we're using, in this case  USB stick at /dev/sdb:
$ sudo fdisk -l
Disk /dev/sda: 128.0 GB, 128035676160 bytes

255 heads, 63 sectors/track, 15566 cylinders, total 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001e3bf

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   244058111   122028032   83  Linux
/dev/sda2       244060158   250068991     3004417    5  Extended
/dev/sda5       244060160   250068991     3004416   82  Linux swap / Solaris

Disk /dev/sdb: 8178 MB, 8178892800 bytes
252 heads, 62 sectors/track, 1022 cylinders, total 15974400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ea687

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4194303     2096128   83  Linux
/dev/sdb2         4194304     8386559     2096128   83  Linux
 There's a couple partitions there, so let's wipe them out:
$ sudo dd if=/dev/zero of=/dev/sdb bs=1M count=3232+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 10.8418 s, 3.1 MB/s
With an empty device, we start by slapping a GPT label on it:
$ sudo parted /dev/sdb mklabel gpt
Information: You may need to update /etc/fstab.
Now create two partitions, a small one for MBR and a second to store the GRUB files. The sizes here are somewhat random and don't need to be this large:
$ sudo parted /dev/sdb mkpart primary 0% 1%
Information: You may need to update /etc/fstab.

$ sudo parted /dev/sdb mkpart primary 2% 5%
Information: You may need to update /etc/fstab.
Let's take a look at things and set the bios_grub and hidden flags on the first partition:
$ sudo parted /dev/sdb
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: SCSIDISK SCSI_DISK_1234 (scsi)
Disk /dev/sdb: 8179MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB primary
2 164MB 409MB 245MB primary

(parted) set 1 bios_grub
New state? [on]/off?

(parted) set 1 hidden
New state? [on]/off?
Another quick look to make sure things are where they should be, then create a file system on the second partition. As the warning below suggests, we don't have to use parted(1) but for this simple example it's good enough.
(parted) print
Model: SCSIDISK SCSI_DISK_1234 (scsi)
Disk /dev/sdb: 8179MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  2097kB  1049kB               primary  hidden, bios_grub
 2      164MB   409MB   245MB                primary
$ sudo parted /dev/sdb mkfs
WARNING: you are attempting to use parted to operate on (mkfs) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs.  We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Warning: The existing file system will be destroyed and all data on the  
partition will be lost. Do you want to continue?
parted: invalid token: 2
Yes/No? yes
Partition number? 2
File system type?  [ext2]?
Information: You may need to update /etc/fstab.
Mount the device and create a 'boot' directory where we'll install GRUB on:
$ sudo mkdir /mnt/sdb2
$ sudo mount /dev/sdb2 /mnt/sdb2
$ sudo mkdir /mnt/sdb2/boot
Finally, install GRUB and copy the current grub.cfg to the new device:
$ sudo grub-install --boot-directory=/mnt/sdb2/boot/ /dev/sdb
Installing for i386-pc platform.
Installation finished. No error reported.
$ cp /boot/grub/grub.cfg /mnt/sdb2/boot/grub/
Booting off of this USB drive will bring up the same GRUB menu as the default (currently booted drive) so you'll at least want to change the title or something like that so you know that you're booting from USB.

No comments:

Post a Comment