How to Defragment an XFS Filesystem in Linux(5 Simple and Effective Steps)

In this article, I will take you through the steps to defragment an xfs filesystem in Linux. Before we go the defragmentation of an xfs filesystem it is very important to understand why we need to defragment a filesystem. You might have Known or heard about the problem of fragmentation in a filesystem which is usually caused by the random memory block allocation with unallocated space between them.

In a filesystem different programs request different sizes of memory at different times and releases in random order when done execution. This leads to the creation of random chunks of unallocated spaces all over the memory which affects the performance of the System since data is not allocated continuously. This is more of a issue in magnetic disk rather than SSD so to solve this problem we need to perform defragmentation.

What is Defragmentation

Defragmentation is a process to remove the unallocated memory space between two allocated chunks of memory. In this process unordered memory allocation are moved to form a continuous memory allocation which will bring the allocation in order and remove any randomly available unallocated space. This improves the system performance and bring uniformity in allocation.

How to Defragment an XFS Filesystem in Linux(Simple and Effective Steps)

How to Defragment an XFS Filesystem in Linux

Also Read: How to Install Arpwatch tool on RHEL/CentOS 7/8{Simple and Effective Steps}

Step 1: Prerequisites

a) You should have a running Linux System.

b) You should have xfs_db and xfs_fsr tool installed in the System. Please check xfs_db Man Page and xfs_fsr Man Page for more info.

c) You should have root or sudo access to run privileged commands. Please Check How to Add User to Sudoers to know more about providing sudo access to the User.

Advertisements

Step 2: Create an XFS Partition

We have attached a single 8GB Disk to create an xfs partition for our testing purpose. As you can see from below output sdb is the disk we are going to use here.

[root@localhost ~]# lsblk
NAME          MAJ:MIN  RM SIZE   RO TYPE MOUNTPOINT
sda             8:0     0  40G    0 disk
├─sda1          8:1     0  1G     0 part   /boot
└─sda2          8:2     0  39G    0 part
├─centos-root  253:0    0  37G    0 lvm     /
└─centos-swap  253:1    0   2G    0 lvm    [SWAP]
sdb             8:16    0   8G    0 disk
sdc             8:32    0   8G    0 disk
└─sdc1          8:33    0   8G    0 part
sr0             11:0    1   1024M 0 rom

To create a partition on disk /dev/sdb, run fdisk /dev/sdb command and write n to create a new partition and press Enter. Then write p to make it a primary partition and then press Enter. Since there are no previous partition on this disk so by default first partition will get selected by just pressing Enter. To create a partition on entire 8GB disk, use the default value for first and last sector. Finally write w to save the changes and exit from the fdisk utility.

[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-16777215, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215):
Using default value 16777215
Partition 1 of type Linux and of size 8 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

List the partition created on the disk /dev/sdb using fdisk -l /dev/sdb command.

[root@localhost ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5d09da13

Device Boot Start      End   Blocks  Id System
/dev/sdb1   2048   16777215  8387584 83 Linux

Create an xfs filesystem forcibly(-f) on partition /dev/sdb1 using mkfs.xfs -f /dev/sdb1 command as shown below.

[root@localhost ~]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=524224 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2096896, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

Step 3: Mount the Filesystem

Next step is to mount the filesystem by using mount /dev/sdb1 /u01 command as shown below. You can also specify the filesystem if you like using -t option but it will get mounted without that as well.

[root@localhost ~]# mount /dev/sdb1 /u01

Step 4: Check if an XFS partition requires defragmentation

Before we go and check the XFS Partition, let’s create a big file file.txt with some data under /u01 mount point using seq 10000000 >> file.txt command. This command will write the sequence from 1 to 10000000 in file.txt. You can check and confirm the contents of the file using cat file.txt command.

[root@localhost ~]# cd /u01/
[root@localhost u01]# seq 10000000 >> file.txt
[root@localhost u01]# du -sh file.txt
128M file.txt

Now check if the partition requires defragmentation using xfs_db -c frag -r /dev/sdb1 command as shown below. Here we are running frag command in read only mode for /dev/sdb1 disk.

[root@localhost /]# xfs_db -c frag -r /dev/sdb1
actual 3, ideal 1, fragmentation factor 66.67%
Note, this number is largely meaningless.
Files on this filesystem average 3.00 extents per file

-c : xfs_db commands may be run interactively (the default) or as arguments on the command line.

-r : Open device or filename read-only.

Step 5: Defragment an XFS Filesystem

To run the defragmentation you need to use xfs_fsr -v /dev/sdb1 command as shown below. Time to defragment the disk will be based on the total size of the disk. Larger size will take longer time to defragment.

[root@localhost /]# xfs_fsr -v /dev/sdb1

-v : verbose.
 

 

Popular Recommendations:-

Python3: ModuleNotFoundError: No Module Named “prettytable” in Linux 

How to List all the Installed Python Modules in Linux{2 Easy Methods}

Solved: ModuleNotFoundError: No Module Named “requests” in Python 3

How to Install and Enable EPEL Repository on RHEL/CentOS 7/8{Simple and Easy Steps}

Solved: FATAL: Authentication Helper Program /usr/lib/squid/basic_ncsa_auth: (2) No Such File or Directory

How to Install and Configure Squid Proxy Server on RHEL/CentOS 7/8

Primitive Data Types in Java – int, char, byte, short, long, float, double and boolean

Advertisements

Leave a Reply