XAPBob
Posts: 92
Joined: Tue Jan 03, 2012 2:40 pm

Virtual USB stick using NFS...

Fri Mar 06, 2020 8:57 pm

Recently found (here on the forum) that the RasPi4 USB-C port can be used in gadget mode, and is unrelated to the USB A ports.

That means, if I’m not mistaken that I’ll be able to have the pi act as a gadget AND have wired ethernet...

Last time I looked at the requirements on a PiZero for being a “smart usb stick” they all seemed to expose a virtual file system..

Is there an existing mechanism to expose an NFS share (even if only read only)...
Would have a few interesting applications I can think of...

User avatar
topguy
Posts: 6466
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Virtual USB stick using NFS...

Sat Mar 07, 2020 7:02 pm

So what you want to do is..
- Plug the Pi ( the first computer) into a second computer and basically share the files located on a third computer.
My guess is that you want to share the files from a NAS to a media-player or TV which only can play media from usb-stick/disk.. am I close ?

The problem is that memstick emulation does not emulate a filesystem, it emulates a block-device with a filesystem on top of that, that is why the guides specify that you need to create a big file to represent the block-device. And frankly I can not think of any way to link that to any networked filesystem unfortunately.

User avatar
thagrol
Posts: 2961
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Virtual USB stick using NFS...

Sat Mar 07, 2020 11:09 pm

XAPBob wrote:
Fri Mar 06, 2020 8:57 pm
Recently found (here on the forum) that the RasPi4 USB-C port can be used in gadget mode, and is unrelated to the USB A ports.

That means, if I’m not mistaken that I’ll be able to have the pi act as a gadget AND have wired ethernet...

Last time I looked at the requirements on a PiZero for being a “smart usb stick” they all seemed to expose a virtual file system..
Nope. The USB host sees a block device. Software on the host then handles the filesystem level stuff.

If by "virtual file system" you mean the mass storage gadget's ability to use a flat file as its backing store, it isn't using it as a "virtual file system" it's using it as a virtual block device.
Is there an existing mechanism to expose an NFS share (even if only read only)...
Would have a few interesting applications I can think of...
Nope. At least not with NFS. While you can have a 4B running as a mass storage gadget and still have access to the USB A ports and ethernet what you want isn't possible due to the way the usb mass storage gadget works.

The g_mass_storage module (and mass storage via libcomposite) provides the host with block level access to its backing storage and requires the backing storeage be an entire block device, a partition, or a flat file. An NFS export (or smb/cifs share) share is none of those things.

If using the flat file storage option, the file can be located on a network share but you'd need to jump through a few hoops to get stuff on to it.

There is a different approach that gets close to what you want: use a SAN protocol to expose a partition or block device over your network. See my write up here: https://www.instructables.com/id/NAS-Ac ... d-Devices/

The mass storage gadget works the same way on zero, zeroW A, A+, 3A+ and 4B. It's the same driver module and USB hardware.

Be aware that whichever option you choose for your backing store attempting to write to it from both Pi and USB host at the same time will corrupt your data. Writing to it from one side whist reading on the other will lead to the reading side missing changes.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

User avatar
thagrol
Posts: 2961
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Virtual USB stick using NFS...

Sat Mar 07, 2020 11:14 pm

topguy wrote:
Sat Mar 07, 2020 7:02 pm
So what you want to do is..
- Plug the Pi ( the first computer) into a second computer and basically share the files located on a third computer.
My guess is that you want to share the files from a NAS to a media-player or TV which only can play media from usb-stick/disk.. am I close ?

The problem is that memstick emulation does not emulate a filesystem, it emulates a block-device with a filesystem on top of that, that is why the guides specify that you need to create a big file to represent the block-device. And frankly I can not think of any way to link that to any networked filesystem unfortunately.
There are at least two :):
  • Stick the big file on your network server. Loopback mount it on the server for local access (but be careful with write access you can trivialy corrupt it).
  • Don't use NFS or Samba. Use a SAN* protocol to export a partiton or block device. (Similar issues with write access from multiple devices)
*SAN = Storage Area Network

Edit:
And there's the method the MagPi suggested but that I wouldn't recommend. Put the shared storage on the zeroW, copy stuff to/from that. Reload g_mass_storage when done updating.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Return to “Other projects”