Shrinking a XFS root Logical Volume partition

About XFS file system

  • Starting with RHEL 7 and consequently Oracle Linux 7, XFS has become the default file system.
  • According to XFS FAQ, it is not possible to reduce a XFS volume
  • The only way to shrink it is to do a complete dump, mkfs and restore.

Check Status of our XFS partitions

 Check current status of our XFS filesystems / and /swap 

[root@ractw21 ~]#  df -PhT
Filesystem                  Type      Size  Used Avail Use% Mounted on
devtmpfs                    devtmpfs  2.7G     0  2.7G   0% /dev
tmpfs                       tmpfs     2.8G   88K  2.8G   1% /dev/shm
tmpfs                       tmpfs     2.8G  8.8M  2.8G   1% /run
tmpfs                       tmpfs     2.8G     0  2.8G   0% /sys/fs/cgroup
/dev/mapper/ol_ractw21-root xfs        46G  8.8G   37G  20% /
/dev/sda1                   xfs      1014M  302M  713M  30% /boot
kits                        vboxsf    2.8T  2.1T  670G  77% /media/sf_kits
tmpfs                       tmpfs     557M  8.0K  557M   1% /run/user/0
/dev/sr0                    iso9660   4.4G  4.4G     0 100% /run/media/root/OL-7.3 Server.x86_64
/dev/sr1                    iso9660    57M   57M     0 100% /run/media/root/VBOXADDITIONS_5.1.18_114002
[root@ractw21 ~]# lvs --segments -o +devices
  LV   VG         Attr       #Str Type   SSize  Devices       
  root ol_ractw21 -wi-ao----    1 linear 45.12g /dev/sda2(992)
  swap ol_ractw21 -wi-ao----    1 linear  3.88g /dev/sda2(0)  

[root@ractw21 ~]# pvs
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sda2  ol_ractw21 lvm2 a--  49.00g 4.00m

Summary :
 - Our Volume Group ol_ractw21 has a size of 49 GB with only 4Mb free Space
 - We have 2 Logical volumes
      root with a Logical Volumen Size fo 45.15 GByte 
      swap  with a Logical Volumen Size 3.88 GByte
We expect an error when we would like to increate the swap space by 4 GByte

[root@ractw21 ~]#  lvextend -L+4G /dev/ol_ractw21/swap
  Insufficient free space: 1024 extents needed, but only 1 available

Preparing a Backup Disk by using VirtualBox Disk

Create a 20 GByte Disk wiht virtualbox 
M:\VM> VBoxManage createhd --filename M:\VM\RACTW2\tmp20G.vdi --size 20480 --format VDI --variant Fixed

Shutdown VM and attach disk
M:\VM> VBoxManage storageattach ractw21 --storagectl "SATA" --port 1 --device 0 --type hdd --medium M:\VM\RACTW2\tmp20G.vdi

Reboot the system in Rescue Mode by booting Installation Media
   Select Rescue a Oracle Linux system.
   Select option 3 - Skip to shell.

Check for newly created disk Device /dev/sdb 
[root@ractw21 ~]# ls -l  /dev/sd*
brw-rw----. 1 root disk 8,  0 Apr 12 11:11 /dev/sda
brw-rw----. 1 root disk 8,  1 Apr 12 11:11 /dev/sda1
brw-rw----. 1 root disk 8,  2 Apr 12 11:11 /dev/sda2
brw-rw----. 1 root disk 8, 16 Apr 12 11:11 /dev/sdb

Using parted to create a new partition table 

Mount the new XFS partition
[root@ractw21 ~]# mkdir /backup
[root@ractw21 ~]# mount /dev/sdb1 /backup
[root@ractw21 ~]# df /dev/sdb1      
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sdb1       20509264 45080  19399328   1% /backup
# mkdir /mnt/root
# mount  /dev/ol_ractw21/root  /mnt/root

Backup root partition
# xfsdump -f /backup/root.dmp /mnt/root
please enter label for this dump session (timeout in 300 sec)
-> root
please enter label for media in drive 0 (timeout in 300 sec)
-> root
xfsdump: media file size 1113760064 bytes
xfsdump: dump complete: 74 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /backup/root.dmp OK (success)
xfsdump: Dump Status: SUCCESS

# umount /mnt/root 
# umount /backup

If umount fails  you may reboot the system and select option 3 - exit to Shell

Drop the old root Logical Volume and restore the root content

# vgchange -ay
# lvremove /dev/ol_ractw21/root
Logical volume "root" successfully removed
Check Free Space 
# pvs  
PV         VG          Fmt     Attr PSize    PFree
/dev/sda2  ol_ractw21  lvm2    a--   49.00g  45.12g

Recreate Logical Volume root and verify status
# lvcreate -L 15G -n root ol_ractw21
WARNING: xfs signature detected on /dev/ol_ractw21/root at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/ol_ractw21/root.
Logical volume "root" created.
# pvs
PV         VG         Fmt  Attr PSize  PFree
/dev/sda2  ol_ractw21 lvm2 a--  49.00g 30.12g

# vgchange -a y
2 logical volume(s) in volume group "ol_ractw21" now active
# lvscan
ACTIVE            '/dev/ol_ractw21/swap' [3.88 GiB] inherit
ACTIVE            '/dev/ol_ractw21/root' [15.00 GiB] inherit

Create XFS file system for newly created root partition
# mkfs.xfs -L root /dev/ol_ractw21/root

# mkdir /mnt/root
# mount  /dev/ol_ractw21/root  /mnt/root
# mkdir /backup
# mount /dev/sdb1  /backup

Restore backup
# xfsrestore -f /backup/home.dmp /mnt/root
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /mnt/root/root.dmp OK (success)
xfsrestore: Restore Status: SUCCESS

Make sure that there is no mismatch between the UUID of the actual device and the UUID
used in the file system table (fstab) of the restored LVM root volume.
If necessary, edit the file system table accordingly.
Normally this is should not be necessary, because LVM root and home volumes are
usually not mounted using their UUID value.

# lsblk -o name,size,mountpoint,uuid /dev/sda
sda          64G
|-sda1        1G            433b636e-9b4a-4ea1-8960-6c2288433ac5

# cat /mnt/root/etc/fstab
/dev/mapper/ol-root                       /      xfs     defaults        0 0
UUID=433b636e-9b4a-4ea1-8960-6c2288433ac5 /boot  xfs     defaults        0 0

# umount /mnt/root
# umount /backup

Reboot the  System Verify new storage status
[root@ractw21 ~]# pvs
PV         VG         Fmt  Attr PSize  PFree
/dev/sda2  ol_ractw21 lvm2 a--  49.00g 30.12g