libvirt - virtio (paravirtualized device drivers)

最後更新: 2021-12-30

介紹

virtio driver 即是 paravirtualized device drivers, 它一共包括以下 3 個 driver

  • network devices (virtio_net)
  • block devices (virtio_blk)
  • balloon devices (virtio_balloon)

* The host implementation is in userspace - qemu

目錄

  • Linux Guest 要支援 virtio 才用到
  • ...
  • Windows 下載 virtio
  • Windows Server 2008 安裝時設定用 virtio
  • virtio SCSI controller

 


Linux Guest 要支援 virtio 才用到

 

linux kernel >= 2.6.25

CONFIG_VIRTIO_PCI=y (Virtualization -> PCI driver for virtio devices)              <-- virtio_pci
CONFIG_VIRTIO_BALLOON=y (Virtualization -> Virtio balloon driver)
CONFIG_VIRTIO_BLK=y (Device Drivers -> Block -> Virtio block driver)
CONFIG_VIRTIO_NET=y (Device Drivers -> Network device support -> Virtio network driver)
CONFIG_VIRTIO=y (automatically selected)                                           <-- virtio
CONFIG_VIRTIO_RING=y (automatically selected)                                      <-- virtio_ring

 


Qemu Usage

 

Disk

-drive file=/images/xpbase.qcow2,if=virtio

NET

-device virtio-net-pci,netdev=net0

 


Module

 

---

kvmnet - NDIS miniport network driver for Windows guests (virtio-net)

---

vioscsi - (virtio-scsi-pci)

viostor - STORPORT miniport driver for Windows guests  ( virtio-blk-pci )

---

vioinput

  • virtio-keyboard-pci
  • virtio-mouse-pci
  • virtio-tablet-pci
  • virtio-input-host-pci

---

viorng - A paravirtualized device that is exposed as a hardware RNG device to the guest (entropy)

---

 


vhost-net

 

vhost-net module is a kernel-level backend for virtio networking that reduces virtualization overhead by

moving virtio packet processing tasks out of user space (the QEMU process) and into the kernel (the vhost-net driver).

Flow

Guest (virtio_net) -> (vhost_net) Host-> tap - |Bridge| - NIC

Host Module

lsmod | grep vhost_net

vhost_net              24576  3
vhost                  45056  2 vhost_scsi,vhost_net
tap                    24576  1 vhost_net

To disable vhost-net

# Setting the driver name to qemu forces packet processing into QEMU user space

XML

<interface type="bridge">
  <source bridge='vmbr0'/>
  <model type="virtio"/>
  <driver name="qemu"/>
  ...
</interface>

Enable 時的情況

qemu-system-x86_64 -netdev type=tap,id=guest0,script=kvm-ifup,vhost=on \
                   -device virtio-net-pci,netdev=guest0,mac=ma:c:a:dd:re:ss ..

 


libvirt Usage

 

virtio disk

Guest:

/boot/grub/device.map

 "(hd0) /dev/sda" to "(hd0) /dev/vda"

/boot/grub/menu.list

"root=/dev/sda1" to "root=/dev/vda1"

/etc/fstab

XML:

    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/mnt/vm/centos6.3.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <!-- <address type='drive' controller='0' bus='0' unit='0'/> -->
      // 下次會自動建立 <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
      // 因為所有 virtio 都是 PCI 來
    </disk>

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

Net

XML 設定:

    <interface type='bridge'>
      <mac address='52:54:00:f1:62:0b'/>
      <model type='virtio' />                                   <-- 加入
      <source bridge='virbr0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>

Linux speed test

                      PC
                      |
                  Host(vmbr0)[iperf]
                      |
KVM(virtio)[iperf] -----> KVM(virtio)[iperf]

iperf: Version 2

virtio_net: 2.6.32-754.12.1.el6.i686

CPU: AMD A6-5400K(2core, 3.6GHz): 100%

VM to VM

[  3]  0.0-30.0 sec  13.4 GBytes  3.85 Gbits/sec

VM to Host

[  3]  0.0-30.0 sec  26.2 GBytes  7.50 Gbits/sec

Window NIC tuning

# Disable Windows Scaling heuristics

netsh int tcp set heuristics wsh=disabled

# To turn off the default receive window auto tuning behavior:

netsh int tcp set global autotuninglevel=disabled

# To increase the TCP Windows with the faster rate Compound TCP should be enabled.

netsh int tcp set global congestionprovider=ctcp

memballoon

VM 的 XML加入即可:

    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>

Usage:

VM Guest 一共有 1024 MB, 不過可用的只有 512MB, 因為 memballoon 會儲起 512 MB

 <memory>1024000</memory>
 <currentMemory>512000</currentMemory>

詳見:

http://datahunter.org/virsh_vram

 


Windows 下載 virtio

 

下載: virtio 的 driver:

Direct DL:

https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso

新方法:

wget https://fedorapeople.org/groups/virt/virtio-win/virtio-win.repo -O /etc/yum.repos.d/virtio-win.repo

yum install virtio-win

ls /usr/share/virtio-win/*.iso

Doc

https://fedoraproject.org/wiki/Windows_Virtio_Drivers

 


Balloon Devices

 

安裝 balloon devices

 

blnsvr -i

Service Installed
Service is starting...

Service not started.
  Current State: 1
  Exit Code: 0
  Service Specific Exit Code: 0
  Check Point: 0
  Wait Hint: 0

在系統裝置內誤認為"PCI standant RAM Controller"

devcon install BALLOON.inf "PCI\VEN_1AF4&DEV_1002&SUBSYS_00051AF4&REV_00"

Device node created. Install is complete when drivers are updated...
Updating drivers for PCI\VEN_1AF4&DEV_1002&SUBSYS_00051AF4&REV_00 from C:\driver
\ballon\BALLOON.inf.
Drivers updated successfully.

啟用:

net start balloonservice

刪除:

blnsvr -u

 


Windows Server 2008 安裝時設定用 virtio

 

安裝時入 ISO:

attach-disk s2008 /kvm/iso/virtio-win-0.1-59.iso hdc --type cdrom

 

Unsigned x64 driver

The customer wants to install an unsigned x64 driver to Microsoft Windows Server 2008,

but is not allowed to do this even if logon as Local Administrator.

Solution

To disable this restriction, press F8 at boot time and choose for the boot option Disable Driver Signature Enforcement.

Old virtio driver

https://launchpad.net/kvm-guest-drivers-windows/+download

virtio-win-drivers-20120712-1.iso

WHQL signed builds

WHQL signed builds are only available with a paid RHEL subscription.

The drivers are cryptographically signed with Red Hat’s vendor signature.

However they are not signed with Microsoft’s WHQL signature.

https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/

New version virtio

Microsoft deprecated signing of kernel-mode drivers with SHA1 certificates in late 2019,

hence as of VirtIO 1.1.6, all drivers are signed with SHA2 certificate only.

Windows 7 and Windows Server 2008R2 do not have SHA2 support out of the box.

Load Driver 時會出現

No signed device drivers were found

 


在安裝好的 Guest 內啟用 virtio disk

 

  1. Stop the machine and add a virtio disk
  2. Start the machine. You will be prompted to install the driver.
  3. This is enough to allow to boot on the virtio disk

 

 


virtio SCSI controller

 

好處:

1.  HDD 不會被改名 (sda -> vda)

A scsi controller has an optional attribute model

  • virtio-transitional         # virtio 0.9 and virtio 1.0 guest drivers
  • virtio-non-transitional  # only work with virtio 1.0 guest drivers
  • virtio-scsi                   # This device will work like a virtio-non-transitional device
                                        when plugged into a PCI Express slot

XML

Controller:

<controller type='scsi' index='0' model='virtio-scsi'></controller>

Disk:

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source file='/kvm/vm/redpill/os.qcow2'/>
  <target dev='sdb' bus='scsi'/>
</disk>

VM Boot Console

[    3.950753] scsi6 : Virtio SCSI HBA
[    3.960586] scsi 6:0:0:1: Direct-Access     QEMU     QEMU HARDDISK            2.5+ PQ: 0 ANSI: 5
[   11.377062] sd 0:0:0:0: Attached scsi generic sg0 type 0
[   11.378259] sd 6:0:0:1: Attached scsi generic sg1 type 0

Remark: virtio-blk and virtio-scsi

virtio-blk: most performant

guest: app -> Block Layer -> virtio-blk

virtio-scsi: SCSI passthrough(scsi-block), supports SCSI UNMAP command, SCSI Persistent Reservations

guest: app -> Block Layer -> SCSI Layer -> virtio-scsi

 

Creative Commons license icon Creative Commons license icon