It is easy to prepare a new hard drive for Linux. Although there are many filesystem formats and partitioning schemes available, the basic process is the same.
This guide will explain the following:
Identify the new disk in the system.
You can create a single partition that covers the entire drive. Most operating systems require a partition layout even if one filesystem exists.
Formatting the partition using the Ext4 filesystem, which is the default in most modern Linux distributions.
Mounting and setting up Auto-mounting the filesystem at boot
Step 1 – Install Parted
Partitioning the drive can be done using the parted utility. Linux defaults most of the commands required to interact with low-level files. Partitioning is created by part, one of the rare exceptions.
You can install parted on Ubuntu or Debian servers by typing:
sudo apt update
sudo apt-install parted
You can install Fedora, Rocky Linux or RHEL servers by simply typing:
sudo dnf install parted
You should have all the commands in this tutorial installed before you can proceed to the next step.
Step 2 – Identify the New Disk in the System
Before you can set up the drive you must be able to identify it correctly on the server.
You can identify a new drive by looking for a missing partitioning scheme. Partitioned will give an error if it asks for the layout of your partitions. This information can be used to identify the new disk.
sudo parted -l | grep Error
For the unpartitioned new disk, you should see an error in the disk label:
Error: /dev/sda: unrecognized disk label
You can also use lsblk to search for the right size disk with no partitions.
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 /
Important: Before making any changes, make sure to run lsblk each time you connect to your server. You should verify that the /dev/sd* or /dev/hd* disk identifications are consistent between boots. If they are not, there is a risk of formatting the wrong drive or partitioning it.
You might consider using persistent disk identifiers such as /dev/disk/byuuid or /dev/disk/bylabel. For more information, see our introduction to storage concepts in Linux article.
You can partition your drive by knowing the name the kernel assigned it.
Step 3 – Partition the New Drive
As we mentioned in the introduction you will create one partition that spans the entire disk.
Choose a Partitioning Standard
First, specify the partitioning standards you want to use. You have two options: MBR and GPT. GPT is the more recent standard while MBR is supported by older operating systems more. GPT is the better choice for a cloud server.
Pass the disk that you have identified to choose the GPT standard with mklabel:gpt
sudo parted /dev/sda mklabel gpt
Use mklabel.msdos to use the MBR format
sudo parted /dev/sda mklabel msdos
Create a New Partition
After you have selected the format, you can create partitions that span the entire drive using parted-a:
sudo parted -a opt /dev/sda mkpart primary ext4 0% 100%
This command can be broken down as follows:
- Partited – An opt runs parted that sets the default optimal alignment type.
- /dev/sda indicates the disk you are partitioning.
- mkpart primary ex4 creates a standalone partition (i.e. Bootable partition that is not extended from another, using the ext4 filesystem.
- 0% = 100% This partition should be able to span the entire disk.
You can find more information on Parted’s manual page.
You should be able to see the new partition if you run 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 /
Now you have created a new partition, but it hasn’t been initialized as a filesystem. Although the differences between the two steps are somewhat arbitrary and specific to Linux filesystems, they are still two steps in practice.
Step 4 – Create a Filesystem for the New Partition
Once you have created a partition, you can use Ext4 to initialize it. Although Ext4 isn’t the only available filesystem, it is the easiest option to create a single Linux volume. Windows uses filesystems such as NTFS or exFAT but they are not supported on other platforms. They can be read-only in certain contexts and cannot be used for boot drives for other operating systems. MacOS uses HFS+ or APFS with the same caveats. You can also use newer Linux filesystems, such as ZFS or BTRFS. However, these have different requirements and are better suited for multi-disk arrays.
Use the utility mkfs.ext4 to initialize an Ext4 Filesystem. The -L flag can be used to add a partition label. You can choose a name to help identify the drive.
Not the whole disk. Disks can be named in Linux with sda or sdb or hda. These disks’ partition names have numbers at the end. You would use something like “sda1” instead of “sda”.
sudo mkfs.ext4 -L datapartition /dev/sda1
You can use the e2label command to modify the partition label.
sudo e2label /dev/sda1 newlabel
With lsblk, you can view all the possible ways to identify your partition. The name, label, UUID, and partition ID should be found.
Some versions of lsblk can print all this information using the –fs argument
sudo lsblk –fs
These options can be specified manually by using lsblk-o, followed by the appropriate options.
sudo lsblk -o NAME,FSTYPE,LABEL,UUID,MOUNTPOINT
This is the output you should expect. You can refer to the new filesystem using different methods as indicated by the highlighted output.
NAME FSTYPE LABEL UUID MOUNTPOINT sda └─sda1 ext4 datapartition 4b313333-a7b5-48c1-a957-d77d637e4fda vda └─vda1 ext4 DOROOT 050e1e34-39e6-4072-a03e-ae0bf90ba13a /
Keep track of the output. You’ll need it to mount the filesystem in step 2.
Step 5 — Mount the New Filesystem
You can now mount the filesystem.
The Filesystem Hierarchy Standard recommends that temporary mounted filesystems, such as removable drives, be placed in the /mnt or subdirectory. You can mount any scheme you like. This tutorial will show you how to mount the drive under /mnt/data.
Use mkdir to create the directory
sudo mkdir -p /mnt/data
Mounting the Filesystem Temporarily
You can temporarily mount the filesystem by typing:
sudo mount -o defaults /dev/sda1 /mnt/data
Mounting the Filesystem Automatically at Boot
To automatically mount the filesystem each time your server boots, add an entry in the /etc/fstab directory. This file contains information about the permanent or routinely mounted disks on your system. Use nano or any other text editor to open the file.
sudo nano /etc/fstab
To display filesystem identifiers, you used sudo lsblk -fs in the final step. Any of these can be used in this file. This example uses the partition name, but you can see how the lines would look if you used the two other identifiers in the commented-out lines.
. . . ## 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
These options are available without the LABEL=datapartition element.
/mnt/data indicates the path to the disk.
The ext4 signifies that this is an Ext4 part.
defaults refer to the fact that this volume should only be mounted using the default options such as read-write support.
The value of 0 2 indicates that the filesystem should have been validated by the local computer in the event of errors. However, it should be considered as a priority after your root volume.
Not: The man page for the /etc/fstab files contains information about each field. For details about mount options for specific filesystem types, see a man [filesystem] (like ext4).
When you’re done, save and close the file. If you’re using nano, press Ctrl+X and, when asked, click Y.
Mounting the filesystem that you have not mounted previously is now possible with mount -a
sudo mount -a
Once you have mounted the volume, it is time to check that the filesystem remains accessible.
The df command will output information about the disk’s availability. Sometimes, df may include information about temporary filesystems called “tmpfs” in the output. This can be removed by adding -x to tmpfs.
df-h x tmpfs
Mounted on: Filesystem Size
/dev/vda1 20G 1.3G 18G 7% /
/dev/sda1 99G 60M 94G 1% /mnt/data
By writing to a text file, you can verify that the disk is properly mounted and has read-and-write capabilities.
echo “success” | sudo tee /mnt/data/test_file
To ensure that the write was done correctly, read the file again
After you’ve verified that your new filesystem works correctly, you can delete the file.
sudo rm /mnt/data/test_file
Now your new drive is ready to be mounted, partitioned, and formatted. This is how you convert a raw disk to a filesystem Linux can use. While there are more advanced methods for formatting, partitioning, and mounting that may be appropriate in certain cases, the above is a good start.