03.Dynamic expansion of directories under Linux, taking allocating part of home space to root as an example
Introduction
This article will introduce how to allocate part of the home directory to the root directory under Linux.
General idea: Uninstall home, release its disk capacity occupation -> expand root, increase its disk space -> rebuild and restore home
The operation demonstration environment is CentOS system, and other operating systems such as openEuler can also be used as a reference.
Related article reference:
01. Directory expansion under Linux, taking increasing the disk capacity allocation of the root directory as an example
The operating system version is as follows
name | Version |
---|---|
CentOS | 7.6+ |
1. Allocation details
Executing the lsblk command, you can see that
centos-home has allocated more storage space, while root only has 50G. If there is
no additional disk space to continue to allocate (if there is, please refer to: 01. Directory expansion under Linux to increase the size of the root directory Taking disk capacity allocation as an example )
, you need to free up part of the home space and allocate it to root (in actual application scenarios, root usage is relatively large after all)
[root@localhost harbor]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 66.7M 0 rom /mnt
vda 252:0 0 1T 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 1023G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 31.5G 0 lvm [SWAP]
└─centos-home 253:2 0 941.5G 0 lvm /home
[root@localhost harbor]#
2. Uninstall home
2.1 Terminate the process
Kill all processes under the /home file system (Note: Do not execute fuser -km home in the /home directory)
Execution error: -bash: fuser: command not found.
Problem analysis: When minimizing the installation of centos, there is no fuser command, resulting in the inability to fence.
Solution: yum install psmisc
[root@localhost portainer]# cd ~
[root@localhost ~]# fuser -km home
2.2 Backup /home
[root@localhost ~]# tar cvf /run/home.tar /home
tar: 从成员名中删除开头的“/”
/home/
/home/harbor/
/home/harbor/harbor.v2.8.2.tar.gz
2.3 Uninstall /home
[root@localhost ~]# umount /home
[root@localhost ~]#
2.4 Delete the logical volume lv where /home is located
Tip: Logical volume centos/home contains a filesystem in use
Solution: Remount mount /home and execute 2.1 to terminate the process (or other methods to terminate the process under the /home file system)
[root@localhost ~]# lvremove /dev/mapper/centos-home
Do you really want to remove active logical volume centos/home? [y/n]: y
Logical volume "home" successfully removed
[root@localhost ~]#
3. Expand root
3.1 Expand the logical volume lv where /root is located
After uninstalling home, about 950G will be left vacant.
Here, 800G will be allocated to root.
[root@localhost ~]# lvextend -L +800G /dev/mapper/centos-root
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 850.00 GiB (217600 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]#
3.2 Expand the /root file system
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 222822400
[root@localhost ~]#
3.3 Check whether the expansion is successful
You can see that centos-root has been successfully expanded to 1023G
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 66.7M 0 rom /mnt
vda 252:0 0 1T 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 1023G 0 part
├─centos-root 253:0 0 850G 0 lvm /
└─centos-swap 253:1 0 31.5G 0 lvm [SWAP]
[root@localhost ~]#
4. Rebuild /home
4.1 Create the logical volume lv where /home is located
[root@localhost ~]# lvcreate -L 141G -n /dev/mapper/centos-home
Logical volume "home" created.
[root@localhost ~]#
4.2 Create the /home file system
[root@localhost ~]# mkfs.xfs /dev/mapper/centos-home
meta-data=/dev/mapper/centos-home isize=512 agcount=4, agsize=9240576 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=36962304, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=18048, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]#
4.3 Mount the newly created file system to the /home directory
[root@localhost ~]# mount /dev/mapper/centos-home
[root@localhost ~]#
4.4 Restore /home contents
Remember to restore stopped programs under home
[root@localhost ~]# tar xvf /run/home.tar -C /
home/
home/harbor/
home/harbor/harbor.v2.8.2.tar.gz
4.5 Delete backup
[root@localhost ~]# rm -rf /run/home.tar
[root@localhost ~]#
Related article reference:
01. Directory expansion under Linux, taking increasing the disk capacity allocation of the root directory as an example