Ubuntu – How to simulate a USB plug


I have an app that implements some automations when an USB key is plugged.

While developing, I have to test it in a specific environment (derivative from Debian Squeeze).
The app subscribe to HAL events via DBUS.
The problem is: my desk is on the third floor while my test machines are in the basement.

I would really like to have a way to simulate a USB plug.

  • usbip does not work on Ubuntu.
  • I can't manage to use the g_mass_storage module on Ubuntu (I lack the dummy_hcd module) and in Debian.

Maybe it is possible to simulate a dbus call?

My work station is on Ubuntu 12.04.

Best Answer

I found another solution that suited me. In fact I realised that I did not really need to simulate an USB drive, but rather any type of storage device (my app watches HAL or UDisks for "DeviceAdded" events).

So I did the following:

  • create a filesytem image, for example with the "virt-make-fs" command
    sudo virt-make-fs --type=ntfs -- fs.tar.gz fs-test.img

  • upload the img file on the test machine

  • setup the image as a loop device: sudo losetup /dev/loop0 fs-test.img

And that's it, UDisks just triggered the "DeviceAdded" DBus event!
(however, HAL does not trigger anything in this case, so it's a good opportunity to drop it since it is deprecated)

If you want, you can manually mount the image with something like sudo mount /dev/loop0 /mnt/fs.

If you need to use pmount, don't forget to add /dev/loop0 to /etc/pmount.allow.

To unset the loop device, just use sudo losetup -d /dev/loop0.

Related Question