How to create partition using fdisk

fdisk is a command-line utility that provides disk partitioning functions. This tutorial explains the different utilities of fdisk.

View all Disk Partitions in Linux

The following basic command list all existing disk partition on your system. The ‘-l‘ argument stand for (listing all partitions) is used with fdisk command to view all available partitions on Linux. The partitions are displayed by their device’s names. For example: /dev/sda, /dev/sdb or /dev/sdc.

[root@localhost.com ~]# fdisk -l

Disk /dev/sda: 637.8 GB, 637802643456 bytes

255 heads, 63 sectors/track, 77541 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 2624 20972857+ 83 Linux

/dev/sda3 2625 4582 15727635 83 Linux

/dev/sda4 4583 77541 586043167+ 5 Extended

/dev/sda5 4583 5887 10482381 83 Linux

/dev/sda6 5888 7192 10482381 83 Linux

/dev/sda7 7193 7845 5245191 83 Linux

/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris

/dev/sda9 8368 77541 555640123+ 8e Linux LVM

 

View Specific Disk Partition in Linux

To view all partitions of specific hard disk use the option ‘-l‘ with device name. For example, the following command will display all disk partitions of device /dev/sda. If you’ve different device names, simple write device name as /dev/sdb or /dev/sdc.

[root@localhost.com ~]# fdisk -l /dev/sda

Disk /dev/sda: 637.8 GB, 637802643456 bytes

255 heads, 63 sectors/track, 77541 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 2624 20972857+ 83 Linux

/dev/sda3 2625 4582 15727635 83 Linux

/dev/sda4 4583 77541 586043167+ 5 Extended

/dev/sda5 4583 5887 10482381 83 Linux

/dev/sda6 5888 7192 10482381 83 Linux

/dev/sda7 7193 7845 5245191 83 Linux

/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris

/dev/sda9 8368 77541 555640123+ 8e Linux LVM

 

Check all Available fdisk Commands

If you would like to view all commands which are available for fdisk. Simply use the following command by mentioning the hard disk name such as /dev/sda as shown below. The following command will give you output similar to below.

[root@localhost.com ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

switch off the mode (command 'c') and change display units to

sectors (command 'u').

Command (m for help):

Type ‘m‘ to see the list of all available commands of fdisk which can be operated on /dev/sda hard disk. After, I enter ‘m‘ on the screen, you will see the all available options for fdisk that you can be used on the /dev/sda device.

[root@localhost.com ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

switch off the mode (command 'c') and change display units to

sectors (command 'u').

Command (m for help): m

Command action

a toggle a bootable flag

b edit bsd disklabel

c toggle the dos compatibility flag

d delete a partition

l list known partition types

m print this menu

n add a new partition

o create a new empty DOS partition table

p print the partition table

q quit without saving changes

s create a new empty Sun disklabel

t change a partition's system id

u change display/entry units

v verify the partition table

w write table to disk and exit

x extra functionality (experts only)

Command (m for help):

 

Print all Partition Table in Linux

To print all partition table of hard disk, you must be on command mode of specific hard disk say /dev/sda.

[root@localhost.com ~]# fdisk /dev/sda

From the command mode, enter ‘p‘ instead of ‘m‘ as we did earlier. As I enter ‘p‘, it will print the specific /dev/sda partition table.

Command (m for help): p

Disk /dev/sda: 637.8 GB, 637802643456 bytes

255 heads, 63 sectors/track, 77541 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 2624 20972857+ 83 Linux

/dev/sda3 2625 4582 15727635 83 Linux

/dev/sda4 4583 77541 586043167+ 5 Extended

/dev/sda5 4583 5887 10482381 83 Linux

/dev/sda6 5888 7192 10482381 83 Linux

/dev/sda7 7193 7845 5245191 83 Linux

/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris

/dev/sda9 8368 77541 555640123+ 8e Linux LVM

Command (m for help):

 

How to Delete a Partition in Linux

If you would like to delete a specific partition (i.e /dev/sda9) from the specific hard disk such as /dev/sda. You must be in fdisk command mode to do this.

[root@localhost.com ~]# fdisk /dev/sda

Next, enter ‘d‘ to delete any given partition name from the system. As I enter ‘d‘, it will prompt me to enter partition number that I want to delete from /dev/sda hard disk. Suppose I enter number ‘4‘ here, then it will delete partition number ‘4‘ (i.e. /dev/sda4) disk and shows free space in partition table. Enter ‘w‘ to write table to disk and exit after making new alterations to partition table. The new changes would only take place after next reboot of system. This can be easily understood from the below output.

[root@localhost.com ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

switch off the mode (command 'c') and change display units to

sectors (command 'u').

Command (m for help): d

Partition number (1-4): 4

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

You have new mail in /var/spool/mail/root

Warning : Be careful, while performing this step, because using option ‘d‘ will completely delete partition from system and may lost all data in partition.

 

How to Create a New Partition in Linux

If you’ve free space left on one of your device say /dev/sda and would like to create a new partition under it. Then you must be in fdisk command mode of /dev/sda. Type the following command to enter into command mode of specific hard disk.

[root@localhost.com ~]# fdisk /dev/sda

After entering in command mode, now press “n” command to create a new partition under /dev/sda with specific size. This can be demonstrated with the help of following given output.

[root@localhost.com ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

switch off the mode (command 'c') and change display units to

sectors (command 'u').

Command (m for help): n

Command action

e extended

p primary partition (1-4)

e

While creating a new partition, it will ask you two options ‘extended‘ or ‘primary‘ partition creation. Press ‘e‘ for extended partition and ‘p‘ for primary partition. Then it will ask you to enter following two inputs.

  1. First cylinder number of the partition to be create.
  2. Last cylinder number of the partition to be created (Last cylinder, +cylinders or +size).

You can enter the size of cylinder by adding “+5000M” in last cylinder. Here, ‘+‘ means addition and 5000M means size of new partition (i.e 5000MB). Please keep in mind that after creating a new partition, you should run ‘w‘ command to alter and save new changes to partition table and finally reboot your system to verify newly created partition.

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

 

How to Format a Partition in Linux

After the new partition is created, don’t skip to format the newly created partition using ‘mkfs‘ command. Type the following command in the terminal to format a partition. Here /dev/sda4 is my newly created partition.

[root@localhost.com ~]# mkfs.ext4 /dev/sda4

 

Leave a Reply

Your email address will not be published. Required fields are marked *