将Centos7的HOME分区空间重新分配给/root分区

流程方法一:

查看分区

1
2
3
4
5
6
7
8
9
10
[root@srvLessCodeTest ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 16K 3.9G 1% /dev/shm
tmpfs 3.9G 9.0M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 19G 32G 37% /
/dev/sda1 1014M 196M 819M 20% /boot
/dev/mapper/centos-home 421G 33M 421G 1% /home
tmpfs 783M 0 783M 0% /run/user/0

备份home目录

1
2
[root@srvLessCodeTest ~]# mkdir /home-backup
[root@srvLessCodeTest ~]# mv /home/* /home-backup/

移除/home分区

1
2
3
4
5
6
[root@srvLessCodeTest ~]# umount /home
[root@srvLessCodeTest ~]#
[root@srvLessCodeTest ~]# lvremove /dev/centos/home
Do you really want to remove active logical volume centos/home? [y/n]: y
Logical volume "home" successfully removed
[root@srvLessCodeTest ~]#

查看VG空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@srvLessCodeTest ~]# vgdisplay 
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <479.00 GiB
PE Size 4.00 MiB
Total PE 122623
Alloc PE / Size 14816 / <57.88 GiB
Free PE / Size 107807 / 421.12 GiB
VG UUID dJSZJu-XpUR-0ThA-esuO-1Avs-3S4Q-Mb7zTp

重新创建home卷

1
2
3
4
5
6

[root@srvLessCodeTest ~]# lvcreate -L 64G -n home centos
WARNING: xfs signature detected on /dev/centos/home at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/centos/home.
Logical volume "home" created.
[root@srvLessCodeTest ~]#

查看卷空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@srvLessCodeTest ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID SzSQ5r-NL2Q-OQHE-7fy7-TGUZ-u2Jf-dMIAtL
LV Write Access read/write
LV Creation host, time srvlesscode.china.local, 2024-03-19 11:11:34 +0800
LV Status available
# open 2
LV Size <7.88 GiB
Current LE 2016
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID wi7SQ8-qKfQ-tzth-vCzj-1FDJ-B7Yi-AFzVPn
LV Write Access read/write
LV Creation host, time srvlesscode.china.local, 2024-03-19 11:11:36 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID C6AzYU-1Tvp-cV9Z-8I9q-ntdf-nTHm-pcNADc
LV Write Access read/write
LV Creation host, time srvLessCodeTest.china.local, 2024-05-22 12:08:26 +0800
LV Status available
# open 0
LV Size 64.00 GiB
Current LE 16384
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

格式化新home卷

1
2
3
4
5
6
7
8
9
10
11
[root@srvLessCodeTest ~]# mkfs -t xfs /dev/centos/home 
Discarding blocks...Done.
meta-data=/dev/centos/home isize=512 agcount=4, agsize=4194304 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=16777216, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=8192, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

挂载/home卷

1
[root@srvLessCodeTest ~]# mount /dev/centos/home /home

恢复home分区文件

1
2
[root@srvLessCodeTest ~]# mv /home-backup/* /home
[root@srvLessCodeTest ~]# rm -rf /home-backup

再次查看卷组剩余空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@srvLessCodeTest ~]# vgdisplay 
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <479.00 GiB
PE Size 4.00 MiB
Total PE 122623
Alloc PE / Size 31200 / <121.88 GiB
Free PE / Size 91423 / 357.12 GiB
VG UUID dJSZJu-XpUR-0ThA-esuO-1Avs-3S4Q-Mb7zTp

扩展/root分区

1
2
3
4
[root@srvLessCodeTest ~]# lvextend -L +357.12G /dev/centos/root
Rounding size to boundary between physical extents: 357.12 GiB.
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 407.12 GiB (104223 extents).
Logical volume centos/root successfully resized.

使用xfs_growfs命令扩展/root分区的XFS文件系统【操作完成】

1
2
3
4
5
6
7
8
9
10
11
[root@srvLessCodeTest ~]# xfs_growfs /dev/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 106724352

流程方法二:

查看磁盘使用情况

1
df -h

备份分区文件

1
tar cvf /data/home.tar /home

卸载删除/home分区

1
2
umount /home
lvremove /dev/mapper/centos-home 【按Y】

扩展/root逻辑卷

1
lvextend -L +315G /dev/mapper/centos-root

扩展文件系统

1
xfs_growfs /dev/mapper/centos-root

重建/home逻辑卷

1
lvcreate -L 64.18G /dev/mapper/centos-home

创建/home文件系统

1
mkfs.xfs /dev/mapper/centos-home

重新挂载/home分区

1
mount /dev/mapper/centos-home

恢复并删除备份

1
2
tar xvf /data/home.tar -C /
rm -rf /data/home.tar

【完成扩展】可查看分区结果

1
df -h

将Centos7的HOME分区空间重新分配给/root分区
http://anximin.github.io/2024/05/22/Linux_LVMcentos7/
作者
Sylar
发布于
2024年5月22日
许可协议