Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


RHCSA - Create & Configure File Systems: Create, Mount, Unmount & Use vfat, ext4 & xfs File Systems

File Systems Overview

A file system on Linux is a method for organizing and managing data stored on storage devices, providing a structured way to interact with files and directories. It abstracts the complexities of interacting directly with the physical storage medium, offering a hierarchical structure that organizes data and metadata. The file system manages permissions and access control, ensuring data security.

Linux supports various file system types, and each has its strengths and purposes, allowing users and applications to store, retrieve, and manage data efficiently and reliably. The below table details the three file systems that Red Hat have chosen for this objective:

File System Description
ext4 ext4 is a widely used and mature Linux file system. It supports journaling for data integrity and offers good performance. It's compatible with various Linux distros.
XFS XFS is designed for scalability and high-performance in large-scale environments. It's suitable for big files and provides crash recovery capabilities.
VFAT VFAT is a simple file system with broad compatibility across platforms, making it suitable for portable storage. However, it lacks advanced features and data journaling.

Lesson Setup

To actively participate in the exercises, make sure to attach an additional empty disk to your system. While the exercises assume that the additional disk is 1G in size, feel free to use whatever disk size is available to you.

Do not use a disk with data on as following the exercises will mean that data will be deleted.

Once the disk is attached, you should be able to see it by running the lsblk command, which in this example shows as the sdb device:

After rebooting, the disk may not retain the same device name, making it essential to always run lsblk to verify that you are working with the correct disk.

$ lsblk

NAME           MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda              8:0    0   20G  0 disk 
├─sda1           8:1    0  600M  0 part /boot/efi
├─sda2           8:2    0    1G  0 part /boot
└─sda3           8:3    0   17G  0 part 
  ├─os_vg-root 253:0    0   15G  0 lvm  /
  └─os_vg-swap 253:1    0    2G  0 lvm  [SWAP]
sdb              8:16   0    1G  0 disk

You will now need to create three partitions on the /dev/sdb device so that you can practice creating, mounting, using and unmounting file systems.

Create three 100MB partition on disks /dev/sdb:

sudo parted -s /dev/sdb mklabel gpt mkpart ext4 0% 100MB
sudo parted -s /dev/sdb mkpart xfs 100MB 200MB
sudo parted -s /dev/sdb mkpart vfat 200MB 300MB

Confirm the partitions exist:

lsblk /dev/sdb
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   1G  0 disk 
├─sdb1   8:17   0  94M  0 part 
├─sdb2   8:18   0  96M  0 part 
└─sdb3   8:19   0  95M  0 part  

Create ext4, xfs & vfat File Systems

To format a partition on top of a partition, logical volume, etc, you use the mkfs command. There are two ways to use this command, either with the -t filesystem option or the mkfs.filesystem method. In the exercises below you will use both.

Create ext4 File System

Create an ext4 filesystem on partition /dev/sdb1:

Create an ext4 filesystem:

sudo mkfs -t ext4 /dev/sdb1

Command output:

mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 96256 1k blocks and 24096 inodes
Filesystem UUID: aa7349ba-7718-45cf-aa88-474ceaf0290d
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

Confirm the filesystem type is set:

lsblk -o +FSTYPE /dev/sdb1

Command output shows FSTYPE column of ext4:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS FSTYPE
sdb1   8:17   0  94M  0 part             ext4

Create xfs File System

Create an xfs filesystem on partition /dev/sdb2:

Create an ext4 filesystem:

sudo mkfs.xfs /dev/sdb2

Command output:

meta-data=/dev/sdb2      isize=512    agcount=4, agsize=6144 blks
         =               sectsz=512   attr=2, projid32bit=1
         =               crc=1        finobt=1, sparse=1, rmapbt=0
         =               reflink=1    bigtime=1 inobtcount=1
data     =               bsize=4096   blocks=24576, imaxpct=25
         =               sunit=0      swidth=0 blks
naming   =version 2      bsize=4096   ascii-ci=0, ftype=1
log      =internal log   bsize=4096   blocks=1368, version=2
         =               sectsz=512   sunit=0 blks, lazy-count=1
realtime =none           extsz=4096   blocks=0, rtextents=0    

Confirm the filesystem type is set:

lsblk -o +FSTYPE /dev/sdb2

Command output shows FSTYPE column of xfs:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS FSTYPE
sdb2   8:18   0  96M  0 part             xfs

Create vfat File System

Create a vfat filesystem on partition /dev/sdb3:

Create an ext4 filesystem:

sudo mkfs -t vfat /dev/sdb3

Command output:

mkfs.fat 4.2 (2021-01-31)

Confirm the filesystem type is set:

lsblk -o +FSTYPE /dev/sdb3

Command output shows FSTYPE column of vfat:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS FSTYPE
sdb3   8:19   0  95M  0 part             vfat

Configuring ext4, xfs & vfat File Systems

In this section we will go through some commands to amend and view information for the ext4, xfs and vfat file systems you have created.

There are many configurable items when it comes to file systems. It is important to review the man page if a tasks requires you to perform configuration on a filesystem.

Configure ext4 File System

To configure the ext4 filesystem you can use the tune2fs command.

Working with ext4 file systems:

List the contents of the file system superblock, including the current values of the parameters that can be set via tune2fs:

sudo tune2fs -l /dev/sdb1

Command output:

tune2fs 1.46.5 (30-Dec-2021)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          aa7349ba-7718-45cf-aa88-474ceaf0290d
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize     metadata_csum
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              24096
Block count:              96256
Reserved block count:     4812
Overhead clusters:        11693
Free blocks:              84549
Free inodes:              24085
First block:              1
Block size:               1024
Fragment size:            1024
Group descriptor size:    64
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         2008
Inode blocks per group:   502
Flex block group size:    16
Filesystem created:       Fri Aug  4 08:24:41 2023
Last mount time:          n/a
Last write time:          Fri Aug  4 08:24:41 2023
Mount count:              0
Maximum mount count:      -1
Last checked:             Fri Aug  4 08:24:41 2023
Check interval:           0 (<none>)
Lifetime writes:          279 kB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:               256
Required extra isize:     32
Desired extra isize:      32
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      31ec034a-ce62-4c6d-b6bf-a74a7f9534d9
Journal backup:           inode blocks
Checksum type:            crc32c
Checksum:                 0x0ce28adb

Amend the file system label:

sudo tune2fs -L ext4_label /dev/sdb1

Confirm label ext4_label was set correctly:

lsblk -o +LABEL /dev/sdb1

Command output:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS LABEL
sdb1   8:17   0  94M  0 part             ext4_label

Configure xfs File System

To change parameters of the xfs filesystem you use the xfs_admin command, and to display xfs filesystem geometry information you use the xfs_info command.

Working with xfs file systems:

View filesystem geometry information:

sudo xfs_info /dev/sdb2

Command output:

meta-data=/dev/sdb2              isize=512    agcount=4, agsize=6144 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1
data     =                       bsize=4096   blocks=24576, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=1368, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Amend the file system label:

sudo xfs_admin -L xfs_label /dev/sdb2

Command output:

writing all SBs
new label = "xfs_label"

Confirm label xfs_label was set correctly:

sudo xfs_admin -l /dev/sdb2    

Command output:

label = "xfs_label"

Configure vfat File System

There are not many tools for configuring vfat file systems, however you can set the filesystem label using the fatlabel command.

Add label to vfat file system:

sudo fatlabel /dev/sdb3 FAT_LABEL

Display the label of the vfat filesystem:

sudo fatlabel /dev/sdb3

Command output:

FAT_LABEL

Filesystem Repairs

Performing file system checks is crucial for maintaining the integrity, stability, and performance of your system. Over time, file systems can develop inconsistencies due to factors like power disruptions, hardware failures, and software issues. These inconsistencies can lead to data corruption, reduced performance, and even system crashes. Regular file system checks help identify and rectify these problems, preventing data loss and ensuring that your files remain accessible and accurate.

ext4 Filesystem Repairs

To perform filesystem checks and repairs on ext4 file systems, you use the e2fsck command.

Repair an ext4 filesystem:

The -f option forces checking even if the file system seems clean, which it will be for our exercise. The -v option give verbose output which may be useful in a situation where there is errors.

sudo e2fsck -fv /dev/sdb1

Command output:

e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information

          11 inodes used (0.05%, out of 24096)
           0 non-contiguous files (0.0%)
           0 non-contiguous directories (0.0%)
             # of inodes with ind/dind/tind blocks: 0/0/0
             Extent depth histogram: 3
       11707 blocks used (12.16%, out of 96256)
           0 bad blocks
           0 large files

           0 regular files
           2 directories
           0 character device files
           0 block device files
           0 fifos
           0 links
           0 symbolic links (0 fast symbolic links)
           0 sockets
------------
           2 files

xfs Filesystem Repairs

To perform filesystem checks and repairs on xfs file systems, you use the xfs_repair command.

Repair an xfs filesystem:

sudo xfs_repair /dev/sdb2

Command output:

Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - found root inode chunk
Phase 3 - for each AG...
        - scan and clear agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
Phase 5 - rebuild AG headers and trees...
        - reset superblock...
Phase 6 - check inode connectivity...
        - resetting contents of realtime bitmap and summary inodes
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
done

vfat Filesystem Repairs

To perform filesystem checks and repairs on vfat file systems, you use the fsck command.

Repair a vfat filesystem:

The -V option give verbose output.

sudo fsck.fat -V /dev/sdb3

Command output:

fsck.fat 4.2 (2021-01-31)
Starting check/repair pass.
Starting verification pass.
/dev/sdb3: 1 files, 0/48531 clusters

Mounting ext4, xfs & vfat File Systems

To mount the file systems, you first need to create the mount points which is the location you want the devices to be accessible under. This will typically be under the /mnt directory but it can be anywhere you want really.

Create mount points for the ext4, xfs & vfat file systems:

sudo mkdir -pv /mnt/{ext4,xfs,vfat}

Command output:

mkdir: created directory '/mnt/ext4'
mkdir: created directory '/mnt/xfs'
mkdir: created directory '/mnt/vfat'

In the previous exercises we added filesystem labels. These labels will be used to mount the file systems. You will notice that we are not using the -t flag with the mount command in the exercise below, which is because the mount command is smart enough to recognize the underlying filesystem type of the device and mount accordingly.

Mount the ext4, xfs & vfat file systems:

sudo mount -L ext4_label /mnt/ext4
sudo mount -L xfs_label /mnt/xfs
sudo mount -L FAT_LABEL /mnt/vfat

Confirm the file systems are mounted. The -h flag gives human readable values and the -T flag shows the filesystem type:

df -hT /mnt/ext4 /mnt/xfs /mnt/vfat

Command output:

Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/sdb1      ext4   83M   14K   76M   1% /mnt/ext4
/dev/sdb2      xfs    91M  5.7M   85M   7% /mnt/xfs
/dev/sdb3      vfat   95M     0   95M   0% /mnt/vfat

Mounting at Boot Time

The exercise above shows how to temporarily mount the file systems. To have them mount at boot time, entries need adding to /etc/fstab. Review the following lesson for how to do this:

Unmounting ext4, xfs & vfat File Systems

Unmounting the file systems is straight forward. You just use the umount command for either the; filesystem label, device or mount point. All three methods will be used in the exercise below.

Unmounting ext4, xfs & vfat file systems:

Unmount the ext4 filesystem by label:

sudo umount -v /dev/disk/by-label/ext4_label

Command output:

umount: /mnt/ext4 (/dev/sdb1) unmounted

Unmount the xfs filesystem by device:

sudo umount -v /dev/sdb2

Command output:

umount: /mnt/xfs (/dev/sdb2) unmounted

Unmount the vfat filesystem by mount point:

sudo umount -v /mnt/vfat

Command output:

umount: /mnt/vfat unmounted

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.