最後更新: 2017-09-13
目標
- SCSI 裝置(attach-disk)
- Passthrough Disk
- USB Disk Image
- Host USB: attach-device
- List Support Device
- Allow libvirt full access USB
SCSI 裝置(attach-disk)
virsh attach-disk VM DISK TARGET [--subdriver qcow2]
Hot attach disk: raw
virsh attach-disk domain-name --source /path/to/disk/image --target sdb
等同於
# 沒有 --source 及 --target
virsh attach-disk domain-name /path/to/disk/image sdb
會建立的 XML:
<disk type='file' device='disk'> <driver name='qemu' type='raw' cache='writeback'/>
Qcow2 image disk 裝置:
qemu-img create -f qcow2 vdb.qcow2 5g
virsh attach-disk domain-name /path/to/another/image vdb --subdriver qcow2
會建立的 XML:
<disk type='file' device='disk'> <driver name='qemu' type='qcow2'/>
其他選項:
- --current # Default
- --print-xml # print XML document rather than attach the disk
- --persistent # 會寫到 xml config file
- --mode readonly | shareable # Shareable between domains. Default: No
<readonly/> <shareable/>
- --type <disk | cdrom> # target device type. Indicates how the disk is to be exposed to the guest OS. Default: disk
disk: <disk type='block' device='disk'> # type=? 係會自動決定的
cdrom: <disk type='file' device='cdrom'>
- --driver qemu # qemu only supports a name of "qemu", but multiple types including "raw", "bochs", "qcow2", and "qed".
- --subdriver raw | qcow2 # Default: raw
- --cache none | writeback # Default: none
<driver name='qemu' cache='writeback'/>
P.S.
沒有 --persistent 時, Domain destory, start 後 disk 會 detact 左
Checking
domblklist centos6
Target Source ------------------------------------------------ vda /ssd/iredmail/centos6.qcow2 vdb /ssd/iredmail/vdb.qcow2
Detach
detach-disk vmname disk
Passthrough Disk
XML
<disk type='block' device='disk'>
<driver name='qemu' type='raw'/>
<source dev='/dev/sdc'/>
<target dev='vda' bus='virtio'/>
</disk>
P.S.
避免 mount 錯 Disk
use /dev/disk/by-id/ as source
對於不支援 virtio 的 system 要用 sata
sata XML:
<target dev='sda' bus='sata'/>
即使 <target dev='sdX' bus='virtio'/>, lsblk 仍會見到是 vdX
Example
<disk type='block' device='disk'> <driver name='qemu' type='raw'/> <source dev='/dev/disk/by-id/ata-WDC_WD2002FAEX-007BA0_WD-WMAWP0494690'/> <target dev='sdg' bus='sata'/> </disk>
USB Disk Image
s2008.xml
<controller type='scsi'/> <controller type='scsi' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </controller>
# dd if=/dev/zero of=/kvm/s2008/usbdisk.raw bs=1M count=64
# virsh attach-disk s2008 /kvm/s2008/usbdisk.raw sda
P.S.
直接 attach-disk 上 s2008 而它又不是 --type cdrom 時, 那 VM 是會死的 !!
s2008.xml
# Guest 's RAID Card
<devices> ............. <controller type='scsi' model='lsilogic'/> </devices>
scsi-disk.xml
# Disk Type
<disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/kvm/s2008/usbdisk.raw'/> <target dev='sda' bus='scsi'/> </disk>
要用 SCSI archicture: SCSI supports hot plugging
- vdX, the disk is connected to a virtio controller
- sdX, the disk is connected to a scsi controller
- hdX, the disk is connected to an IDE controller
# virsh attach-device s2008 scsi-disk.xml
Host USB: attach-device
lsusb
Bus 002 Device 003: ID 0411:01f0 BUFFALO INC. (formerly MelCo., Inc.)
mkdir /etc/libvirt/HotPlug
vim /etc/libvirt/HotPlug/BackupDisk.xml
<hostdev mode='subsystem' type='usb'> <source> <vendor id='0x0411'/> <product id='0x01f0'/> </source> </hostdev>
掛載 USB 儲存裝置:
# 掛載
virsh attach-device xpenology /etc/libvirt/HotPlug/BackupDisk.xml
Device attached successfully
# 查看
virsh qemu-monitor-command xpenology --hmp 'info usb'
Device 0.2, Port 1, Speed 480 Mb/s, Product HD-LBU3
OR
virsh dumpxml xpenology
<hostdev mode='subsystem' type='usb' managed='no'> <source> <vendor id='0x0411'/> <product id='0x01f0'/> <address bus='2' device='3'/> </source> <alias name='hostdev0'/> </hostdev>
卸載 USB 儲存裝置:
virsh detach-device xpenology /etc/libvirt/HotPlug/BackupDisk.xml
Device detached successfully
managed
There are two modes in which a PCI device can be attached, "managed" or "unmanaged" mode,
although at time of writing only KVM supports "managed" mode attachment.
In managed mode, the configured device will be automatically detached from the host OS drivers when the guest is started,
and then re-attached when the guest shuts down.
In unmanaged mode, the device must be explicit detached ahead of booting the guest.
The guest will refuse to start if the device is still attached to the host OS.
The libvirt 'Node Device' APIs provide a means to detach/reattach PCI devices from/to host drivers.
Alternatively the host OS may be configured to blacklist the PCI devices used for guest, so that they never get attached to host OS drivers.
In both modes, the virtualization technology will always perform a reset on the device before starting a guest, and after the guest shuts down.
This is critical to ensure isolation between host and guest OS. There are a variety of ways in which a PCI device can be reset.
Some reset techniques are limited in scope to a single device/function, while others may affect multiple devices at once.
Auto mount by udev rule:
/etc/udev/rules.d/90-BackupDisk.rules
ACTION=="add", \ SUBSYSTEM=="usb", \ SYSFS{idVendor}=="0411", \ SYSFS{idProduct}=="01f0", \ RUN+="/usr/bin/virsh attach-device xpenology /etc/libvirt/HotPlug/BackupDisk.xml" ACTION=="remove", \ SUBSYSTEM=="usb", \ SYSFS{idVendor}=="0411", \ SYSFS{idProduct}=="01f0", \ RUN+="/usr/bin/virsh detach-device xpenology /etc/libvirt/HotPlug/BackupDisk.xml"
service udev reload
Device info.
In order to determine what attributes to use for matching, you can use udevadm monitor:
udevadm monitor --property --udev --subsystem-match=usb/usb_device
UDEV [21887.137896] add /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2 (usb) ACTION=add BUSNUM=002 DEVNAME=/dev/bus/usb/002/003 DEVNUM=003 DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2 DEVTYPE=usb_device ID_BUS=usb ID_MODEL=HD-LBU3 ID_MODEL_ENC=HD-LBU3 ID_MODEL_ID=01f0 ID_REVISION=0001 ID_SERIAL=BUFFALO_HD-LBU3_010920005E15 ID_SERIAL_SHORT=010920005E15 ID_USB_INTERFACES=:080650: ID_VENDOR=BUFFALO ID_VENDOR_ENC=BUFFALO ID_VENDOR_FROM_DATABASE=BUFFALO INC. (formerly MelCo., Inc.) ID_VENDOR_ID=0411 MAJOR=189 MINOR=130 PRODUCT=411/1f0/1 SEQNUM=2298 SUBSYSTEM=usb TYPE=0/0/0 USEC_INITIALIZED=887053724
* If you want to match a specific USB port, you will probably want to use the DEVPATH attribute.
* To match a specific device instead, use the ID_VENDOR_ID and ID_MODEL_ID attributes.
Note:
It may be tempting to use ATTR{busnum} and ATTR{devpath} to match the USB bus and port number.
However, those attributes are only available when the device is added, and not when the device is removed.
This leads to the device not being properly removed from the VM.
List Support Device
-device driver[,prop[=value][,...]] add device (based on driver) prop=value,... sets driver properties use '-device help' to print all possible drivers use '-device driver,help' to print all possible properties
kvm -device help |& grep scsi
kvm -device scsi-disk,help
name "scsi-disk", bus SCSI, desc "virtual SCSI disk or CD-ROM (legacy)"
name "megasas", bus PCI, desc "LSI MegaRAID SAS 1078"
name "megasas-gen2", bus PCI, desc "LSI MegaRAID SAS 2108"
name "mptsas1068", bus PCI, desc "LSI SAS 1068"
Allow libvirt full access USB
log
[1387980.187472] audit: type=1400 audit(1496842729.126:405): apparmor="DENIED" operation="open"
profile="libvirt-78c13701-b7f0-4e43-bcd2-fcca1c8ee3a7"
name="/run/udev/data/+usb:8-0:1.0" pid=24884
comm="qemu-system-x86" requested_mask="r" denied_mask="r" fsuid=108 ouid=0
# config apparmor
/etc/apparmor.d/abstractions/libvirt-qemu
# For hostdev access. The actual devices will be added dynamically /sys/bus/usb/devices/ rw, /dev/bus/usb/*/[0-9]* rw, /sys/devices/**/usb[0-9]*/** rw, /run/udev/** rw,
# re-start service
/etc/init.d/apparmor restart
/etc/init.d/libvirt-bin restart