Adding partitions with parted

CentOS Debian Linux

fdisk was always so straightforward.

However parted requires just that extra bit of hardware knowledge.

I had two disks, /dev/sda and /dev/sdb, on which I wanted to create a swap partition on each, and the remainder of the disk was to be part of a RAID volume.

UPDATE: 05/05/2020

A quick and easy way to create partitions and file systems is to start parted with the -a optimal flag.

The ‘optimal’ flag will create optimised partitions.

parted -a optimal /dev/sdx

Then once in parted, create the partition and filesystem/

mkpart primary ext4 0% 100%

Quit, and you’re done.
——————————————————————————————————
Long, older version
——————————————————————————————————

So I started parted: (/dev/sda first)

parted /dev/sda

Display existing partitons using the ‘print’ or shorthand ‘p’ command:

This is a new disk. and hence it is blank.

(parted) p
Model: ATA WDC WD40EFRX-68W (scsi)
Disk /dev/sda: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table:
Disk Flags:

Number  Start   End     Size    File system     Name     Flags

The next step is to initialise the disk, and the ‘mklabel’ command is used for this:

mklabel accepts several options, but generally you would use ‘gpt’ or ‘msdos’ – typically, use ‘gpt’ for large (>1TB) disks.

mklabel gpt

Set the units to GB:

unit gb

Create the first (swap) partition using the command;

mkpart primary linux-swap 1049 2000000 (this creates a partition starting at 1049kb and ending at 2000000kb [2GB)

You will note that I’m using the type ‘linux-swap’ as I want to create a swap partition here.

(parted) mkpart primary linux-swap 1049 2000000
(parted) p
Model: ATA WDC WD40EFRX-68W (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name     Flags
 1      0.00GB  2.00GB  2.00GB  linux-swap(v1)  primary

Now create the second partition to be used for the RAID volume:

mkpart primary ext4 2.00GB 4001GB (this creates a partition starting at 2GB and ending at 4001GB [4TB])

Here I am using ‘ext4’ as the partition type, as the raid will be formatted as ext4.

(parted) mkpart primary ext4 2.00GB 4001GB
(parted) p
Model: ATA WDC WD40EFRX-68W (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name     Flags
 1      0.00GB  2.00GB  2.00GB  linux-swap(v1)  primary
 2      2.00GB  4001GB  3999GB  ext4            primary

Now we need to enable RAID on the partition, this is done with the ‘set’ command:

set 2 raid on

(parted) set 2 raid on
(parted) p
Model: ATA WDC WD40EFRX-68W (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name     Flags
 1      0.00GB  2.00GB  2.00GB  linux-swap(v1)  primary
 2      2.00GB  4001GB  3999GB  ext4            primary  raid

You can now see that partition 2 is type raid.

Follow the same procedure as above for the second disk. You can change to the second disk by using the ‘select’ command in parted.

(parted) select /dev/sdb
Using /dev/sdb
(parted)

Partition alignment can be checked with the ‘align-check’ command (still in parted).

(parted) align-check
alignment type(min/opt)  [optimal]/minimal? optimal
Partition number? 1
1 aligned
(parted) align-check
alignment type(min/opt)  [optimal]/minimal? optimal
Partition number? 2
2 aligned

This will align the partitions optimally on the disk.