Ubuntu Server 18.04 – Partitioning and formatting volumes

How to set a static IP address on Windows Server 2019

Once you’ve installed a physical or virtual disk, you’re well on your way to benefiting from additional storage. But in order to utilize a disk, it must be partitioned and formatted. We used the fdisk command earlier to see a list of current partitions on our disk, but this 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 would first determine the naming designation the disk received using either the lsblk or sudo fdisk -l commands as we’ve done earlier. In my case, I added a new disk to my sample server, and using fdisk -l, it’s pretty easy to see which disk is the new one:

sudo fdisk -l
Beginning the process of adding a new storage volume to a virtual server

In this case, we can see the /dev/sdb has no partitions (and this volume wasn’t present before I added it), so we know what disk we need to work on. Next, we’ll use the fdisk command with sudo, using the device’s name as an option. In my case, I would execute the following to work with disk /dev/sdb:

sudo 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. 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.

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 that you have a few options toward the bottom for the 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 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 2,048) 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 the fdisk command from one of my servers, to give you an idea of what the entire process looks like:

Example run of the fdisk command

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:

sudo mkfs.ext4 /dev/sdb1

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

Formatting a volume using the ext4 filesystem

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:

sudo 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.

Comments are closed.