Partitioning Disks

Posted in: Linux by Example (4 min read)

There are a few Linux tools that you can use to work with partitions, but the best overall choice is parted. It's worth getting yourself familiar with it.

If you don't have it already, you can install the package with something like:

apt/dnf install parted

Getting started

Let's open parted in interactive mode on our disk (as root):

parted --align optimal /dev/sda

First, let's see some info about our disk:

print

In my case:

Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name                  Flags

As you can see, I'm using an empty 1TB disk as an example. This disk has both physical and logical sector sizes of 512B.

Some modern disks have 4k physical sectors, i.e.:

Sector size (logical/physical): 512B/4096B

In that case, you have to be more careful with aligning your partitions, but don't worry, parted can help us with that (that's why we used "--align optimal" switch).

Partition table

Nowadays, there are not many compelling reasons to use MBR anymore, and my recommendation is to go with GPT if possible (it's generally possible to boot from GPT partitions even on older BIOS systems).

OK, Let's make a new GPT partition table.

Note: Before executing this, double-check that you're working on a correct disk because this command will destroy your existing partition table.

mklabel gpt

Partitions

To create new partitions, we're going to use parted's mkpart command in this form:

mkpart part-label (fs-type) start end

bios_grub

First, we're going to create a small partition required for booting grub in legacy BIOS mode. If you plan to only boot your system in UEFI mode, you can skip this step, but since it takes only 1MB, it's handy to have it around (i.e. if you'll ever have trouble booting in UEFI mode).

mkpart bios_grub 1MiB 2MiB

We also need to set bios_grub flag on that partition:

set 1 bios_grub on

boot

Next, we need an EFI system partition for EFI bootloader and drivers.

mkpart esp fat32 2MiB 300MiB

We also need to set esp flag:

set 2 esp on

You can use this same partition also as your /boot partition (in that case, around 300MiB is usually enough).

Or you can keep /boot and /efi separate (but keep in mind that not all bootloaders support that). In that case, just create an additional partition for /boot (around 200MiB), and /efi can be ~100MiB.

Rest

At this point I recommend creating one partition in the rest of the unpartitioned space, which we'll use as a container for everything else (more on that later):

mkpart rest 301MiB -1MiB

(negative number means we're starting from the opposite side of the disk)

Next steps

let's see the result:

print

Depending on the values you chose, it should look something like this:

Disk /dev/sda: 953870MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start    End        Size       File system  Name                  Flags
 1      1.00MiB  2.00MiB    1.00MiB                                       bios_grub
 2      2.00MiB  315MiB     313MiB     fat32        EFI System Partition  boot, esp
 3      315MiB   953869MiB    953554MiB

As you can see, almost all our space is allocated to "/dev/sda3" partition. To avoid future repartitioning of the disk, you can use that as a container for LVM, BTRFS, or other types of volumes.

We'll go through that (and encrypting partitions with LUKS) in the next posts.

To exit parted, just type quit or press Ctrl+D.


Comments, suggestions, and questions are welcome.