Partitioning and formatting volumes

In order to utilize a disk, it must be partitioned and formatted. The fdisk command does much more than just show us what partitions are available, it allows us to manage them as well. In this section, I'll walk you through partitioning as well as formatting new volumes.

In order to begin the process of partitioning a disk, we'll use the fdisk command as root, using a device name as an option. For example, if you have a disk such as /dev/sdb that needs to be configured, you would execute the following:

# fdisk /dev/sdb

Note that I didn't include a partition number here, as fdisk works with the disk directly (and we also have yet to create any partitions). In this section, I'm assuming you have a disk that has yet to be partitioned, or one you won't mind wiping out. When executed correctly, fdisk will show you an introductory message and give you a prompt:

Main prompt of fdisk

At this point, you can press m on your keyboard for a menu of possible commands you can execute. In this example, I'll walk you through the commands required to set up a new disk for the first time.

Note

I'm sure it goes without saying, but I have to make sure you're aware of the destructive possibilities of fdisk. If you run fdisk against the wrong drive, irrecoverable data loss may result. It's common for an administrator to memorize utilities such as fdisk to the point where using it becomes muscle-memory. But always make sure that you take the time to make sure that you're running such commands against the appropriate disk.

Before we continue with creating a new partition, some discussion is required with regards to MBR and GPT partition tables. When creating partitions, you'll have the option to use an MBR partition table or a GPT partition table. GPT is the newer standard, while MBR has been around for quite some time and is probably what you've been using if you've ever created partitions in the past. By default, fdisk will create a partition table in the MBR format. But with MBR partition tables, you have some limitations to consider. First, MBR only allows you to create up to four primary partitions. In addition, it also limits you to using somewhere around 2 TB of a disk. If the capacity of your disk is 2 TB or less, this won't be an issue. However, disks larger than 2 TB are becoming more and more common. GPT doesn't have a 2 TB restriction, so if you have a very large disk, the decision between MBR and GPT has pretty much been made for you. In addition, GPT doesn't have a restriction of up to four primary partitions, as fdisk with a GPT partition table will allow you to create up to 128. It's certainly no wonder why GPT is fast becoming the new standard! It's only a matter of time before GPT becomes the default, so unless you have good reason not to, I recommend using it if you have a choice.

When you first enter the fdisk prompt, you can press m to access the menu, where you'll see toward the bottom that you have a few options in regards to which partition style you'd like to use. If you make no selection and continue with the process of creating a partition, it will default to MBR. Instead, you can press g at the prompt to specifically create a GPT partition table, or to to switch back to MBR. Note that this will obviously wipe out the current partition table on the drive, so hopefully you weren't storing anything important on the disk.

Continuing on, after you've made your choice and created either an MBR or GPT partition table, we're ready to proceed. Next, at the fdisk prompt, type n to tell fdisk that you would like to create a new partition. Then, you'll be asked if you would like to create a primary or extended partition (if you've opted for MBR). With MBR, you would want to choose primary for the first partition and then you can use extended for creating additional partitions. If you've opted for GPT, this prompt won't appear, as it will create your partition as primary no matter what.

The next prompt that will come up will ask you for the partition number, defaulting to the next available number. Press Enter to accept the default. Afterwards, you'll be asked for the first sector for the partition to use (accept the default of 2048) and then the last sector to use. If you press Enter to accept the default last sector, your partition will consist of all the free space that was remaining. If you'd like to create multiple partitions, don't accept the default at this prompt. Instead, you can clarify the size of your new partition by giving it the number of megabytes or gigabytes to use. For example, you can enter 20G here to create a partition of 20 GB.

At this point, you'll be returned to the fdisk prompt. To save your changes and exit fdisk, press w and then Enter. Now if you run the fdisk -l command as root, you should see the new partition you created. Here is some example output from my server, where I just partitioned an 8 GB virtual disk with a single partition:

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 16777215 16775168 8G 83 Linux

After you create your partition layout for your new disk and you're satisfied with it, you're ready to format it. If you've made a mistake or you want to redo your partition layout, you can do so by entering the fdisk prompt again and then pressing g to create a new GPT layout or o to create a new MBR layout. Then, continue through the steps again to partition your disk. Feel free to practice this a few times until you get the hang of the process.

Formatting is done with the mkfs command. To format a device, you execute mkfs with a period (.), followed by the type of filesystem you would like to format the target as. The following example will format /dev/sdb1 as Ext4:

# mkfs.ext4 /dev/sdb1

Your output will look similar to mine in the following screenshot:

Main prompt of fdisk

If you've opted for a filesystem type other than Ext4, you can use that in place of Ext4 when using mkfs. The following example creates an XFS filesystem instead:

# mfs.xfs /dev/sdb1

So, now that we've created one or more partitions and formatted them, we're ready to mount the newly created partition(s) on our server. In the next section, I'll walk you through mounting and unmounting storage volumes.