997-扩展KVM虚拟机逻辑卷
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了997-扩展KVM虚拟机逻辑卷相关的知识,希望对你有一定的参考价值。
由于要做云原生的实验,虚拟机逻辑卷需要扩容一下,这里写一下分享
借个图说明:
LVM: Logical Volume Manager 可以允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小,允许在多个物理设备间重新组织文件系统
LVM可以弹性的更改LVM的容量:通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备中的PE加到LV中以加大容量
实现过程:
- 将设备指定为物理卷
- 用一个或者多个物理卷来创建一个卷组,物理卷是用固定大小的物理区域(Physical Extent,PE)来定义的
- 在物理卷上创建的逻辑卷, 是由物理区域(PE)组成
- 可以在逻辑卷上创建文件系统并挂载
存放位置:
软链接:
/dev/mapper/VG_NAME-LV_NAME
/dev/VG_NAME/LV_NAME
下面是实现过程
#kvm宿主机
[root@ubuntu2204 images]#qemu-img resize ubuntu2204_210.qcow2 +40G
Image resized.
[root@ubuntu2204 images]#qemu-img info ubuntu2204_210.qcow2
image: ubuntu2204_210.qcow2
file format: qcow2
virtual size: 60 GiB (64424509440 bytes)
disk size: 6.38 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
extended l2: false
#进入虚拟机 (可以拉到最后对比一下扩展逻辑卷后)
[root@ubuntu2204 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1024M 0 rom
vda 252:0 0 60G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 1.8G 0 part /boot
└─vda3 252:3 0 18.2G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 10G 0 lvm /
#在对虚拟机扩容时候,由于linux系统没有对其磁盘信息进行更新,导致了磁盘实际容量和linux系统容量不一致
[root@ubuntu2204 ~]#fdisk -l
GPT PMBR size mismatch (41943039 != 125829119) will be corrected by write.
The backup GPT table is not on the end of the device.
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 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 type: gpt
Disk identifier: D384CE11-18E2-430D-81B5-7B76B8389C45
Device Start End Sectors Size Type
/dev/vda1 2048 4095 2048 1M Bios boot
/dev/vda2 4096 3719167 3715072 1.8G Linux filesystem
/dev/vda3 3719168 41940991 38221824 18.2G Linux filesystem
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 10 GiB, 10737418240 bytes, 20971520 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
#利用分区工具重置更新一下信息
[root@ubuntu2204 ~]#parted -l
Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:
Number Start End Size File system Flags
1 0.00B 10.7GB 10.7GB ext4
Warning: Not all of the space available to /dev/vda appears to be used, you can
fix the GPT to use all of the space (an extra 83886080 blocks) or continue with
the current setting?
Fix/Ignore? fix #这里选fix
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1904MB 1902MB ext4
3 1904MB 21.5GB 19.6GB
#之后就是增加新的分区,做成物理卷,向虚拟机自带的卷组种加物理卷,扩展逻辑卷
#增加新的分区
[root@ubuntu2204 ~]#fdisk /dev/vda
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.
This disk is currently in use - repartitioning is probably a bad idea.
Its recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): p
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 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 type: gpt
Disk identifier: D384CE11-18E2-430D-81B5-7B76B8389C45
Device Start End Sectors Size Type
/dev/vda1 2048 4095 2048 1M BIOS boot
/dev/vda2 4096 3719167 3715072 1.8G Linux filesystem
/dev/vda3 3719168 41940991 38221824 18.2G Linux filesystem
Command (m for help): m
Help:
GPT
M enter protective/hybrid MBR
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help): n
Partition number (4-128, default 4):
First sector (41940992-125829086, default 41940992):
Last sector, +/-sectors or +/-sizeK,M,G,T,P (41940992-125829086, default 125829086):
Created a new partition 4 of type Linux filesystem and of size 40 GiB.
Command (m for help): p
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 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 type: gpt
Disk identifier: D384CE11-18E2-430D-81B5-7B76B8389C45
Device Start End Sectors Size Type
/dev/vda1 2048 4095 2048 1M BIOS boot
/dev/vda2 4096 3719167 3715072 1.8G Linux filesystem
/dev/vda3 3719168 41940991 38221824 18.2G Linux filesystem
/dev/vda4 41940992 125829086 83888095 40G Linux filesystem
Command (m for help): w
The partition table has been altered.
Syncing disks.
[root@ubuntu2204 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1024M 0 rom
vda 252:0 0 60G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 1.8G 0 part /boot
├─vda3 252:3 0 18.2G 0 part
│ └─ubuntu--vg-ubuntu--lv 253:0 0 18.2G 0 lvm /
└─vda4 252:4 0 40G 0 part
#用新的分区做物理卷
[root@ubuntu2204 ~]#pvcreate /dev/vda4
Physical volume "/dev/vda4" successfully created.
[root@ubuntu2204 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/vda3 ubuntu-vg lvm2 a-- 18.22g 0
/dev/vda4 lvm2 --- 40.00g 40.00g
#将新的物理卷加入原有的逻辑卷组
[root@ubuntu2204 ~]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- 18.22g 0
[root@ubuntu2204 ~]#vgextend ubuntu-vg /dev/vda4
Volume group "ubuntu-vg" successfully extended
[root@ubuntu2204 ~]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 2 1 0 wz--n- <58.22g <40.00g
[root@ubuntu2204 ~]#vgdisplay ubuntu-vg
--- 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 <58.22 GiB
PE Size 4.00 MiB
Total PE 14904
Alloc PE / Size 4665 / 18.22 GiB
Free PE / Size 10239 / <40.00 GiB
VG UUID bTBtyC-plLU-B45H-0hPS-KFdF-HXq1-gzb673
#扩展卷组中的逻辑卷,利用所有空间
[root@ubuntu2204 ~]#lvextend -r -l +100%free /dev/mapper/ubuntu--vg-ubuntu--lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from 18.22 GiB (4665 extents) to <58.22 GiB (14904 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 8
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 15261696 (4k) blocks long.
[root@ubuntu2204 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- <58.22g
[root@ubuntu2204 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1024M 0 rom
vda 252:0 0 60G 0 disk
├─vda1 252:1 0 1M 0 part
├─vda2 252:2 0 1.8G 0 part /boot
├─vda3 252:3 0 18.2G 0 part
│ └─ubuntu--vg-ubuntu--lv 253:0 0 58.2G 0 lvm /
└─vda4 252:4 0 40G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 58.2G 0 lvm /
以上是关于997-扩展KVM虚拟机逻辑卷的主要内容,如果未能解决你的问题,请参考以下文章