跨磁盘扩容根目录

LVM 的基本概念

物理卷 Physical Volume (PV):可以在上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopback file)。物理卷包括一个特殊的 header,其余部分被切割为一块块物理区域(physical extents)

卷组 Volume group (VG):将一组物理卷收集为一个管理单元

逻辑卷 Logical volume (LV):虚拟分区,由物理区域(physical extents)组成

物理区域 Physical extent (PE):硬盘可供指派给逻辑卷的最小单位(通常为 4MB)

查看磁盘关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查看磁盘关系

root@hello:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 55.6M 1 loop /snap/core18/2667
loop1 7:1 0 55.6M 1 loop /snap/core18/2679
loop2 7:2 0 63.2M 1 loop /snap/core20/1738
loop3 7:3 0 63.3M 1 loop /snap/core20/1778
loop4 7:4 0 91.8M 1 loop /snap/lxd/23991
loop5 7:5 0 91.8M 1 loop /snap/lxd/24061
loop6 7:6 0 49.6M 1 loop /snap/snapd/17883
loop7 7:7 0 49.8M 1 loop /snap/snapd/17950
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 98.5G 0 lvm /
sdb 8:16 0 100G 0 disk
root@hello:~#

新建分区

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
# 新建分区
root@hello:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xd97cd23b.

Command (m for help): g
Created a new GPT disklabel (GUID: CED3C27F-6F17-D940-A99F-191D881FCD91).

Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-209715166, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715166, default 209715166):

Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.

Command (m for help): p
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: CED3C27F-6F17-D940-A99F-191D881FCD91

Device Start End Sectors Size Type
/dev/sdb1 2048 209715166 209713119 100G Linux filesystem

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

root@hello:~#

查看磁盘关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 查看磁盘关系
root@hello:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 55.6M 1 loop /snap/core18/2667
loop1 7:1 0 55.6M 1 loop /snap/core18/2679
loop2 7:2 0 63.2M 1 loop /snap/core20/1738
loop3 7:3 0 63.3M 1 loop /snap/core20/1778
loop4 7:4 0 91.8M 1 loop /snap/lxd/23991
loop5 7:5 0 91.8M 1 loop /snap/lxd/24061
loop6 7:6 0 49.6M 1 loop /snap/snapd/17883
loop7 7:7 0 49.8M 1 loop /snap/snapd/17950
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 98.5G 0 lvm /
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 100G 0 part
root@hello:~#

创建PV并查看

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
# 创建PV并查看
root@hello:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <99.00 GiB / not usable 0
Allocatable yes
PE Size 4.00 MiB
Total PE 25343
Free PE 127
Allocated PE 25216
PV UUID Dys0fV-H7vi-KfCz-5Flh-n724-mjP4-dtzzJ5

root@hello:~#
root@hello:~# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
root@hello:~#
root@hello:~#
root@hello:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <99.00 GiB / not usable 0
Allocatable yes
PE Size 4.00 MiB
Total PE 25343
Free PE 127
Allocated PE 25216
PV UUID Dys0fV-H7vi-KfCz-5Flh-n724-mjP4-dtzzJ5

"/dev/sdb1" is a new physical volume of "<100.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <100.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID iR6wd1-QDJc-oqm7-dxF5-JzvB-e2Ta-LSciIm

root@hello:~#

扩展VG并查看

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
# 扩展VG并查看
root@hello:~# vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <99.00 GiB
PE Size 4.00 MiB
Total PE 25343
Alloc PE / Size 25216 / 98.50 GiB
Free PE / Size 127 / 508.00 MiB
VG UUID MJt4Ho-TZ8N-vBhS-TMnK-nSPa-2orh-MbV9jr

root@hello:~#
root@hello:~# vgextend ubuntu-vg /dev/sdb1
Volume group "ubuntu-vg" successfully extended
root@hello:~#
root@hello:~# vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 198.99 GiB
PE Size 4.00 MiB
Total PE 50942
Alloc PE / Size 25216 / 98.50 GiB
Free PE / Size 25726 / 100.49 GiB
VG UUID MJt4Ho-TZ8N-vBhS-TMnK-nSPa-2orh-MbV9jr

root@hello:~#

扩展LV并查看

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
# 扩展LV并查看
root@hello:~# lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/ubuntu-lv
LV Name ubuntu-lv
VG Name ubuntu-vg
LV UUID 5DDQEu-kuMX-VU3G-Gck0-5Pjq-bMzO-cHnbIr
LV Write Access read/write
LV Creation host, time ubuntu-server, 2021-09-23 11:50:37 +0800
LV Status available
# open 1
LV Size 98.50 GiB
Current LE 25216
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

root@hello:~#
root@hello:~# lvextend /dev/ubuntu-vg/ubuntu-lv /dev/sdb1
Size of logical volume ubuntu-vg/ubuntu-lv changed from 98.50 GiB (25216 extents) to <198.50 GiB (50815 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
root@hello:~#
root@hello:~# lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/ubuntu-lv
LV Name ubuntu-lv
VG Name ubuntu-vg
LV UUID 5DDQEu-kuMX-VU3G-Gck0-5Pjq-bMzO-cHnbIr
LV Write Access read/write
LV Creation host, time ubuntu-server, 2021-09-23 11:50:37 +0800
LV Status available
# open 1
LV Size <198.50 GiB
Current LE 50815
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

root@hello:~#

扩展根目录

1
2
3
4
5
6
# 扩展根目录
root@hello:~# resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 25
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 52034560 (4k) blocks long.

查看空间和关系

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
# 查看空间和关系
root@hello:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 393M 6.0M 387M 2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4 196G 31G 156G 17% /
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 ext4 974M 247M 660M 28% /boot
tmpfs tmpfs 393M 4.0K 393M 1% /run/user/0
root@hello:~#
root@hello:~#
root@hello:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 55.6M 1 loop /snap/core18/2667
loop1 7:1 0 55.6M 1 loop /snap/core18/2679
loop2 7:2 0 63.2M 1 loop /snap/core20/1738
loop3 7:3 0 63.3M 1 loop /snap/core20/1778
loop4 7:4 0 91.8M 1 loop /snap/lxd/23991
loop5 7:5 0 91.8M 1 loop /snap/lxd/24061
loop6 7:6 0 49.6M 1 loop /snap/snapd/17883
loop7 7:7 0 49.8M 1 loop /snap/snapd/17950
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 198.5G 0 lvm /
sdb 8:16 0 100G 0 disk
└─sdb1 8:17 0 100G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 198.5G 0 lvm /
root@hello:~#

关于

https://www.oiox.cn/

https://www.oiox.cn/index.php/start-page.html

CSDN、GitHub、51CTO、知乎、开源中国、思否、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客

全网可搜《小陈运维》

文章主要发布于微信公众号