How to Partition and Format Disk Drives on Linux - Cherry Servers
Formatting and partitioning disks is a key aspect of Linux administration. You can use formatting and partitioning to address use cases like prepping storage media for use, addressing space issues with existing disks, or wiping a filesystem.
This article will walk you through how you can partition and format disks to complete common Linux administration tasks.
What is disk formatting in Linux?
Disk formatting is the process that prepares a storage partition for use. Formatting deletes the existing data on the partition and sets up a filesystem.
Some of the most popular filesystems for Linux include:
- Ext4 - Ext4 is a common default filesystem on many modern Linux distributions. It supports file sizes up to 16TB and volumes up to 1EB. It is not supported on Windows by default.
- NTFS - NTFS is a popular filesystem developed by Microsoft. It supports 8PB max volume and file sizes. The Linux kernel added full support for NTFS in version 5.15.
- FAT32 - Is an older filesystem, but you may still see it used in the wild. It supports a 4GB max file size and a 2TB max volume size. Many *nix and Windows operating systems support FAT32.
What is partitioning in Linux?
Partitioning is the process of creating logical boundaries on a storage device. Common examples of storage devices include hard disk drives (HDDs), solid-state drives (SSDs), USB flash drives, and SD cards. Creating a partition on a drive logically separates it from other partitions. This logical separation can be useful for a variety of scenarios, including limiting the growth of a filesystem and installing multiple operating systems on a single drive.
How to Partition and Format Disk Drives on Linux
Now let's dive into partitioning and formatting disks on a Linux system.
Prerequisites
Before we begin, you'll need:
- Access to the terminal of a Linux system. We'll use Ubuntu 22.04 LTS.
- sudo/root privileges
- An available disk you want to format and partition. We are going to use a server with custom partitioning layout from Cherry Servers.
- Backups of any data you don't want to lose (optional)
How to view disks in Linux
To view available disks in Linux, run this command:
fdisk -l | grep "Disk /"
Output should look similar to:
The fsdisk
output above included loop devices which are logical pseudo-devices, but not real disks. If you need a more refined view of your disks, use the lsblk -I 8 -d
command. "-I 8" specifies a the kernel device number for block devices and the -d excludes partitions.
The output should look similar to:
If you need more information to properly identify your drives, use lshw -class disk
. The output will include additional identifying information such as the product, size, vendor, bus, and logical name (the device’s path), similar to this:
How to view existing partitions in Linux
Before you create a new partition, you may want to view your existing partitions. To view existing partitions in Linux, use the lsblk
command. The output should look similar to:
Partitions have a TYPE
of part
and are nested under their disks in the output like sda1
in our example.
If you want to see information like file system types, disk labels and UUIDs, use the command lsblk -f
. The output should look similar to:
How to Partition a Disk in Linux
There are several ways to partition disks in Linux, including parted
and gparted
, but we'll focus on the popular fdisk
utility here. For our case, we'll assume our disk is mounted on /dev/sda
. We will create a primary partition and use the default partition number, first sector, and last sector that fdisk
selects. You can modify these options based on your requirements.
Note: If you're partitioning a disk that is currently mounted, first unmount it with the command `umount </path/to/disk>.
To begin, we'll open our drive in fdisk
with this command:
fdisk /dev/sda
That will launch the interactive fdisk
utility and you should see output similar to:
At the Command (m for help):
prompt, type n
to create a new partition. The output should look similar to:
It shows that the disk that is mounted on /dev/sda directory has one primary partition that is formatted and being used at the moment.
We'll press enter to select the default and create a new primary partition. Then, we'll be prompted to give a partition number.
We'll use the default of 2
and then get prompted for a sector number.
We'll press enter to accept the default first sector, and then get prompted for a last sector.
Again, we'll press enter to accept the default and fdisk
will create the partition. Note that if we wanted to create a smaller partition, we could use a smaller gap between our first and last block. This would enable us to create multiple partitions on the drive.
The full output looks like this:
You may enter p
to see a partition table and make sure your changes are correct:
As you can see, we now have two partitions on the /dev/sda
disk. At the Command (m for help):
prompt, input a w
to write the changes to the Linux system. The output should look similar to:
fdisk
will then exit and you'll be back at Linux shell. We can see our newly created partition sda
by running the command lsblk /dev/sda
. The output should look similar to:
How to format a disk in Linux
Now that our disk is fully partitioned, we can format the newly created sda2
partition. The general syntax for formatting a disk partition in Linux is:
mkfs.<filesystem> </path/to/disk/partition>
For example, to format our newly created /dev/sda2
partition, we can use this command:
mkfs.ext4 /dev/sda2
The output should look similar to:
To use an NTFS filesystem instead, the command is:
mkfs.ntfs /dev/sda2
To use a FAT32 filesystem instead, the command is:
mkfs.fat -F 32 /dev/sda2
The -F
parameter specifies the FAT-TYPE
, which determines if the file allocation tables are 12, 16, or 32-bit.
How to mount a disk in Linux
Once a disk is partitioned and formatted, we can mount the filesystem in Linux.
First, if your mount point doesn't already exist, created it with the mkdir
command. The general command syntax is:
mkdir </path/for/your/mount/point>
For example, to make our mount point /var/cherry
, use this command:
mkdir /var/cherry
Next, we mount our partition using the mount
command. The general command structure to mount a disk partition in Linux is:
mount -t <filesystem_type> -o <options> </path/to/disk/partition> </path/for/your/mount/point>
Note: If you omit the -t
option, the mount
command will default to auto
and attempt to guess the correct filesystem type.
For example, to mount our /dev/sda2
(which has an Ext4 filesystem) to /var/cherry
in read/write mode, we can use this command"
mount -t ext4 -o rw /dev/sda2 /var/cherry
If there are no errors, the command will not return any output.
You can confirm your partitions mount point is correct with the lsblk /dev/sda
command. The output should include a new mountpoint /var/cherry
for your newly formatted /dev/sda2
device:
Finally, to ensure the disk automatically mounts when your Linux system boots, you need to add it to /etc/fstab
.
⚠️ Warning: Be careful! Errors in /etc/fstab
can cause your system not to boot!
The general format for an /etc/fstab
partition entry is
</path/to/disk/partition> </path/for/your/mount/point> <filesystem_type> <options_from_mount> <dump> <pass_number>
Paraphrasing Ubuntu's Fstab File Configuration,<dump>
enables or disables backups using the command dump. It can be set to 1 (enabled) or 0 (disabled) and is generally disabled. <pass_number>
determines the order fsck
checks the partition for errors when the system boots. Generally, a system's root device is 1 and other partitions are 2. 0 disables the fsck
check on boot.
To edit /etc/fstab
, open it in a text editor like nano
or vim
and make the changes. For our /dev/sda2
partition mounted at /var/cherry
, we'll use this configuration:
/dev/sda2 /var/cherry ext4 rw 0 0
Save the changes and close your text editor when you're done.
Conclusion
That's it! Now you know the basics of how to partition and format disks on Linux. For a deeper dive on the topic of partitioning, formatting, and mounting drives, we recommend reading the man pages for the specific tools we used here like the mkfs.<type>
utilities (e.g. mkfs.ext4), fdisk, mount, and fstab.