Skip to content

Instantly share code, notes, and snippets.

@troyfontaine
Last active March 28, 2024 06:26
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
@setbabara
Copy link

Thanks. Thanks. Thanks. for help me.

@sceptapular
Copy link

This was perfect! Worked exactly as expected for me. I tried many other links earlier this year and they gave me mixed results and lots of headache. This one was straight-forward; I'm puzzled as to why it didn't come up in my search results before! Thank you!!!

@troyfontaine
Copy link
Author

This was perfect! Worked exactly as expected for me. I tried many other links earlier this year and they gave me mixed results and lots of headache. This one was straight-forward; I'm puzzled as to why it didn't come up in my search results before! Thank you!!!

Happy to have helped!

@wrljet
Copy link

wrljet commented Oct 9, 2020

@jonbrohauge: I think you need to find & kill some processes which might be using the disk. For me it was quite a fresh server - so it worked I guess.

This worked for me:

https://www.thegeekdiary.com/lvremove-command-fails-with-error-lvm-cant-remove-open-logical-volume/

@Michael1969
Copy link

Thanks. worked without any problem.

@mhakim168
Copy link

mhakim168 commented Jan 1, 2021

after remove the /home on fstab, the user can't login, always get back to login screen.

got this error :

login as: xxxxxxx
xxxxx@xxxxxxx's password:
Last login: Fri Jan 1 08:04:02 2021
Could not chdir to home directory /home/xxxxx: Permission denied
-bash: /home/xxxxxx/.bash_profile: Permission denied
-bash-4.2$ su

@troyfontaine
Copy link
Author

troyfontaine commented Jan 1, 2021

after remove the /home on fstab, the user can't login, always get back to login screen.

got this error :

login as: xxxxxxx
xxxxx@xxxxxxx's password:
Last login: Fri Jan 1 08:04:02 2021
Could not chdir to home directory /home/xxxxx: Permission denied
-bash: /home/xxxxxx/.bash_profile: Permission denied
-bash-4.2$ su

It sounds like you missed a detail on a step @mhakim168? Did you ensure to use the -a flag when copying the home directory to the temporary directory and then back to /home?

@xxShoTz
Copy link

xxShoTz commented Jan 17, 2021

Thank you. Works great!

@xiaurrehman143
Copy link

Thanks and it works for me

@shahinpashayev86
Copy link

Thank you. Works great!

@moshovd
Copy link

moshovd commented Oct 21, 2021

Thank you so much! Each and every step worked perfectly on my Centos 8.

@rgfpy
Copy link

rgfpy commented Nov 9, 2021

thank you! work great in Centos 7

@touille03
Copy link

Thank you ! The helped my day a loooot !

@BrankoBumBum
Copy link

thank you, this made my life easier :)

@sharemindDavid
Copy link

It sounds like you missed a detail on a step @mhakim168? Did you ensure to use the -a flag when copying the home directory to the temporary directory and then back to /home?

@troyfontaine what should i do if i have not done that?...

@sharemindDavid
Copy link

how do i link my home partition with my /home i followed all steps except the part where you copy the home directory to a temp directory instead i used tar to archive it to my flash drive, i copy'd it back but it just lives in the root partition as /home

@troyfontaine
Copy link
Author

how do i link my home partition with my /home i followed all steps except the part where you copy the home directory to a temp directory instead i used tar to archive it to my flash drive, i copy'd it back but it just lives in the root partition as /home

That was the goal of this gist-to get rid of the dedicated /home partition to provide additional space to the root partition. So you copy the contents of the old /home to the directory that was previously used to mount the dedicated partition and it should just work.

@deven60
Copy link

deven60 commented Jun 26, 2022

this can be applied for kali linux

@Rycieos
Copy link

Rycieos commented Jul 22, 2022

Can confirm this works on Rocky Linux 9 (CentOS). Only need to substitute /dev/mapper/rl-home for /dev/cl/home and /dev/mapper/rl-root for /dev/cl/root.

@yossibnv1506
Copy link

yossibnv1506 commented Nov 9, 2022

Thanks you ,very clear guide !!
I would like to do it also with my CENTOS8 KVM server .
I have only 50G in /dev/mapper/cl-root and only 5G are free .
So there is not enough space to add more machines there ,
while in : /dev/mapper/cl-home I have 504G available ,which is a lot of wasted disk space I am no using .
Just want to make sure all this process is safe .
Could you please tell me , if there is data loss risk on cl-root partition , where all my machines are stored , 10xx

@chaakin
Copy link

chaakin commented Nov 30, 2022

I also want to say a big thank you for providing this guide!
Had one issue with Step5 where adding the number from 'lvs' output didn't work, but the command provided by @BuxtaVonGradec did work.
For anyone using Centos 8.5 converted to Centos Stream, this command worked for me on Step5:
lvextend -r -l +100%FREE /dev/mapper/centos-root

@surenderverma509
Copy link

Guys i am getting this message after reboot the server

error getting authority: error initializing authority: could not connect: connection refused (g-io-error-quark, 1)

Any suggestion for this..

@Fahrurrozy27
Copy link

Dude I asking you, when you do this, is the root data still available ?

@troyfontaine
Copy link
Author

Dude I asking you, when you do this, is the root data still available ?

Everything at / survived without issue for myself.

@Fahrurrozy27
Copy link

Dude I asking you, when you do this, is the root data still available ?

Everything at / survived without issue for myself.

Thank you, it's worked like a charm > https://ibb.co/rbTXk49

@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.

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