Skip to main content

Managing partitions in Linux with fdisk

From the stone age, we bring you fdisk: A still-useful tool for the modern sysadmin to be familiar with.

Lately, I have been doing a lot of research and writing around disk management utilities in Linux and the different options that we, as sysadmins and operators, have at our disposal to accomplish these types of tasks. Disk management has been and will most likely be one of the most crucial considerations for system administrators. Everything that we do is tied to a storage device in some way. Ensuring that our projects, backups, customer, and user data are all appropriately segmented and stored is absolutely essential.

Because of this, there have been multiple utilities developed to ease the burden of storage management. I wrote a series of articles on the popular Logical Volume Manager (LVM) recently, and it led me to explore the other options we have for creating and partitioning disks and volumes.

I want to take a look specifically at the command suite fdisk.

fdisk

The fdisk command suite is a classic disk partitioning utility that was developed in the early 1980s. It was released for a wide variety of operating systems. We are going to focus on it as a Linux tool here, however. It has a huge amount of functionality. From listing, creating, removing, and formating, fdisk can do it all. So let's take a look at how it works.

Listing partitions with fdisk

The fdisk -l command allows you to view all partitions on your system:

[root@rhel ~]# fdisk -l
Disk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 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 /dev/sda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: dos
Disk identifier: 0x184931d5
    
Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 62914559 60815360  29G 8e Linux LVM
    
Disk /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 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

Now that we know what partitions we have, we are going to create and format a new one.

Creating partitions with fdisk

To create a new partition with fdisk, you need to select a primary disk that has unused space. Once you decide on the disk you want to use, you will need to run the following (I have opted to work in /dev/sdb):

[root@rhel ~]# fdisk /dev/sdb
    
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only until you decide to write them.
Be careful before using the write command.
 
Does not contain a recognized partition table.
    Created a new DOS disklabel with disk identifier 0x569c5370.
    
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151): +500
    
Created a new partition 1 of type 'Linux' and of size 250.5 KiB.
**Be sure to write your changes to disk using the `w` flag**

You will see that we first entered the command prompt for the disk we wanted to use /dev/sdb. Use the n option to create a new partition. We then have to decide if we are creating a primary partition or an extended partition. For this example, I created a primary partition (designated with the selection p). We will then label the partition using a partition number. If this is the first new partition you are creating, feel free to use the number 1. Otherwise, number appropriately. After we have made our selections, we need to designate the size of the partition we are creating. As the /dev/sdb was only 1 GB, I have decided to keep it small here and only create a 250 KiB partition. I left the First sector portion blank and entered +500.  Partition created! Now let's format.

Formatting partitions in Linux

As fdisk doesn't have an internal format command, we are going to use the mkfs command.

The syntax is something like this:

[root@rhel ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.44.6 (5-Mar-2019)
        
Filesystem too small for a journal
Creating filesystem with 248 1k blocks and 32 inodes
        
Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

Deleting partitions with fdisk

To remove a partition using fdisk, you need to enter the command prompt for fdisk on the disk you are removing from. For example, to remove the partition I created in the previous example (/dev/sdb1), I need to enter the command line for /dev/sdb. Then give the d option for delete and select the partition number I wish to remove. As there is only one partition in /dev/sdb it deleted the only existing one here without further input. Again, be sure to write your changes to the disk with the w flag.

[root@rhel ~]# fdisk /dev/sdb
    
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only until you decide to write them.
Be careful before using the write command.
    
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Wrap up

I highly recommend that you check out the fdisk utility for yourself. There is no better practice than firing up a virtual machine and playing in the filesystem. If you mess up, you can just blow the VM away and start again. I also would urge you to compare your fdisk experience with that of LVM. I personally prefer the LVM utility as it is a bit more user friendly, however, that is subjective at best. If you are interested in my LVM series, you can find it at the bottom of the article. I will also be exploring the parted command in a future article, so keep an eye out for that!

[ Free online course: Red Hat Enterprise Linux technical overview. ]

Topics:   Storage   Linux  
Author’s photo

Tyler Carrigan

Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.