需求场景
我们在使用虚拟机时,可能会出现磁盘空间不够用的情况。
首先,我们要扩展虚拟机的可用磁盘空间。如图所示,当前容量已经占用43.3GB,我们扩展到256GB
打开虚拟机发现,也无法直接使虚拟机磁盘空间相应的增加(sda仍然为64G,没有完全使用到我们分配的256G),还需要执行分区指令并且重启机器。
shell
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 256G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 63G 0 part
├─openeuler-root 253:0 0 39.7G 0 lvm /
├─openeuler-swap 253:1 0 3.9G 0 lvm [SWAP]
└─openeuler-home 253:2 0 19.4G 0 lvm /home
sr0 11:0 1 3.5G 0 rom
不关机扩容的方法,将在下面演示。
扩容流程
扩展分区
分区操作操作不当有可能导致数据丢失,建议操作前保存快照。
对/dev/sda进行分区,执行
shell
fdisk /dev/sda
输入p,打印当前分区表
shell
Command (m for help): p
Disk /dev/sda: 256 GiB, 274877906944 bytes, 536870912 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: dos
Disk identifier: 0xa4b00bb9
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 134217727 132118528 63G 8e Linux LVM
删除/dev/sda2分区,输入d,然后输入2
shell
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
删除分区后,重新创建新分区,输入n,然后一路敲回车,询问是否删除签名,选择否
shell
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (2-4, default 2):
First sector (2099200-536870911, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-536870911, default 536870911):
Created a new partition 2 of type 'Linux' and of size 255 GiB.
Partition #2 contains a LVM2_member signature.
Do you want to remove the signature? [Y]es/[N]o: n
The signature will be removed by a write command.
再次输入p,发现已经完成分区,且扩容到了256G
shell
Command (m for help): p
Disk /dev/sda: 256 GiB, 274877906944 bytes, 536870912 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: dos
Disk identifier: 0xa4b00bb9
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 536870911 534771712 255G 83 Linux
Filesystem/RAID signature on partition 2 will be wiped.
最后输入w,完成写入
shell
Command (m for help): w
The partition table has been altered.
Syncing disks.
此时,输入lsblk,发现sda2大小更新了,但是其挂载的目录大小仍然没有更新
shell
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 256G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 255G 0 part
├─openeuler-root 253:0 0 39.7G 0 lvm /
├─openeuler-swap 253:1 0 3.9G 0 lvm [SWAP]
└─openeuler-home 253:2 0 19.4G 0 lvm /home
sr0 11:0 1 3.5G 0 rom
更新物理卷分区
执行命令pvresize /dev/sda2
shell
[root@localhost ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
随后执行pvdisplay查看pv(物理卷),已经正常扩容
shell
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name openeuler
PV Size <255.00 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 65279
Free PE 49152
Allocated PE 16127
PV UUID KtV3ym-hIBa-0FK4-zc00-rJyU-EdZP-ekjIJl
随后执行vgdisplay查看vg(逻辑卷组),已经正常扩容
shell
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name openeuler
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
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 <255.00 GiB
PE Size 4.00 MiB
Total PE 65279
Alloc PE / Size 16127 / <63.00 GiB
Free PE / Size 49152 / 192.00 GiB
VG UUID x9qfXc-DeIs-RPGa-qnv6-zn2Z-Gn88-xgkrbo
随后执行lvdisplay查看lv(逻辑卷),还没有扩容
shell
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/openeuler/swap
LV Name swap
VG Name openeuler
LV UUID WP4Zwq-S24S-nkdQ-r4D1-8bd6-vCk1-njx21g
LV Write Access read/write
LV Creation host, time localhost, 2023-11-23 05:57:24 +0800
LV Status available
# open 2
LV Size <3.93 GiB
Current LE 1005
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/openeuler/home
LV Name home
VG Name openeuler
LV UUID dC5j80-nMm0-fkbI-n9oV-mNOe-Fvja-Sj5Mf3
LV Write Access read/write
LV Creation host, time localhost, 2023-11-23 05:57:24 +0800
LV Status available
# open 1
LV Size <19.38 GiB
Current LE 4961
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/openeuler/root
LV Name root
VG Name openeuler
LV UUID XcZDI5-1X7Y-hDX4-C0dT-zN6w-rUMV-ujqYhi
LV Write Access read/write
LV Creation host, time localhost, 2023-11-23 05:57:25 +0800
LV Status available
# open 1
LV Size 39.69 GiB
Current LE 10161
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
更新lv分区
为/增加空闲的70%的空间
shell
lvextend -l +70%FREE /dev/mapper/openeuler-root
shell
[root@localhost ~]# lvextend -l +70%FREE /dev/mapper/openeuler-root
Size of logical volume openeuler/root changed from 39.69 GiB (10161 extents) to 174.09 GiB (44568 extents).
Logical volume openeuler/root successfully resized.
为/home增加剩余的空间
shell
lvextend -l +100%FREE /dev/mapper/openeuler-home
再次查看lv大小,已经完全分配好
shell
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/openeuler/swap
LV Name swap
VG Name openeuler
LV UUID WP4Zwq-S24S-nkdQ-r4D1-8bd6-vCk1-njx21g
LV Write Access read/write
LV Creation host, time localhost, 2023-11-23 05:57:24 +0800
LV Status available
# open 2
LV Size <3.93 GiB
Current LE 1005
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/openeuler/home
LV Name home
VG Name openeuler
LV UUID dC5j80-nMm0-fkbI-n9oV-mNOe-Fvja-Sj5Mf3
LV Write Access read/write
LV Creation host, time localhost, 2023-11-23 05:57:24 +0800
LV Status available
# open 1
LV Size <76.98 GiB
Current LE 19706
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/openeuler/root
LV Name root
VG Name openeuler
LV UUID XcZDI5-1X7Y-hDX4-C0dT-zN6w-rUMV-ujqYhi
LV Write Access read/write
LV Creation host, time localhost, 2023-11-23 05:57:25 +0800
LV Status available
# open 1
LV Size 174.09 GiB
Current LE 44568
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0