How to Create & Delete Partitions in Linux using Fdisk

How to Create & Delete Partitions in Linux using Fdisk

This post will help you with How to Create & Delete Partitions in Linux using Fdisk Command.

If you are interested in learning, Request you to go through the below recommended tutorial.

How to Create & Delete Partitions in Linux using Fdisk

This article explains you how to manage the Disk in Linux using fdisk partitioning tool. Previously we have explained the disk management for beginners to understand the basics.

Topics included:
1. How to view the list of existing hard disks
2. How to view the details of specific hard disks
3. How to find or calculate the free space available in the exisiting disks.
4. How to create a Disk Partition Using fdisk.
5. How to make or create a filesystem in Linux.
6. How to mount the filesystem to use the disk parition.
7. How to delete the partitions in Linux using fdisk.

The fdisk command is a interactive command line tool for viewing and managing hard disk partitions on Linux. It’s one of the most powerful tools we can use to manage partitions, but it’s confusing to new users. Using fdisk you can create a new partition, delete an existing partition, or change existing partition.

Linux allows only 4 primary partitions. You can have a much larger number of logical partitions by sub-dividing one of the primary partitions. Only one of the primary partitions can be sub-divided.

Refer this link to know more about the basic concepts of disk partitions.

1. How to view the list of existing hard disks

Before we create a new partition, or modify an existing partition, we want to view all available disk and partitions in the systems.

[[email protected] ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes                 ==> Disk 1
64 heads, 32 sectors/track, 51200 cylinders                  51200 Cylinders available in Disk 1
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009f913
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         501      512000   83  Linux                 Partition 1 of Disk 1
/dev/sda2             502       51200    51915776   8e  Linux LVM                 Partition 2 of Disk 1
Disk /dev/sdb: 1024 MB, 1024000000 bytes                 ==> Disk 2
32 heads, 62 sectors/track, 1008 cylinders                 1008 Cylinders available in Disk 2
Units = cylinders of 1984 * 512 = 1015808 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 8388608 bytes
Disk identifier: 0x00000000

Above fdisk output shows that, we have two disk /dev/sda and /dev/sdb. Disk /dev/sda has /dev/sda1 and /dev/sda2 partitions and its partitions types are 83 (Linux) and 8e (Linux LVM) respectively.

But the disk /dev/sdb doesnt have any partitions.

2. How to view the details of specific hard disks

To view all partitions of the specific /dev/sda hard disk, instead of showing all disks. Run the fdisk command along with specfic disk as below.

[[email protected] ~]# fdisk -l /dev/sda
Disk /dev/sda: 53.7 GB, 53687091200 bytes
64 heads, 32 sectors/track, 51200 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009f913
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         501      512000   83  Linux
/dev/sda2             502       51200    51915776   8e  Linux LVM

3. How to find or calculate the free space available in the existing disks.

Before create any new disk partition, we should confirm that we have free space available in our disk to create new partitions. Its necessary to calculate the free space to create a new partitions.

On Disk /dev/sda. we have 51200 cylinders which shows in the second line. In this disk, we have two partitions /dev/sda1 and /dev/sda2, cylinders starts from 2 to 501 and 502 to 51200 cylinders respectively. So we dont have more cyclinders to create new partitions.

On Disk /dev/sdb, we have 1008 cylinders, but we dont have any partitions on this disk. So we can create new partitions by using this cylinders.

4. How to Create Disk Partition Using fdisk.

To create a partitions using fdisk tool on /dev/sdb, follow the below procedures.

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x10bd01c3.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.
Command (m for help):

Type “p” to print the partitions or type “m” for help.

Command (m for help): p
Disk /dev/sdb: 1024 MB, 1024000000 bytes
32 heads, 62 sectors/track, 1008 cylinders
Units = cylinders of 1984 * 512 = 1015808 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 8388608 bytes
Disk identifier: 0x10bd01c3
Device Boot      Start         End      Blocks   Id  System

Above prints the output as no partitions available. Type “n” to create or add a new partition to the disk. At this time, utility will prompt for series inputs.

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1008, default 1):1
Last cylinder, +cylinders or +size{K,M,G} (1-1008, default 1008):
Using default value 1008.

1. Select the partition types. Type “p” to create a primary partition or Type “e” to create a extended partitions. we just go with primary partition.
2. Enter the partition number from 1 to 4, because it allows 4 primary partitions. Type “1” to set a partition number as “1”.
3. Enter the first cylinder to start from first cylinder on the disk or give other values. We just go with default value to start from the first cylinder and it takes the first cyclinder default as “1”, Just enter to accept.
4. Enter the last cyclinder in cylinder values or size in K, M, G. If you want to use the full disk to create a new partition. just enter the last cyclinder or Enter the specific size you want alloed within the cylinders (example : +1G or +512M). We use the full disk till 1008 cylinder and it takes the default last cylinder as 1008. just enter.

Now press “p” to print the partitions.

Command (m for help): p
Disk /dev/sdb: 1024 MB, 1024000000 bytes
32 heads, 62 sectors/track, 1008 cylinders
Units = cylinders of 1984 * 512 = 1015808 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 8388608 bytes
Disk identifier: 0x10bd01c3
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1008      999905   83  Linux

Now its shows one partition which we have created now and cylinder starts from 1 to 1008. So we have used the entire cylinders of the disk.

Note : By default partition type ID would be 83 which is linux partition. If you want to change the type of the partitions from 83 to some other Parition ID.. Lets say for example to create a NTFS Partitions. Press “t” and enter the Partition type ID in Hex code format. If you dont know the ID after pressing “t” key, press “L” to list the Hex code. Note the ID and enter the ID. For NTFS Parition ID, press “7”.

Hex code (type L to list codes): 7
Changed system type of partition 1 to 7 (HPFS/NTFS).

Once finished creating partition, Type “w” to save the changes on the disk, if not your changes will not affect.

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

If you get any error as Partition re-read calling is failed so reboot the server. Just run “partprobe” command to reread the partition tables without reboot.

View the partitions of the disk /dev/sdb to confirm the partition is created or not.

[[email protected] ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 1024 MB, 1024000000 bytes
32 heads, 62 sectors/track, 1008 cylinders
Units = cylinders of 1984 * 512 = 1015808 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 8388608 bytes
Disk identifier: 0x10bd01c3
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1008      999905   83  Linux

Yes its created as /dev/sdb1.

5. How to Create filesystem in Linux?

To create a filesystem, use mkfs command. lets say we want the filesystem to ext3.

[[email protected] ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=2048 blocks
62592 inodes, 249976 blocks
12498 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=260046848
8 block groups
32768 blocks per group, 32768 fragments per group
7824 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

6. How to mount filesystem to use the disk paritions.

[[email protected] ~]# mount /dev/sdb1 /mnt

View the disk utilization report.

[[email protected] ~]# df -h
Filesystem                                       Size  Used Avail Use% Mounted on
/dev/mapper/vg_gslwebsvr-lv_root                  45G  7.6G   36G  18% /
tmpfs                                            1.9G     0  1.9G   0% /dev/shm
/dev/sda1                                        485M   43M  417M  10% /boot
/root/RHEL6.5-20131111.0-Server-x86_64-DVD1.iso  3.6G  3.6G     0 100% /media
/dev/sdb1                                        962M   18M  896M   2% /mnt

Newly created partition /dev/sdb1 is mounted on /mnt and it shows the utilization as well. This is how we create a new partitions using fdisk command.

7. How to delete partitions in Linux using fdisk.

To delete the partitions, confirm the disk partitions is not mounted.

Lets say for example, we will delete the partition /dev/sdb1, but it is mounted. Lets unmount it.

[[email protected] ~]# umount /dev/sdb1

Use the fdisk tool to get enter into partition tool.

[[email protected] ~]# fdisk /dev/sdb
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 “p” to list the partitions to confirm the partitions.

Command (m for help): p
Disk /dev/sdb: 1024 MB, 1024000000 bytes
32 heads, 62 sectors/track, 1008 cylinders
Units = cylinders of 1984 * 512 = 1015808 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 8388608 bytes
Disk identifier: 0x10bd01c3
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1008      999905   83  Linux
Command (m for help):

Press “d” to delete the partition. At this time, utility will prompt for series inputs.

Command (m for help): d
Selected partition 1

Its automatically selected the partitions as “1′, because we had only one partition if you have more partitions, it will prompt to enter the partition number.

Again type “p” to list the paritions to confirm the parition deleted or not.

Disk /dev/sdb: 1024 MB, 1024000000 bytes
32 heads, 62 sectors/track, 1008 cylinders
Units = cylinders of 1984 * 512 = 1015808 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 8388608 bytes
Disk identifier: 0x10bd01c3
Device Boot      Start         End      Blocks   Id  System

Partition /dev/sdb1 got deleted. Now press “w” to save the changes. This is how we delete a partitions using fdisk command in Linux.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.

 create partitions in linux, create filesystems in linux, partitioning with fdisk, linux partitions explained, linux partitions commands, linux partitioning