Posted on
Filesystem

How to Create and Mount a New Filesystem

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

How to Create and Mount a New Filesystem in Linux Bash

When managing Linux systems, understanding how to create and manage filesystems is crucial. Filesystems are the methods and data structures that an operating system uses to control how data is stored and retrieved. Without a filesystem, it would be impossible to store data in an organized manner. In this guide, we will walk through the processes of creating a new filesystem and mounting it using the Linux command line, also known as Bash.

Step 1: Choose a Storage Device

Before creating a filesystem, you must have a storage device (like a hard drive or SSD) that is not already allocated. You can list all connected storage devices by using the lsblk command.

lsblk

This command will show you all the available block devices and their mount points. Identify the device that does not have a mount point, indicating that it’s free to use. For example, it might be listed as /dev/sdx.

Step 2: Create a Partition

Once you have identified a free device, you can create a partition on it using fdisk or parted. Here, we’ll use fdisk.

sudo fdisk /dev/sdx

Replace /dev/sdx with your device identifier. You'll enter a menu where you can create a new partition by following the on-screen prompts. These typically involve pressing n to create a new partition, selecting primary partition types, and specifying the size of the partition.

After creating the partition, use the w command to write the changes to the disk. You can verify the new partition by running lsblk again.

Step 3: Creating the Filesystem

Now that you have a partition, you can format it with a filesystem. There are many types of filesystems you can create, such as ext4, xfs, or btrfs. To format the partition with an ext4 filesystem, which is widely supported and robust, you can use the mkfs command:

sudo mkfs.ext4 /dev/sdx1

Make sure to replace /dev/sdx1 with your partition identifier. This command will format the new partition with the ext4 filesystem.

Step 4: Mounting the Filesystem

To use the filesystem, you need to mount it. First, create a mount point — a directory where the filesystem will be accessible:

sudo mkdir /mnt/mynewdrive

Now, mount the new filesystem to this directory:

sudo mount /dev/sdx1 /mnt/mynewdrive

Check that the mount was successful by listing the contents of the mount point or by using the df -h command, which shows disk space usage:

df -h

Step 5: Making the Mount Permanent

Mounted filesystems are reset when the system reboots. To make the new mount persistent across reboots, you need to add it to the /etc/fstab file. This file contains information about disk partitions and their mount points.

Open /etc/fstab in a text editor:

sudo nano /etc/fstab

Add a line following this format:

/dev/sdx1   /mnt/mynewdrive   ext4   defaults    0   2

Replace the partition and mount point with your configuration. Save and close the file. The new filesystem will now be automatically mounted on boot.

Conclusion

Congratulations! You've just walked through creating and mounting a new filesystem in Linux. These steps are crucial for managing storage on a Linux system, whether you're setting up a new server or organising data on a desktop. With this knowledge, you can effectively handle your storage needs and maintain your data in an efficient, organized manner. Happy computing!