#############################################################宿主机操作#############################################################
1.关闭虚拟机
virsh shutdown centos7.044-clone
进入vfs目录,查看文件系统
cd /home/data/kvm/vfs
2.查看文件系统
virt-filesystems --long -h --all -a centos7.0.qcow2vm1-clone-clone-2-clone
注意:如果没有virt-filesystems这个命令,可以这样安装:
yum search virt-filesystems
yum install libguestfs-tools.noarch -y
3.备份
cp centos7.0.qcow2vm1-clone-clone-2-clone centos7.0.qcow2vm1-clone-clone-2-clone.bak
cp /etc/libvirt/qemu/centos7.044-clone.xml /etc/libvirt/qemu/centos7.044-clone.xml.bak
4.查看原虚拟机的磁盘大小信息
qemu-img info centos7.0.qcow2vm1-clone-clone-2-clone
5.扩大300G,扩到500G
qemu-img resize centos7.0.qcow2vm1-clone-clone-2-clone +300G
6.再次备份
cp centos7.0.qcow2vm1-clone-clone-2-clone centos7.0.qcow2vm1-clone-clone-2-clone.ori
7.扩大分区
virt-resize -expand /dev/sda2 centos7.0.qcow2vm1-clone-clone-2-clone.ori centos7.0.qcow2vm1-clone-clone-2-clone
8.检查磁盘有没有扩好
qemu-img info centos7.0.qcow2vm1-clone-clone-2-clone
9.检查文件系统有没有扩好
virt-filesystems --long -h --all -a centos7.0.qcow2vm1-clone-clone-2-clone
10.扩大成功,开启虚拟机
virsh start centos7.044-clone
#############################################################宿主机操作#############################################################
#############################################################宿主机操作输出#############################################################
[root@promote ~]# virsh shutdown centos7.044-clone
域 centos7.044-clone 被关闭
[root@promote ~]# cd /home/data/kvm/vfs/
[root@promote vfs]# virt-filesystems --long -h --all -a centos7.0.qcow2vm1-clone-clone-2-clone
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 1.0G -
/dev/centos/home filesystem xfs - - 145G -
/dev/centos/root filesystem xfs - - 50G -
/dev/centos/swap filesystem swap - - 3.9G -
/dev/centos/home lv - - - 145G /dev/centos
/dev/centos/root lv - - - 50G /dev/centos
/dev/centos/swap lv - - - 3.9G /dev/centos
/dev/centos vg - - - 199G /dev/sda2
/dev/sda2 pv - - - 199G -
/dev/sda1 partition - - 83 1.0G /dev/sda
/dev/sda2 partition - - 8e 199G /dev/sda
/dev/sda device - - - 200G -
[root@promote vfs]# cp centos7.0.qcow2vm1-clone-clone-2-clone centos7.0.qcow2vm1-clone-clone-2-clone.bak
[root@promote vfs]# qemu-img info centos7.0.qcow2vm1-clone-clone-2-clone
image: centos7.0.qcow2vm1-clone-clone-2-clone
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 199G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
[root@promote vfs]# qemu-img resize centos7.0.qcow2vm1-clone-clone-2-clone +300G
Image resized.
[root@promote vfs]# cp centos7.0.qcow2vm1-clone-clone-2-clone centos7.0.qcow2vm1-clone-clone-2-clone.ori
[root@promote vfs]# virt-resize -expand /dev/sda2 centos7.0.qcow2vm1-clone-clone-2-clone.ori centos7.0.qcow2vm1-clone-clone-2-clone
[ 0.0] Examining centos7.0.qcow2vm1-clone-clone-2-clone.ori
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
**********
Summary of changes:
/dev/sda1: This partition will be left alone.
/dev/sda2: This partition will be resized from 199.0G to 499.0G. The LVM
PV on /dev/sda2 will be expanded using the ‘pvresize’ method.
**********
[ 7.5] Setting up initial partition table on centos7.0.qcow2vm1-clone-clone-2-clone
[ 8.2] Copying /dev/sda1
[ 10.0] Copying /dev/sda2
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[2687.0] Expanding /dev/sda2 using the ‘pvresize’ method
Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.
[root@promote vfs]# qemu-img info centos7.0.qcow2vm1-clone-clone-2-clone
image: centos7.0.qcow2vm1-clone-clone-2-clone
file format: qcow2
virtual size: 500G (536870912000 bytes)
disk size: 199G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
[root@promote vfs]# virt-filesystems --long -h --all -a centos7.0.qcow2vm1-clone-clone-2-clone
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 1.0G -
/dev/centos/home filesystem xfs - - 145G -
/dev/centos/root filesystem xfs - - 50G -
/dev/centos/swap filesystem swap - - 3.9G -
/dev/centos/home lv - - - 145G /dev/centos
/dev/centos/root lv - - - 50G /dev/centos
/dev/centos/swap lv - - - 3.9G /dev/centos
/dev/centos vg - - - 499G /dev/sda2
/dev/sda2 pv - - - 499G -
/dev/sda1 partition - - 83 1.0G /dev/sda
/dev/sda2 partition - - 8e 499G /dev/sda
/dev/sda device - - - 500G -
[root@promote vfs]# virsh start centos7.044-clone
域 centos7.044-clone 已开始
#############################################################宿主机操作输出#############################################################
#############################################################虚机操作#############################################################
1.查看fdisk
fdisk -l
2.使用vgscan查询物理卷,可以查到本机物理卷名称为“centos”
vgscan
3.使用新增物理卷扩展cl:(vgdisplay 查看vg的名称和路径)
vgdisplay
4.扩展lv (lvdisplay 查看lv 逻辑卷的路径和名称)
lvdisplay
5.扩展lv
lvextend -L +50G /dev/mapper/centos-root
lvextend -L +250G /dev/mapper/centos-home
6.接着用 dh –f,发现实际容量并没有变化,因为我们的系统还不认识刚刚添加进来的磁盘的文件系统,所以还需要对文件系统进行扩容。
df -h
7.键入:xfs_growfs 加上要扩展的分区名,或者 resize2fs – f 加上要扩展的分区名
xfs_growfs /dev/mapper/centos-root
xfs_growfs /dev/mapper/centos-home
#############################################################虚机操作#############################################################
#############################################################虚机操作输出#############################################################
[root@localhost ~]# fdisk -l
磁盘 /dev/vda:536.9 GB, 536870912000 字节,1048576000 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000d03e6
设备 Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 1048573567 523237184 8e Linux LVM
磁盘 /dev/mapper/centos-root:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:4160 MB, 4160749568 字节,8126464 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-home:155.8 GB, 155818393600 字节,304332800 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@localhost ~]# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
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 <499.00 GiB
PE Size 4.00 MiB
Total PE 127743
Alloc PE / Size 50942 / 198.99 GiB
Free PE / Size 76801 / 300.00 GiB
VG UUID sJXcYL-amzu-3274-RyBY-kfIk-XCFL-R9Zk22
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID xRAVBy-65WL-nryd-atEy-J30e-0Eiw-nkFj5t
LV Write Access read/write
LV Creation host, time localhost, 2020-06-02 17:28:41 +0800
LV Status available
# open 2
LV Size <3.88 GiB
Current LE 992
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID WQnlYt-kQxY-b01v-zVO6-VHnd-ME67-Ge3bwr
LV Write Access read/write
LV Creation host, time localhost, 2020-06-02 17:28:42 +0800
LV Status available
# open 1
LV Size <145.12 GiB
Current LE 37150
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID u6tH91-RUVn-Busd-Y0CB-0hPV-YyRT-VAZzH8
LV Write Access read/write
LV Creation host, time localhost, 2020-06-02 17:28:42 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@localhost ~]# lvextend -L +50G /dev/mapper/centos-root
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 100.00 GiB (25600 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]# lvextend -L +250G /dev/mapper/centos-home
Size of logical volume centos/home changed from <145.12 GiB (37150 extents) to <395.12 GiB (101150 extents).
Logical volume centos/home successfully resized.
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.8M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 47G 3.3G 94% /
/dev/vda1 1014M 152M 863M 15% /boot
/dev/mapper/centos-home 146G 139G 7.0G 96% /home
tmpfs 1.6G 0 1.6G 0% /run/user/1001
tmpfs 1.6G 0 1.6G 0% /run/user/0
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 26214400
[root@localhost ~]# xfs_growfs /dev/mapper/centos-home
meta-data=/dev/mapper/centos-home isize=512 agcount=4, agsize=9510400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=38041600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=18575, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 38041600 to 103577600
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.8M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root 100G 47G 54G 47% /
/dev/vda1 1014M 152M 863M 15% /boot
/dev/mapper/centos-home 396G 139G 257G 35% /home
tmpfs 1.6G 0 1.6G 0% /run/user/1001
tmpfs 1.6G 0 1.6G 0% /run/user/0
[root@localhost ~]#
#############################################################虚机操作输出#############################################################
Linux virsh KVM虚机扩容硬盘
于 2022-08-18 10:55:56 首次发布