Partition and Format Storage Devices on Linux

How to Partition and Format Storage Devices on Linux

Partitioning and formatting storage devices in Linux is one of the key tasks of Linux system administrators to better manage the operating system. Linux administrators perform formatting and partitioning processes to perform tasks such as managing available Storage Devices space, erasing and modifying file systems, preparing storage media for use, fixing errors, or deleting data.

Therefore, to optimize management, you need to format and partition storage devices and hard drives in Linux. Fortunately, how to format and mount disk partitions and prepare a new disk for use in Linux is not a complicated process, you can do it easily by following the steps that we will provide.

To use hard disks and store information, you must learn how to partition and format Storage Devices. In this article, we will thoroughly teach how to partition and format storage devices in Linux, read this article until the end.

What is the purpose of partitioning on Linux?

Partitioning divides the space of various storage devices such as hard disk drives (HDDs), solid-state drives (SSDs), USB flash drives, and SD cards by creating logical boundaries for better management. In Linux, partitions are known by names such as “/”, “/home” and “/boot”, and you can create a separate partition for each of the folders in the root.

Partitioning by dividing the storage drive into several partitions and separating the partitions from each other provides the possibility of efficient use of storage space for different scenarios. Also, Partitioning has advantages such as installing multiple operating systems on one drive and defining limits for expanding the file system.

What is hard disk formatting on Linux?

To use the storage partition, you must prepare it through the formatting process. The formatting process does this by deleting the data in the partition and starting the filesystem.

Getting to know the most common Linux filesystems

  • NTFS: The NT file system (NTFS) is one of the popular file systems developed by Microsoft and is used to organize, save and restore files on all types of hard drives in Windows operating systems. This file system can support a maximum of 8 PB volume and file size. It is also implemented in version 5.15 of the Linux kernel.
  • FAT32: This file system supports a maximum file size of 4 GB and a volume of 2 TB and is compatible with the Windows operating system, but it is old.
  • Ext4: This file system is supported by default in many modern Linux distributions. Unlike other cases that were compatible with Windows, Windows does not support this file system by default. Ext4 can support a maximum file size of 16 TB and a volume of 1 EB.

Partitioning and Formatting Storage Devices on Linux

Now that you are familiar with partitioning and formatting storage devices, we will teach you how to partition and format Storage Devices on Linux. Before starting, we will describe the minimum requirements for the execution of the recipes, which you should consider.

prerequisites

  • Linux System or Linux VPS.
  • A user account with Root/Sudo privileges
  • Access to the terminal environment
  • Access to the disk that you intend to partition and format.
  • Making a backup of important data to ensure that you get a copy of the data (optional)

Step 1: Installing the storage drive partitioning tool

The parted tool is one of the useful tools in drive partitioning. In most Linux distributions, this tool is already installed, but if your Linux operating system does not benefit from this helpful tool, you should start the partitioning process by installing the parted tool. Before installing the tool, update your system repository with the following command:

sudo apt update

Then, if you want to install the parted tool on Debian and Ubuntu operating systems and its derivatives, run the following command in the command line:

sudo apt install parted

If you intend to install the parted tool on RHEL, CentOS, and Fedora, use the following command:

sudo dnf install parted

Step 2: Finding the new disk on the server

Correct identification of the new disk in the Linux system is one of the prerequisites for starting the drive and Format. If we want to use the easiest method to identify the new drive, you must first prepare a list of the partition layout of your system disks through the parted tool. Then identify the hard disk that is associated with the error of not having a valid partition scheme as a new hard disk. Therefore, by running the following command, search for a disk with no partition scheme to find the new disk:

sudo parted -l | grep Error
Output

Error: /dev/sda: unrecognised disk label

As you can see, the unrecognized disk label error can guide you in identifying a new disk that is not partitioned.

Running the lsblk command is another way to find an existing and accessible disk without associated partitions:

lsblk
Output

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 /

In the output of the lsblk command, you will see a list of Block devices that displays devices such as RAM disks, USB drives, hard drives, and CD/ROM and their related information. The description of the output is as follows:

NAME: displays the name of the device.

MAJ:MIN: provides the main number of the device.

RM: Specifies the portability of the device, where number 0 means it cannot be moved and number 1 means it can be moved.

SIZE: Specifies the size of the device.

RO: Determines whether the device is read-only or not.

TYPE: Displays the device type.

MOUNTPOINT: specifies the connection point of the device.

It is recommended to run the lsblk command when reconnecting to the server and before making changes. /dev/sd* and /dev/hd* disk identifiers are not always compatible between boots. Therefore, be careful not to misidentify the disk identifier because then you will do incorrect formatting and partitioning. Fixed disk identifiers such as /dev/disk/by-uuid, /dev/disk/by-label, or /dev/disk/by-id are less risky.

As you can see, the output of this command does not provide information about the devices’ file systems. If you need a list of file systems and related information, you should use the following command:

lsblk -f

In the output of the above command, a list of block devices is provided. You can identify unformatted partitions by identifying partitions that do not provide information about the file system.

To get more detailed information about your disks, run the following command:

lsblk -I 8 -d

-I 8: specifies the kernel device number for block devices.

-d: excludes partitions.

Step 3: How to partition the new drive

For disk partitioning in Linux, there are various tools such as gparted, fdisk, etc., our training is through the popular parted tool, which requires you to set the partitioning standard and then create a new partition that covers the entire disk.

  • Determining the partitioning standard

First, select the partitioning standard. Two standards are used for partitioning:

  1. MBR: It is an older standard that is compatible with older operating systems.
  2. GPT: It is a modern standard that is ideal for cloud servers.

After comparing MBR Vs GPT, you should make the best choice based on your operating system’s needs and compatibility.

If you chose the GPT standard to partition the /dev/sda disk as a new disk, you should use themklabel gptcommand:

sudo parted /dev/sda mklabel gpt

If your operating system is compatible with the MBR standard, you must run the following command:

sudo parted /dev/sda mklabel msdos
  • Creating a new partition

After selecting the standard, enter the following command to create a new partition:

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

primary ext4 in the command above refers to the use of the ext4 file system in the independent partition and 0% 100% is entered to cover the entire disk from the beginning to the end.

Then you can run the lsblk command to make sure the new partition is created.

lsblk
Output

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 /

Step 4: Creating the file system in the new partition

Up to this point in the article, you have successfully created the new partition, but in our opinion, the process of creating the new partition has not been completed because it is not mounted as a file system. This step is not necessary, but Linux users who need to use the file system will find this step useful.

There are various file systems for initializing the created partition, among which Ext4 is the easiest option for Linux. NTFS and exFAT file systems are supported on Windows and have limitations on other platforms.

Run the following command to initialize the Ext4 file system:

sudo mkfs.ext4 -L datapartition /dev/sda1

The names sda, sdb, hda, etc., are Linux disks. In the above command, you should not use only the name of the disk, for example, sda. You must place the partition on the disks that have a number at the end of their name (for example, sda1) and also Be careful to enter the partition path.

To be able to change the label of the partition again, you must use the following command:

sudo e2label /dev/sda1 newlabel

By running the following command, you can get more information to identify the partition using the name, UUID, and label of the partition:

sudo lsblk --fs

You can even manually type the information about the partitions you need to access :

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

NAME   FSTYPE LABEL         UUID                                 MOUNTPOINT

sda                                                             

└─sda1 ext4   datapartition 4b313333-a7b5-48c1-a957-d77d637e4fda

vda                                                             

└─vda1 ext4   DOROOT        050e1e34-39e6-4072-a03e-ae0bf90ba13a /

So you will have access in different ways to the new file system.

This output is important for mounting the filesystem that we will teach later, so copy it somewhere.

Step 5: Formatting the Storage Devices(disk) on Linux

Now that you have completed the partitioning step, it is time to format the new partition. To format the disk partition in Linux, the main syntax is as follows:

mkfs.<filesystem> </path/to/disk/partition>

Based on the file system you are using, the format of the Linux disk using themkfscommand is different; in the following, we will present the desired commands for the most common file systems.

  • Formatting the disk with the ext4 file system

If you are using the ext4 file system, the desired command to format the disk partition you created is as follows:

mkfs.ext4 /dev/sda1
  • Formatting the disk with the NTFS file system

If you want to format the disk partition you created, while using the NTFS file system, you must run the following command:

mkfs.ntfs /dev/sda1
  • Formatting the disk with the FAT32 file system

To format the disk when using the FAT32 file system, run the following command:

mkfs.fat -F 32 /dev/sda1

The -F parameter is used to determine the FAT-TYPE, which specifies whether the file allocation tables are 12, 16, or 32-bit.

Step 6: Mounting the disk partition in Linux

At this stage, if you want to follow the correct method, you should use the /mnt directory or its subdirectory for temporarily installed file systems. It is up to you to choose the mount location or its design. Our tutorial mounts in the drive under /mnt/data.

1.First create a directory:

sudo mkdir -p /mnt/data

2. To temporarily mount the file system, enter the following command:

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

3. To set the automatic mount of the file system at boot time, you should add an entry to the /etc/fstab file that includes all the information of the permanent disks. For this purpose, open the file with your favorite text editor or nano:

sudo nano /etc/fstab

4. Enter the following command to access the file system identifiers:

sudo lsblk --fs

 

/etc/fstab

. . .

## 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

After completing the tasks, save the file and if you used the nano editor, close it by pressing Ctrl+X, then Y, and Enter.

5. Now you can mount the file system that you did not mount by running the following command:

sudo mount -a

6. In the last step after mounting, it is better to make sure that the file system is available:

df -h -x tmpfs
Output

Filesystem      Size  Used Avail Use% Mounted on

/dev/vda1        20G  1.3G   18G   7% /

/dev/sda1        99G   60M   94G   1% /mnt/data

7. In addition, you can check the ability to read and write the mounted disk:

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

8. To make sure that write is executed correctly, read the file again using the following command:

cat /mnt/data/test_file
Output

success

9. After making sure that the file system is running correctly, you can remove the file using the following command:

sudo rm /mnt/data/test_file

FAQ

  • Root partition: It contains the Linux kernel and its boot files, and you can consider any size for it, but the best case is at least 17 megabytes and at most 25 megabytes.
  • swap partition: it is better to be at least the size of the system memory.
  • Home partition: It is recommended to consider at least 15 GB for this partition.

Determining the size of the partitions depends on the size of the hard drive and your needs.

lsblk

By running this command, you can get information about the disks in Linux.

The sudo mkfs -t ext4 /dev/sdb1 command formats the disk partition with the ext4 file system.

Conclusion

In this tutorial, you were able to format and create a partition in different file systems in Linux. Now your new drive is formatted and partitioned for use. Using this tutorial, you can format and manipulate the partition to improve management, as well as use a raw disk as a file system in Linux for storage. We hope that reading this article will be useful for you.

Thank you for your support.

Leave a Reply

Your email address will not be published. Required fields are marked.