44-KVM虚拟化-存储管理和磁盘扩容

时间:2023-01-02 22:04:44

KVM存储模式

基于文件系统的存储

  • dir: Filesystem Directory 需要有挂载点的文件系统
  • fs: Pre-Formatted Block Device 无需挂载的文件系统,如:位于SAN存储的文件系统,可支持多个主机同时访问,而本地文件系统不支持
  • netfs: Network Exported Directory 网络文件系统,比如:NFS,SAMBA等

基于设备的存储 - 无需文件系统,性能更好,但可管理性差,无法实现快照

  • Disk: Physical Disk Device
  • Iscsi: isCSI Target
  • logical:LVM Volume Group

虚拟磁盘类型

  • 固定Fixed

在配置时,指定磁盘大小
不管在虚拟磁盘上实际存储多少数据,都将占用相同大小宿主机的磁盘空间
  • 动态Dynamic

初始空间占用小
随着空间的使用逐渐增长到最大容量,但是只根据需求使用更多的空间
  • 差异Differencing

因为创建是差异磁盘,所以只保存变更的数据
例如,将操作系统安装在父盘,然后创建差异化磁盘来执行进一步配置

44-KVM虚拟化-存储管理和磁盘扩容

虚拟镜像文件格式

KVM 上的磁盘镜像格式包括:

  • raw

此为默认磁盘格式,但并是一种真正的磁盘格式,而是代表虚拟机所使用的原始镜像,它并不存储元数据,因此可作为保证虚拟机兼容性的候选方案。然而,也正因为它不存储元数据,因此不支持某些高级特往,比如快照和压缩等格式简单,容易转换为其他的格式。
如果主机文件系统允许,raw 文件可以是预分配(pre-allocated)或 稀疏(sparse)。稀疏文件根据需求分配主机磁盘空间,因此它是一种精简配置形式(thin provisioning)。预分配文件的所有空间需要被预先分配,但它比稀疏文件性能好。当对磁盘 I/O 性能要求非常高,而且通常不需要通过网络传输镜像文件时,可以使用 raw文件
优点 : 性能好
缺点 : 空间占用大,功能较少,生产不推荐使用
  • cow

copy-on-write格式,昙花一现
  • qcow

QEMU早期的copy-on-write格式,过渡性方案
  • qcow2
qcow2 镜像文件提供许多高级磁盘镜像特征,如快照、压缩及加密。它们可以用来代表通过模板镜像创建的虚拟机。因为只有虚拟机写入的扇区部分才会分配在镜像中,所以 qcow2 文件的网络传输效率较高。RHEL 7.0 及更新版本支持 qcow2 v3 镜像文件格式按需进行分配磁盘空间,不管文件系统是否支持
优点 :
空间节约,功能丰富
支持快照
支持zlib的磁盘压缩
支持AES的加密
缺点 : 性能较差,生产推荐使用

案例:查看不同磁盘文件格式支持选项
#raw
[root@ubuntu2204 ~]#qemu-img create -f raw -o ?
Supported raw options:
size=<size> - Virtual disk size

The protocol level may support further options.
Specify the target filename to include those options.

#qcow2
[root@ubuntu2204 ~]#qemu-img create -f qcow2 -o ?
Supported qcow2 options:
backing_file=<str> - File name of a base image
backing_fmt=<str> - Image format of the base image
cluster_size=<size> - qcow2 cluster size
compat=<str> - Compatibility level (v2 [0.10] or v3 [1.1])
compression_type=<str> - Compression method used for image cluster compression
data_file=<str> - File name of an external data file
data_file_raw=<bool (on/off)> - The external data file must stay valid as a raw image
encrypt.cipher-alg=<str> - Name of encryption cipher algorithm
encrypt.cipher-mode=<str> - Name of encryption cipher mode
encrypt.format=<str> - Encrypt the image, format choices: 'aes', 'luks'
encrypt.hash-alg=<str> - Name of encryption hash algorithm
encrypt.iter-time=<num> - Time to spend in PBKDF in milliseconds
encrypt.ivgen-alg=<str> - Name of IV generator algorithm
encrypt.ivgen-hash-alg=<str> - Name of IV generator hash algorithm
encrypt.key-secret=<str> - ID of secret providing qcow AES key or LUKS passphrase
encryption=<bool (on/off)> - Encrypt the image with format 'aes'. (Deprecated in favor of encrypt.format=aes)
extended_l2=<bool (on/off)> - Extended L2 tables
lazy_refcounts=<bool (on/off)> - Postpone refcount updates
preallocation=<str> - Preallocation mode (allowed values: off, metadata, falloc, full)
refcount_bits=<num> - Width of a reference count entry in bits
size=<size> - Virtual disk size

The protocol level may support further options.
Specify the target filename to include those options.
  • vmdk

VMware环境当中默认使用的磁盘格式
  • vhd/vhdx

微软默认采用的文件格式
  • vdi

VirtualBox 采用的文件格式

查看KVM支持的磁盘格式

[root@ubuntu2204 ~]#qemu-img --help|grep Support
Supported formats: blkdebug blklogwrites blkverify bochs cloop compress copy-before-write copy-on-read dmg file ftp ftps gluster host_cdrom host_device http https iscsi iser luks nbd null-aio null-co nvme parallels preallocate qcow qcow2 qed quorum raw rbd replication ssh throttle vdi vhdx vmdk vpc vvfat

使用qemu-img管理虚拟磁盘文件

#qemu-img 包括以下子命令
check #检查完整性
create #创建镜像
commit #提交更改
compare #比较
convert #转换
info #获得信息
map #映射
snapshot #快照管理
rebase #在已有的镜像的基础上创建新的镜像
resize #调整大小
amend #修订镜像格式选项

#格式:qemu-img create [-q] [--object objectdef] [-f fmt] [-b backing_file] [-F backing_fmt] [-u] [-o options] filename [size]
  • 案例:创建raw格式文件
#案例1:创建raw格式非稀疏文件
[root@ubuntu2204 ~]#dd if=/dev/zero of=/opt/vm2.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 6.2694 s, 171 MB/s
[root@ubuntu2204 ~]#qemu-img info /opt/vm2.img
image: /opt/vm2.img
file format: raw
virtual size: 1 GiB (1073741824 bytes) --> 显示1G
disk size: 1 GiB --> 实际占用磁盘1G

#案例2:创建raw格式稀疏文件
[root@ubuntu2204 ~]#dd if=/dev/zero of=/opt/vm3.img bs=1M count=0 seek=1024
0+0 records in
0+0 records out
0 bytes copied, 0.000217691 s, 0.0 kB/s
[root@ubuntu2204 ~]#qemu-img info /opt/vm3.img
image: /opt/vm3.img
file format: raw
virtual size: 1 GiB (1073741824 bytes) --> 显示1G
disk size: 0 B --> 实际占用磁盘1字节

#案例3:指定将非稀疏文件复制为稀疏格式文件
[root@ubuntu2204 ~]#cp --sparse=always /opt/vm2.img /opt/vm2.img.bk
[root@ubuntu2204 ~]#qemu-img info /opt/vm2.img
image: /opt/vm2.img
file format: raw
virtual size: 1 GiB (1073741824 bytes)
disk size: 1 GiB
[root@ubuntu2204 ~]#qemu-img info /opt/vm2.img.bk
image: /opt/vm2.img.bk
file format: raw
virtual size: 1 GiB (1073741824 bytes)
disk size: 0 B

#案例4:指定将稀疏文件复制为非稀疏格式文件
[root@ubuntu2204 ~]#cp --sparse=never /opt/vm3.img /opt/vm3.img.bk
[root@ubuntu2204 ~]#qemu-img info /opt/vm3.img
image: /opt/vm3.img
file format: raw
virtual size: 1 GiB (1073741824 bytes)
disk size: 0 B
[root@ubuntu2204 ~]#qemu-img info /opt/vm3.img.bk
image: /opt/vm3.img.bk
file format: raw
virtual size: 1 GiB (1073741824 bytes)
disk size: 1 GiB
  • 案例:创建qcow2格式文件
#qcow2 格式选项
backing_file #指定后端镜像文件
backing_fmt #设置后端镜像的镜像格式
cluster_size #设置镜像中的簇大小,取值在512到2M之间,默认值为64K
preallocation #设置镜像文件空间的预分配模式
encryption #用于设置加密

[root@ubuntu2204 ~]#qemu-img create -f qcow2 /opt/vm1.img 2g
Formatting '/opt/vm1.img', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=2147483648 lazy_refcounts=off refcount_bits=16
[root@ubuntu2204 ~]#qemu-img info /opt/vm1.img
image: /opt/vm1.img
file format: qcow2
virtual size: 2 GiB (2147483648 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
  • 案例:检查虚拟磁盘
*对于关机状态的虚拟机磁盘,可以检查文件错误
[root@centos8 ~]#virsh list
Id Name State
----------------------------------------------------
2 centos8 running
[root@centos8 ~]#qemu-img check /var/lib/libvirt/images/centos8.qcow2
qemu-img: Could not open '/var/lib/libvirt/images/centos8.qcow2': Failed to get
shared "write" lock
Is another process using the image [/var/lib/libvirt/images/centos8.qcow2]?
[root@centos8 ~]#virsh suspend centos8
Domain centos8 suspended
[root@centos8 ~]#virsh list
Id Name State
----------------------------------------------------
2 centos8 paused
[root@centos8 ~]#qemu-img check /var/lib/libvirt/images/centos8.qcow2
qemu-img: Could not open '/var/lib/libvirt/images/centos8.qcow2': Failed to get
shared "write" lock
Is another process using the image [/var/lib/libvirt/images/centos8.qcow2]?
[root@centos8 ~]#qemu-img check /var/lib/libvirt/images/centos7.qcow2
No errors were found on the image.
25572/327680 = 7.80% allocated, 1.44% fragmented, 0.00% compressed clusters
Image end offset: 1676869632

磁盘预分配策略

raw 文件的预分配策略和文件系统是否支持有关,而qcow2则无关

  • off

此为缺省策略,即不使用预分配策略,预分配后的虚拟磁盘占用空间很小,不属于稀疏映像类型,生成的磁盘文件占用空间很小
  • metadata

只预分配元数据(metadata),预分配后的磁盘文件属于稀疏映像类型,相当于vmware中的磁盘置备选项: Thin Provision(精简配置)
生成的磁盘文件为稀疏格式,实际占用的空间比off策略稍大一些
  • falloc

分配文件的块并标识它们的状态为未初始化,即只分配空间,但不置零(不格式化). 预分配后的虚拟磁盘属于非稀疏映像类型,相对full模式来说,创建虚拟磁盘的速度要快很多,相当于vmware中的磁盘置备选项:厚置备延迟置零
生成的磁盘文件实际占用的空间和分配的空间相同大小
  • full

分配所有磁盘空间并置零,预分配后的虚拟磁盘属于非稀疏映像类型,创建最慢,相当于vmware中的磁盘置备选项: 厚置备置零
生成的磁盘文件实际占用的空间和分配的空间相同大小

案例:创建预分配置策略

#案例1:默认开启默认预分配策略 - 稀疏格式,根据实际需要自动分配磁盘空间
[root@ubuntu2204 ~]#qemu-img create -f qcow2 /opt/vm1.img 2g
Formatting '/opt/vm1.img', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=2147483648 lazy_refcounts=off refcount_bits=16
[root@ubuntu2204 ~]#qemu-img info /opt/vm1.img
image: /opt/vm1.img
file format: qcow2
virtual size: 2 GiB (2147483648 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false

#案例2:指定off关闭预分配
[root@ubuntu2204 ~]#qemu-img create -f qcow2 /opt/vm2.img 1g -o preallocatinotallow=off
Formatting '/opt/vm2.img', fmt=qcow2 cluster_size=65536 extended_l2=off preallocation=off compression_type=zlib size=1073741824 lazy_refcounts=off refcount_bits=16
[root@ubuntu2204 ~]#qemu-img info /opt/vm2.img
image: /opt/vm2.img
file format: qcow2
virtual size: 1 GiB (1073741824 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false

#案例3:指定预分配metadata
[root@ubuntu2204 ~]#qemu-img create -f qcow2 /opt/vm3.img 1g -o preallocatinotallow=metadata
Formatting '/opt/vm3.img', fmt=qcow2 cluster_size=65536 extended_l2=off preallocation=metadata compression_type=zlib size=1073741824 lazy_refcounts=off refcount_bits=16
[root@ubuntu2204 ~]#qemu-img info /opt/vm3.img
image: /opt/vm3.img
file format: qcow2
virtual size: 1 GiB (1073741824 bytes)
disk size: 324 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false

#案例4:指定预分配falloc
[root@ubuntu2204 ~]#qemu-img create -f qcow2 /opt/vm4.img 1g -o preallocatinotallow=falloc
Formatting '/opt/vm4.img', fmt=qcow2 cluster_size=65536 extended_l2=off preallocation=falloc compression_type=zlib size=1073741824 lazy_refcounts=off refcount_bits=16
[root@ubuntu2204 ~]#qemu-img info /opt/vm4.img
image: /opt/vm4.img
file format: qcow2
virtual size: 1 GiB (1073741824 bytes)
disk size: 1 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false

#文件系统显示大小
[root@ubuntu2204 ~]#ll -h /opt/vm*
-rw-r--r-- 1 root root 193K Jan 1 17:40 /opt/vm1.img
-rw-r--r-- 1 root root 193K Jan 1 18:04 /opt/vm2.img
-rw-r--r-- 1 root root 1.1G Jan 1 18:08 /opt/vm3.img
-rw-r--r-- 1 root root 1.1G Jan 1 18:08 /opt/vm4.img
#查看真实大小
[root@ubuntu2204 ~]#du -h /opt/vm*
196K /opt/vm1.img
196K /opt/vm2.img
324K /opt/vm3.img
1.1G /opt/vm4.img

虚拟磁盘格式转换

#qemu-img 可以将不同格式的虚拟磁盘文件进行格式转化

#案例:将vmdk转化为raw 和qcow2格式
[root@centos8 ~]#qemu-img info CentOS8.2.vmdk
image: CentOS8.2.vmdk
file format: vmdk
virtual size: 200G (214748364800 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
cid: 2898578192
parent cid: 4294967295
create type: monolithicSparse
extents:
[0]:
virtual size: 214748364800
filename: CentOS8.2.vmdk
cluster size: 65536
format:
#默认转化为raw格式
[root@centos8 ~]#qemu-img convert CentOS8.2.vmdk CentOS8.2.img
#比较大小
[root@centos8 ~]#ll -h CentOS8.2.vmdk CentOS8.2.img
-rw-r--r-- 1 root root 1.6G Sep 20 16:00 CentOS8.2.vmdk
-rw-r--r-- 1 root root 200G Sep 20 16:10 CentOS8.2.img
[root@centos8 ~]#du -h CentOS8.2.vmdk CentOS8.2.img
1.6G CentOS8.2.vmdk
1.5G CentOS8.2.img
[root@centos8 ~]#qemu-img info CentOS8.2.img
image: CentOS8.2.img
file format: raw
virtual size: 200G (214748364800 bytes)
disk size: 1.4G
[root@centos8 ~]#mv CentOS8.2.img /var/lib/libvirt/images/
[root@centos8 ~]#virt-install --import --name=centos8-test2 --vcpus=1 --ram=2048
--disk bus=scsi,path=/var/lib/libvirt/images/CentOS8.2.img --network
network=default --graphics vnc,listen=0.0.0.0 --os-type=Linux --osvariant=
centos8 --noautoconsole --boot hd
#转化为qcow2格式
[root@centos8 ~]#qemu-img convert -f vmdk -O qcow2 CentOS8.2.vmdk
CentOS8.2.qcow2
#比较大小
[root@centos8 ~]#ll -h CentOS8.2.vmdk CentOS8.2.qcow2
-rw-r--r-- 1 root root 1.6G Sep 20 16:00 CentOS8.2.vmdk
-rw-r--r-- 1 root root 1.6G Sep 20 16:12 CentOS8.2.qcow2
[root@centos8 ~]#du -h CentOS8.2.vmdk CentOS8.2.qcow2
1.6G CentOS8.2.vmdk
1.6G CentOS8.2.qcow2
[root@centos8 ~]#qemu-img info CentOS8.2.qcow2
image: CentOS8.2.qcow2
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 1.5G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
[root@centos8 ~]#mv CentOS8.2.qcow2 /var/lib/libvirt/images/
[root@centos8 ~]#virt-install --import --name=centos8-test3 --vcpus=1 --ram=2048
--disk bus=scsi,path=/var/lib/libvirt/images/CentOS8.2.qcow2 --network
network=default --graphics vnc,listen=0.0.0.0 --os-type=Linux --osvariant=
centos8 --noautoconsole --boot hd

调整虚拟磁盘大小

*做虚拟磁盘调整之前需要注意以下几点:

  • 操作之前,一定要做好数据备份
  • 增加文件大小后,需要在客户机中使用fdisk、parted等分区工具进行相应的操作才能真正让客户机使用到增加后的镜像空间。
  • 缩小镜像之前,要在客户机中保证里面的文件系统有空余空间,否则会数据丢失。另外xfs文件系统不支持缩减
  • qcow2不支持缩小镜像的操作

案例:扩展虚拟磁盘

#思路:扩展分区-扩容物理卷-扩容逻辑卷组-扩容指定逻辑卷
[root@ubuntu2204 ~]#qemu-img info /var/lib/libvirt/images/centos7-mooreyxia.qcow2
image: /var/lib/libvirt/images/centos7-mooreyxia.qcow2
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 1.6 GiB
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK ICOUNT
1 centos7-s1 0 B 2023-01-01 19:01:27 00:00:00.000 0
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
extended l2: false
[root@ubuntu2204 ~]#du -sh /var/lib/libvirt/images/centos7-mooreyxia.qcow2
1.6G /var/lib/libvirt/images/centos7-mooreyxia.qcow2

#扩容 - 增加10G空间
[root@ubuntu2204 ~]#qemu-img info /var/lib/libvirt/images/centos7-mooreyxia.qcow2
image: /var/lib/libvirt/images/centos7-mooreyxia.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 1.6 GiB
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK ICOUNT
1 centos7-s1 0 B 2023-01-01 19:01:27 00:00:00.000 0
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
extended l2: false

#qemu-img resize 扩展空间后,在虚拟机内还需执行如下操作才能最终扩容
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 20G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 9G 0 part
├─centos-root 253:0 0 8G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 908M 0 908M 0% /dev
tmpfs 919M 0 919M 0% /dev/shm
tmpfs 919M 8.6M 911M 1% /run
tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/mapper/centos-root 8.0G 1.3G 6.8G 16% /
/dev/vda1 1014M 153M 862M 15% /boot
tmpfs 184M 0 184M 0% /run/user/0

#重新创建/dev/vda2,容量使用所有空间,/dev/vda2扩容到20G
[root@localhost ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x000df5df

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 20971519 9436160 8e Linux LVM

Command (m for help): d
Partition number (1,2, default 2):
Partition 2 is deleted

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x000df5df

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x000df5df

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (2099200-41943039, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039):
Using default value 41943039
Partition 2 of type Linux and of size 19 GiB is set

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x000df5df

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 41943039 19921920 83 Linux

Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x000df5df

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 41943039 19921920 8e Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

#同步文件系统,扩展逻辑卷
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 20G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 19G 0 part
├─centos-root 253:0 0 8G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]

[root@localhost ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sr0
vda
├─vda1 xfs 1a55b4e7-3e79-4319-8fd0-7ceb3d1ab505 /boot
└─vda2 LVM2_member 0Sybbx-w3aS-ZdSN-1zQO-eXpw-0zqz-YcYT4V
├─centos-root xfs f1205cb4-8a06-44a8-bece-89e8610e5a77 /
└─centos-swap swap 7ccb94cc-3909-4af8-9abf-e1b63d627f52 [SWAP]

#当前物理卷空间并没有扩容,需要执行下面操作扩容PV
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <9.00g 0

[root@localhost ~]# pvresize --setphysicalvolumesize 19G /dev/vda2
WARNING: /dev/vda2: Overriding real size <19.00 GiB. You could lose data.
/dev/vda2: Requested size 19.00 GiB exceeds real size <19.00 GiB. Proceed? [y/n]: y
WARNING: /dev/vda2: Pretending size is 39845888 not 39843840 sectors.
Physical volume "/dev/vda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <19.00g 10.00g

#逻辑卷组被自动扩容
[root@localhost ~]# vgs -a
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0

#扩容指定逻辑卷
[root@localhost ~]# lvextend -r -l +100%free /dev/centos/root
Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to <18.00 GiB (4607 extents).
Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=524032 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2096128, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, versinotallow=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2096128 to 4717568
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <18.00g
swap centos -wi-ao---- 1.00g
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 20G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 19G 0 part
├─centos-root 253:0 0 18G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]

--------------------------------------------------------------------------------
#如果是分区不是逻辑卷,则执行如下操作
#如果是xfs文件系统
xfs_growfs /
#如果是ext
resize2fs /dev/vda2
----------------------------------------------------------------------------------

我是moore,大家一起加油!新年快乐!