菜鸟玩云计算之十二: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
分享到:
相关推荐
菜鸟级傻瓜版教程:安装另类ipa格式软件的两个主要方法(ipad版)借鉴.pdf
code 博文链接:https://limdengrock.iteye.com/blog/1018141
快递物流研究笔记之一:商流加持的物流巨头:菜鸟VS京东物流.pdf
菜鸟级傻瓜版教程:安装另类ipa格式软件的两个主要方法(ipad版)终版.pdf
Python基础语法-菜鸟教程-函数⽤法:input()和raw_input() 1.input()和raw_input()⽤法 (1).input() 1)⽤法解释 def input(*args, **kwargs): # real signature unknown """ Read a string from standard input....
菜鸟简单利用虚拟机搭建局域网
让菜鸟学会安装使用虚拟机.doc
通过联机黑白棋看云计算、移动互联网和物联网 终于把通信的一个小项目——联机版的黑白棋做出来了~ 说来真是惭愧,本来在暑假结束的时候就应该去做的,但是~╮(╯▽╰)╭ 先来说说黑白棋吧: 胡老师说:不要在没做一...
本教程采用Linux centos6.2版本虚拟机安装方法,内容比较适合入门级朋友,期待大家一起学习。
与VM相比小巧很多,但是对虚拟机的基本要求都可以满足,菜鸟必备
菜鸟网络-秦磊:菜鸟绿色物流实践与思考-2020.8-8页精品报告2020.pdf
快递物流系列研究笔记之七:菜鸟的物流江湖.pdf
五:Day01-Linux虚拟机
快速掌握VMware虚拟机技术的必备技巧
前⾔ 云计算,为我们新⼿、菜鸟带来了机会------年轻就是最⼤的资本啊。也正因为如此,我们更应该努⼒,没有理由放弃努⼒。(当然,如果你 有⼀天娶了个⽩富美或者⾼富帅,亦或是买彩票中了百万⼤奖,那就另当别论了...
电子-嵌入式系统开发之道菜鸟成长日志与项目经理的私房菜.part2.rar,综合电子技术数字,模拟,高频电路
云计算技术金融应用规范
菜鸟教程(Runoob)提供的免费网上编译器! 包含C++ C C# HTML/CSS/JavaScript PHP Python等许多语言的编译器! 还有画图 进制转换等常用工具! 还有资源共享! 欢迎来到(http://c.runoob.com)!
快递物流系列研究笔记之七:菜鸟的物流江湖