A filesystem is an organization of data and metadata on the storage device. If you want to access any files in Unix like operating system, the filesystem has to be mounted where the file resides.
The well-know Linux filesystems are Ext, Ext2, Ext3, Ex4, BtrFS, ReiserFS, ZFS, XFS, JFS, and Swap.
Let's create a partition on Linux, create a filesystem, and learn how to mount that filesystem.
Step 1: Create a Partition
Before creating a file system, make sure you have enough unallocated disk space ( or free cylinders).
You can check disk space using
fdisk -l or
parted print free command:
$ sudo fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 1020 8193118+ 83 Linux /dev/sda2 1021 1147 1020127+ 82 Linux swap / Solaris
Here you can see 1305 cylinders are present on '/dev/sda' disk and used up to 1147 cylinders. Therefore, we can create a new partition.
You may use fdisk or parted command to create a new partition.
In the following example, I am using fdisk command to create a partition on the hard drive named '/dev/sda' (first hard disk).
$ sudo fdisk /dev/sda Press n Press p Press “Enter” for default starting cylinder” Enter 100MB+ Now Change the partition type to 83 and finally reboot the system.
Step 2: Set Disk Label on the partition
You can use e2label command to set or change disk labels. The e2label command must be run as root user.
The following command set disk datafiles on the disk partition named '/dev/sda3'.
$ sudo e2label /dev/sda3 datafiles
To view the disk label, run the following command:
$ sudo e2label datafiles
Step 3: Create a filesystem
In Linux, you can create filesystem using mkfs, mkfs.ext2, mkfs.ext3, mkfs.ext4, mke4fs or mkfs.xfs commands. On RHEL and CentOS system you may install an additional package called
e4fsprogs which can manage ext4 filesystem.
The following commands create an ext4 filesystem on the '/dev/sda3' disk partition:
$ sudo mkfs.ext4 /dev/sda3 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 26208 inodes, 104420 blocks 5221 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 13 block groups 8192 blocks per group, 8192 fragments per group 2016 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done
Step 3: Mounting a Filesystem
The most commonly used method for mounting the filesystem is either manually using mount command or by adding entries in /etc/fstab file, so that filesystem gets mounted during boot time.
$ sudo mount /dev/sda3 /data
In the above example, we have mounted '/dev/sda3' partition to '/data' directory.
You can verify by executing the following command:
$ sudo mount | grep -i sda3 /dev/sda3 on /data type ext4 (rw)
Also, you can unmount /dev/sda3 using umount command.
$ sudo umount /data
Whenever linux system reboots the '/data' filesystem gets unusable. If you want to use the filesystem again, you have to mount it manually.
To avoid this repeated mounting after Linux boot, we have to add entries in /etc/fstab file so it will be persistent over reboots.
Here we will brief about /etc/fstab configuration file. You should add an entry in fstab file as follows:
LABEL=datafiles /data ext4 defaults 1 2 or /dev/sda3 /data ext4 defaults 1 2
An example fstab file:
$ cat /etc/fstab #device name mountpoint Type of fs options dump fsck LABEL=/ / ext4 defaults 1 1 LABEL=SWAP-sda2 swap swap defaults 0 0 /dev/sda3 /data ext4 defaults 0 0
device name: Name of the device/partition or source path (What to mount) /dev/sda3
mount point: Where data is attached to the filesystem (Where to mount) /data
type of the FS: Type of the filesystem are ext2, ext3, ext4, nfs, proc, etc.
options: In this option, you can apply a security policy to the particular file system. For example, when you mount, you can either set no execution of the binaries or you can set read-only filesystem. By default, the filesystem is having rw, suid, rw, exec, auto, nouser and async.
dump: This is used for filesystem backup. If value zero is set, backup is ignored. If 1 is set, the filesystem is backed up.
fsck: This option is to determine on which order the filesystems should be checked.
Display Mount Information
You can run
df -h or
lsblk command to get mounted device information such as mount point, filesystem size, etc.
$ df -h
The findmnt is a very handy tool to list all mounted filesystem, run the command as below:
In this tutorial, we learn how to create a filesystem in Linux by first creating a partition, formatting, and finally mounted. If you have any questions or feedback, feel free to leave a comment.