记一次虚机扩容

本人工作用的ubuntu,由于工作中会用到QQ,钉钉等交流软件,遂在ubuntu中用kvm虚了个win7在用。当时虚的时候给了20G空间,眼看磁盘块满了,于是乎决定给虚机扩容。
先看下磁盘信息
wangpei@wangpei:~/VirtualBox-VMs/win7$ qemu-img info win7
image: win7
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 20G
磁盘是raw格式,20G的空间,下面看看分区情况
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ sudo virt-df -h ../../win7/win7
Filesystem Size Used Available Use%
win7:/dev/sda1 100M 24M 75M 25%
win7:/dev/sda2 20G 19G 1.1G 95%
可以看到sda2分区已经用到95%的空间了,急需扩容
宿主机的该分区还有点空间,就用这个分区吧
/dev/sda4 100G 33G 68G 33% /home/wangpei/VirtualBox-VMs/win7-large
在宿主机的该分区挂载的目录下新建一个50G的虚拟磁盘
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ qemu-img create -f raw win7 +50G
Formatting 'win7', fmt=raw size=53687091200
开始扩容,virt-resize是将源虚机磁盘的所有分区copy到新的虚拟磁盘,此处的源虚拟磁盘我用的是相对路径../../win7/win7
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ sudo virt-resize --expand /dev/sda2 ../../win7/win7 win7
[ 0.0] Examining ../../win7/win7


Summary of changes:

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

/dev/sda2: This partition will be resized from 19.9G to 49.9G. The
filesystem ntfs on /dev/sda2 will be expanded using the 'ntfsresize'
method.


[ 4.3] Setting up initial partition table on win7
[ 4.5] Copying /dev/sda1
[ 4.8] Copying /dev/sda2
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 224.3] 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.
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$
看看新的虚拟磁盘信息
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ qemu-img info win7
image: win7
file format: raw
virtual size: 50G (53687091200 bytes)
disk size: 18G
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ sudo virt-df -h win7
Filesystem Size Used Available Use%
win7:/dev/sda1 100M 24M 75M 25%
win7:/dev/sda2 50G 19G 31G 38%
可以看到新的虚拟磁盘已经扩大到了50G,sda2分区的使用率是38%
现在需要修改虚机的磁盘路径
sudo sed -i '/source file/s//home/wangpei/VirtualBox-VMs/win7/win7//home/wangpei/VirtualBox-VMs/win7-large/win7/' /etc/libvirt/qemu/win7.xml
重新定义虚机
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ sudo virsh define /etc/libvirt/qemu/win7.xml
Domain win7 defined from /etc/libvirt/qemu/win7.xml

wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ virsh list --all
Id Name State


  • win7                           shut off
    

启动虚机,win7在启动时会进行磁盘检测,检测完重启即可。
wangpei@wangpei:~/VirtualBox-VMs/win7-large/virtualm$ virsh list
Id Name State


5 win7 running

你可能感兴趣的:(记一次虚机扩容)