Every operating system supports one or more types of filesystems for storing and managing data in the form of files and directories. Linux supports different types of filesystems and managing them can be a cumbersome task, if not done correctly. In this guide, we'll learn about different filesystems of Linux and how you can create and manage them—like a pro. I'll include practical step-by-step examples to help you master filesystem creation in Linux. Depending on the Linux distribution, the filesystem management commands may differ. So, let's get started and learn how to create filesystems in a few easy steps.
Although the filesystem commands given below are fairly safe, I'll still recommend backing up your important data before diving into this tutorial. A secondary hard drive is the best option to learn it.
While you are learning filesystem creation, do not allow large space for it. It's just the learning phase, so go for a smaller-size filesystem. Finally, it's time to go through the tutorial. Let's dive in!
Understanding Linux Filesystems
The first step to mastering filesystems in Linux is to learn about the most common ones used by the majority of users across the globe. These filesystems are also used by web servers and similar Linux-powered devices. Let's go through them one by one.
1. ext4 (Fourth Extended Filesystem)
The following are the primary features of this filesystem.
- Chances are very high that this will be the default filesystem on your Linux distribution. It's the most popular one.
- It can support files up to 16 TB in size.
- The overall performance and reliability score of this filesystem is extremely good.
2. XFS Filesystem
Business computers must use this filesystem. Why? Here are the reasons.
- It's one of the most efficient and high-performance filesystems supported by Linux.
- It's best suited for extremely large files and applications performing parallel I/O operations.
- Due to its high-performance rating, it's often used in large business computers requiring high throughput.
3. Btrfs (B-tree Filesystem)
And here's another one you may consider for special use cases.
- It's a comparatively modern filesystem and incorporates some advanced features missing in older filesystems.
- It natively supports RAID mirroring, file compression, and snapshots which makes it an ideal candidate for datacenter Linux computers.
- Although it has several advanced features, its usage in mission-critical systems is very low as experts believe that it's still in the experimental stage.
4. ZFS Filesystem
This is yet another powerful and popular filesystem. Here are its features.
- This filesystem is best known for its data integrity features. That's why it is often used on servers running database management systems.
- It includes an advanced storage management system that enables you to handle large datasets with single or multiple computers in an easy manner.
- If you ever decide to use it, you must remember that it requires additional kernel modules for its functioning.
Now that we know about all the major filesystems supported by Linux, let's move on to the next step.
Preparing for Filesystem Creation
Filesystem creation requires some tools and applications. Let's first see what are the basic requirements for creating a filesystem on Linux.
- First of all, you'll need a block device. In Linux, a block device denotes a hard drive, an SSD, or a physical partition on a disk.
- Next, you need to have enough privileges on your Linux system to fire filesystem creation commands. Generally, it means the root privilege.
- Lastly, all the filesystem creation utilities should be installed beforehand.
Prerequisites and Installation
Before creating a filesystem on your Linux system, install the following tools:
# On a Ubuntu/Debian system
sudo apt-get install e2fsprogs xfsprogs btrfs-progs
# On a CentOS/RHEL system
sudo yum install e2fsprogs xfsprogs btrfs-progs
Now that we have all the necessary tools, let's move on to the actual process of filesystem creation.
Practical Filesystem Creation Examples
Now we'll cover all the filesystems mentioned in the first section. Through practical examples, you can easily try them out on your Linux system. Let's start it.
1. Creating an ext4 Filesystem
It is the most widely used filesystem in the Linux world. So, here's how to make an ext4 filesystem.
# Firstly, list all the available block devices
lsblk
# Let's assume /dev/sdb is an unpartitioned drive
# Create a partition on this drive
sudo fdisk /dev/sdb
# You can use the interactive mode for guided creation
# Next, format the partition with the ext4 filesystem
sudo mkfs.ext4 /dev/sdb1
# Now, create a mount point for the drive
sudo mkdir /mnt/demoext4drive
# Finally, mount the filesystem
sudo mount /dev/sdb1 /mnt/demoext4drive
# Make this mount point persistent by adding its entry to /etc/fstab
echo '/dev/sdb1 /mnt/demoext4drive ext4 defaults 0 2' | sudo tee -a /etc/fstab
As you can see, it's a multi-step process. Initially, you may struggle to create a partition. But, once you get familiar with it, everything goes smoothly.
2. Creating an XFS Filesystem
If your use case is building a high-performance Linux system which had to handle large-size files, go for this filesystem. Here's the creation process.
Here we presume that the block device is listed as /dev/sdc in your Linux system and you have created a partition /dev/sdc1 on this block device.
# Create an XFS filesystem
sudo mkfs.xfs /dev/sdc1
# Create a mount point
sudo mkdir /mnt/demoxfsdrive
# Mount the filesystem
sudo mount /dev/sdc1 /mnt/demoxfsdrive
# Add to /etc/fstab
echo '/dev/sdc1 /mnt/demoxfsdrive xfs defaults 0 2' | sudo tee -a /etc/fstab
As you can see the filesystem creation process is almost identical to the previous one. Give it a try.
3. Creating a Btrfs Filesystem
If you want to create a storage system to back up your important data, go for this filesystem as it supports several advanced features like snapshots. It makes data recovery, dead simple. Here's how to create this powerful filesystem.
# Create Btrfs filesystem
sudo mkfs.btrfs /dev/sdd1
# Create a mount point
sudo mkdir /mnt/demobtrfsdrive
# Mount the filesystem
sudo mount /dev/sdd1 /mnt/demobtrfsdrive
# Add an entry to /etc/fstab
echo '/dev/sdd1 /mnt/demobtrfsdrive btrfs defaults 0 2' | sudo tee -a /etc/fstab
# Create a snapshot
sudo btrfs subvolume snapshot /mnt/demobtrfsdrive /mnt/demobtrfsdrive/snapshot_$(date +%Y%m%d)
Make changes to the block device path as per your system. If you are building a custom NAS for your home, I'll recommend using this filesystem.
Advanced Filesystem Management
Now that we know how to create these filesystems, let's learn about some of the chores related to the management of these filesystems. They are easy to learn and all you need is to know about certain commands. Let's check them out.
Checking Filesystem Health
To check a filesystem's integrity, you can use the following commands.
# For ext4
sudo e2fsck /dev/sdb1
# For XFS
sudo xfs_repair /dev/sdc1
# For Btrfs
sudo btrfs check /dev/sdd1
It can help in detecting if a filesystem is corrupt or not. One can also repair the filesystem using these commands.
Resizing Filesystems
Sometimes, we look to expand the size of our existing filesystem. Fortunately, all of the modern filesystems allow resizing. Here's how you can do it for the filesystems discussed above.
# Resize ext4 (after expanding partition)
sudo resize2fs /dev/sdb1
# Resize XFS
sudo xfs_growfs /mnt/demoxfsdrive
Make sure to expand the partition before firing these resizing commands. You can use the fdisk utility to expand the existing partition.
Best Practices
Lastly, we'll take a look at some of the best practices of Linux filesystem management. These best practices will ensure the filesystems remain in a healthy state and their management is smooth and easy.
- You must ensure that your critical data is backed up before you dive into the creation or modification of filesystems.
- Depending on the use case, select the appropriate filesystem.
- Always make it a habit to check filesystem health at regular intervals.
- Always check the filesystem's performance metrics and the advanced features supported by it before making a final decision.
- Do not hesitate to use the LVM (Logical Volume Management) system for easy and flexible storage management.
Conclusion
Creating and managing filesystems in Linux requires understanding different filesystem types, their strengths, and appropriate use cases. By mastering these techniques, you can optimize storage performance, reliability, and data management.