fdisk – Easy Way To Manage Disk Partitions In Linux
======
Hard disks can be divided into one or more logical disks called partitions.
This division is described in the partition table (MBR or GPT) found in sector 0 of the disk.
Linux needs at least one partition, namely for its root file system and we can’t install Linux OS without partitions.
Once created, a partition must be formatted with an appropriate file system before files can be written to it.
To do so, we need some utility to perform this in Linux.
There are many utilities are available for that in Linux. We had written about **[Parted Command][1]** in the past and today we are going to discuss about fdisk.
fdisk command is one of the the best tool to manage disk partitions in Linux.
It supports maximum `2 TB`, and everyone prefer to go with fdisk.
This tool is used by vast of Linux admin because we don’t use more than 2TB now a days due to LVM and SAN. It’s used in most of the infra structure around the world.
Still if you want to create a large partitions, like more than 2TB then you have to go either **Parted Command** or **cfdisk Command**.
Disk partition and file system creations is one of the routine task for Linux admin.
If you are working on vast environment then you have to perform this task multiple times in a day.
### How Linux Kernel Understand Hard Disks?
As a human we can easily understand things but computer needs the proper naming conversion to understand each and everything.
In Linux, devices are located on `/dev` partition and Kernel understand the hard disk in the following format.
* **`/dev/hdX[a-z]:`** IDE Disk is named hdX in Linux
* **`/dev/sdX[a-z]:`** SCSI Disk is named sdX in Linux
* **`/dev/xdX[a-z]:`** XT Disk is named sdX in Linux
* **`/dev/vdX[a-z]:`** Virtual Hard Disk is named vdX in Linux
* **`/dev/fdN:`** Floppy Drive is named fdN in Linux
* **`/dev/scdN or /dev/srN:`** CD-ROM is named /dev/scdN or /dev/srN in Linux
### What Is fdisk Command?
fdisk stands for fixed disk or format disk is a cli utility that allow users to perform following actions on disks. It allows us to view, create, resize, delete, move and copy the partitions.
It understands MBR, Sun, SGI and BSD partition tables and it doesn’t understand GUID Partition Table (GPT) and it is not designed for large partitions.
fdisk allows us to create a maximum of four primary partitions per disk. One of these may be an extended partition and it holds multiple logical partitions.
1-4 is reserved for four primary partitions and Logical partitions start numbering from 5.
![][3]
### How To Install fdisk On Linux
You don’t need to install fdisk in Linux system because it has installed by default as part of core utility.
### How To List Available Disks Using fdisk Command
First we have to know what are the disks were added in the system before performing any action. To list all available disks on your system run the following command.
It lists possible information about the disks such as disk name, how many partitions are created in it, Disk Size, Disklabel type, Disk Identifier, Partition ID and Partition Type.
```
$ sudo fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Units: sectors of 1 core.md Dict.md lctt2014.md lctt2016.md lctt2018.md LICENSE published README.md scripts sources translated 512 = 512 bytes
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
```
### How To Create A Disk Partition Using fdisk Command
If you would like to create a new partition use the following steps. In my case, i'm going to create 4 partitions (3 Primary and 1 Extended) on `/dev/sdc` disk. To the same for other partitions too.
As this takes value from partition table so, hit `Enter` for first sector. Enter the size which you want to set for the partition (We can add a partition size using KB,MB,G and TB) for last sector.
For example, if you would like to add 1GB partition then the last sector value should be `+1G`. Once you have created 3 partitions, it will automatically change the partition type to extended as a default. If you still want to create a fourth primary partitions then hit `p` instead of default value `e`.
```
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): Enter
Using default response p.
Partition number (1-4, default 1): Enter
First sector (2048-20971519, default 2048): Enter
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help): p
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 core.md Dict.md lctt2014.md lctt2016.md lctt2018.md LICENSE published README.md scripts sources translated 512 = 512 bytes
### How To Create A Extended Disk Partition Using fdisk Command
Make a note, you have to use remaining all space when you create a extended partition because again you can able to create multiple logical partition in that.
```
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (3 primary, 0 extended, 1 free)
e extended (container for logical partitions)
Select (default e): Enter
Using default response e.
Selected partition 4
First sector (6293504-20971519, default 6293504): Enter
Last sector, +sectors or +size{K,M,G,T,P} (6293504-20971519, default 20971519): Enter
Created a new partition 4 of type 'Extended' and of size 7 GiB.
Command (m for help): p
Disk /dev/sdc: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 core.md Dict.md lctt2014.md lctt2016.md lctt2018.md LICENSE published README.md scripts sources translated 512 = 512 bytes
### How To View Unpartitioned Disk Space Using fdisk Command
As described in the above section, we have totally created 4 partitions (3 Primary and 1 Extended). Extended partition disk space will show unpartitioned until you create a logical partitions in that.
Use the following command to view the unpartitioned space for a disk. As per the below output we have `7GB` unpartitioned disk.
```
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): F
Unpartitioned space /dev/sdc: 7 GiB, 7515144192 bytes, 14678016 sectors
Units: sectors of 1 core.md Dict.md lctt2014.md lctt2016.md lctt2018.md LICENSE published README.md scripts sources translated 512 = 512 bytes
### How To Format A Partition Or Create A FileSystem On The Partition
In computing, a file system or filesystem controls how data is stored and retrieved through inode tables.
Without a file system, the system can't find where the information is stored on the partition. Filesystem can be created in three ways. Here, i'm going to create a filesystem on `/dev/sdc1` partition.
```
$ sudo mkfs.ext4 /dev/sdc1
or
$ sudo mkfs -t ext4 /dev/sdc1
or
$ sudo mke2fs /dev/sdc1
mke2fs 1.43.5 (04-Aug-2017)
Creating filesystem with 262144 4k blocks and 65536 inodes
Writing superblocks and filesystem accounting information: done
```
When you creating a filesystem on tha partition that will create the following important things on it.
* **`Filesystem UUID:`** UUID stands for Universally Unique Identifier, UUIDs are used to identify block devices in Linux. It's 128 bit long numbers represented by 32 hexadecimal digits.
* **`Superblock:`** Superblock stores metadata of the file system. If the superblock of a file system is corrupted, then the filesystem cannot be mounted and thus files cannot be accessed.
* **`Inode:`** An inode is a data structure on a filesystem on a Unix-like operating system that stores all the information about a file except its name and its actual data.
* **`Journal:`** A journaling filesystem is a filesystem that maintains a special file called a journal that is used to repair any inconsistencies that occur as the result of an improper shutdown of a computer.
### How To Mount A Partition In Linux
Once you have created the partition and filesystem then we need to mount the partition to use.
To do so, we need to create a mountpoint to mount the partition. Use mkdir command to create a mountpoint.
```
$ sudo mkdir -p /mnt/2g-new
```
For temporary mount, use the following command. You will be lose this mountpoint after rebooting your system.
```
$ sudo mount /dev/sdc1 /mnt/2g-new
```
For permanent mount, add the partition details in the fstab file. It can be done in two ways either adding device name or UUID value.
Permanent mount using Device Name:
```
# vi /etc/fstab
/dev/sdc1 /mnt/2g-new ext4 defaults 0 0
```
Permanent mount using UUID Value. To get a UUID of the partition use blkid command.