Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 requires you to be able to ssh into the instance 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 customation-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

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 up with the changes.

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

reihell commented Aug 21, 2019

Very good instruction (as root ofc and only logged on user ;-))

@dsculptor
Copy link

dsculptor commented Aug 22, 2019

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

@danielcatao
Copy link

danielcatao commented Nov 28, 2019

thank you very much

@nlundkvist
Copy link

nlundkvist commented Dec 30, 2019

Thanks for this guide, it was very helpful for me. I did encounter an error when trying to grow the filesystem though, even though this is the device that's mounted as / and the filesystem is XFS:

[root@cloud ~]# xfs_growfs /dev/mapper/cl-root xfs_growfs: /dev/mapper/cl-root is not a mounted XFS filesystem

I was able to grow the filesystem by issuing this command instead (CentOS 8):

[root@cloud ~]# xfs_growfs /

Thought I'd share this in case someone else has an issue like this as well.

@troyfontaine
Copy link
Author

troyfontaine commented Jan 5, 2020

Thanks for this guide, it was very helpful for me. I did encounter an error when trying to grow the filesystem though, even though this is the device that's mounted as / and the filesystem is XFS:

[root@cloud ~]# xfs_growfs /dev/mapper/cl-root xfs_growfs: /dev/mapper/cl-root is not a mounted XFS filesystem

I was able to grow the filesystem by issuing this command instead (CentOS 8):

[root@cloud ~]# xfs_growfs /

Thought I'd share this in case someone else has an issue like this as well.

Hi @nlundkvist are you sure you're even using LVM? I haven't really played with CentOS 8-so I can't confirm that the above Gist even applies to it?

Thank you for sharing your experience on CentOS 8 though!

@BuxtaVonGradec
Copy link

BuxtaVonGradec commented Feb 12, 2020

100% works

innovation Step 5:

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

@Icukurra
Copy link

Icukurra commented Apr 6, 2020

Thanks for this guide, it was very helpful for me. I did encounter an error when trying to grow the filesystem though, even though this is the device that's mounted as / and the filesystem is XFS:
[root@cloud ~]# xfs_growfs /dev/mapper/cl-root xfs_growfs: /dev/mapper/cl-root is not a mounted XFS filesystem
I was able to grow the filesystem by issuing this command instead (CentOS 8):
[root@cloud ~]# xfs_growfs /
Thought I'd share this in case someone else has an issue like this as well.

Hi @nlundkvist are you sure you're even using LVM? I haven't really played with CentOS 8-so I can't confirm that the above Gist even applies to it?

Thank you for sharing your experience on CentOS 8 though!

This also happened to me on CentOS 8 and xfs_growfs / worked while the original command did not. Tis odd, but its working.

@automacaosamos
Copy link

automacaosamos commented Jun 26, 2020

thank you saved my life...

@bglobal
Copy link

bglobal commented Jul 21, 2020

0

Yes, it gave me below>: [root@virtualmin log]# xfs_growfs / xfs_growfs: / is not a mounted XFS filesystem

Then I issued resize2fs command and it worked:

[root@virtualmin /]# resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required old desc_blocks = 4, new_desc_blocks = 34 Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 141819904 (4k) blocks. The filesystem on /dev/mapper/VolGroup-lv_root is now 141819904 blocks long.

[root@virtualmin /]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root ext4 533G 47G 460G 10% / tmpfs tmpfs 7.6G 18M 7.6G 1% /dev/shm /dev/sda1 ext4 477M 164M 288M 37% /boot

@setbabara
Copy link

setbabara commented Sep 8, 2020

Thanks. Thanks. Thanks. for help me.

@sceptapular
Copy link

sceptapular commented Oct 5, 2020

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

troyfontaine commented Oct 5, 2020

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

Michael1969 commented Nov 17, 2020

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

xiaurrehman143 commented Mar 30, 2021

Thanks and it works for me

@shahinpashayev86
Copy link

shahinpashayev86 commented Aug 4, 2021

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

touille03 commented Nov 10, 2021

Thank you ! The helped my day a loooot !

@BrankoBumBum
Copy link

BrankoBumBum commented Apr 19, 2022

thank you, this made my life easier :)

@sharemindDavid
Copy link

sharemindDavid commented May 26, 2022

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

sharemindDavid commented May 27, 2022

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

troyfontaine commented May 27, 2022

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.

@tamahau30494
Copy link

tamahau30494 commented Sep 30, 2022

This worked great for me as well @Rycieos, thanks for your post. It was exactly what I needed.

@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

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