在VMWare下扩展 ubuntu 20.04 磁盘分区(命令行扩展 /dev/mapper/ubuntu--vg-ubuntu--lv)

image-20220420151248081

虚拟机的磁盘分配小了,跑了点程序,空间开始不足。需要扩展下磁盘大小。

1 . VM里面调整虚拟磁盘大小

如上图所示。需要关闭虚拟机。

其实增加一块硬盘也行,在Ubuntu20.04的 LVM文件系统下,扩展当前磁盘和额外加一块硬盘没啥使用区别。尤其对虚拟机而言(实体机可能会碰到若干块磁盘性能不一的情况)。

2. 在Ubuntu guest系统内扩展LVM

a. 确认安装虚拟机的时候用了LVM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 391M 2.0M 389M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 19G 14G 4.6G 75% /
tmpfs 2.0G 16K 2.0G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda2 976M 207M 703M 23% /boot
/dev/loop0 56M 56M 0 100% /snap/core18/2284
/dev/loop3 44M 44M 0 100% /snap/snapd/14978
/dev/loop1 56M 56M 0 100% /snap/core18/2344
/dev/loop2 68M 68M 0 100% /snap/lxd/22753
/dev/loop4 68M 68M 0 100% /snap/lxd/22526
/dev/loop5 62M 62M 0 100% /snap/core20/1376
/dev/loop6 44M 44M 0 100% /snap/snapd/15177
/dev/loop7 62M 62M 0 100% /snap/core20/1405
tmpfs 391M 0 391M 0% /run/user/1000

​ 所以,确定是使用lvm,看名字,组名是ubuntu-vg,lv是ubuntu-lv。

b. 命令验证下

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
$sudo vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
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 <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID zI0Csz-Sii1-0fpN-dxtc-oRrr-yo8d-yqHHVk
$sudo lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu-vg/ubuntu-lv
LV Name ubuntu-lv
VG Name ubuntu-vg
LV UUID Gg0p3f-ZnVI-zZ76-0kUN-tjDe-LwXu-Gt2oQC
LV Write Access read/write
LV Creation host, time ubuntu-server, 2022-01-14 02:50:56 +0000
LV Status available
# open 1
LV Size <19.00 GiB
Current LE 4863
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

​ 基本OK。

c. 创建下物理卷

一般来说,物理卷对应传统的分区概念。作为LVM的”库存弹药“使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 查看下
$sudo fdisk -l
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: VMware Virtual S
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: C9FEEF7F-29EC-4678-9D77-33541B85AA3F
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 41940991 39839744 19G Linux filesystem
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 18.102 GiB, 20396900352 bytes, 39837696 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

​ 嗯。40G的硬盘上面3个分区后,还有18.102G的空闲空间。

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
# 创建个sda4,把空闲空间搞成一个分区好了
$sudo parted /dev/sda
GNU Parted 3.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1076MB 1074MB ext4
3 1076MB 21.5GB 20.4GB
21.5GB 42.9GB 21.5GB Free Space
(parted) mkpart primary ext4 21.5GB 100%
(parted) print free
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1076MB 1074MB ext4
3 1076MB 21.5GB 20.4GB
4 21.5GB 42.9GB 21.5GB ext4 primary
42.9GB 42.9GB 1032kB Free Space
(parted) quit
$sudo pvcreate /dev/sda4
Physical volume "/dev/sda4" successfully created.

​ 创建了一格ext4类型主分区。设备路径是/dev/sda4。

d. 加入到LVM的ubuntu-vg组里面(从命令上翻译就是:把ubuntu-vg组扩展到/dev/sda4上)

1
2
3
4
5
$sudo vgextend ubuntu-vg /dev/sda4
Volume group "ubuntu-vg" successfully extended
$sudo lvm lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <19.00 GiB (4863 extents) to 38.99 GiB (9982 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.

e. 最后扩展下文件系统,顺手再检查下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$sudo resize2fs -p /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 10221568 (4k) blocks long.
$df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 391M 2.0M 389M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 39G 14G 24G 36% /
tmpfs 2.0G 16K 2.0G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda2 976M 207M 703M 23% /boot
/dev/loop0 56M 56M 0 100% /snap/core18/2284
/dev/loop3 44M 44M 0 100% /snap/snapd/14978
/dev/loop1 56M 56M 0 100% /snap/core18/2344
/dev/loop2 68M 68M 0 100% /snap/lxd/22753
/dev/loop4 68M 68M 0 100% /snap/lxd/22526
/dev/loop5 62M 62M 0 100% /snap/core20/1376
/dev/loop6 44M 44M 0 100% /snap/snapd/15177
/dev/loop7 62M 62M 0 100% /snap/core20/1405
tmpfs 391M 0 391M 0% /run/user/1000

3. 对比

根文件系统从之前的:

1
/dev/mapper/ubuntu--vg-ubuntu--lv 19G 14G 4.6G 75% /

扩展为:

1
/dev/mapper/ubuntu--vg-ubuntu--lv 39G 14G 24G 36% /

enjoy~