SoFunction
Updated on 2025-03-03

Steps to Partition and Format Storage Devices in Linux

Introduction

Preparing a new disk for use on a Linux system is an easy process. While there are many tools, file system formats, and partitioning schemes that may change the process due to special needs, the basic principles are still the same.

This guide will cover the following procedures:

  • Identify the new disk on the system.
  • Create a single partition spanning the entire drive (most operating systems expect a partition layout, even if only one file system exists)
  • Format partitions using Ext4 file system (the default in most modern Linux distributions)
  • Mount and set the file system to automatically mount at startup

Step 1 — Install Parted

To partition the drive, you will usepartedUtilities. Most of the commands required to interact with low-level file systems are available by default on Linux. Create partitionpartedIt is one of the occasional exceptions.

If you are on an Ubuntu or Debian server and have not installed itparted, you can install it by entering the following command:

sudo apt update
sudo apt install parted

If you are on a RHEL, Rocky Linux, or Fedora server, you can install it by entering the following command:

sudo dnf install parted

The other commands used in this tutorial should be pre-installed, so you can move on to the next step.

Step 2 — Identify the new disk on the system

Before you set up a drive, you need to be able to correctly identify it on the server.

If this is a brand new drive, one way to identify it on the server is to look for missing partition schemes. If you requestpartedLists the partition layout of the disk, and for any disks that do not have a valid partitioning scheme, it will generate an error. This can be used to help identify new disks:

sudo parted -l | grep Error

You should see a new, unpartitioned disk appearunrecognized disk labelmistake:

Error: /dev/sda: unrecognized disk label

You can also uselsblkCommand and find a disk with the correct size without associated partitions:

lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   100G  0 disk 
vda    253:0    0    20G  0 disk 
└─vda1 253:1    0    20G  0 part /

Once you know the name the kernel allocates for your disk, you can partition the drive.

Step 3 — Partitioning the new drive

As mentioned in the introduction, you will create a single partition spanning the entire disk in this guide.

Select partition criteria

First, you need to specify the partition criteria to use. There are two options: GPT and MBR. GPT is a more modern standard, while MBR is more supported in older operating systems. For typical cloud servers, GPT is a better choice.

To select the GPT standard, usepartedAnd usemklabel gptPass the disk you identified:

sudo parted /dev/sda mklabel gpt

To use the MBR format, usemklabel msdos

sudo parted /dev/sda mklabel msdos

Create a new partition

After selecting the format, you can useparted -aCreate partitions across the entire drive:

sudo parted -a opt /dev/sda mkpart primary ext4 0% 100%

You can break this command down as follows:

  • parted -a optRun parted and set the defaultoptimal alignment type.
  • /dev/sdaIt is the disk you want to partition.
  • mkpart primary ext4Create a separate (i.e. bootable, not extended from another) partition, using the ext4 file system.
  • 0% 100%Indicates that this partition should span the start to the end of the disk.

For more information, see Parted's man page.

If you checklsblk, you should see a new partition available:

lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   100G  0 disk 
└─sda1   8:1    0   100G  0 part 
vda    253:0    0    20G  0 disk 
└─vda1 253:1    0    20G  0 part /

You have now created a new partition, but have not initialized it to the file system. The difference between these two steps is arbitrary to some extent and is unique to how the Linux file system works, but in practice they are still two steps.

Step 4 - Create a file system on a new partition

Now that you have a available partition, you can initialize it to the Ext4 file system. Ext4 is not the only file system option, but it is the most straightforward option for a single standalone Linux volume. Windows uses file systems such as NTFS and exFAT, but their support on other platforms is limited (which means in some cases they will be read-only and cannot be used as boot drives for other operating systems), and macOS uses HFS+ and APFS, and there are the same considerations. There are also Linux file systems that are newer than Ext4, such as ZFS and BTRFS, but these file systems have different requirements and are usually more suitable for multi-disk arrays.

To initialize the Ext4 file system, use the mkfs.ext4 utility. You can use the -L flag to add partition tags. Choose a name that will help you identify this particular drive:

sudo mkfs.ext4 -L datapartition /dev/sda1

If you want to change the partition tag in the future, you can usee2labelOrder:

sudo e2label /dev/sda1 newlabel

You can uselsblkView all the different ways to identify partitions. You should find the partition's name, label, and UUID.

Some versions oflsblkWill use--fsParameters print all this information:

sudo lsblk --fs

You can also specify them manually, usinglsblk -oFollowed by related options:

sudo lsblk -o NAME,FSTYPE,LABEL,UUID,MOUNTPOINT

You should receive such output. The highlighted output indicates different methods you can use to reference a new file system:

NAME   FSTYPE LABEL         UUID                                 MOUNTPOINT
sda                                                              
└─sda1 ext4   datapartition 4b313333-a7b5-48c1-a957-d77d637e4fda 
vda                                                              
└─vda1 ext4   DOROOT        050e1e34-39e6-4072-a03e-ae0bf90ba13a /

Note this output because you will use it when mounting the file system next.

Step 5 —— Mount the new file system

Now you can mount the file system for use.

The file system hierarchy standard recommends the use of the /mnt directory or its subdirectories to temporarily mount the file system (such as a removable drive). It has no suggestions for mounting longer-lasting storage, so you can choose any scheme you like. In this tutorial, you will mount the drive under /mnt/data.

Create this directory using mkdir:

sudo mkdir -p /mnt/data

Temporary mount file system

You can enter the following command to temporarily mount the file system:

sudo mount -o defaults /dev/sda1 /mnt/data

Automatically mount the file system on startup

In order to automatically mount the file system every time the server starts, you need to/etc/fstabAdd an entry to the file. This file contains information about all permanent or frequently mounted disks of the system. usenanoOr open the file by your favorite text editor:

sudo nano /etc/fstab

In the previous step, you usedsudo lsblk --fsThe command displays the identifier of the file system. You can use any of these in this file. This example uses the partition tag, but you can see what the lines using the other two identifiers look like, and they are commented out:

. . .
## Use one of the identifiers you found to reference the correct partition# /dev/sda1 /mnt/data ext4 defaults 0 2
# UUID=4b313333-a7b5-48c1-a957-d77d637e4fda /mnt/data ext4 defaults 0 2
LABEL=datapartition /mnt/data ext4 defaults 0 2

Apart fromLABEL=datapartitionIn addition to elements, these options are as follows:

  • /mnt/dataIt is the path to the disk being mounted.
  • ext4Indicates that this is an Ext4 partition.
  • defaultsIt means that the volume should be mounted using the default options, such as read and write support.
  • 0 2It means that the file system should be verified by the local machine when an error occurs, but as the second priority, not your root volume.

Save and close the file when finished. If you are usingnano,according toCtrl+X, then pressY, and then pressEnter

If the file system was not mounted before, you can now use itmount -aCommand to mount it:

sudo mount -a

Test mount

After mounting the volume, we should check to make sure the file system is accessible.

You candfThe output of the command to check whether the disk is available. sometimesdfThe output will include the temporary file system (calledtmpfs) unnecessary information you can attach-x tmpfsTo exclude this information:

df -h -x tmpfs
File system        capacity  Used  Available Used% Mounting point
/dev/vda1        20G  1.3G   18G   7% /
/dev/sda1        99G   60M   94G   1% /mnt/data

You can also check if the disk has read and write functions by writing to a test file:

echo "success" | sudo tee /mnt/data/test_file

Read the file again to make sure the write is executed correctly:

cat /mnt/data/test_file
success

After verifying that the new file system is running properly, you can delete the file:

sudo rm /mnt/data/test_file

in conclusion

Your new drive should now be partitioned, formatted, mounted, and ready to use. This is a general process you can use to convert the original disk to a file system that Linux can use for storage. In some cases, more complex partitioning, formatting, and mount methods may be more appropriate, but the above methods are a good starting point for general purposes.

The above is the detailed operation steps for partitioning and formatting storage devices in Linux. For more information on partitioning and formatting storage devices in Linux, please pay attention to my other related articles!