Make NAS appear as a USB drive

My TV can record to USB-storage, which is quite common. What I would like was to record to my NAS instead, where there's plenty of room and recordings can be shared and managed better. So I'm looking for an adapter-thingy (hw/sw) that will make my NAS appear to the TV as a USB block storage device. Any ideas? Googling has turned up nothing.

Please note that this is the exact opposite of the very common scenario of making a USB device available as a networked drive. There are tons of solutions for that, so let me stress that I'm looking for the opposite: make my NAS appear as a physical USB drive that can plug straight into my TV, like so:

[NAS] ——— Ethernet ——— [adapter] ——— USB ——— [TV]

Solution 1:

I know that this post is quite old but I've stumbled across it when I was looking for a solution for the same problem. I haven't seen a ready-made solution that you can buy, but several do it yourself (DIY) approaches.

Those DIY approaches are therefore all based on building your own "adapter". The adapter is a Linux-based computer, e.g. a single-board computer (SBC) such as the Raspberry Pi. It acts as a USB mass storage device ("flash drive") to your smart TV on the one side and accesses (read and write) a network attached data storage (NAS) on the other side.

                 iSCSI via                              Mass Storage
                TCP/IP via                            Device Class via
[NAS] ————— Ethernet or Wi-Fi ———— [Linux computer] ———————— USB ————————— [Smart TV]

Matt Olan has realized this approach earlier this year when he has tried to store games and backups of his PlayStation 4 earlier this year: https://matt.olan.me/making-a-piscsi-usb-drive-part-1/ and https://matt.olan.me/making-a-piscsi-usb-drive-part-2/ - he has also done some performance measurements.

Getting started with an installation of the Linux operating system (OS) on the SBC

If you use a Raspberry Pi based SBC you should start with a Linux installation. The current version of the Raspberry Pi OS (previously called Raspbian) can be downloaded from https://www.raspberrypi.org/software/.

You can then also follow the guide on https://www.raspberrypi.org/documentation/configuration/wireless/headless.md to set up your Raspberry Pi headlessly, i.e. you can already modify the contents (the boot folder) on the SD card. You can also start with SSH being enabled (by creating a new empty file named ssh inside the boot directory). Then use raspi-config to change further settings.

Providing a mass storage device to your smart TV

Usually a computer acts as a USB host. What we need here instead is a computer acting as a USB slave device. The hardware of the computer needs to provide a USB On-The-Go (OTG) port. In Linux speak this kind of system is also called a USB gadget. You will need to modify your Linux installation.

The USB driver dwc2 needs to be used in /boot/config.txt and /etc/modules.

The "Mass Storage Gadget" (MSG) is used here. It "provides support for the USB Mass Storage class. It can appear to a host as a set of up to 8 SCSI disk drives (called Logical Units often referred to as LUNs, even though it technically stands for Logical Unit Number), although most of the time a single LUN is all you will need. The information stored for each LUN must be maintained by the gadget somewhere, either in a normal file or in a block device such as a disk partition or even a ramdisk. This file or block device is called the backing storage for the gadget (...)." (source: http://www.linux-usb.org/gadget/file_storage.html)

The module g_mass_storage is loaded via modprobe pointing to that backing storage:

sudo modprobe g_mass_storage file=/dev/sd... stall=0

We will use a block device as provided via iSCSI, see below.

(There's also a nice article about only making the Pi Zero W a USB flash drive, so without the connection to the NAS on the other side. Some parts still apply for our scenario: https://magpi.raspberrypi.org/articles/pi-zero-w-smart-usb-flash-drive - Hint: If you take this approach make sure that your container file is large enough for your Smart TV. I just discovered that my LG TV only accepts flash drives with a capacity of at least 4 GBytes.)

Acessing your NAS

Limitation: You cannot directly provide access to a directory shared in the network as a backing storage for the gadget. The underlying communication protocol of your NAS often is Server Message Block (SMB) or Common Internet File System (CIFS) or Network File System (NFS).

"The g_mass_storage module needs a block device or a flat file, most network shares aren't either." (source: https://www.raspberrypi.org/forums/viewtopic.php?t=220468)

Instead, it requires your NAS to provide an iSCSI, an Internet Small Computer Systems Interface, "an Internet Protocol (IP)-based storage networking standard for linking data storage facilities. It provides block-level access to storage devices by carrying SCSI commands over a TCP/IP network. iSCSI is used to facilitate data transfers over intranets (...)." (source: https://en.wikipedia.org/wiki/ISCSI)

As it is based on TCP/IP networks, it is basically independent from the underlying link layer, i.e. you should be able to access it both via a wired Ethernet connection or wireless Wi-Fi. This depends on your local setup. Keep in mind, that the throughput via Ethernet should be higher.

You need to ask your NAS' user manual on how to enable it as an iSCSI target.

  • QNAP how-to
  • Synology how-to
  • ...

On the SBC running Linux, you need to perform some more steps to initiate an iSCSI connection to your NAS. Matt Olan has a pretty nice description in the "Method" section of https://matt.olan.me/making-a-piscsi-usb-drive-part-1/ - it also describes the previous steps of setting up the mass storage gadget as sketched above.

Hardware selection hints

  • Some of the Raspberry Pi SBCs connect their USB to a USB hub or tie the USB ID pin permanently to ground on their printed circuit boards. This does not make them usable as USB slave device via USB OTG.
  • Dependent on your SBC, you may only have either Wi-Fi or Ethernet connectivity.
  • You might want to pick a SBC with USB 3.0 OTG to achieve higher data rates. Make sure that "super-speed" is used and not "high-speed" only.
  • Raspberry Pi Zero W and the Raspberri Pi 4 Model B seem to work (no guarantee).
  • Matt Olan has first used a Raspberry Pi Zero W (USB 2.0 OTG, WiFi) and then a RockPi 4b (USB 3.0 OTG).

Alternate way without iSCSI support

If your NAS does not provide an iSCSI, a different approach can be taken. The files on the NAS are accessed via SMB.

           SMB or NFS or … via                          Mass Storage
            usually TCP/IP via                        Device Class via
[NAS] ————— Ethernet or Wi-Fi ———— [Linux computer] ———————— USB ————————— [Smart TV]

But, as mentioned earlier, SMB access is not supported with g_mass_storage and this is why we need to take a work-around:

  • a backing storage on the adapter is created and provided to the Smart TV (USB host)
  • it needs to be created as a file with a filesystem in it
  • this is done by using dd, fdisk and mkdosfs (for partitioning and creation of a FAT32 filesystem; the settings like sectors and cylinders should be chosen to match g_mass_storage and your file)
  • access on the gadget can be performed using the loop device driver to map the partition with the backing storage, then mount it (mount -t vfat)
  • mutual exclusive access of the backing storage: it can only be accessed by either the USB host or the adapter itself at once: "Only one system (normally, the USB host) may write to the backing storage, and if one system is writing that data, no other should be reading it." (source: http://www.linux-usb.org/gadget/file_storage.html)
  • therefore, the data needs to be copied between adapter and NAS:
    • e.g. from the adapter to the NAS after a movie has been recorded
    • e.g. from the NAS to the adapter before watching a recorded movie from the adapter
  • further info and instructions can be found here: http://www.linux-usb.org/gadget/file_storage.html (English), https://raspberrypi.stackexchange.com/questions/112957/mount-smb-share-as-file (English) and https://forum-raspberrypi.de/forum/thread/36132-usb-gadget-mass-storage-emulation/ (German)

The drawback of this approach is that you need to synchronize/schedule accesses to the backing storage. Copying the data from A to B and vice versa will take some time and you will need additional steps to automate this.

Other attempts

  • I do not recall the reference but the approach mentioned above seems to require a Linux kernel version >= 4.4.
  • If your Smart TV supports the Media Transfer Protocol (MTP) you may be able to take a different approach.
  • Matt Olan has mentioned that some "NAS manufacturers such as QNAP offer support for direct USB storage"
  • I've read short summaries about QEMU's Virtual VFAT Filesystem (vvfat), but I am not sure if it can be applied in this scenario.

Solution 2:

I looked a bit further and the "Infinite USB Memory"-stick from Infinitec claims to do just this. Unfortunately it gets a pretty bad review from Engadget where it was reviewed yesterday (what an incredible coincidence, as I've been thinking about this for quite some time!). But while this particular product may not work very well it does actually offer the exact functionality I was looking for.

Solution 3:

If your NAS is a Linux-based PC, maybe, just maybe you might, with a lot of work, get something like this PCI USB 2.0 Peripheral Controller to work. This PCI card adds a "peripheral side" USB port to your system, NOT a "host side" port. So the possibility is to turn your entire PC into a USB device connectible to another PC. I know Linux has support for "USB Gadgets" - with the proper programs running it would certainly seem possible.

If your NAS can expose Samba shares, you might be able to do something with a rooted Android phone. On my Droid, the SD card appears internally as /mnt/sdcard. Running Samba on it is certainly possible. I've never tried anything like mount -t cifs //192.168.X.X/mysambashare /mnt/sdcard/mysambashare and then tried to use the phone as a USB storage device ... If I have time to try it, I'll update.