Sunday, October 8, 2017

Linux USB Mass Storage Emulation


My purpose is to emulate an USB device locally in a Linux environment. I mean that I want to “mount” a local backing storage file (made with dd) as an USB device inside the root user workspace.


After a Google search, I found the Linux USB Gadget API and the Mass Storage Function : http://www.linux-usb.org/gadget/file_storage.html. But it's not crystal clear : it seems that it can act only as client/server with USB connection between a Linux host and another computer (Linux, Windows or Mac).


I started to set a POC on a Debian Jessie with customized 3.16.7 kernel, but I’m facing the following error "insmod: can't insert 'g_mass_storage.ko': No such device" when I try to load the g_mass_storage module.


Has anyone experienced this kind of stuff ?
Thanks.


Mathieu


Answer



Tom Yan,


Thanks for your useful answer.
I successfuly loaded the dummy-hcd module. Then, modprobe of the g_mass_storage module :


modprobe g_mass_storage file=/root/myUSBStorage/mass_storage.img stall=0 removable=y iSerialNumber=1234567890


I have now the following dmesg :


[  205.564158] g_mass_storage gadget: Mass Storage Function, version: 2009/09/11
[ 205.564164] g_mass_storage gadget: Number of LUNs=1
[ 205.564170] lun0: LUN: removable file: /root/mass_storage.img
[ 205.564191] g_mass_storage gadget: Mass Storage Gadget, version: 2009/09/11
[ 205.564194] g_mass_storage gadget: g_mass_storage ready
[ 205.780209] usb 3-1: new high-speed USB device number 2 using dummy_hcd
[ 205.952109] usb 3-1: New USB device found, idVendor=0525, idProduct=a4a5
[ 205.952124] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 205.952133] usb 3-1: Product: Mass Storage Gadget
[ 205.952140] usb 3-1: Manufacturer: Linux 3.11.0-15-generic with dummy_udc
[ 205.952145] usb 3-1: SerialNumber: 1234567890
[ 205.956075] g_mass_storage gadget: high-speed config #1: Linux File-Backed Storage
[ 206.020057] usb-storage 3-1:1.0: USB Mass Storage device detected
[ 206.020255] usb-storage 3-1:1.0: Quirks match for vid 0525 pid a4a5: 10000
[ 206.020477] scsi3 : usb-storage 3-1:1.0
[ 206.020856] usbcore: registered new interface driver usb-storage
[ 207.036061] scsi 3:0:0:0: Direct-Access Linux File-Stor Gadget 0311 PQ: 0 ANSI: 2
[ 207.036807] sd 3:0:0:0: Attached scsi generic sg2 type 0
[ 207.076081] sd 3:0:0:0: [sdb] 8192 512-byte logical blocks: (4.19 MB/4.00
MiB)
[ 207.088158] sd 3:0:0:0: [sdb] Write Protect is off
[ 207.088174] sd 3:0:0:0: [sdb] Mode Sense: 0f 00 00 00
[ 207.100210] sd 3:0:0:0: [sdb] Write cache: enabled, read cache: enabled,
doesn't support DPO or FUA
[ 207.180419] sdb: sdb1
[ 207.268281] sd 3:0:0:0: [sdb] Attached SCSI removable disk

FYI, I had to create the partition table and filesystem inside the emulation. I can now successfuly mount the emulated media.


mount
/dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/sdb1 on /mnt type vfat (rw)

Mathieu


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...