0%

Linux | 同一个硬盘,将一个 lvm 分区的空间转移给另一个

环境

  • VMware 上运行的 CentOS 7
  • 1 块 SATA 盘

介绍

一台 CentOS 开发机报错“硬盘空间满了”,暂时又没有办法硬盘扩容。查看机器详情后发现它在使用 lvm 管理存储,根//home单独挂载在不同的逻辑卷,报错的是根/,但是/home还有足够的空间,所以可以把/home的空间转移给/

实际操作

备份 home >> 卸载 home >> 删除 home >> 将 home 的空间添加到 / >> 重新分配 home >> 格式化 home >> 完成

用到的命令:

1
2
3
4
5
6
df -h          # 查看磁盘空间
lsblk        # 查看块设备详情
fdisk -l # 查看分区详情
lvremove\lvcreate # 逻辑卷删除/创建
lvdisplay\vgdisplay\pvdisplay #查看逻辑卷/卷组/物理卷
xfs_growfs # 加载xfs_growfs

备份、删除/home

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@test33 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root 230G 230G 20K 100% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 9.2M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 239M 776M 24% /boot
/dev/mapper/cl-home 66G 33M 66G 1% /home
tmpfs 380M 12K 380M 1% /run/user/42
overlay 230G 230G 20K 100% /var/lib/docker/overlay2/e439b425de8d1312a33938d8bfc4fbeb445759e36ea1bda36de7d9a0ba9562df/merged
overlay 230G 230G 20K 100% /var/lib/docker/overlay2/2bbaf357045ec30e9e8f729f6c82784a41353e43209ec9484e40e03a7fe36647/merged
overlay 230G 230G 20K 100% /var/lib/docker/overlay2/b78940a1c074d763256e2279599c5ad43066f16121af11b9af502c7adedc2e65/merged
overlay 230G 230G 20K 100% /var/lib/docker/overlay2/afe2f63c026b30d1fe5da5d481eefa40acc2c0308b5ebd32705df197477911e5/merged
shm 64M 0 64M 0% /var/lib/docker/containers/9dd9255c8c8bb5e3260a45d5c89e575cdaca90e8649f4e14c69e405382dc3c83/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/b6d7a17c93493bf150fa417b63ded2ec5db1a62c436a689fb7557f9270e61b23/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/f54bb5d50c4a055ef34cf55cb98425fca073a1f46a13042e7567219d6e015e31/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/edc22af86393c9b0d8fcb59f60144e2f309a0e45fda676997e13eb2d6680dd5b/mounts/shm
tmpfs 380M 0 380M 0% /run/user/0
[root@test33 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 119G 0 part
│ ├─cl-root 253:0 0 230G 0 lvm /
│ ├─cl-swap 253:1 0 3.9G 0 lvm [SWAP]
│ └─cl-home 253:2 0 65.1G 0 lvm /home
├─sda3 8:3 0 80G 0 part
│ └─cl-root 253:0 0 230G 0 lvm /
└─sda4 8:4 0 100G 0 part
└─cl-root 253:0 0 230G 0 lvm /
sr0 11:0 1 1024M 0 rom
[root@test33 ~]# mkdir /tmp/home
[root@test33 ~]# cp -r /home/* /tmp/home
[root@test33 ~]# umount /home
[root@test33 ~]# lvremove /dev/mapper/cl-home
Do you really want to remove active logical volume cl/home? [y/n]: y
Logical volume "home" successfully removed
[root@test33 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 119G 0 part
│ ├─cl-root 253:0 0 230G 0 lvm /
│ └─cl-swap 253:1 0 3.9G 0 lvm [SWAP]
├─sda3 8:3 0 80G 0 part
│ └─cl-root 253:0 0 230G 0 lvm /
└─sda4 8:4 0 100G 0 part
└─cl-root 253:0 0 230G 0 lvm /
sr0 11:0 1 1024M 0 rom

场景一:用掉所有/home空间,/home不再单独挂载空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@test33 ~]# lvextend -l +100%FREE /dev/mapper/cl-root
Size of logical volume cl/root changed from 295.00 GiB (75520 extents) to 295.11 GiB (75549 extents).
Logical volume cl/root successfully resized.
[root@test33 ~]# xfs_growfs /dev/mapper/cl-root
meta-data=/dev/mapper/cl-root isize=512 agcount=24, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=77332480, 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 77332480 to 77362176
[root@test33 ~]# sudo vi /etc/fstab
// 注释掉/home的挂载
[root@test33 ~]# sudo mount -a

场景二:只用掉部分/home空间,/home仍以挂载方式存在

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[root@test33 ~]# lvextend -L 295G /dev/mapper/cl-root
Size of logical volume cl/root changed from <230.00 GiB (58879 extents) to 295.00 GiB (75520 extents).
Logical volume cl/root successfully resized.
[root@test33 ~]# xfs_growfs /dev/mapper/cl-root
meta-data=/dev/mapper/cl-root isize=512 agcount=19, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=60292096, 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 60292096 to 77332480
[root@test33 ~]# ls /dev/cl
root swap
[root@test33 ~]# lvcreate -l +100%free -n home cl
Logical volume "home" created.
[root@test33 ~]# ls /dev/cl
home root swap
[root@test33 ~]# mkfs.xfs /dev/cl/home
meta-data=/dev/cl/home isize=512 agcount=4, agsize=7424 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=29696, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@test33 ~]# ls /dev/mapper/
cl-home cl-root cl-swap control
[root@test33 ~]# mount /dev/mapper/cl-home /home

[root@test33 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 119G 0 part
│ ├─cl-root 253:0 0 295G 0 lvm /
│ ├─cl-swap 253:1 0 3.9G 0 lvm [SWAP]
│ └─cl-home 253:2 0 116M 0 lvm /home
├─sda3 8:3 0 80G 0 part
│ └─cl-root 253:0 0 295G 0 lvm /
└─sda4 8:4 0 100G 0 part
└─cl-root 253:0 0 295G 0 lvm /
sr0 11:0 1 1024M 0 rom
[root@test33 ~]# fdisk -l

磁盘 /dev/sda:536.9 GB, 536870912000 字节,1048576000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b5a9c

设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 251658239 124779520 8e Linux LVM
/dev/sda3 251658240 419430399 83886080 83 Linux
/dev/sda4 419430400 629145599 104857600 83 Linux

磁盘 /dev/mapper/cl-root:316.8 GB, 316753838080 字节,618659840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/cl-swap:4160 MB, 4160749568 字节,8126464 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/cl-home:121 MB, 121634816 字节,237568 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[root@test33 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root 295G 225G 71G 77% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 18M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 239M 776M 24% /boot
tmpfs 380M 12K 380M 1% /run/user/42
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/b78940a1c074d763256e2279599c5ad43066f16121af11b9af502c7adedc2e65/merged
shm 64M 0 64M 0% /var/lib/docker/containers/9dd9255c8c8bb5e3260a45d5c89e575cdaca90e8649f4e14c69e405382dc3c83/mounts/shm
tmpfs 380M 0 380M 0% /run/user/0
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/57f869e119cb2f7fdd412f015eefee639b997e1638c456129ed4e21c47cf397c/merged
shm 64M 0 64M 0% /var/lib/docker/containers/09a9262208972446b9f3bb2e97838ebf22c2f1e66090618593e8cafd8b86a000/mounts/shm
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/ae47756e9851081ad120a06dbfea8cba4275d9297c44863b4f9410bd000de412/merged
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/2420c90bd4403953c535b70aa3d06891c58092398681c9fc5be202b5ad67bb2e/merged
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/c612edf73c386189365a6583f41090802f6b2a4cd5e88cb3ca22e3a526d7d612/merged
shm 64M 0 64M 0% /var/lib/docker/containers/c06950567393ef037c649b9ce2f0e19052e0e3c418acdc7eeed9b5330c166b1c/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/ccf645e71a455df8802e8b83884b96bc81857852031593e7fd5ddef1c89d903e/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/2ffab189252844eb7f444b75191e6988fe622adc88e0f87a8c16b7b61f254a99/mounts/shm
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/06d76af657c7c0f7a2b11759af09e62b31f5d4ba62b923692a61677f6ad87b21/merged
shm 64M 0 64M 0% /var/lib/docker/containers/bf10d736de9b8c209c11c9f25a3d30bb415701c9c2b750aa95a4e31994524405/mounts/shm
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/a01fa0fba6e4b5cfa2972da7878af59852aa75a10808fa20f06e285a57ca92a1/merged
overlay 295G 225G 71G 77% /var/lib/docker/overlay2/35a81c327fd00e51bceed151fb35967c35d24338ec5dd3ed9086fc384c7f7803/merged
shm 64M 0 64M 0% /var/lib/docker/containers/89f265fbf738ce64e4ab923d3e8d27cdad599106c6dcc1de55802d2c72ff70c9/mounts/shm
shm 64M 0 64M 0% /var/lib/docker/containers/16026f7e55b6b5338478caff0613564f56b56e0a41495377d9b298091e1a57f3/mounts/shm
/dev/mapper/cl-home 113M 6.1M 107M 6% /home
[root@test33 ~]#

恢复/home的内容

1
2
[root@test33 ~]# cp -r /tmp/home/* /home/
[root@test33 ~]# rm -rf /tmp/home

参考

https://www.cnblogs.com/-abm/p/11349240.html

欢迎关注我的其它发布渠道