- 博客/
kvm虚拟机磁盘扩容
虚拟机基础镜像制作完成后,磁盘容量及固定了,如果遇到需要扩容磁盘容量的情况,我们如何基于当前镜像快速扩容满足需求呢?本文主要介绍如何使用qemu-img resize
和virt-resise
工具对虚拟机镜像分区扩容,以及两者的区别。
qemu-img resize#
本文示例中CentOS7虚拟机镜像的系统分区为dos标准分区,基础镜像磁盘大小为50G
[root@kvm-1 sty_vm1]# qemu-img info img-centos7.qcow2
image: img-centos7.qcow2
file format: qcow2
virtual size: 50G (53687091200 bytes)
disk size: 2.1G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
qemu-img resize
扩容20G
[root@kvm-1 sty_vm1]# qemu-img resize img-centos7.qcow2 +20G
Image resized.
#磁盘镜像文件已经扩容
[root@kvm-1 sty_vm1]# qemu-img info img-centos7.qcow2
image: img-centos7.qcow2
file format: qcow2
virtual size: 70G (75161927680 bytes)
disk size: 2.1G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
虚拟机有三个分区,/dev/vda3挂载到根文件系统
[root@kvm-1 sty_vm1]# virt-filesystems --long -h --all -a img-centos7.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 1.0G -
/dev/sda2 filesystem swap - - 3.9G -
/dev/sda3 filesystem xfs - - 45G -
/dev/sda1 partition - - 83 1.0G /dev/sda
/dev/sda2 partition - - 82 3.9G /dev/sda
/dev/sda3 partition - - 83 45G /dev/sda
/dev/sda device - - - 50G -
#登录到虚拟机查看挂载点
[root@localhost] ~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 70G 0 disk
├─vda2 253:2 0 3.9G 0 part
├─vda3 253:3 0 45.1G 0 part /
└─vda1 253:1 0 1G 0 part /boot
虚拟机磁盘文件扩容20G之后,我们还需要扩容文件系统。
1.创建新分区的方式#
使用fdisk
新建一个主分区/dev/vda4
[root@kvm-1 sty_vm1]# virsh start sty_vm1
域 sty_vm1 已开始
[root@localhost] ~$ fdisk /dev/vda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):p
磁盘 /dev/vda:75.2 GB, 75161927680 字节,146800640 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0009a1e8
设备 Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 10227711 4064256 82 Linux swap / Solaris
/dev/vda3 10227712 104857599 47314944 83 Linux
命令(输入 m 获取帮助):n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
已选择分区 4
起始 扇区 (104857600-146800639,默认为 104857600): <-- 回车键使用默认值
将使用默认值 104857600
Last 扇区, +扇区 or +size{K,M,G} (104857600-146800639,默认为 146800639): <-- 回车键使用默认值
将使用默认值 146800639
分区 4 已设置为 Linux 类型,大小设为 20 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
[root@localhost] ~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 70G 0 disk
├─vda2 253:2 0 3.9G 0 part
├─vda3 253:3 0 45.1G 0 part /
└─vda1 253:1 0 1G 0 part /boot
# 刷新分区表,可以看到新建的磁盘分区
[root@localhost] ~$ partprobe
[root@localhost] ~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 70G 0 disk
├─vda4 253:4 0 20G 0 part
├─vda2 253:2 0 3.9G 0 part
├─vda3 253:3 0 45.1G 0 part /
└─vda1 253:1 0 1G 0 part /boot
[root@localhost] ~$ mkfs.xfs /dev/vda4
meta-data=/dev/vda4 isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost] ~$ mkdir /data0
[root@localhost] ~$ mount /dev/vda4 /data0
[root@localhost] ~$ df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 3.8G 8.5M 3.8G 1% /run
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/vda3 46G 1.9G 44G 5% /
/dev/vda1 1014M 293M 722M 29% /boot
tmpfs 772M 0 772M 0% /run/user/0
/dev/vda4 20G 33M 20G 1% /data0
2.扩容已有分区的方式#
基于上面的基础镜像,直接扩容根文件系统。
登录到虚拟机可以看到最后一个分区为/dev/vda3,并挂载到了根目录,可以直接使用fdisk重建分区vda3
[root@localhost] ~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 50G 0 disk
├─vda2 253:2 0 3.9G 0 part
├─vda3 253:3 0 45.1G 0 part /
└─vda1 253:1 0 1G 0 part /boot
[root@localhost] ~$ fdisk -l /dev/vda
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0009a1e8
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 10227711 4064256 82 Linux swap / Solaris
/dev/vda3 10227712 104857599 47314944 83 Linux
使用fdisk
重建主分区vda3
[root@kvm-1 sty_vm1]# qemu-img resize img-centos7.qcow2 +50G
Image resized.
[root@localhost] ~$ fdisk /dev/vda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):p
磁盘 /dev/vda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0009a1e8
设备 Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 10227711 4064256 82 Linux swap / Solaris
/dev/vda3 10227712 104857599 47314944 83 Linux
命令(输入 m 获取帮助):d
分区号 (1-3,默认 3):
分区 3 已删除
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
分区号 (3,4,默认 3):
起始 扇区 (10227712-209715199,默认为 10227712):
将使用默认值 10227712
Last 扇区, +扇区 or +size{K,M,G} (10227712-209715199,默认为 209715199):
将使用默认值 209715199
分区 3 已设置为 Linux 类型,大小设为 95.1 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
# 刷新分区表
[root@localhost] ~$ partprobe
[root@localhost] ~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 100G 0 disk
├─vda2 253:2 0 3.9G 0 part
├─vda3 253:3 0 95.1G 0 part /
└─vda1 253:1 0 1G 0 part /boot
# 磁盘分区已经扩容,但文件系统并没有
[root@localhost] ~$ df -Th /
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/vda3 xfs 46G 1.9G 44G 5% /
# 扩容文件系统
[root@localhost] ~$ xfs_growfs /dev/vda3
meta-data=/dev/vda3 isize=512 agcount=6, agsize=2301824 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=11828736, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=4495, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 11828736 to 24935936
[root@localhost] ~$ df -Th /
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/vda3 xfs 96G 1.9G 94G 2% /
virt-resize#
virt-resize
基于拷贝镜像模式,对指定的系统分区扩容,支持windows
1.Centos7分区扩容#
# 1.查看系统分区情况
[root@kvm-1 sty_vm1]# virt-filesystems --long -h --all -a img-centos7.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 1.0G -
/dev/sda2 filesystem swap - - 3.9G -
/dev/sda3 filesystem xfs - - 45G -
/dev/sda1 partition - - 83 1.0G /dev/sda
/dev/sda2 partition - - 82 3.9G /dev/sda
/dev/sda3 partition - - 83 45G /dev/sda
/dev/sda device - - - 50G -
# 2.创建一个新的空镜像文件,大小为扩容后的磁盘容量
[root@kvm-1 sty_vm1]# qemu-img create -f qcow2 img-centos7+20G.qcow2 70G
Formatting 'img-centos7+20G.qcow2', fmt=qcow2 size=75161927680 cluster_size=65536 lazy_refcounts=off refcount_bits=16
# 3.对指定分区/dev/sda3扩容
[root@kvm-1 sty_vm1]# virt-resize --expand /dev/sda3 img-centos7.qcow2 img-centos7+20G.qcow2
[ 0.0] Examining img-centos7.qcow2
**********
Summary of changes:
/dev/sda1: This partition will be left alone.
/dev/sda2: This partition will be left alone.
virt-resize: warning: unknown/unavailable method for expanding the xfs
filesystem on /dev/sda3
/dev/sda3: This partition will be resized from 45.1G to 65.1G.
**********
[ 2.7] Setting up initial partition table on img-centos7+20G.qcow2
[ 2.9] Copying /dev/sda1
[ 4.3] Copying /dev/sda2
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[ 7.0] Copying /dev/sda3
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.
[root@kvm-1 sty_vm1]# mv img-centos7+20G.qcow2 img-centos7.qcow2
[root@kvm-1 sty_vm1]# virsh start sty_vm1
域 sty_vm1 已开始
[root@localhost] ~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 70G 0 disk
├─vda2 253:2 0 3.9G 0 part
├─vda3 253:3 0 65.1G 0 part /
└─vda1 253:1 0 1G 0 part /boot
[root@localhost] ~$ df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 3.8G 8.5M 3.8G 1% /run
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/vda3 46G 2.0G 44G 5% /
/dev/vda1 1014M 314M 701M 31% /boot
tmpfs 772M 0 772M 0% /run/user/0
# 4.登录虚拟机使用xfs_growfs扩容xfs文件系统
[root@localhost] ~$ xfs_growfs /dev/vda3
meta-data=/dev/vda3 isize=512 agcount=6, agsize=2301824 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=11828736, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=4495, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 11828736 to 17071296
[root@localhost] ~$ df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs tmpfs 3.8G 8.5M 3.8G 1% /run
tmpfs tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/vda3 xfs 66G 1.9G 64G 3% /
/dev/vda1 xfs 1014M 325M 690M 32% /boot
tmpfs tmpfs 772M 0 772M 0% /run/user/0
2.WinServer分区扩容#
WindowsServer2019的虚拟机镜像扩容
# 1.查看系统分区情况
root@kvm-1 ori]# virt-filesystems --long -h --all -a win2019-ori.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ntfs 系统保留 - 549M -
/dev/sda2 filesystem ntfs - - 49G -
/dev/sda1 partition - - 07 549M /dev/sda
/dev/sda2 partition - - 07 49G /dev/sda
/dev/sda device - - - 50G -
# 2.创建一个新的空镜像文件,大小为扩容后的磁盘容量
[root@kvm-1 ori]# qemu-img create -f qcow2 win2019-ori+20G.qcow2 70G
Formatting 'win2019-ori+20G.qcow2', fmt=qcow2 size=75161927680 cluster_size=65536 lazy_refcounts=off refcount_bits=16
# 3.对指定分区/dev/sda2扩容
root@kvm-1 ori]# virt-resize --expand /dev/sda2 win2019-ori.qcow2 win2019-ori+20G.qcow2
[ 0.0] Examining win2019-ori.qcow2
**********
Summary of changes:
/dev/sda1: This partition will be left alone.
/dev/sda2: This partition will be resized from 49.5G to 69.5G. The
filesystem ntfs on /dev/sda2 will be expanded using the ‘ntfsresize’
method.
**********
[ 2.6] Setting up initial partition table on win2019-ori+20G.qcow2
[ 2.8] Copying /dev/sda1
[ 3.4] Copying /dev/sda2
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧
00:00
[ 88.8] Expanding /dev/sda2 using the ‘ntfsresize’ method
virt-resize: error: libguestfs error: ntfsresize: /dev/sda2: ntfsresize
v2015.3.14 (libntfs-3g)
Device name : /dev/sda2
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 53109322240 bytes (53110 MB)
Current device size: 74583965696 bytes (74584 MB)
New volume size : 74583962112 bytes (74584 MB)
Checking filesystem consistency ...
Accounting clusters ...
Cluster accounting failed at 19922 (0x4dd2): missing cluster in $Bitmap
Cluster accounting failed at 19923 (0x4dd3): missing cluster in $Bitmap
Cluster accounting failed at 19924 (0x4dd4): missing cluster in $Bitmap
Cluster accounting failed at 19925 (0x4dd5): missing cluster in $Bitmap
Cluster accounting failed at 19926 (0x4dd6): missing cluster in $Bitmap
Cluster accounting failed at 19927 (0x4dd7): missing cluster in $Bitmap
Cluster accounting failed at 19928 (0x4dd8): missing cluster in $Bitmap
Cluster accounting failed at 19929 (0x4dd9): missing cluster in $Bitmap
Cluster accounting failed at 20146 (0x4eb2): extra cluster in $Bitmap
Cluster accounting failed at 20147 (0x4eb3): extra cluster in $Bitmap
Filesystem check failed! Totally 15840 cluster accounting mismatches.
ERROR: NTFS is inconsistent. Run chkdsk /f on Windows then reboot it TWICE!
The usage of the /f parameter is very IMPORTANT! No modification was
and will be made to NTFS by this software until it gets repaired.
If reporting bugs, run virt-resize with debugging enabled and include the
complete output:
virt-resize -v -x [...]
第一次执行virt-resize
报错,执行到ntfsresize时提示NTFS文件系统检查失败.查询官方文档了解到,需要对虚拟机所有分区执行chkdsk /f
,重启后正常关机
WINDOWS CHKDSK
Windows disks which use NTFS must be consistent before virt-resize can be used. If the ntfsresize operation fails, try booting the original VM and running
chkdsk /f
on all NTFS partitions, then shut down the VM cleanly. For further information see: https://bugzilla.redhat.com/show_bug.cgi?id=975753After resize Windows may initiate a lengthy “chkdsk” on first boot if NTFS partitions have been expanded. This is just a safety check and (unless it find errors) is nothing to worry about.
引用自:https://libguestfs.org/virt-resize.1.html
本文windows虚拟机只有一个C盘,登录vm执行chkdsk C: /f
,重启完成后再关机
PS C:\Users\Administrator> chkdsk C: /f
文件系统的类型是 NTFS。
无法锁定当前驱功器。
由于该卷正被另一进程使用,无法运行 Chkdsk。
是否计划在下一次系统重新启动时检查此卷?(Y/N) y
将在下一次系统重新启动时检查此卷。
再次执行virt-resize --expand
[root@kvm-1 ori]# virt-resize --expand /dev/sda2 win2019-ori.qcow2 win2019-ori+20G.qcow2
[ 0.0] Examining win2019-ori.qcow2
**********
Summary of changes:
/dev/sda1: This partition will be left alone.
/dev/sda2: This partition will be resized from 49.5G to 69.5G. The
filesystem ntfs on /dev/sda2 will be expanded using the ‘ntfsresize’
method.
**********
[ 14.6] Setting up initial partition table on win2019-ori+20G.qcow2
[ 14.8] Copying /dev/sda1
[ 15.4] Copying /dev/sda2
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 103.6] Expanding /dev/sda2 using the ‘ntfsresize’ method
Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.
# /dev/sda2 NTFS文件系统已成功扩容
[root@kvm-1 ori]# virt-filesystems --long -h --all -a win2019-ori+20G.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ntfs 系统保留 - 549M -
/dev/sda2 filesystem ntfs - - 69G -
/dev/sda1 partition - - 07 549M /dev/sda
/dev/sda2 partition - - 07 69G /dev/sda
/dev/sda device - - - 70G -
两种工具对比#
qemu-img resize + fdisk | virt-resise | |
---|---|---|
操作系统 | Linux | Linux、Windows |
快照镜像扩容 | 不支持对有快照的镜像扩容 | 支持 |
主分区扩容 | 只支持最后一个主分区的无损扩容 | 支持所有主分区无损扩容 |
扩展分区扩容 | 不支持 | 不支持 |
扩容速度 | 命令行操作,较快 | 复制模式,和磁盘大小有关,越大越慢 |