Unfortunately, partitioning your disk is rather complicated.

— GNU Parted User Manual

If you have no idea how partitions on hard drives work, you might enjoy reading my notes on partitioning in general.

I don’t have notes for normal Linux fdisk because I actually have all of the options completely memorized after using it extensively for 17 years. But the other day I wanted to make a RAID array and I hurriedly partitioned the physical disks with one partition each letting fdisk take the maximum space and had a nasty surprise. Despite being 4TB drives, the drives eventually partitioned around 2TB, something I didn’t notice until the RAID volume was suspiciously much smaller than it should have been.

Turns out that fdisk can no longer handle modern hard drives. Any partition over 2TB simply can’t be done (at this time, 2015).

partition length of 7814037160 sectors exceeds the
msdos-partition-table-imposed maximum of 4294967295

This means that if you have big drives (>2TB) you probably should not use msdos/MBR/fdisk style partitions.

Looking around, the answer is GNU Parted, a very different partitioning tool. Its interface is probably much more sensible seeming to people not heavily invested in fdisk and it appears to have many more features. Importantly, it can partition disks with partitions beyond the 2TB limitation of fdisk. After playing around with it a bit and understanding it better, GNU Parted is a huge improvement over fdisk.

Of course there is the official GNU Parted User Manual.

Parted has a command line shell mode but you can also enter complete commands from your own shell’s command line by doing stuff like:

parted /dev/sdc print

This example shows what the partition scheme on the drive is.

Simple Partitioning Operations

How does one recapture the functionality enjoyed with fdisk, but with large drives?

Partition Table

GNU Parted seems to want you to make what they call a "label". This is not the kind of label you create with -L when making file systems. The help mklabel command calls it "create a new disklabel (partition table)". I believe that mktable is a (better named) synonym. This command takes one of these arguments: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop. You can see that Parted is pretty flexible about handling partitions on disks no matter where they come from or go to. That’s pretty good. In the Linux universe I’m pretty much always going to use "gpt" which stands for "GUID Partition Table" which is a replacement for "MBR" partition tables, called "msdos" in Parted’s terminology. (GUID is Globally Unique IDentifier BTW.)

parted /dev/sdc mklabel gpt

Making New GPT Partitions

Now make the partitions. It can be helpful to look at the "Disk /dev/sdx: ???GB" line of parted /dev/sdx print to have the correct numbers ready.

parted /dev/sdc mkpart primary 0GB 4000GB

RAID Considerations

Note that the disk output said this:

# parted /dev/sdb print | grep "Disk /dev"
Disk /dev/sdb: 4001GB

But since I was setting this up for a RAID set, I felt that leaving a few bytes at the end was wise in case a replacement 4TB drive didn’t have the exact same number of bytes.

Since I was needing a RAID ready partition, I used this command to set the "raid" flag on the partition.

parted /dev/sdb set 1 raid on

Now the output of print looks like this.

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  4000GB  4000GB               primary  raid

If you have a big set of drives to worry about, something like this can be handy.

for D in b c d e f g h i; do echo == $D ;\
parted -s /dev/sd${D} mktable gpt ;\
parted -s /dev/sd${D} mkpart primary 0GB 4000GB ;\
parted -s /dev/sd${D} set 1 raid on ;\
done

Delete Partition

To delete a partition. Note this is obviously very dangerous to do when you don’t intend to do it. Use lsblk and df, etc to make dang sure you’re hitting the right device. This deletes partition #1 as reported by the print command.

parted /dev/sdc rm 1

Setting Boot Flag

I’m not having luck with this but it’s something like this.

toggle 1 boot
set 1 boot on

Serious MBR Management

Sometimes you need to back up a master boot record. Sometimes you need to restore it or plant one that’s been provided. This can be done at a low level with dd. The trick is knowing how much to move.

Master boot records comprise the first 512 bytes of the drive device. That would be /dev/sda and not /dev/sda1 whose very existence is defined in the MBR. To backup the full MBR use this.

dd if=/dev/sda of=/boot/sda_mbr.backup bs=512 count=1

The MBR is composed of three major parts, the bootstrap at 446 bytes, the partition table at 64 bytes, and the signature at 2 bytes. So if you want the disk’s full MBR including the partition table, use bs=512. If you just need to redo the boot loading code, use bs=446.

Note that even on GPT partitioned drives, there is still a "protective" (i.e. legacy) master boot record in the first 512 bytes (logical block address 0 or LBA0). But details grow in complexity rapidly; read about them here.