Formatting & Partitioning is the process of dividing a hard disk drive into multiple partitions, each of which can be used to store data. The partitions are created to help manage the storage space of the hard disk and to improve performance.

Linux: Formatting & Partitioning
Linux: Formatting & Partitioning

The following are the commonly used commands for formatting and partitioning a hard disk in Linux:

fdisk

fdisk is a command-line utility used to manage disk partitions. It allows you to create, delete, and modify partitions on a disk, as well as display information about existing partitions.

Options:

OptionDescription
-lDisplays information about all available partitions on the specified device.
-uSpecifies the units in which the partition sizes should be displayed, either in sectors or in cylinders.
-cToggles the display of the cylinder numbers in the partition table.

Example:

# fdisk -l /dev/sda

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x9dcda8e1

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1  *     2048 39061503 39059456 18.6G 83 Linux

gdisk

gdisk is a similar command-line utility to fdisk, but it’s used to manage GPT (GUID Partition Table) disks. It’s used to create, delete, resize, and manipulate disk partitions on GPT disks.

Options:

OptionDescription
-lDisplays information about all available partitions on the specified device.
-uSpecifies the units in which the partition sizes should be displayed, either in sectors or in cylinders.
-cToggles the display of the cylinder numbers in the partition table.

Example

gdisk -l /dev/sda

GPT fdisk (gdisk) version 1.0.4

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 41943040 sectors, 20.0 GiB
Model: ATA ST2000DM001-1CH1
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 08D1F622-E6BF-4996-B25D-B2506C2FC66B
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 41943039
Partitions will be aligned on 2048-sector boundaries
Total free space is 4074 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048      39061503   18.6 GiB    0700  Linux filesystem

parted

parted is a command-line utility in Linux used to manage disk partitions. Disk partitions are divided into multiple segments on a hard drive or solid-state drive, which allows for organization and efficient use of the disk space.

Here are some commonly used options for the parted command:

OptionDescription
-lLists the disk partitions on the specified device
-sRuns the command in a non-interactive script mode
mkpartCreates a new partition on the specified device
rmDeletes the specified partition
resizeResizes the specified partition
alignSpecifies the alignment of the partition
printDisplays information about the specified partition

Here’s an example of how you could use the parted command to display information about the disk partitions on a device:

$ parted /dev/sda print

Model: ATA TOSHIBA MQ01ABF0 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  538MB   537MB   primary  ext4         boot
 2      538MB   500GB   500GB   primary  xfs

In this example, the parted /dev/sda print command is used to display information about the disk partitions on the device /dev/sda. The output shows that there are two partitions on the device, both of which are listed with their starting and ending locations, size, type, file system, and flags. The first partition is an ext4 partition with a boot flag, and the second is an xfs partition.

mkpart

The mkpart option of the parted command allows you to create a new partition on a disk without creating a new file system on it. This option is particularly useful when you want to create partitions for file systems that are not supported by Parted, such as LVM. When creating a new partition with this option, you need to specify the file system type for the partition in order to set the appropriate partition code in the partition table.

It is important to note that the file system type is a required parameter for data partitions, which are partitions that are not extended. The start and end parameters specify the location of the partition on the disk, where the distance is measured from the beginning of the disk. Here’s an example of how you could use mkpart along with the parted command to create a new partition:

$ sudo parted /dev/sda mkpart primary ext4 0% 100%

In this example, the sudo parted /dev/sda mkpart primary ext4 0% 100% command is used to create a new primary partition on the device /dev/sda with the ext4 file system. The partition will occupy the entire disk space from 0% to 100% of the disk. After running this command, you can verify the new partition by using the parted command with the print option:

$ sudo parted /dev/sda print

Model: ATA TOSHIBA MQ01ABF0 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  500GB   500GB   primary  ext4

In this example, the output shows that there is now one partition on the device /dev/sda, which is a primary partition with the ext4 file system and occupies the entire disk space.

resizepart

The resizepart option in the parted command is used to resize an existing partition on a disk. This option allows you to change the size of a partition by specifying a new start and end position. Here’s an example of how you could use the parted command to resize an existing partition:

$ sudo parted /dev/sda resizepart 2 50GB 100GB

In this example, the sudo parted /dev/sda resizepart 2 50GB 100GB command is used to resize the second partition on the device /dev/sda. The partition will be resized to start at 50GB and end at 100GB.

Options available with resizepart option:

OptionDescription
part-numberSpecifies the partition number to resize
startSpecifies the start location of the partition in GB or MB
endSpecifies the end location of the partition in GB or MB

It is important to note that resizing a partition may result in data loss, so it is recommended to backup your data before using this option. Additionally, some file systems do not support resizing, so it is important to check the compatibility of your file system before using this option.

Reference