以 tmp_ka30 虚拟机为例, 源机器磁盘大小40GB
准备操作
关闭指定虚拟机
qcow2的磁盘扩容,貌似不能在线扩容,如果有能在线扩容的方法,wanxialianwei@gami.com 告诉我,感激不尽
1 2 3 4 5 6 7 8 9 10 11 12
| [root@jzbj tmp]
[root@jzbj tmp] Id 名称 状态 ---------------------------------------------------- 16 k120-anolis_ka29 running 26 daily-anolis_ka28 running 63 h331-anolis_ka28 running - tmp_ka30 关闭
|
寻找img文件路径
/home/lib/libvirt/images/tmp_ka30.img
就是本例的镜像文件路径
1 2 3 4 5 6 7 8 9 10 11 12
| [root@jzbj tmp] ... <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/home/lib/libvirt/images/tmp_ka30.img'/> <backingStore/> <target dev='vda' bus='virtio'/> <alias name='ua-box-volume-0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </disk>
|
移动qcow2 镜像
一定要记得备份文件, 谨记. 问就是血泪教训.
开始扩容
创建新的磁盘镜像
1 2 3 4 5 6 7 8 9 10 11 12 13
| [root@jzbj images] Formatting 'tmp_ka30.img', fmt=qcow2 size=53687091200 encryption=off cluster_size=65536 lazy_refcounts=off
[root@jzbj images] image: tmp_ka30.img file format: qcow2 virtual size: 50G (53687091200 bytes) disk size: 196K cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false
|
检查原镜像信息
这一步的主要作用是确定要迁移的磁盘
1 2 3 4 5
| [root@jzbj images] Name Type VFS Label MBR Size Parent /dev/sda1 filesystem ext4 - - 20G - /dev/sda1 partition - - 83 20G /dev/sda /dev/sda device - - - 40G -
|
resize磁盘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| [root@jzbj images] [ 0.0] Examining tmp_ka30.img.origin **********
Summary of changes:
/dev/sda1: This partition will be resized from 20.0G to 50.0G. The filesystem ext4 on /dev/sda1 will be expanded using the ‘resize2fs’ method.
********** [ 2.5] Setting up initial partition table on tmp_ka30.img [ 2.7] Copying /dev/sda1 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 [ 70.5] Expanding /dev/sda1 using the ‘resize2fs’ method
Resize operation completed with no errors. Before deleting the old disk, carefully check that the resized disk boots and works correctly.
|
启动,验证扩容
检查扩容
1 2 3 4 5
| [root@jzbj images] Name Type VFS Label MBR Size Parent /dev/sda1 filesystem ext4 - - 50G - /dev/sda1 partition - - 83 50G /dev/sda /dev/sda device - - - 50G -
|
启动虚拟机,检查
1 2 3 4 5 6 7 8 9 10
| 启动虚拟机 确认磁盘信息 [root@ka30 ~] Filesystem Size Used Avail Use% Mounted on devtmpfs 964M 0 964M 0% /dev tmpfs 981M 0 981M 0% /dev/shm tmpfs 981M 8.4M 973M 1% /run tmpfs 981M 0 981M 0% /sys/fs/cgroup /dev/vda1 50G 2.0G 45G 5% / tmpfs 197M 0 197M 0% /run/user/1001
|
去除镜像空洞
当磁盘的实际使用量和磁盘文件大小不一致的时候需要去除磁盘空洞
1 2 3 4 5 6 7 8
|
mv tmp_ka30.img tmp_ka30.img.origin
virt-sparsify --tmp /ssd1t/kvm/images/ tmp_ka30.img.origin -f qcow2 tmp_ka30.img
|
接着就是重启虚拟机来验证, 后面省略了.
虚拟机在/etc/fstab 中写入错误无法启动的恢复流程
查看虚拟机镜像的磁盘分区
1 2
| virt-filesystems -a tmp_ka30.img
|
挂载镜像分区
1 2
| guestmount -a tmp_ka30.img -m /dev/sda1 /mnt/sysmount
|
根据需求修改系统文件
这边就需要根据个人需求了, vm的文件系统已经挂载到/mnt/sysmount, 修改即可.
卸载分区