4
VBoxManage list usbhost

shows the physical flashdrive if I plug it in my notebook.

Question: But can I emulate a USB flashdrive without a physical one? So that it will show up in the "VBoxManage list usbhost" output, so I can attach it as USB device in a running VirtualBox guest.

  • Maybe this question, which refers to this project, helps. You'd still have to implement the USB storage protocol. So: Yes, in principle that can be done, but I'm not aware of any ready-made tool for it. It would be easier to emulate a generic block device. – dirkt Jan 17 '17 at 16:29
6

To my surprise, there's already a ready-made way to emulate USB devices:

1) Load the dummy_hcd module. This is a loopback module that acts both as a USB gadget controller driver (client emulation), and a USB root hub (host emulation):

$ sudo modprobe dummy_hcd
$ lsusb
...
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

(This is a new root hub).

2) Make an empty file that represents your storage device:

$ dd bs=1024 count=128 if=/dev/zero of=/tmp/backing_file

(This is a small one, make it larger as appropriate)

3) Load a gadget driver module to emulate the USB mass storage device (flash etc.):

$ sudo modprobe g_mass_storage file=/tmp/backing_file idVendor=0x1d6b idProduct=0x0104 iManufacturer=Myself iProduct=VirtualBlockDevice iSerialNumber=123
$ lsusb
...
Bus 005 Device 004: ID 1d6b:0104 Linux Foundation Multifunction Composite Gadget
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ lsscsi
...
[7:0:0:0]    disk    Linux    File-Stor Gadget 0408  /dev/sdh 

I picked some sort-of sensible idendtification, you can leave these options out, and then you seem to get random ones.

4) You can do that with other types of USB devices as well, as long as there are gadget modules available for them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.