Tuesday, December 1, 2020

Extending / in Default CentOS Partitioning Scheme

The default partitioning scheme for CentOS is not ideal, because the installer only give 50Gb to /. and the rest to /home. Luckily, the partitions are being setup in LVM, thus enable us to change it without reinstalling the operating system. 

In this example, I will show how to remove logical volume used by /home, and assign it to / instead.

First of all, you need to be root in order to do this.


This is the original partition layout:

# df -Th

Filesystem          Type      Size  Used Avail Use% Mounted on

...

/dev/mapper/cl-root xfs        50G  4.1G   46G   9% /

/dev/mapper/cl-home xfs       1.9T   14G  1.9T   1% /home

/dev/vda1           ext4      976M  194M  716M  22% /boot

...


As you can see, we are not going to use 1.9T in /home, so we are going to assign that to / instead.

Second step, make a backup of /home
# cp -apv /home /home2

Next, unmount /home
# umount -fl /home

Remove the logical volume
# lvremove /dev/cl/home

If you get an error saying that the logical volume is in used, you need to kill off the process first.

Get the major, minor number of the logical volume.
# dmsetup -c info cl-home
Name             Maj Min Stat Open Targ Event  UUID                                                                
cl-home          253   2 L--w    1    1      0 LVM-urbQSwHWhdpFDeVDQA3yFkOjVMkWFGloYGbgzAGW8YOuwacqVZLvVx23qNVJTFHc

Search for the process and process id using the major,minor number
# lsof | grep "253,2"
java       179094                           user  cwd       DIR              253,2        95 1073741952 /home
...

kill off the process
# kill 179094

Try to remove the logical volume again
# lvremove /dev/cl/home

Once deleted, extend the logical volume of /dev/cl/root, to use 100% of free space available in the volume group
# lvextend -l +100%FREE cl-root

Check the filesystem type
# df -Th /
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs        50G  4.6G   46G  10% /

Extend the filesystem, xfs_growfs is xfs filesystem grow tool. For ext4, you need to use resize2fs command
# xfs_growfs /

Check that your / partition is showing the new size
# df -Th /
Filesystem          Type  Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs   2.0T   19G  2.0T   1% /

Copy back the content of /home2 to /home
# mv /home2/* /home

IMPORTANT: Remove the old /home mount entry in /etc/fstab
# sed -i.ori '/home/d' /etc/fstab

The last step is very important, it can cause your operating system to fail to boot if /etc/fstab is not configured properly.

Enjoy your newly gained disk space in / partition.

No comments:

Post a Comment