Skip to content

Instantly share code, notes, and snippets.

@highblue
Forked from troyfontaine/readme.md
Last active September 5, 2023 14:32
Show Gist options
  • Save highblue/2218b43a9f77db5aeb1b8e59056108c3 to your computer and use it in GitHub Desktop.
Save highblue/2218b43a9f77db5aeb1b8e59056108c3 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 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

[root@cdpnodes ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                  16G     0   16G   0% /dev
tmpfs                     16G     0   16G   0% /dev/shm
tmpfs                     16G  9.0M   16G   1% /run
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G  1.5G   49G   3% /
/dev/mapper/centos-home  350G  1.5G   49G   1% /
/dev/sda1               1014M  154M  861M  16% /boot
tmpfs                    3.2G     0  3.2G   0% /run/user/1000

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

cp -a /home /tmp/

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 centos -wi-a----- 93.24g                                                    
  root centos -wi-ao---- 50.00g                                                    
  swap centos -wi-ao---- 15.75g  

Step 4 - Remove the home LVM volume

lvremove /dev/mapper/centos-home

这里需要注意,如果遇到了‘contains a filesystem in use’,可能是前面的umount没有成功。可以再运行一遍,sudo umount -fl /dev/centos/home,然后再试试lvremove是否可以运行;实在不行,需要运行fuser -km /home 如果没有fuser,则需要yum install psmisc 同时注意不要在/home folder下运行 同时注意,运行完以后会断开当前的ssh连接,需要再次以root用户连接。所以注意下。 重新连接之后再运行 lvremove /dev/mapper/centos-home

Step 5 - Resize the root LVM volume

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

lvextend -L+200G /dev/mapper/centos-root

Step 6 - Resize the root partition

xfs_growfs /dev/mapper/centos-root

这一步对我的系统是

xfs_growfs /

Step 6.5 - Recreate the lvs of home and mount again

估算一下现在总计多少空间

lvcreate -L 230G -n /dev/mapper/centos-home
mkfs.xfs /dev/mapper/centos-home
mount /dev/mapper/centos-home

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

cp -a /tmp/home /

Step 8 - Remove the temporary location

rm -rf /tmp

可以参考https://blog.51cto.com/mflag/5357602

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/centos-home.

Step 10 - Don't miss this! 忽略

Run the following command to sync systemd up with the changes.

dracut --regenerate-all --force

好像这一步会造成/home不能挂载。在Centos8上我遇到了。

@highblue
Copy link
Author

磁盘空间不足的解决办法

@highblue
Copy link
Author

总体上,是将/home的空间挪到/root中。

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