Learning fdisk to create and delete partitions

Before we begin, it is better to make a backup of the disk data on which we plan to do operations, otherwise we risk losing data. It is always better to be safe.

By default, fdisk is installed on many Linux distributions. It is even present in various recovery tools and installation programs. Fdisk is a universal tool to operate with disk partitions. If you are good at it then you don’t need anything else because fdisk is often installed as default package in Linux distributions. It is simple console tool for many years.

Show disk partitions

First command is to show all disks and its partitions:

sudo fdisk -l
...
Disk /dev/sdb: 223.6 GiB, 240065183744 bytes, 468877312 sectors
Disk model: WDC WDS240G2G0A-
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: *

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 314574847 314572800  150G 83 Linux


Disk /dev/sda: 465.8 GiB, 500106780160 bytes, 976771055 sectors
Disk model: WDC WD5000AAKS-0
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: *

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 542722047 542720000 258.8G 83 Linux

This command may take some time.

Notice, all fdisk commands are executed with sudo because it is very serious and responsibly to work with data.

If we need to show partitions of a single disk, we can make it exactly:

sudo fdisk -l /dev/sdb
...
Disk /dev/sdb: 223.6 GiB, 240065183744 bytes, 468877312 sectors
Disk model: WDC WDS240G2G0A-
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: *

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 314574847 314572800  150G 83 Linux

This command is very fast and takes less time to be complete.

Best to use the latest version of fdisk and modern Linux kernel (not too old), then all operations will be accurate.

Journey to fdisk command line prompt

We need to run the command specifying the disk with which we want to work:

sudo fdisk /dev/sda
...
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): 

Now we are inside fdisk. This is where all disk operations are performed.

If we enter the letter m, all available options will appear:

Command (m for help): m
...

Help:

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

-------AND MORE-----------

Enter the p (means print) and we’ll see all disk partitions:

Command (m for help): p
...

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 542722047 542720000 258.8G 83 Linux

Create a new partition

All operations will not be saved until you enter “w” letter and confirm the changes. So we can practice at the beginning.

Enter n to start setting up a new partition :

Command (m for help): n
...
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

This asks what type of partition we want to create. Frequently users required primary. An extended partition may include several partitions, this saves space for partitions when the user wants to create a lot of them.

So, if you don’t care about it just create a primary partition.

Create primary partition, enter p :

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
...
Partition number (2-4, default 2): 

Here, just press [Enter], since partitions are in order:

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): [Enter]
...
First sector (542722048-976771054, default 542722048): 

This is where the partition will start. To find out how much space is available to us for this partition, make some calculations. Subtract from the last sector the value of the first one and then convert it in gigabytes:

(976771054 - 542722048) * 512 (set your sector size here) / 1024 / 1024 / 1024 = 206 gigabytes is available for this partition if it starts here

If this is what we need, press [Enter]:

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): [Enter]
First sector (542722048-976771054, default 542722048): [Enter]
...
Last sector, +/-sectors or +/-size{K,M,G,T,P} (542722048-976771054, default 976771054): 

Here we are asked how much space we want to allocate for this partition. Enter the size we want, use +, number and capital letter (M for megabyte, G for gigabyte):

Last sector, +/-sectors or +/-size{K,M,G,T,P} (542722048-976771054, default 976771054): +120G

Here I have allocated 120 gigabytes for the partition.

In the past on this partition I had Linux installed, so I was warned about it:

Last sector, +/-sectors or +/-size{K,M,G,T,P} (542722048-976771054, default 976771054): +120G

Created a new partition 2 of type 'Linux' and of size 120 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: Y

The signature will be removed by a write command.

Command (m for help): 

I don’t care so I deleted the signature because this new partition will be clean.

Changes are stored in fdisk, but not applied to disk, we can check that:

Command (m for help): p
...
Device     Boot     Start       End       Sectors                  Size Id Type
/dev/sda1            2048 542722047 542720000        258.8G 83 Linux
/dev/sda2       542722048 794380287 251658240   120G 83 Linux

To save changes to disk, enter w letter:

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

Partition is created:

sudo fdisk -l /dev/sda
...
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1            2048 542722047 542720000 258.8G 83 Linux
/dev/sda2       542722048 794380287 251658240   120G 83 Linux

Create file system on partition

We cannot mount a partition and use it to store files until we create a file system on a partition. For this purpose mkfs utility is used. Let’s create ext4 filesystem in our partition:

sudo mkfs.ext4 /dev/sda2
...
Creating filesystem with 31457280 4k blocks and 7864320 inodes
Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done  

The file system on the partition was created.

Mount our new partition

When the file system on the partition was created it can be mounted and used to store files:

sudo mkdir /mnt/Our_Partition
sudo mount -t ext4 /dev/sda2 /mnt/Our_Partition/

That’s all.

Delete partition

If the partition is no longer needed, we can delete it. But before that you need to make sure that you unmount it:

sudo umount /mnt/Our_Partition

We can then use the familiar to us fdisk utility, run it with the device where the partition is located and need to be deleted:

sudo fdisk /dev/sda

Show the list of partitions:

Command (m for help): p
...
Device     Boot     Start       End   Sectors                   Size Id Type
/dev/sda1            2048 542722047 542720000     258.8G 83 Linux
/dev/sda2       542722048 794380287 251658240   120G 83 Linux

To start the partition deletion process, enter d letter:

Command (m for help): d
...
Partition number (1,2, default 2): 

Specify the partition number to be deleted:

Partition number (1,2, default 2): 2
...
Partition 2 has been deleted.

Changes are stored in fdisk but not yet written to disk.

Enter w to write changes to the disk, then the partition will be really deleted:

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

Make sure the partition is deleted:

sudo fdisk -l /dev/sda
...
Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1        2048 542722047 542720000 258.8G 83 Linux

Now I have only one partition, the second has been deleted.