Introduction: NAS Access for Non-Networked Devices
So you have a device that can read files/picture/video etc from a USB stick but has no network support, or you have a PC you don't want on your network but do want to share some files with.
This instructable will show how, with a RaspberryPi and a little magic you can make your NAS appear as a USB storage device.
Step 1: Limitations
- Your NAS must support ATA over Ethernet (aka AoE)
- AoE is not routable. Access is only possible on the local LAN not over the internet.
- Due to limitations of consumer devices (and to a lesser extent windows) an entire device must be exported.
- Clients will likely be able to mount all partitions on the disk. For one way around this see Step 7.
- The data you want to share must be in the first partition of the disk. The second etc. partitions may be mountable but that depends on the device.
- For best compatability with consumer devices (and Windows) that partition should be formatted as FAT32
- Due to the low level of disk access used read/write access is possible from only one device at a time, all others must be disconnected in order to avoid file system corruption. This includes the server. Simultaneous read only access is possible, as is simulanteous read only sharing via NFS, CIFS, etc.
Step 2: Requirements
- A NAS.(not pictured)
- A wireless network. (not pictured)
- A RaspberryPi ZeroW
- A microSD card
- A micro-USB cables
Optionally during setup:
- 5v USB power supply
- Micro-USB OTG adapter
- Mini HDMI male to female HDMI adapter
- USB Hub
- USB keyboard and mouse
- Monitor and HDMI cable
Step 3: Setting Up the Server
As there is a wide variety of NAS systems in the wild I'm going to base these instructions on my NAS: a Debian Jessie based linux PC. If you NAS is a raspberry pi, these instruction should work as raspbian is derived from debian.
All command are to be run in a terminal on the server (e.g. via ssh). I'm assuming that the HDD you want to share is already correctly formatted, mounted, and contains data.
1. Update local package lists:
sudo apt-get update
2. Optionally (but recommended) upgrade installed packages:
sudo apt-get upgrade
3. Install the vblade package:
sudo apt-get install vblade
Vblade is the server side tool for exporting block devices over AoE.
4. Identify the device you want to share. This must be the entire device, not just one partition. In my case this is /dev/sde
5. Edit /etc/fstab and ensure that all partitions on the device are being mounted read only (add "ro" to the mount options)
7. Identify which network interface you'll be using to share the device, most likely eth0. You can get a list of interfaces by running
8. Decide on shelf and slot numbers for the export. The combination of shelf and slot number must be unique for each export across your entire network.
9. Setup the export.
sudo vblade-persist setup <shelf> <slot> <network device> <device> sudo vblade-persist auto <shelf> <slot> sudo vblade-persist start <shelf> <slot>
sudo vblade-persist setup 0 0 eth0 /dev/sde sudo vblade-persist auto 0 0 sudo vblade-persist start 0 0
Step 4: Setting Up the Pi ZeroW
As before all commands must be executed in a terminal.
1. Follow the guides here to setup your Pi ZeroW. While doing so you'll need to ensure it can access your wifi network and enable ssh.
2. Edit /boot/config.txt, add
3. Update and upgrade installed packages:
sudo apt-get update sudo apr-get upgrade
4. Install the ATA over Ethernet client tools:
sudo apt-get install aoetools
5. Load the AoE module at boot.
Edit /etc/modules and add
6. Load the aoe module:
sudo modprobe aoe
7.. Check that the AoE device can be found and accessed:
sudo aoe-detect && sleep 5 sudo aoe-stat
This should return something like:
e0.0 60.011GB wlan0 1024 up
"e0.0" will depend on the shelf and slot numbers used above. Size will depend on the size of the device being shared. AoE block devices are found under /dev/etherd/
Temporary mount the device:
sudo mount /dev/etherd/e0.0p1 /mnt
Check the output of
Unmount the drive
sudo umount /mnt
8. Setup the mass storage gadget on boot.
Download the attached aoeinit.bash. It takes one argument, the path to the an AoE device, does some basic validation and starts the g_mass_storage kernel module. Don't just take my word for it though, read it through it before using it for your own peace of mind.
Copy aoeinit.bash to /usr/local/sbin and make sure it is marked as executable:
sudo cp aoeinit.bash /usr/local/sbin sudo chmod /usr/local/sbin/aoeinit.bash +x
Edit /etc/rc.local and add
/usr/local/sbin/aoeinit.bash /path/to/aoe/device &
above the "exit 0" line.
/usr/local/sbin/aoeinit.bash /dev/etherd/e0.0 &
Do not forget the &
9. Reboot and you're done.
Step 5: Changing Shared Files
Because of the risk of filesystem corruption with read/write access from multiple clients, you need to go through a few steps when you need to change any files on the shared device.
All steps below must be run in a terminal on the server.
1. Stop the vblade server process for the device:
sudo vblade-persist stop <shelf> <slot>
2. Remount the partitions on the shared device as read/write:
sudo mount -o remount,rw <mount point or device path>
sudo umount <mount point or device path> sudo mount -o rw <mount point or device path>
Only the partitions containing data that is being changed need to be remounted.
3. Make all desired changes
4. Remount partitions as read only:
sudo mount -o remount,ro <mount point or device path>
sudo umount <mount point or device path> sudo mount -o ro <mount point or device path>
This step may fail due to open files. If it does, either wade through lsof and sync, or reboot.
5. Restart the vblade server process:
sudo vblade-persist start <shelf> <slot>
6. Reboot the Pi ZeroW
Step 6: A Note on Security
Given that one of the reasons for doing this is isolating the end machine from the network, the are a few things that can be done to help improve on this. The Pi ZeroW can be a weak point in that the default user and password are well know; and in that its SD card can be easily remove and modified.
The following suggestions should improve things
1. Change the default user to something other than "pi"
2. Use a strong password. Especially when keeping the "pi" user.
3. Disable ssh if remote access is not required.
There's not much that can be done about the SD card other than using a cse that makes removint non-trivial.
Step 7: Options and Tweaks
1. You don't have to use FAT32. You can use any filesystem that is understood by the end (USB host) device.
2. The default behaviour for the aoeinit.bash script is to start g_mass_storage as read only. If you want read/write acess rename aoeinit.bash to aoeinit-rw.bash (or use a sym-link) and use this name in rc.local.
3. Withouh using ssh there is no way to safely shutdown the Pi ZeroW before power off which could lead to data corruption. There are several online tutorials on running a Pi with a "read only root" than can be used to avoid this.
4. If you don't want to share an entire physical device, you can use a file instead:
Create the file:
dd if=/dev/zero of=/path/to/file.img bs=1024k count=<size>
fallocate -l <size> /path/to/file.img
dd is slow but fills the file with 0s, fallocate is fast but old data will be present and readable.
Use the usual linux disk management tools (fdisk, gparted, etc) to partition and format the file as a disk:
sudo fdisk /path/to/file.img
In the server instructions above, use /path/to/file.img in place of /path/to/device. For local access (and acess to clients via NFS etc) use a loopback mount:
sudo mount -o loop,ro /path/to/file.img /mount/point
We have a be nice policy.
Please be positive and constructive.