5

I'm writing a program for controlling USB storage device connections (this is a classic subject). All it's ok with my program, but now, I want to write some tests. So in order to do that, I realized I'll need some way of simulate USB connections.

And not only the connections, I need to be able to set the device properties: capacity, format, etc...

4
  • what has the USB simulation to do with qemu?
    – user55518
    Commented Mar 12, 2014 at 22:04
  • Well... I saw into systemd code (freedesktop.org/wiki/Software/systemd) that they usit for tests on udev. I then make the conclusion that they must be using qemu to simulate some "hardware thigns." Commented Mar 13, 2014 at 12:11
  • How ever if you know other way for doing that, I'll thank you point me the right direction. Commented Mar 13, 2014 at 12:11
  • well, qemu/kvm is good for testing. in case of a failure you don't need to reboot. Special usb hardware needs to be forwarded to qemu/kvm with usb_add. I am not up to date with the latest dev, so I can't tell you.
    – user55518
    Commented Mar 13, 2014 at 14:09

1 Answer 1

5

I found the solution in QEMU documentation:

USB emulation

QEMU can emulate a PCI UHCI, OHCI, EHCI or XHCI USB controller. You can plug virtual USB devices or real host USB devices (only works with certain host operating systems). QEMU will automatically create and connect virtual USB hubs as necessary to connect multiple USB devices. […]

Connecting USB devices

USB devices can be connected with the -device usb-... command line option or the device_add monitor command. Available devices are:

usb-storage,drive=drive_id

Mass storage device backed by drive_id (see the disk images chapter in the System Emulation Users Guide). This is the classicbulk-only transport protocol used by 99% of USB sticks. This example shows it connected to an XHCI USB controller and with a drive backed by a raw format disk image:

qemu-system [...]                                          \
 -drive if=none,id=stick,format=raw,file=/path/to/file.img \
 -device nec-usb-xhci,id=xhci                              \
 -device usb-storage,bus=xhci.0,drive=stick
usb-uas

USB attached SCSI device. This does not create a SCSI disk, so you need to explicitly create a scsi-hd or scsi-cd device on the command line, as well as using the -drive option to specify what those disks are backed by. One usb-uas device can handle multiple logical units (disks). This example creates three logical units: two disks and one cdrom drive:

qemu-system [...]                                                \
 -drive if=none,id=uas-disk1,format=raw,file=/path/to/file1.img  \
 -drive if=none,id=uas-disk2,format=raw,file=/path/to/file2.img  \
 -drive if=none,id=uas-cdrom,media=cdrom,format=raw,file=/path/to/image.iso \\
 -device nec-usb-xhci,id=xhci                                    \
 -device usb-uas,id=uas,bus=xhci.0                               \
 -device scsi-hd,bus=uas.0,scsi-id=0,lun=0,drive=uas-disk1       \
 -device scsi-hd,bus=uas.0,scsi-id=0,lun=1,drive=uas-disk2       \
 -device scsi-cd,bus=uas.0,scsi-id=0,lun=5,drive=uas-cdrom
usb-bot

Bulk-only transport storage device. This presents the guest with the same USB bulk-only transport protocol interface as usb-storage, but the QEMU command line option works like usb-uas and does not automatically create SCSI disks for you. usb-bot supports up to 16 LUNs. Unlike usb-uas, the LUN numbers must be continuous, i.e. for three devices you must use 0+1+2. The 0+1+5 numbering from the usb-uas example above won't work with usb-bot.

usb-mtp,rootdir=dir

Media transfer protocol device, using dir as root of the file tree that is presented to the guest.

Other available devices are: usb-ccid, usb-audio, usb-kbd, u2f-{emulated,passthru}, canokey, etc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .