Partitioning Disks in Linux


Introduction

In Linux, a disk partition is a logical division of a hard disk drive (HDD) or solid-state drive (SSD) that is used to manage data in a more organized manner. Partitions allow you to separate data by function, such as separating system files from user files, or to set up multiple operating systems on a single physical disk.

There are several tools available for creating and managing disk partitions in Linux, including fdisk, parted, and gparted. In this article, we will focus on using fdisk and parted as they are widely available on most Linux distributions.

Creating a Partition with fdisk

fdisk is a command-line utility that allows you to create, delete, and modify partitions on a disk. To use fdisk, you must have root privileges.

To list the available disks and their partitions, use the fdisk -l command. This will list all the disks and their partitions, including the size and type of each partition.

$ sudo fdisk -l
Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 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: 0x000dfc6f
Device        Boot     Start       End         Sectors  Size  Id Type
/dev/sda1 *   2048     206847     204800       100M      7    HPFS/NTFS/exFAT
/dev/sda2     206848   500117503  499910656    238G      7    HPFS/NTFS/exFAT

In the above example, /dev/sda is the disk and /dev/sda1 and /dev/sda2 are its partitions.

To create a new partition, use the fdisk command followed by the name of the disk you want to partition. For example, to partition the /dev/sda disk, use the following command −

$ sudo fdisk /dev/sda

This will open the fdisk prompt. To create a new partition, type n and press Enter. You will be prompted to choose between creating a primary or extended partition.

To create a primary partition, type p and press Enter. You will then be prompted to enter the partition number and the size of the partition in sectors.

Command (m for help): n
Partition     type
   p        primary (0 primary, 0 extended, 4 free)
   e        extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-500118191, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-500118191, default 500118191):+10G

In the above example, we are creating a primary partition with the number 1 and a size of 10 GB.

To create an extended partition, type e and press Enter. You will then be prompted to enter the partition number and the size of the partition in sectors.

After creating the partition, you need to write the changes to the disk by typing w and pressing Enter. You will then be prompted to confirm the changes. Type y and press Enter to confirm.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

You can also use fdisk to delete a partition by typing d and pressing Enter, and then selecting the partition number you want to delete.

Creating a Partition with Parted

Parted is another command-line utility that allows you to create, delete, and modify partitions on a disk. Like fdisk, parted requires root privileges to use.

To list the available disks and their partitions, use the parted -l command. This will list all the disks and their partitions, including the size and type of each partition.

$ sudo parted -l
Model: ATA WDC WD25 00AAJS-00L (scsi)
Disk /dev/sda: 250GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number   Start    End   Size    Type    File system    Flags
  1      1049kB  106MB  105MB  primary   ntfs          boot
  2      106MB   250GB  250GB  primary   ntfs

To create a new partition using parted, use the parted command followed by the name of the disk you want to partition. For example, to partition the /dev/sda disk, use the following command −

$ sudo parted /dev/sda

This will open the parted prompt. To create a new partition, type mkpart and press Enter. You will then be prompted to enter the partition name, file system type, and start and end points for the partition.

(parted) mkpart
Partition name? []? data
File system type? [ext2]? ext4
Start? 0%
End? 100%

In the above example, we are creating a new partition called "data" with an ext4 file system and using the entire disk for the partition.

You can also specify the start and end points for the partition in terms of percentage of the disk or in units of megabytes (MB) or gigabytes (GB). For example, to create a partition that uses the first 10 GB of the disk, you can use the following command −

(parted) mkpart data ext4 0% 10GB

To delete a partition using parted, type rm and press Enter, and then select the partition number you want to delete.

Conclusion

In this article, we have discussed how to create and delete partitions in Linux using the fdisk and parted utilities. Partitioning your disk can help you organize your data and improve the performance of your system. It is important to carefully plan your partition layout to ensure that you are making the most efficient use of your disk space.

Updated on: 12-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements