在 Linux 中扩容分区、逻辑卷、文件系统

准备工作

  • fdisk -l
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
root@dev215 [~]# fdisk -l
GPT PMBR size mismatch (209715199 != 419430399) will be corrected by write.
The backup GPT table is not on the end of the device.
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 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: 406F0824-6F10-4C6F-8522-FFF0A017AA44

Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 2101247 2097152 1G Linux filesystem
/dev/sda3 2101248 209713151 207611904 99G Linux LVM


Disk /dev/mapper/opencloudos-root: 66.52 GiB, 71424802816 bytes, 139501568 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/opencloudos-home: 32.48 GiB, 34871443456 bytes, 68108288 sectors
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 typedos 表示为 MBR 分区,为 gpt 表示为 GPT 分区。

  • lsblk
1
2
3
4
5
6
7
8
root@dev215 [~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
├─opencloudos-root 251:0 0 66.5G 0 lvm /
└─opencloudos-home 251:1 0 32.5G 0 lvm /home

扩容分区

1
yum install -y cloud-utils-growpart gdisk

MBR

1
LC_ALL=en_US.UTF-8 growpart /dev/vdb 1

GPT

1
LC_ALL=en_US.UTF-8 growpart /dev/sda 3

出现以 CHANGED 开头的信息表示扩容成功。

1
2
3
4
5
6
7
8
9
10
11
root@dev215 [/data]# growpart /dev/sda 3
CHANGED: partition=3 start=2101248 old: size=207611904 end=209713151 new: size=417329119 end=419430366

root@dev215 [/data]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 199G 0 part
├─opencloudos-root 251:0 0 66.5G 0 lvm /
└─opencloudos-home 251:1 0 32.5G 0 lvm /home

扩容逻辑卷

在上面的 lsblk 信息中,实际上根目录 //home 的类型都是 lvm

1
2
3
pvresize /dev/sda3
lvextend -L +100G /dev/opencloudos/root
xfs_growfs /
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
root@dev215 [/data]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 opencloudos lvm2 a-- <199.00g 100.00g

root@dev215 [/data]# pvresize /dev/sda3
Physical volume "/dev/sda3" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

root@dev215 [/data]# lvdisplay
--- Logical volume ---
LV Path /dev/opencloudos/home
LV Name home
VG Name opencloudos
LV UUID Mrv5DQ-JeQx-mKdM-Vn9u-qOtw-Yl9k-TbkTjH
LV Write Access read/write
LV Creation host, time dev215, 2024-11-01 10:35:13 +0800
LV Status available
# open 1
LV Size <32.48 GiB
Current LE 8314
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 251:1

--- Logical volume ---
LV Path /dev/opencloudos/root
LV Name root
VG Name opencloudos
LV UUID pmaZSg-3JnI-yXfm-8N5Z-arW7-mstK-P0Atir
LV Write Access read/write
LV Creation host, time dev215, 2024-11-01 10:35:14 +0800
LV Status available
# open 1
LV Size <66.52 GiB
Current LE 17029
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 251:0

root@dev215 [/data]# lvextend -L +100G /dev/opencloudos/root
Size of logical volume opencloudos/root changed from <66.52 GiB (17029 extents) to <166.52 GiB (42629 extents).
Logical volume opencloudos/root successfully resized.

root@dev215 [/data]# lvdisplay
--- Logical volume ---
LV Path /dev/opencloudos/home
LV Name home
VG Name opencloudos
LV UUID Mrv5DQ-JeQx-mKdM-Vn9u-qOtw-Yl9k-TbkTjH
LV Write Access read/write
LV Creation host, time dev215, 2024-11-01 10:35:13 +0800
LV Status available
# open 1
LV Size <32.48 GiB
Current LE 8314
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 251:1

--- Logical volume ---
LV Path /dev/opencloudos/root
LV Name root
VG Name opencloudos
LV UUID pmaZSg-3JnI-yXfm-8N5Z-arW7-mstK-P0Atir
LV Write Access read/write
LV Creation host, time dev215, 2024-11-01 10:35:14 +0800
LV Status available
# open 1
LV Size <166.52 GiB
Current LE 42629
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 251:0

root@dev215 [/data]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 opencloudos lvm2 a-- <199.00g 0

扩容文件系统

ext*

1
resize2fs /dev/vdb1

xfs

1
2
yum install -y xfsprogs
xfs_growfs /mnt

Ref

  • https://web.archive.org/web/20241113015538/https://help.aliyun.com/zh/ecs/user-guide/extend-the-partitions-and-file-systems-of-disks-on-a-linux-instance
  • https://web.archive.org/web/20241113015851/https://help.aliyun.com/zh/ecs/use-cases/extend-an-lv-by-using-lvm