28.8 C
Singapore
Saturday, November 30, 2024
HomeTechnologyComputersHow to reduce/shrink Linux XFS volumes in LVM

How to reduce/shrink Linux XFS volumes in LVM

XFS is the default file system on CentOS 7 and RHEL 7 as a very viable alternative to ext3 and ext4, particularly with awesome new freeze and snapshot features of xfs (xfs_freeze, snapshot, xfs_unfreeze, delayed allocation, etc). However, unlike conventional ext containers, a major limitation of XFS is inability to shrink or reduce this file system container. Instead of recreating your volume partitions from scratch, a workaround is to unmount your desired partition to be shrunk, recreate a new smaller and xfsdump all your existing data into your new smaller partition.

Here is an example on how you go about doing it to resize my /home partition in the vg00 Logical Volume.

  1. Backup all your data if you’ve not done so.
  2. Install dependences if you have not: yum install xfsdump
  3. Note the following:
    • Your Volume Group using the “pvs” command to display your device “/dev/sdb1” and its associated Volume group, in this example it’s “vg00”.
    • Your file system size which you will use to compare with the new partition to ensure all your files are transferred, do a “df -h /home/” and the the size of your partition to shrink.
    • Your UUID doing cat /etc/fstab and note your /home partition UUID. You can similarly list your partitions and their respective UUID using sudo blkid. Make a note of the UUID of your partition to shrink (/home). This is to ensure your shrunk partition is mounted at boot.
  4. Backup the data using xfsdump (backing up /home to dump at the -f parameter)
    xfsdump -f /tmp/home.dump /home

    Do ensure that you have enough space in /tmp to hold the entire partition you are dumping, or substitute “/tmp” to a partition of your liking with enough space and remember to delete it thereafter when done.

  5. Unmount your old home partition:
    cd /
    umount /home
  6. Delete your old /home partition:
    lvremove /dev/vg00/home
  7. Recreate your new /home volume with a smaller size, say 5GB:
    lvcreate –name home -L 5GB vg00
  8. Format your newly-created XFS /home partition
    mkfs.xfs /dev/vg00/home
  9. Mount your new /home partition:
    mount /dev/vg00/home /home
  10. Restore the data using xfsrestore, substituting “/tmp” otherwise if you changed it in Step 3:
    xfsrestore -f /tmp/home.dump /home
  11. Get your new UUID for fstab.
    sudo blkid
  12. Update your /etc/fstab with the UUID to ensure that your new shrunk /home partition is auto-mounted on boot. And verify your /hom parition size using “df -h /home/” again.

And there you have it, all your /home partition files in a smaller XFS container. A thing to learn from this is to adopt a best practice to create your XFS LVMs using the smallest possible volume sizes during the point of OS install, where you can easily expand them quickly as needed using xfs_growfs using your utilized disk space.

Repeat steps 4-10 for every XFS volume you wish to resize.

Hope this quick guide helps and happy Linux-ing!

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles