`
923723914
  • 浏览: 636237 次
文章分类
社区版块
存档分类
最新评论

菜鸟玩云计算之十二:KVM虚拟机更改大小

 
阅读更多

菜鸟玩云计算之十二:KVM虚拟机更改大小

参考:

http://www.missionfamilybank.org/expanding-resizing-your-qcow2-virtual-machine-image-with-lvm-and-ext4/

早晚我们会遇到虚拟机的大小不够用的情况。此时可以用virt-resize命令重新分配虚拟机大小。假设我们的虚拟机vm-gitlab6需要扩充到40G。那么首先关闭虚拟机,然后备份虚拟机文件。

以下操作都在下面的虚机目录下进行:

/var/lib/libvirt/images/vm-gitlab6/ubuntu-kvm/

备份文件

$ sudo virsh shutdown vm-gitlab6

$ sudo cp /var/lib/libvirt/images/vm-gitlab6/ubuntu-kvm/vm-gitlab6-img1.qcow2 /var/lib/libvirt/images/vm-gitlab6/ubuntu-kvm/vm-gitlab6-img1-back.qcow2

查看虚机分区大小

$ sudo virt-df -h vm-gitlab6-img1.qcow2

W: kvm binary is deprecated, please use qemu-system-x86_64 instead
Filesystem Size Used Available Use%
vm-gitlab6-img1.qcow2:/dev/sda1 18G 1.1G 16G 6%

创建新的镜像文件

vm-gitlab6-img1-big.qcow2,给40G大小(瞬间结束,让人以为错误)

$ sudo qemu-img create -f qcow2 vm-gitlab6-img1-big.qcow2 40G

下面就是关键的虚拟机扩容

$ sudo virt-resize --expand /dev/sda1 vm-gitlab6-img1.qcow2 vm-gitlab6-img1-big.qcow2 


Examining vm-gitlab6-img1.qcow2 ...
W: kvm binary is deprecated, please use qemu-system-x86_64 instead
**********

Summary of changes:

/dev/sda1: This partition will be resized from 18.6G to 32.5G. The
filesystem ext4 on /dev/sda1 will be expanded using the 'resize2fs'
method.

/dev/sda2: This partition will be left alone.

**********
Setting up initial partition table on vm-gitlab6-img1-1.qcow2 ...
Copying /dev/sda1 ...
100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
Copying /dev/sda2 ...
100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
W: kvm binary is deprecated, please use qemu-system-x86_64 instead
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.

再看看新文件大小

$ sudo virt-df -h vm-gitlab6-img1-big.qcow2

W: kvm binary is deprecated, please use qemu-system-x86_64 instead
Filesystem Size Used Available Use%
vm-gitlab6-img1-big.qcow2:/dev/sda1 32G 1.1G 29G 4%

最后把vm-gitlab6-img1-big.qcow2改为vm-gitlab6-img1.qcow2即可。

$ sudo rm vm-gitlab6-img1.qcow2

$ sudo mv vm-gitlab6-img1-big.qcow2 vm-gitlab6-img1.qcow2

$ sudo chmod 755 vm-gitlab6-img1.qcow2


启动虚拟机

$ sudo virsh start vm-gitlab6
如果一切正常,原来那个vm-gitlab6-img1-back.qcow2就可以删除了。


shrink虚拟机

如果新虚拟机文件过大,如下:

$ ls -lh vm-gitlab6-img1-big.qcow2
-rw-r--r-- 1 root root 27G 10月 7 16:55 vm-gitlab6-img1-big.qcow2

用下面的命令转存一下,就可以缩小虚拟机文件大小(不是分区大小)

$ sudo qemu-img convert -O qcow2 vm-gitlab6-img1-big.qcow2 vm-gitlab6-img1-shrink.qcow2

$ ls -lh vm-gitlab6-img1-shrink.qcow2
-rw-r--r-- 1 root root 1.7G 10月 7 17:06 vm-gitlab6-img1-shrink.qcow2

查看分区

$ virt-df -h vm-gitlab6-img1-shrink.qcow2
W: kvm binary is deprecated, please use qemu-system-x86_64 instead
Filesystem Size Used Available Use%
vm-gitlab6-img1-shrink.qcow2:/dev/sda1 32G 1.1G 29G 4%

最后用vm-gitlab6-img1-shrink.qcow2替换vm-gitlab6-img1.qcow2即可。
==============================================================

下面内容将破坏虚拟机的内容,因此不能使用:

改变镜像文件大小(不是分区大小,是我们ls -lh显示的大小)

查看一下发现vm-gitlab6-img1.qcow2的大小变为27G,肯定是上面这个过程搞的。我希望它看起来小点。

因为qcow2格式不支持qemu-img resize,首先把它转为raw格式。

$ sudo qemu-img convert -f qcow2 vm-gitlab6-img1.qcow2 -O raw img1.raw

然后缩小raw镜像:

$ sudo qemu-img resize img1.raw -- -20G

最后转回去qcow2:
$ sudo qemu-img convert -f raw img1.raw -O qcow2 img1p.qcow2

完美解决!

cheungmine@gmail.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics