Partition and Format featured image

Configuring Storage Devices in Linux- Learning to Partition and Format

Introduction

There are many situations where you may need a new disk on your Linux system. For the most part, creating a new disk is a straightforward process. However, things can become complicated if you are trying to tinker with partitioning schemes and filesystem formats. To help simplify the process, this tutorial will give you a step by step outline on how to do the following:

  • How to identify the new disk on the system.

  • How to create a partition on the drive.

  • How to format the partition using the Ext4 filesystem.

  • How to mount and configure auto-mounting of the filesystem.

If you would first like to get acquainted with Linux better you can read about how to install Linux on CentOS 7, how to locate files on your Linux system, as well as how to configure authentication on your Linux server.

Installation

Before we begin, you need to make sure you have the right tools installed. The parted utility is used for partitioning the drive. It usually comes preinstalled on the server. If you are using Debian or Ubuntu, you can install it as follows:

Install Parted

If you are using CentOS or Fedora, type in the following to install it:

Identification of the New Disk

The first thing you need to do is identify the new disk on the server. You can find a completely new drive by looking for the absence of a partitioning scheme. For example, you could use the parted command to obtain a list of the partition layouts of all of your disks. The disks that do not have a valid partition scheme will show an error that you can use to identify a new disk. Here is what you will type:

The unpartitioned disk will show an unrecognized disk label error like so:

Another way to identify the new disk is by using the lsblk command. This will show you a list of disks on the system and you have to identify the one with the right size and no partitions:

Here, we identified the first disk in the list as our new disk. Before you make any changes, make sure you check lsblk in every session. Without verifying the disk identifier, it is possible for you to format or partition the wrong disk. This is because identifiers like /dev/sd* and /dev/hd* may not necessarily be the same between boots. Hence, it is better to use identifiers like /dev/disk/by-uuid, /dev/disk/by-label, or /dev/disk/by-id.

How to Partition the New Drive

To partition the drive, you need to know the name of the kernel assigned to your new disk. The partition will run through the entire disk. Let’s begin!

  • Select a standard

First, you must choose the partitioning standard. The MBR standard is supported by a wide variety of operating systems. However, GPT is the more modern solution that is recommended in most cases, considering you do not have any special requirements. Select the GPT standard as follows:

In case you want to use the MBR standard, type in the following:

  • Create a new partition

Next, we will create the partition using the following command:

You can check the partition by running lsblk:

How to Make a Filesystem on a Partition

Next, we will learn how to format the partition as an Ext4 Filesystem. For this purpose, we need to pass the partition to the mkfs.ext4 utility as follows:

As you can see, we added a partition label by passing the -L flag. Subsequently, we added a name using which we can identify our target drive. It is important to ensure that you are only passing in the partition and not the whole disk. For example, where sda would be the disk name, the partition would usually have a number at the end like sda1.

To change the label of the partition at any time, use the e2label command like this:

If you want to know more options to identify your partition, then run the lsblk command. Some versions of this command will show you all information including the name, label, and UUID of the partition:

In case this command does not show all the fields, you can find them manually with this command:

The output will look something like this:

You can use any of the highlighted information to indicate the new filesystem.

How to Mount the New Filesystem

Finally, it is time to mount the filesystem so you can start using it. Typically, as per the recommendations of the Filesystem Hierarchy Standard, you should use /mnt or one of its subdirectories to mount filesystems temporarily. For permanent storage, we do not have any particular recommendations. Therefore, we can decide which scheme to use to mount.

For the purposes of this tutorial, we will mount the new filesystem under /mnt/data. Start by making a directory:

  • Mounting temporarily

Let’s say you want to mount a filesystem temporarily. You can do this with this command:

  • Mounting automatically at boot

You can use the following command to mount the drive automatically every time the server boots:

As you can see, we have made changes in the /etc/fstab file. In this file, you can use any of the different identifiers for our filesystem we acknowledged by running sudo lsblk --fs earlier in the tutorial. In the following example, we inserted the label of the partition. The subsequent lines also show you what it would look like if we had used either of the other identifiers:

Partition and Format modify etc fstab

Once you are done editing, save and close the file. If not already done, you can mount the filesystem by typing:

  • Testing the mount

A good habit is to ensure your filesystem is accessible after you have mounted the volume. You can confirm this by using the df command to check if the disk is available in the output:

Under the /mnt/data directory, you will also locate the lost+found directory. This indicates the root of an Ext* filesystem:

If you want to check that the mounted file has read and write capabilities, try writing to a test file like so:

Simply go over the file once to ensure that the write was executed correctly:

The output shows that the filesystem is functioning as it should. Now, you can safely remove this file:

Conclusion

By the end of this guide, you will know how to partition, format, mount, and test new filesystems or drives on your server. This tutorial addresses the general process of using a raw disk as a filesystem for storage in Linux. In specific cases, more complex methods may be required to partition, format, and mount. However, this tutorial will equip you with the basic knowledge and skills you need to begin with.

Happy Computing!