42

CentOS 7 file system is XFS, And resize2fs doesn't work. I need to shrink /home to 400G and add 100G space to /. What should I do?

# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G   50G  341M 100% /
devtmpfs                 7.8G     0  7.8G   0% /dev
tmpfs                    7.8G   84K  7.8G   1% /dev/shm
tmpfs                    7.8G  778M  7.0G  10% /run
tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/sda1                497M  241M  257M  49% /boot
tmpfs                    1.6G   16K  1.6G   1% /run/user/42
tmpfs                    1.6G     0  1.6G   0% /run/user/0
/dev/mapper/centos-home  500G   20G  480G   4% /home

The output of lvs, vgs and pvs are:

[root@localhost]~# lvs -v
    Using logical volume(s) on command line.
  LV   VG     #Seg Attr       LSize   Maj Min KMaj KMin Pool Origin Data%  Meta%  Move Cpy%Sync Log Convert LV UUID                                LProfile
  home centos    1 -wi-ao---- 499.38g  -1  -1  253    2                                                     4I53D9-7VSm-HN9H-QsSp-FvFU-5R9D-y5VwsN         
  root centos    1 -wi-ao----  50.00g  -1  -1  253    0                                                     LGRoEL-0EHz-G135-p6vx-Lt2s-RvI5-qdT9Sm         
  swap centos    1 -wi-ao----   7.81g  -1  -1  253    1                                                     UYB5xP-cEyV-lWvn-blIq-8s13-9kVB-ykjIWI         
[root@localhost]~# vgs -v
    Using volume group(s) on command line.
  VG     Attr   Ext   #PV #LV #SN VSize   VFree  VG UUID                                VProfile
  centos wz--n- 4.00m   1   3   0 557.26g 64.00m Gd5c08-ujdQ-fsix-o7z6-Wfsv-C0uW-XzDois         
[root@localhost]~# pvs -v
    Using physical volume(s) on command line.
    Found same device /dev/sda2 with same pvid TCmreQr93apETNoTl8bMc54l57FZ5hut
  PV         VG     Fmt  Attr PSize   PFree  DevSize PV UUID                               
  /dev/sda2  centos lvm2 a--  557.26g 64.00m 557.26g TCmreQ-r93a-pETN-oTl8-bMc5-4l57-FZ5hut
[root@localhost]~# 
GoingMyWay
  • 545
  • 2
  • 6
  • 10
  • I found this: http://blog.endpoint.com/2015/01/shrink-xfs-partition-almost-possible.html which says it's not possible but goes on with some kind of answer. – gm3dmo Apr 21 '16 at 10:43

5 Answers5

71

As others have pointed out, XFS filesystem cannot be shrunk.

So your best bet is to backup /home, remove and recreate its volume in a smaller size and give the rest to your /root volume just as Koen van der Rijt outlined in his post.

• backup the contents of /home

tar -czvf /root/home.tgz -C /home .

• test the backup

tar -tvf /root/home.tgz

• unmount home

umount /dev/mapper/centos-home

• remove the home logical volume

lvremove /dev/mapper/centos-home

• recreate a new 400GB logical volume for /home, format and mount it

lvcreate -L 400GB -n home centos
mkfs.xfs /dev/centos/home
mount /dev/mapper/centos-home

• extend your /root volume with ALL of the remaining space and resize (-r) the file system while doing so

lvextend -r -l +100%FREE /dev/mapper/centos-root

• restore your backup

tar -xzvf /root/home.tgz -C /home

• check /etc/fstab for any mapping of /home volume. IF it is using UUID you should update the UUID portion. (Since we created a new volume, UUID has changed)

That's it.

Hope this helps.

Kindly add this to sync the changes:

dracut --regenerate-all --force

Ari
  • 826
  • 7
  • 4
  • 2
    when umounting, there might be the target is busy error, umount -l /dev/mapper/centos-home might be help. and there might be another error reads: "Logical volume centos/home contains a filesystem in use", fuser -kuc /dev/vg_data/backup might be help. – buxizhizhoum Nov 14 '18 at 07:19
  • Following @buxizhizhoum's advice, command `yum install psmisc` was required in order for `fuser` to be available. – mythofechelon Nov 23 '18 at 11:06
  • It really worked. Thank you! I was logged in with ssh and I needed to terminate ssh connection to remove "lvremove". As `bart` user recommended I run `restorecon -R -v /home` after finishing all above steps. – Ikrom Nov 26 '18 at 09:54
  • Thank You. In my scenario the mentioned commands are helped me a lot it is just 10 min job. for the worst case i have taken a backup of VM then proceed with the above commands then I was able to extend root volume. – Harsha Vardhana Dec 30 '18 at 13:46
  • Thanks a lot. Really appreciated - saved my time! – Soumya Kanti Jul 09 '20 at 05:56
  • sorry for reviving thread but `fuser -kuc /dev/vg_data/backup` gives me `Specified filename /dev/vg_data/backup does not exist.` on CentOS 7.5 – Flying Thunder Jul 06 '21 at 10:44
  • after logging out of administrator user and then logging in as root. i was able to delete the logical volume without the "Logical volume centos/home contains a filesystem in use" error – roobee Aug 13 '22 at 00:46
6

An addition to Ari's answer as I was unable to ssh using public key authentication after following his instructions.

With selinux enabled you may get this message in /var/log/messages (with debug enabled in sshd_config) because /home was recreated:

SELinux is preventing /usr/sbin/sshd from read access on the file authorized_keys

Fix it by:

restorecon -R -v /home
Bart
  • 61
  • 1
  • 1
4

You can't shrink an XFS filesystem.
You can only grow them with xfs_growfs.

See: https://access.redhat.com/solutions/540013
and
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/xfsgrow.html

It is currently not possible to reduce the size of a partition or logical volume with the xfs filesystem. As a possible workaround in some environments, thin provisioned LVM volumes can be considered as an additional layer below the XFS filesystem.


Find out what's taking so much space under / and split it out as its own filesystem.

ewwhite
  • 194,921
  • 91
  • 434
  • 799
0

you can only do this on a live system if it is partitioned with LVM - logical volume management. what is the output of:

lvs -v
vgs -v
pvs -v
Sum1sAdmin
  • 1,914
  • 1
  • 11
  • 20
0

The first thing you need to do is check what has used up all your free disk space otherwise this problem will occur again sometime in the future.

If u really need to resize my approach would be:

  • backup the contents of /home. (its only 20g)
  • umount /home
  • destroy the home logical volume (lvremove)
  • allocate space as needed, resize the volumes. (lvextend, resize2fs)
  • recreate a new logical volume for /home, format and mount it (lvcreate, mkfs.xfs)
  • restore backup
  • double check the contents of /etc/fstab that you will mount the correct volume for /home
  • run mount -a or reboot