天天看点

kvm上的Linux虚拟机使用virtio磁盘

kvm上的Linux虚拟机使用virtio磁盘

系统:centos6.6  64位

网上的文章比较少,怎麽将Linux虚拟机的磁盘改为使用virtio磁盘

因为centos6或以上系统已经包含了virtio驱动,所以不需要再执行下面语句加载内核模块

modprobe virtio  virtio_pci  virtio_blk  virtio_net

mkinitrd  --with  virtio  --with  virtio_pci   --with  virtio_blk   --with  virtio_net   -f  boot/initrd-$(uname -r).img  $(uname -r)      

这里说一下具体方法

在物理机上先生成一台虚拟机

1、安装一台Linux机器

qemu-img create -f qcow2 /data/kvmimg/gzxtest04.qcow2 30G

virt-install --name=gzxtest04 --ram 4096 --vcpus=8 --autostart --hvm \

--disk path=/data/kvmimg/gzxtest04.qcow2,size=60,format=qcow2 \

--cdrom /data/download/CentOS-6.6-x86_64-bin-DVD1.iso \

--graphics vnc,listen=0.0.0.0,port=5907 \

--network bridge=br0,model=e1000 --force --connect qemu:///system

2、启动虚拟机并安装好centos6.6系统

3、安装好系统之后,使用poweroff命令关闭虚拟机

4、先备份虚拟机的xml文件

virsh dumpxml gzxtest04 > ~/gzxtest04.xml        

5、修改虚拟机的xml文件

virsh edit gzxtest04      

<disk type='file' device='disk'>

<driver name='qemu' type='qcow2' cache='none'/>

<source file='/data/kvmimg/gzxtest04.qcow2'/>

<target dev='hda' bus='ide'/>

<alias name='ide0-0-0'/>

<address type='drive' controller='0' bus='0' target='0' unit='0'/>

</disk>

修改为

<disk type='file' device='disk'>

<driver name='qemu' type='qcow2' cache='none' io='native'/>

<target dev='vda' bus='virtio'/>

其实就是删除address type这一行,在driver name这一行添加io='native',dev='hda' 改为vda, bus='ide' 改为virtio

6、启动虚拟机

virsh start gzxtest04      

7、在虚拟机里可以看到原来是hdx的分区已经全部变为vdx

kvm上的Linux虚拟机使用virtio磁盘
kvm上的Linux虚拟机使用virtio磁盘

8、在虚拟机里修改grub设备映射表

sed -i  "s/hda/vda"  /boot/grub/device.map       

大功告成

背景知识

KVM虚拟机磁盘的缓存模式

1、默认,不指定缓存模式的情况下,1.2版本qemu-kvm之前是writethough,1.2版本之后qemu-kvm, centos虚拟机默认的缓存模式就是none

2、writethough:使用O_DSYNC语义

3、writeback:不是O_DSYNC语义也不是O_DIRECT语义,虚拟机数据到达宿主机页面缓存page cache就给虚拟机返回写成功报告,页面缓存机制管理数据的合并写入宿主机存储设备

4、none:使用O_DIRECT语义,I/O直接在qemu-kvm用户空间缓存和宿主机存储设备之间发生,要求I/O方式设置为aio=native,不能使用宿主机的page cache,相当于直接访问磁盘,有优越性能

5、unsafe:跟writeback一样,但是不能发出刷盘指令,只有在虚拟机被关闭时候才会将数据刷盘,不安全

6、directsync:同时使用O_DSYNC语义和O_DIRECT语义

缓存模式的数据一致性

writethough、none、directsync

能保证数据一致性,有一些文件系统不兼容none或directsync模式,这些文件系统不支持O_DIRECT语义

writeback

不能保证数据一致性,在数据报告写完成和真正合并写到存储设备上一个时间窗口期,这种模式在宿主机故障时候会丢失数据,因为数据还存在在宿主机的page cache里

unsafe

不保证数据一致性,忽略刷盘指令,只有在虚拟机被关闭时候才会将数据刷盘,不安全

参考文章:https://easyengine.io/tutorials/kvm/enable-virtio-existing-vms/

如有不对的地方,欢迎大家拍砖o(∩_∩)o 

本文版权归作者所有,未经作者同意不得转载。

继续阅读