FDISK to partition the disks in Linux

Introduction:

Maybe this can be well known to you. Still, many of them wonder how to work with “fdisk” utility. Today’s guide we are focusing on how to use “fdisk” for partitioning a disk in Linux.

Fdisk is a partition maintenance program which available by default in all flavoured operating system and can be used to divide space on the disk into partitions and set one active. For testing purpose, we are using with an RHEL Linux server for demonstration purpose and by default, it comes with fdisk utility.

Scenario:

Assume, this can be a running production server for a long time. a new requirement coming to add one more 100G of the disk to some X server. Let’s see how to handle the situation to partition the disk.

Once the disk is added, we list it using “fdisk” command with the option “-l“. For demonstration purpose, we are adding with a new disk 100 GB in size.


Listing the available Disks:

List the known disk by point it as sdb or sdc.

# fdisk -l /dev/sdb
How to use FDISK in Linux to partitioning the disks
Listing disks using fdisk

In case of the server have only a few numbers of disks and it’s easy to locate the newly added one run with

# fdisk -l
How to use FDISK in Linux to partitioning the disks
Listing only specific disk

Most of the real-time production servers you may have a number of disks, In such situation, we need to use with [a-z] to list all the available disk between a to z, Even this can be extended by using /dev/sd[a-z]1.

# fdisk -l /dev/sd[a-z]
How to use FDISK in Linux to partitioning the disks
A listing range of disks using fdisk.

Only to get the disk name and size information it’s better to do a grep.

# fdisk -l /dev/sd* | grep -i 'Disk'
How to use FDISK in Linux to partitioning the disks
List using grep

Creating with Partitions:

Now, let’s start to partition 100 GB disk by having a Primary, extended and logical partition.

[stextbox id=”alert” caption=”Warning”]If you are trying with an existing disk with Data’s in it, will cause data loss. Make sure to choose the right disk.[/stextbox]

# fdisk /dev/sdb

Sample Output:

[root@oel7_server ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x1a0543a3.

Press small “P” to print the existing partitions, hence we are using with a new disk it should be empty.

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x1a0543a3
Device Boot Start End Blocks Id System

Getting Help under fdisk:

If you need some help regarding available options in fdisk press “m” to get the help.

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
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
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):

Creating New Primary Partition:

To create a new partition press “n”.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended

by following choose whether the need to create with a primary partition or extended partition. Hence this is the first disk we are creating with a Primary and extended layout.

Press “P” for the primary partition

  • Press 1 and enter for creating with the first partition.
  • And what size of partition needs to be created, We are using 5G in size for the first partition. While providing the size need to add a “+” before the value.
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +5G
Partition 1 of type Linux and of size 5 GiB is set

Marking partition type:

Once created with the size of partition we need to define what type of partition will be used.

  • Press “t” to choose the type of partition.
  • Select the partition number which needs to be changed with type. Here it’s “1“.

To know all available types Press “l” to list the available types.

For our first partition, we are going to use as an LVM, Type “8e” and press enter to choose Linux LVM as the type.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): l

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix

Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Print and verify the created Partition:

Once completed with changing the type use “p” to print the available partitions.

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x1a0543a3

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM

Again follow the same steps to create with extended partitions, this time we need to press “e” for creating an extended partition

  • Press “n” for creating new.
  • Press “e” for creating with an extended partition.
  • Type number “2” for the extended partition.
  • And just press enter to use the default first sector.
  • We need to use remaining disk space as the extended partition so press enter.
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2): 2
First sector (10487808-209715199, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-209715199, default 209715199):
Using default value 209715199
Partition 2 of type Extended and of size 95 GiB is set

Now time to create with a new logical volume inside the extended partition.

  • Press “n” to create new logical volume.
  • Type “l” to create the logical volume.
  • Hit enter to choose the default size as the first sector.
  • Provide the size of logical volume required to create under the extended partition. Here we using with 10G, Use “+” sign before the size.
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (10489856-209715199, default 10489856):
Using default value 10489856
Last sector, +sectors or +size{K,M,G} (10489856-209715199, default 209715199): +10G
Partition 5 of type Linux and of size 10 GiB is set

Once created press “P” to print the partition layout.

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x1a0543a3

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 209715199 99613696 5 Extended
/dev/sdb5 10489856 31461375 10485760 83 Linux

Change the type by pressing “t“, by following use “83” to mark the partition as Linux.

Note:> If we are not choosing a type for any partition, by default it will mark as “83” Linux partition.

Command (m for help): t
Partition number (1,2,5, default 5): 5
Hex code (type L to list all codes): 83
Changed type of partition 'Linux' to 'Linux'

Press “P” to verify the created partitions.

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x1a0543a3

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 209715199 99613696 5 Extended
/dev/sdb5 10489856 31461375 10485760 83 Linux

Let’s create with swap space partition in 2 GB size.

  • Press “n” for creating the new partition.
  • Type “I” for the logical partition.
  • Type “6” or it will choose automatically to create with 6 when we press enter.
  • Provide the size +2G.
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (31463424-209715199, default 31463424):
Using default value 31463424
Last sector, +sectors or +size{K,M,G} (31463424-209715199, default 209715199): +2G
Partition 6 of type Linux and of size 2 GiB is set

Select the type of partition:

  • Type “t” to select the type.
  • Type “6” and list the available types using “l“.
  • Mark the partition as swap by using “82” Linux swap / Solaris.
Command (m for help): t
Partition number (1,2,5,6, default 6): 6
Hex code (type L to list all codes): l

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Verify by printing the current partitions using “P“.

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x1a0543a3

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 209715199 99613696 5 Extended
/dev/sdb5 10489856 31461375 10485760 83 Linux
/dev/sdb6 31463424 35657727 2097152 82 Linux swap / Solaris


How to delete a partition in fdisk:

I have created with one more partition for demonstration purpose. Now let’s see how to delete a partition under fdisk.

We need to remove with 7th partition /dev/sda7.

Print to list all partitions using “p“.

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x1a0543a3

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 209715199 99613696 5 Extended
/dev/sdb5 10489856 31461375 10485760 83 Linux
/dev/sdb6 31463424 35657727 2097152 82 Linux swap / Solaris
/dev/sdb7 35659776 56631295 10485760 8e Linux LVM

Press “d” to delete the partition, by following give the partition number “7” to remove it.

Command (m for help): d
Partition number (1,2,5-7, default 7): 7
Partition 7 is deleted

Verify by listing the partition layout using “p“.

Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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
Disk label type: dos
Disk identifier: 0x1a0543a3

Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 209715199 99613696 5 Extended
/dev/sdb5 10489856 31461375 10485760 83 Linux
/dev/sdb6 31463424 35657727 2097152 82 Linux swap / Solaris

Still, now we have not made the changes permanent, To make it persistent use “w” to write the partition table and exit from the fdisk utility.

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@oel7_server ~]#

Some case if the newly created partition is not listed while running “fdisk -l” we need to inform the kernel about new partitions using “partprobe” command.

# partprobe /dev/sdb
# dmesg | tail -1

Sample Output:

[root@oel7_server ~]# partprobe /dev/sdb
[root@oel7_server ~]#
[root@oel7_server ~]# dmesg | tail -1
[ 3994.094249] sdb: sdb1 sdb2 < sdb5 sdb6 >
[root@oel7_server ~]#

Finally, let us list the created partitions using “fdisk -l“.

How to use FDISK in Linux to partitioning the disks
List partitions after creation

That’s it we have successfully created with required partition layout in Linux.

Conclusion:

To create with partitions in Linux we can use fdisk utility, Fdisk command will be available in all flavour of Linux and it’s much easier than any other utilities to create the partitions. If you have any feedback provide us with the same in below comment section and feel free to subscribe with us.