Mounting Files and Devices as USB Mass Storage

Background
Devices running WebOS use a usb mass storage driver written by Mike Lockwood in 2008. The driver source can be found in the following location in the source tree: linux-2.6.24/drivers/usb/gadget/f_mass_storage.c

This driver calls DEVICE_ATTR to create a file named "file" in sysfs. When the sysfs file entry is written to, the function store_file is called within the driver. store_file will mount the file or device that is passed in as an argument.

Mounting Examples
The following examples illustrate how to use this special file, you must be root since the "file" entry has permissions 644.

Mount /media/internal echo /dev/mapper/store-media > /sys/devices/platform/musb_hdrc.0/gadget/gadget-lun0/file

Create a 256 MB image and mount it dd if=/dev/zero of=/media/internal/test.img bs=1M count=256 echo /media/internal/test.img > /sys/devices/platform/musb_hdrc.0/gadget/gadget-lun0/file

Applications
There are numerous applications that could benefit from using this feature. Many game consoles write to generic fat16/32 partitions, unfortunately they typically want to format the entire partition for their use. Using this method, a game console CAN format the entire partition (2nd example above) without wiping out precious user data.

Sample Service
A node.js service for WebOS 2.x that implements the base functionality to mount user-specified files can be found here