Intro: Raspbery Pi Wireless Auto-Sorting NAS/Media Server Using MiniDLNA and Samba
I have a couple of spare USB hard drives lying around, and of course a Raspberry Pi (headless). I put them to use as a wireless NAS / Media server. I'd also like my clients to be able to make the Pi automatically download torrents and save them on the NAS. I've already posted a guide to set up your Pi as a wireless access point. Just ignore the bit about 3g if it's not relevant!
To do this, I'll use Samba to share the files over my network (a mixture of Linux and Windows clients), and MiniDLNA to manage media serving. MiniDLNA will allow any UPnP compatible device on your network, such as a television or games console, to access the shares on your Pi. qBittorent will be used as the torrent client. Fsniper will be used for auto-download feature..
I also use fsniper to automatically sort all locally downloaded content into the appropriate files on the NAS.
As an alternative to this, you could use an operating system like FreeNAS or XMBC. I prefer to keep Raspbian and bolt on the extra bits I need.
This guide assumes you are using the 06-12-12 of Raspbian. It should also work on the latest version, but I can't guarantee that as I've not had chance to test it yet. I used the following IP scheme:
Pi Ethernet Port: 192.168.1.10/24
Pi wlan0: 10.0.0.1/24
Note: external hard drives on the Pi should be connected via a powered hub.
Step 1: Automount USB Hard Disks & Ntfs-3g
To use USB hard disks in your NAS setup, we need to make sure they auto-mount at boot. We also need to make sure that if the use the NTFS file system, we are allowed to write to them - this is what ntfs-3g is for. Install ntfs-3g from the terminal:
sudo apt-get install ntfs-3g
Create mount point directory
First, we need to make some folders for the disks to be mounted to. As an example, we'll assume there is a USB hard disk with a single partition, formatted with a NTFS file system. It contains all of your legally owned videos and music, so we will call it 'Media'. Make a folder for the drive to be mounted to, and change the permissions so everyone on the network can read and write to and from it:
sudo mkdir /media/Media
sudo chmod 777 /media/Media
Now, let's set the disk to auto-mount to that folder at boot. We will edit a file called fstab, which deals with all the file systems that are mounted at boot on your Pi. We need to gather some information about the drives first, so in a terminal type:
blkid is a tool we can use to a drives metadata - specifically the UUID. This is what we are going to make fstab use to identify the drives. Drives are automatically assigned to files in the /dev folder, but these assignments are not guaranteed to be the same every time your Pi boots, so we will use the UUID.
We are interested in the UUID of each drive/partition you wish to mount on your NAS. My drives were formatted and partitioned on a Windows client some time ago, and are easily identifiable by their labels. Copy the full output of blkid, then type:
sudo nano /etc/fstab
This will open the fstab file for editing. Bellow the text that is already there, leave a bit of space then paste the output form blkid. We will be deleting this later, but I think it's easier to have all the info infront of you - this won't be an issue on a Pi setup with a monitor, but I was doing this headless so it worked better in that situation.
Entries in fstab are in this format:
Device Name | Mount Point | File System | Options | Dump Options | FS Check Options
One of my entries as an example:
Device Name Mount Point FS Options DO | FSC O
/dev/sda /media/Ipod vfat auto,user,rw,exec 0 0
In the device name column, UUID= and Label can also be used. All entries are seperated by a single tab. Your entry should be structured like this:
Device Name: Use the UUID identified in the blkid output, without the quotes.
Mount Point: In the Mount Point column, enter the location of the mount point we created earlier.
File System: The information for the File System column can be found in the blkid output, after the word TYPE. The file system for FAT32 drives is vfat, and for NTFS drives you should use 3g-ntfs.
After these three columns, paste auto,user,rw,exec 0 0
So the entry for the fictional 'Media' drive would be (I'm just gonna make up a UUID):
UUID=5d2047b5-6eaf-46de-9a52-a3a19dfc3706 /media/Media ntfs-3g auto,user,rw,exec 0 0
Repeat this process (including creating a mount folder) for each drive or partition you want to share. Be sure to delete the output from blkid that you pasted in before you save and exit.
To test your new entries in fstab, in the terminal type:
sudo mount -a
This will attempt to mount all entries in fstab, unless they are already mounted. The second command will output information about currently mounted drives, which will indicate wether or not the entries in fstab are valid. If you see the mount folders you created listed in the output of df, navigate to them and list their contents to confirm all is well.
Make sure you have nailed this part before carrying on - your Pi will have problems booting if there are faulty entries in fstab. If this occurs, you should be given the option of a terminal prompt, at which point you can open fstab in nano like before and remove the bad entry.
Step 2: Install and Configure Samba
Samba is a suite of programs that allows Linux and Windows clients to easily use the same shares. There's a lot more too it than that, so check the Samba website for more info if you're interested.
Install Samba from the terminal:
sudo apt get install samba
Once it's finished downloading and installing, we need to configure it. I make a backup of the 'out of the box' config file, delete the original and then start with a blank canvas:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf_bak
sudo rm /etc/samba/smb.conf
sudo nano /etc/samba/smb.conf
Paste the following into nano:
#### GLOBAL CONFIG #####
workgroup = home
netbios name = nas
server string = %h
wins support = yes
dns proxy = no
security = user
null passwords = yes
guest account = nobody
hosts allow = 127.0.0.1 192.168.137.0/24 10.0.0.0/24
hosts deny = 0.0.0.0/0
interfaces = eth0 wlan0 lo
bind interfaces only = yes
#### PUBLIC SHARE #####
comment = Media Drive
path = /media/Media
browseable = yes
guest ok = yes
writeable = yes
public = yes
available = yes
create mask = 0666
directory mask = 0777
The text above is a really simple Samba config that works for me. I've highlighted the parts that will need your attention:
Hosts Allow defines the networks or individual hosts which will be permitted to access the shares.
Hosts Deny defines the networks or individuals not permitted. The entry I've used explicitly denies everyone not in the allow list.
Interfaces defines the interfaces Samba will allow connections on - if your Pi has a direct connection to the internet, do you want your Samba shares to be accessible via that interface?
Bind Interfaces Only tells Samba to use only those interfaces in the Interfaces list.
Make a public share for each drive or partition you mounted with fstab. Once you have finished with smb.conf, save changes and exit (ctrl+x, y, Enter). Restart the Samba service to apply the changes:
sudo service samba restart
If you have a Windows machine on the same network as the Pi, open a command prompt and type:
net view \\10.0.0.1
Replacing the IP address with that of your Pi's. This should show a list of the shares you defined in smb.conf, confirming your shares are accessible. Samba can be a bit of a mysterious beast at times, so if you have any problems leave a comment and I'll try and help you. I am by no means an expert though :)
Step 3: Access Shares on Windows/Linux Client
Each flavour of windows has a slightly different route to provide the credentials required to access the Samba shares on your Pi via the gui, so I am going to avoid that completely and use the command line. If that disturbs you in any way and you really want to do it via the gui, let me google that for you!
Open a command prompt, and type:
net view \\IP_ADDRESS_OF_PI
This will list the shares published by Samba on your Pi. In order to map the drive so you can see it in My Computer we will use the net command, type the following (I'm continuing to use the 'Media' share example):
net use z: \\IP_ADDRESS_OF_PI\Media /pi:raspberry raspberry /P:Yes
The 'z:' argument specifies which drive to assign the share to. I'm assuming you don't have a 'Z' drive. The next part specifies the location of the Samba share on the network. The following part supplies the credentials in the format USER:DOMAIN PASSWORD. I have used the Raspbian default credentials in this example - change as necessary for your setup. The final argument, 'P:Yes' tells net to auto-connect the share at start up.
There should now be a browseable Z drive, labelled 'Media' in My Computer!
There are many more flavors of Linux than there are of Windows. As such, I'm only going to cover the method I used to access the Samba shares - the OS in question is Linux Mint 13 Xfce. This should work for most Debian based systems though.
First we have to install smbfs, which is the file system we will specify in fstab. Next create an entry in fstab, using the same method as we did when mounting the shares on the Pi (remember to create a mount point directory!):
//10.0.0.1/Media /media/Media smbfs credentials=/home/USERNAME/creds,gid=users 0 0
The UUID has been replaces by the network location of the share, the file system column contains 'smbfs' and in the options we have the location of a file containing the login credentials for the shares. We need to create that file now. While in your home directory:
sudo nano creds
Now paste the following information - again, I have used the default Raspbian credentials, change as necessary:
Mount the shares using the same 'mount' command as you did on the Pi. They should now be
browsable through the file manager!
Step 4: Setup Torrent Auto-download and Download Auto-sort
I set all my browsers on both Windows and Linux to download to the same Downloads folder on a NAS share. I use fsniper installed on the Pi to monitor this folder, and send anything that is created in (downloaded to) it to the relevant folder. For example, image files to an /image folder, music files to a /music folder and executable files to an /apps folder. It also sends any torrents to a folder which is monitored by my torrent client, qBittorent - when the client sees a new torrent file appear in the folder, it automatically downloads it.
To install fsniper on the pi:
tar -xvf fsnuper-1.3.1.tar
sudo apt-get install libmagic-dev
sudo make install
Now we need to create some rules for fsniper. As an example, we'll make a rule which moves all torrent files to the /Torrents folder. This will in turn be monitored by our torrent client.
handler = mv %% ~/torrentMonitor
This config causes fsniper to scan the /media/Downloads directory. Whenever a torrent file appears in the directory, it moves it to the /torrentMonitor folder in the home directory on the Pi. The configs should be pasted into the fsniper config file, which can be opened with:
sudo nano /home/pi/.config/fsniper/config
To make fsniper start on boot, add the following line to /etc/rc.local:
We will be using qBittorrent to monitor the folder all torrents will be placed in by fsniper. As soon as it sees a new one, it will download it. We will be using the command line version, controlling it via a web interface - this means we can control it from any client on the network.
Install and start qBittorrent with:
sudo apt-get install qbttorrent-nox
Access the web interface using a browser on any client on the network, pointed to IP_ADDRESS_OF_PI:8080. The default user:pass is admin:adminadmin.
The option for monitoring a directory, as well as the directory to download to are outlined in the picture at the top of the page.
Steps for configuring monitored directory.
Github has instructions and an init.d script to make qBittorrent run at startup.
Step 5: Setup MiniDLNA
MiniDLNA will scan all folders you tell it for movies, audio and pictures. It will then make these available to any UPnP devices such as televisions and games consoles.
Install MiniDLNA with:
sudo apt-get install minidlna
Next we need to configure MiniDLNA to scan the shared drives that contain your media. The Arch Linux wiki has a good example of a simple config file. Edit the config file with:
sudo nano /etc/minidlna.conf
MiniDLNA runs at boot by default.
Well, there you have it. A Raspberry Pi NAS which automatically sorts your downloads and streams them over your local network! I hope this guide was useful to someone, please leave a comment and let me know what you think :)