How to reduce the root directory of XFS partition format in Linux

How to reduce the root directory of XFS partition format in Linux

Preface

This article describes how to shrink the​ / ​​​​directory of an XFS partition. If you need to shrink the XFS file system on a logical volume, you need to back up, restore, and recreate the logical volume to achieve it.

System environment

CentOS 8

Current system partition layout

This is the current file system layout. The current mount point / capacity is 17G. Our aim is to reduce / capacity.

How to reduce the root directory of an XFS partition?

Backing up your data

Be sure to back up important data before shrinking the partition. If something goes wrong, you can restore the data.

Note: You need to add an extra hard disk to the server to back up the system partition.

Enter rescue mode

In VMware Workstation, load the CD and start the machine. Press F2 when booting, enter BIOS, and switch to the Boot tab. Move the CD-ROM Drive to the Hard Drive. Press F10 to save and reboot.

How to reduce the root directory of XFS partition format_Logical Volume_02

Select Troubleshooting, then choose Enter Rescue Mode.

How to reduce the root directory of XFS partition format_Logical Volume_03

How to reduce the root directory of XFS partition format_Logical Volume_04

When you enter the following interface, select 3 to enter the shell interface directly.

How to reduce the root directory of XFS partition format_html_05

After entering the rescue mode, you can see that the /mnt/sysimage directory does not have a mounted system partition.

# ls -al /mnt/sysimage/ 

How to reduce the root directory of XFS partition format_html_06

Activate Logical Volume Group

Before mounting the partition, you need to activate the logical volume. To activate the volume group:

# vgchange -ay 2 logical volume(s) in volume group "cl" now active # vgs VG #PV #LV #SN Attr VSize VFree cl 1 2 0 wz--n- <19.00g 0 

How to reduce the root directory of XFS partition format_Logical Volume_07

Mount the system partition and the partition used to backup data

First starting with the backup, we need to mount the logical volume somewhere. By temporarily mounting it we can back up ​/​ partition to any other location and need to access it when performing a restore.

Now, create two directories under /mnt/sysimage/. One to mount the actual ​/​ partition of this server, and the other will be used to store backups of ​/​ partition.

# mkdir /mnt/sysimage/root # mkdir /mnt/sysimage/backup # mount /dev/cl/root /mnt/sysimage/root # mount /dev/sda /mnt/sysimage/backup/ 

How to reduce the root directory of XFS partition format_Logical Volume_08

Backing up the / partition using xfsdump

Perform the backup by running ​xfsdump​ .

# xfsdump -l 0 -L "root lv backup" -M "backup" -f /mnt/sysimage/backup/root_backup.img /mnt/sysimage/root 

How to reduce the root directory of XFS partition format

When dump uses level 0 (backing up all files), you will be prompted for a backup session label and a media label. We can avoid this prompt by adding -L and -M to the following -f :

​-l 0​​​ Set the backup level to 0. Level 0 means all files will be backed up.

​-L​ Specifies the label for the backup session

​-M​ specifies the label of the first medium

​-f​ specifies the backup destination file.

Unmounting the file system

Once the backup is complete, we can unmount the file system.

# umount /mnt/sysimage/root/

Reduction/Partitioning

Use the ​lvremove​ command to remove logical volumes and use ​lvcreate​ to create new volumes.

# lvremove /dev/cl/root Do you really want to remove active logical volume cl/root? [y/n]: y Logical volume "root" successfully removed 

How to reduce the root directory of XFS partition format_Logical Volume_10

After that, create the required logical volume size. Below we create a logical volume of size 15G.

# lvcreate -Zy -L 15G -n root cl 

How to reduce the root directory of XFS partition format

Use -Zy to zero out the first 4KiB of data in the new logical volume.

Create a New XFS Partition

# mkfs.xfs /dev/cl/root 

How to reduce the root directory of XFS partition format_File system_12

Mount and restore data

Once the file system is ready, mount it in the /mnt/sysimage/root directory:

# mount /dev/cl/root /mnt/sysimage/root/

Use the ​xfsrestore​ command to restore the previously backed up data:

# xfsrestore -f /mnt/sysimage/backup/root_backup.img /mnt/sysimage/root 

How to reduce the root directory of XFS partition format_logical volume_13

Restart the system and check whether it is normal. Disconnect the CD-ROM drive of the virtual machine, press F2 to start the computer, enter BIOS, set the boot order, put the hard disk in the first boot, press F10 to save and restart:

How to shrink the root directory of an XFS partition

After entering the system, check whether the partition size has been reduced:

[root@localhost ~]# df -hT / Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/cl-root xfs 15G 1.7G 14G 11% / 

How to reduce the root directory of an XFS partition

Summarize

That’s it, with the help of ​xfsdump​ and ​xfsrestore​ utilities, we have completed the work of shrinking XFS file systems and logical volumes.

This is the end of this article about reducing the root directory of the XFS partition format in Linux. For more relevant content about reducing the root directory of the XFS partition format in Linux, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Tutorial on adjusting the size of lvm logical volume partition in Linux (for different file systems such as xfs and ext4)
  • Using XFS file system in Linux environment

<<:  Let IE support CSS3 Media Query to achieve responsive web design

>>:  Detailed explanation of angular content projection

Blog    

Recommend

The difference between method=post/get in Form

Form provides two ways of data transmission - get ...

The latest popular script Autojs source code sharing

Today I will share with you a source code contain...

Eight common SQL usage examples in MySQL

Preface MySQL continued to maintain its strong gr...

How to install mysql on centos and set up remote access

1. Download the mysql repo source $ wget http://r...

Apache Spark 2.0 jobs take a long time to finish when they are finished

Phenomenon When using Apache Spark 2.x, you may e...

Nginx memory pool source code analysis

Table of contents Memory Pool Overview 1. nginx d...

How to recompile Nginx and add modules

When compiling and installing Nginx, some modules...

Several important MySQL variables

There are many MySQL variables, some of which are...

Detailed steps to install MySQL 8.0.27 in Linux 7.6 binary

Table of contents 1. Environmental Preparation 1....

9 Tips for MySQL Database Optimization

Table of contents 1. Choose the most appropriate ...

MySQL 5.7.25 installation and configuration method graphic tutorial

There are two types of MySQL installation files, ...

JS realizes simple picture carousel effect

This article shares the specific code of JS to ac...