Skip to content

Instantly share code, notes, and snippets.

@troyfontaine
Last active June 12, 2024 12:55
Show Gist options
  • Save troyfontaine/87091bd6a5c68f45dd62ced3d12bc377 to your computer and use it in GitHub Desktop.
Save troyfontaine/87091bd6a5c68f45dd62ced3d12bc377 to your computer and use it in GitHub Desktop.
Resize root partition (or how to remove the default /home partition) on CentOS 7 online

Resize root partition (or how to remove the default /home partition) on CentOS 7 online

This process requires that you are able to ssh OR log in locally using the root user account and that no services be running as users out of /home on the target machine.

The examples are from a default installation with no customization-you NEED to know what you're working with for volumes/partitions to not horribly break things.

By default, CentOS 7 uses XFS for the file system and Logical Volume Manager (LVM), creating 3 partitions: /,/home and swap.

NOTE: If you want to be sure that nothing is writing to /home you can either modify the host to boot into single-user mode OR try to use the systemctl isolate runlevel1.target command to switch (not tested! should work).

Step 1 - Copy /home Contents

To backup the contents of /home, do the following:

mkdir /temp
cp -a /home /temp/

Once that is finished at your back at the prompt, you can proceed to step 2.

Step 2 - Unmount the /home directory

umount -fl /home

Step 3 - Note the size of the home LVM volume

We run the lvs command to display the attributes of the LVM volumes

lvs

Sample output:

  LV   VG Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cl -wi-a----- 406.94g
  root cl -wi-ao----  50.00g
  swap cl -wi-ao----   7.81g

Step 4 - Remove the home LVM volume

lvremove /dev/cl/home

Step 5 - Resize the root LVM volume

Based on the output of lvs above, I can safely extend the root LVM by 406GiB.

lvextend -L+406G /dev/cl/root

Step 6 - Resize the root partition

xfs_growfs /dev/mapper/cl-root

Step 7 - Copy the /home contents back into the /home directory

cp -a /temp/home /

Step 8 - Remove the temporary location

rm -rf /temp

Step 9 - Remove the entry from /etc/fstab

Using your preferred text editor, ensure you open /etc/fstab and remove the line for /dev/mapper/cl-home.

Step 10 - Don't miss this!

Run the following command to sync systemd with the changes.

dracut --regenerate-all --force
@lusid1
Copy link

lusid1 commented Jun 14, 2023

worked fine until the next reboot, and that was the end of that.

@mylix
Copy link

mylix commented Jun 27, 2023

I had troubles with this procedure. After unmounting the /home then trying to remove the LVM the system said:

Logical volume centos/home contains a filesystem in use.

@lamnt213
Copy link

lamnt213 commented Jul 5, 2023

Confirmed. It's working for Centos 8 stream.

@pjpmccarthy52
Copy link

Worked on CentOS 9 Stream. Thanks!

@AlexGThess
Copy link

you are amazing!! ty <3 <3 <3

@jpswade
Copy link

jpswade commented Jan 1, 2024

I had to use resize2fs

@fajarra
Copy link

fajarra commented Mar 2, 2024

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- 872.69g
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- 7.81g
but when i lvremove /dev/centos/home
error happened
'On step Logical volume centos/home contains a filesystem in use.'
i use centos 7

@troyfontaine
Copy link
Author

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 872.69g root centos -wi-ao---- 50.00g swap centos -wi-ao---- 7.81g but when i lvremove /dev/centos/home error happened 'On step Logical volume centos/home contains a filesystem in use.' i use centos 7

Did you try re-running step 2? Did you receive any errors when unmounting it?

@fajarra
Copy link

fajarra commented Mar 10, 2024

Size of logical volume cl/root changed from 50.00 GiB (12800 extents) to 750.00 GiB (192000 extents).
Logical volume cl/root successfully resized.
On Stream Centos 8.2, another server

But i don't running step 9. Because there are still 160 GiB left behind.., it's safe ...?

@fajarra
Copy link

fajarra commented Mar 10, 2024

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home centos -wi-ao---- 872.69g root centos -wi-ao---- 50.00g swap centos -wi-ao---- 7.81g but when i lvremove /dev/centos/home error happened 'On step Logical volume centos/home contains a filesystem in use.' i use centos 7

Did you try re-running step 2? Did you receive any errors when unmounting it?

No there are no error message when unmounting

@gte0002
Copy link

gte0002 commented Mar 11, 2024

@fajarra
This is probably due to being logged in as a user then sudo su as root to run through these steps. Since the users are using the /home filesystem to be logged in, it is currently in use and cannot be deleted - thus your issue. You will have to be absolutely sure no users (that aren't root) are logged in when going through these steps.

@troyfontaine
Copy link
Author

@fajarra This is probably due to being logged in as a user then sudo su as root to run through these steps. Since the users are using the /home filesystem to be logged in, it is currently in use and cannot be deleted - thus your issue. You will have to be absolutely sure no users (that aren't root) are logged in when going through these steps.

Exactly-this is why the gist starts with

This process requires that you are able to ssh OR log in locally using the root user account and that no services be running as users out of /home on the target machine.

@RJR-ReinaldoJr
Copy link

RJR-ReinaldoJr commented Jun 12, 2024

Thank you, Perfect! It's working for Rocky9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment