Logo

Armand.nz

Home / About / Linkedin / Github

Shrink home partition and add more space on CentOS

#linux #centOS #rhel |

**For example, Shrink /home partition and add more space on CentOS

Before state:

╭─root@centos ~  
╰─# df -h  
Filesystem Size Used Avail Use% Mounted on  
/dev/mapper/centos-root 50G 2.1G 48G 5% /  
devtmpfs 233M 0 233M 0% /dev  
tmpfs 244M 0 244M 0% /dev/shm  
tmpfs 244M 17M 227M 7% /run  
tmpfs 244M 0 244M 0% /sys/fs/cgroup  
/dev/sda1 1014M 229M 786M 23% /boot  
/dev/mapper/centos-home 45G 54M 45G 1% /home  
tmpfs 49M 0 49M 0% /run/user/0  
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: bash  
# 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 10GB logical volume for /home, format and mount it  
lvcreate -L 10GB -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):

In my case UUID was not used to map the /home volume:

After state:

#
# /etc/fstab
# Created by anaconda on Mon Sep 24 10:56:08 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=fd76016e-9e81-4b8f-839a-65d37acd6b20 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
comments powered byDisqus

Copyright © Armand