Software >> OS >> Unix >> Linux >> RHEL >> LVM >> How to shrink or grow an xfs filesystem


## take note the logical volume details and confirm it is formatted with xfs

[root@rhel7sat1 ~]# lsblk
sda             8:0    0   200G  0 disk
├─sda1          8:1    0     1G  0 part /boot
└─sda2          8:2    0   199G  0 part
  ├─rhel-root 253:0    0    50G  0 lvm  /
  ├─rhel-swap 253:1    0   7.9G  0 lvm  [SWAP]
  └─rhel-home 253:2    0 141.1G  0 lvm  /home
sr0            11:0    1  57.7M  0 rom 

[root@rhel7sat1 ~]# grep \/home /etc/fstab
/dev/mapper/rhel-home   /home                   xfs     defaults        0 0

## We want to shrink /dev/rhel/home currently using 141g and mounted on /home

## (1). Take backup of the filesystem using xfsdump

[root@rhel7sat1 ~]# xfsdump -f /var/tmp/home-xfs.dump /home
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control

 ============================= dump label dialog ==============================

please enter label for this dump session (timeout in 300 sec)
 -> home
session label entered: "home"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of rhel7sat1:/home
xfsdump: dump date: Wed Jul 22 10:48:37 2020
xfsdump: session id: 768efc3e-737c-44d4-a71f-07523bd15e12
xfsdump: session label: "home"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 35328 bytes

 ============================= media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> home
media label entered: "home"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 27552 bytes
xfsdump: dump size (non-dir files) : 1632 bytes
xfsdump: dump complete: 3 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /var/tmp/home-xfs.dump OK (success)
xfsdump: Dump Status: SUCCESS

## (2) unmount

[root@rhel7sat1 ~]# umount /home

## (3) reduce the logical volume

[root@rhel7sat1 ~]# lvreduce -L 9G /dev/rhel/home
  WARNING: Reducing active logical volume to 9.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce rhel/home? [y/n]: y
  Size of logical volume rhel/home changed from 10.00 GiB (2560 extents) to 9.00 GiB (2304 extents).
  Logical volume rhel/home successfully resized

## (4) reformat the reduced LVM to xfs

[root@rhel7sat1 ~]# mkfs.xfs -f /dev/rhel/home
meta-data=/dev/rhel/home         isize=512    agcount=4, agsize=589824 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2359296, 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

## (5) re-mount

[root@rhel7sat1 ~]# mount /dev/rhel/home /home

[root@rhel7sat1 ~]# df -h /home
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-home  9.0G   33M  9.0G   1%

## (6) restore from dump

[root@rhel7sat1 ~]# xfsrestore -f /var/tmp/home-xfs.dmp /home
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: rhel7sat1
xfsrestore: mount point: /home
xfsrestore: volume: /dev/mapper/rhel-home
xfsrestore: session time: Wed Jul 22 11:13:15 2020
xfsrestore: level: 0
xfsrestore: session label: "home"
xfsrestore: media label: "home"
xfsrestore: file system id: 82825a22-5e1e-4ea6-bde9-38077a069767
xfsrestore: session id: 9ab9dda5-52a9-446b-9380-f60cafdd5c21
xfsrestore: media id: 797cce91-d6c8-4d03-94b0-edd03bda3b90
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 5 directories and 7 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /var/tmp/home-xfs.dmp OK (success)
xfsrestore: Restore Status: SUCCESS





## Scenario 1 : Add another partition
## Firstly follow his guide to extend the disk
## assuming the Volume group (vg) is rhel and the Logical volume is var

## Create the partition e.g. for GPT formatted disk, using gdisk, for MBR formatted disk, using fdisk

gdisk /dev/sda
Command (? for help): n
Partition number (4-128, default 4): Enter
First sector .... : Enter
Last sector ..... : Enter
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
Do you want to proceed? (Y/N): Y

## do a partprobe to rescan the disk and detect the new partition


## assuming the new partition was partition 4

pvcreate /dev/sda4
vgextend vgname /dev/sda4
lvextend -l +100%FREE /dev/rhel/var

## resize the filesystem, for XFS, use xfs_growfs

xfs_growfs /dev/rhel/var

## Scenario 2 : Add another disk e.g. /dev/sdb

pvcreate /dev/sdb
vgextend vgname /dev/sdb
lvextend -l +100%FREE /dev/rhel/var

## resize the filesystem, for XFS, use xfs_growfs

xfs_growfs /dev/rhel/var