DEV Community

Nick | OneThingWell.dev
Nick | OneThingWell.dev

Posted on • Originally published at betterways.dev

Linux by Example: Partitioning Disks

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
Enter fullscreen mode Exit fullscreen mode

Getting started

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

parted --align optimal /dev/sda
Enter fullscreen mode Exit fullscreen mode

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

print
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Partitions

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

mkpart part-label (fs-type) start end
Enter fullscreen mode Exit fullscreen mode
  • part-label is the partition label - you'll be able to access the partition later through /dev/disk/by-partlabel/mylabel (note: a lot of people are incorrectly using "primary" here as a partition type (as they would in the MBR mode), which just sets the partition label to "primary")

  • start and end are starting and ending positions for our new partition - to make things simple, we're going to use MiB as a unit.

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
Enter fullscreen mode Exit fullscreen mode

We also need to set bios_grub flag on that partition:

set 1 bios_grub on
Enter fullscreen mode Exit fullscreen mode

boot

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

mkpart esp fat32 2MiB 300MiB
Enter fullscreen mode Exit fullscreen mode

We also need to set esp flag:

set 2 esp on
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Next steps

let's see the result:

print
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.



Note: this is a wiki article from BetterWays.dev: Linux by Example series, you can find the latest (better formatted version) here: https://betterways.dev/linux-by-example-partitioning-disks.

If you find this type of posts interesting, please let me know (with reactions and/or comments).

Top comments (0)