NAS Samba DLNA RAID server on RaspberryPi 3

In this tutorial we build a NAS Samba DLNA RAID server on RaspberryPi 3 Model B. The setup is headless, meaning we access the RaspberryPi only via remote SSH controls. No keyboard, mouse or display are required to be connected to the Pi. We use two external USB HDDs to provide sufficient space for all the music, videos and photos we want to store and play. In order to prevent data loss, we create a RAID 1 System to mirror the data on two HDDs.

1. Requirements

  • RaspberryPi 3 + power adapter
  • Micro SD card (recommended size 4 GB or above)
  • Raspbian Jessie, the operating system (download here)
  • SD Card Reader (in order to copy the operating system)
  • 2 equally sized USB HDDs (using external power or using an external powered USB switch)
  • Network/Internet access via LAN cable (to update packages etc.)

2. Download Raspbian to SD card

If you bought a SD card with a preinstalled operating system, you can skip this chapter. Otherwise we will download Raspbian Jessie onto the SD card. We use a 8 GB SD card. Since we did not have an external card reader available, we used a macbook with internal card reader to format the SD card and download Raspbian.

If you are using windows or linux the steps are the same, you just have to use different tools. From the mac we can download the image directly via shell commands. Windows, Mac and Linux solutions are explained on the RaspberryPi download page.

3. Assemble and start RaspberryPi

Unpack the RaspberryPi and put it into a case if available. Insert the SD card on the bottom side (opposite from the USB ports). Now connect the LAN cable (which should be connected to a router / switch for an internet connection to update and install some packages later on), and the USB power supply. You can setup WIFI later on as well, but the first time you need to access via LAN cable. Of course that only applies to the headless setup.

When powered on (connected the USB power adapter to power grid) you should see a yellow and green light working on the ethernet port, as well as a red and orange (toggling) light on the opposite side (both are red until booted, so give it some time).

Now we use an IP scanner to retrieve the IP from the RaspberryPi in order to connect via SSH. For Windows we can recommend the Advanced IP Scanner. For Mac we use the LanScan that can be downloaded in the AppStore.

Scan all the ranges or check ipconfig (Windows) or ifconfig (Mac/Linux) to see which ranges are possible.

Advanced IP Scanner output to get Raspberry PI IP
Advanced IP Scanner output

Now we see the RaspberryPi and can connect via a SSH tool. On Windows we use Putty. For Mac / Linux you already have a shell that works with SSH. On Mac open terminal and press CMD – Shift – k. There you should see an overview and configure your SSH connection.

We start Putty and use the IP provided by the IP Scanner.

Start putty and enter the IP
Start putty and enter the IP

Click open and log in as user pi with password raspberry.

User: pi Password: raspberry
User: pi – Password: raspberry

Now that we are connected, let us adapt some of the RaspberryPi configuration. Enter:

You will see the following output:

Raspberry Pi Configuration
RaspberryPi Configuration

We strongly recommend to perform option 1 to use maximum SD card space and option 2 to change the password. Do not forget your password if you change it. You can quit the configuration with Escape or CTRL-c.

Now we update libraries and reboot:

Enter these commands one by one and confirm any installation via “y”. After the reboot your terminal will close (because there is no active connection, the Pi is rebooting). Wait some time and reconnect via Putty. The Pi is now up to date and we can start creating the RAID system.

4. Create Raid System

We use mdadm, a Linux tool, to create software raids and available in most Linux distributions. First, download and install the package:

4.1 Connect the HDDs

Now plugin and power your external USB HDDs. They should be powered externally via own power adapters or via an externally powered USB switch. The Pi does not provide enough power to support two HDDs. Run:

or

to see if your external HDDs are available and connected. Compare size, file system type etc. to identify your USB HDDs.

Check fdisk or blkid to see if the HDDs are available
Check fdisk or blkid to see if the HDDs are available

Our disks are connected as /dev/sda1 and /dev/sdb1.

4.2 Remove partitions

Please backup any data you want to keep before proceeding, we build a new RAID system and assume empty HDDs. If your HDDs have previous partitions, we have to remove them using fdisk:

Enter “p” to get an overview of all available partitions. Press “d” to delete available partitions.

Repeat with the default number (always the highest) until all partitions are removed. Repeat for the second HDD (in our case /dev/sdb1).

4.3 Create RAID 1

Now we are set up to create the RAID 1 system. Be careful to adapt your commands if your /dev/**** device names differ. Adapt accordingly and enter:

The parameter –verbose enforces that we will see all debug output during the process. /dev/md0 is the path where we will access the RAID system. The parameter –level defines the RAID type. We use 1 for mirroring (data redundancy). You can use 0 for striping etc. We do not recommend striping, since you will not gain any performance due to the RaspberryPi´s limited BUS and LAN speed. You will however gain more storage. Finally, –raid-devices specifies the amount of HDDs we plan to use.

RAID 1 created successfully
RAID 1 created successfully

4.4 Wait to finish resync

The RAID system is now bulding up and syncing. Depending on your HDD size and speed this may take several hours, or in our case, days for 5 TB (about 70hours). Check the status manually via:

or use the watch command to get updates automatically:

Depending on the time (which shows the finish=xxxx in minutes output) you can quit Putty and do something fun 🙂

Check RAID assembly progress
Check RAID assembly progress

If successful, you should see a similar output:

The RAID1 was successfully created
The RAID1 was successfully created

4.5 Assemble RAID array

After the sync we can continue assembling the RAID array manually:

or automatically:

Lets add the array to our mdadm config so that it is available after a reboot. First make a backup and generate a new config based on the assembled arrays:

Your config should look similar to this:

Make sure you have an entry below “#definitions of extisting MD arrays”. If the automatic config generation did not work you can add it manually using an editor like nano:

Add your MD array definitions and exit with CTRL+X, press “y” to overwrite and “enter“. If you need additional information to add the array definition manually, try the following:

You should see a similar output:

This also verfies that the RAID array is assembled correctly.

4.6 Format the RAID system

If the RAID is correctly initialized we can format it to ext4 file system:

4.7 Mount the RAID system

Now we create a mount point in /media/PI_NAS_5TB. Please adapt your path choice accordingly. We set owner and permissions to the folder.

Note that setting permisisons 777 (everybody can read, write and execute) is considered unsafe. However, i would suggest you start using all permissions in order to avoid problems later on. As soon as everything is running smoothly, you can start adapting the permissions and ownership.

We have to get the UUID in order to auto mount the RAID system in /etc/fstab, to be available after a reboot.

Let us make a backup of the /etc/fstab before editing:

If anything goes wrong, you can revert to the original /etc/fstab:

Now we add our RAID system to /etc/fstab. Adapt your UUID to the output from blkid accordingly:

We use the UUID to identify our RAID. We mount the RAID to the folder we created earlier (/media/PI_NAS_5TB) with read and write (rw) access. The columns are seperated with tabs instead of spaces.

4.8 Test the RAID system

Let us mount and create a file on the RAID system to see if everything works:

If the ls command returns test_file, everything is ok. The RAID 1 system is working. You can perform a reboot via

to ensure that everything is assembled and mounted correctly. After reboot, try entering sudo blkid:

If you see /dev/md0, everything is configured correctly after the reboot, and we move on to install Samba in order to upload some media files.

5. Install Samba

Samba allows to access a directory over a network from almost any device and operating system. We need this to comfortably add data to our RAID system. Load and install the Samba packages:

Now we have to create some folders to store our media data. We create Music, Movies, Series and Photos folder into a public folder.

Lets make our pi user owner of these folders and set 777 permissions recursivly:

We have to adapt the Samba config to share the public folder. As always, before adapting configuration files lets make a backup first:

Open the Samba config:

Adapt the your config accordinly (details below the config example):

You can adapt the Workgroup to your private settings. We use standard WORKGROUP. We recommend to uncomment security = user. This will prompt anyone to enter a valid password and user in order to access files on the public share.  The netbios name = RASPBERRY_NAS option defines the display name of the server in the windows explorer.

In the public folder (last part of the config) we enforce the user/group pi. That is of course not the real meaning of the public folder, feel free to change the name in the brackets to e.g. “private“. Our main target here is to see the server in the network and load some data onto it. Afterwards we can do the data and access seperation.

Press CTRL-X and confirm your changes with “y” and “enter“.

Lets check if the config was adapted correctly. Enter testparam

Lets restart the service:

or use:

Now you should be able to see the RaspberryPi Samba server in the explorer / finder and browse the directories we created earlier. You will be prompted for a user (pi) and password (raspberry).

RASPBERRY_PI_Network_Explorer

Lets change the user password (to access data via Samba, not your root password)

Enter your password twice. Now you will connect to the Samba files via user (pi) and your newly entered password. Remember, to access the Pi via SSH you still use “raspberry” or the adapted password (if you changed the password earlier via raspi-config).

Finally lets start Samba as a service in order to have it available after reboot:

6. Install MiniDLNA

In order to stream data from the Pi RAID to e.g. a Smartphone or SmartTV, we need a media server. We use MiniDLNA:

Adapt the config:

Add media_dir directories according to the config below. A for music, V for video files, P for photos. We set the database directory to db_dir = /home/pi/.minidlna. This is where the MiniDLNA server stores its index database. Make sure to adjust the permissions properly (see 9. troubleshooting). We strongly recommend to uncomment the log_dir in order to have some debug messages if something goes wrong. friendly_name is the display name that shows up in DLNA devices like a SmartTV. inotify=yes will find and index new data in the media_dir directly.

Save and quit the editor. In order to start the minidlna server after reboot enter:

Or start it once via:

After start / reboot try to connect to the MiniDLNA server via browser: Enter your RaspberryPi device IP and use the default port (if you did not adapt it) 8200. In our configuration:

If the MiniDLNA server is started correctly you should see a basic web page:

MiniDLNA_WebPage

If you cannot connect to this server check the MiniDLNA log file.

There is a troubleshooting with common errors at the end of this tutorial. Please have a look.

If you have no problems, you can start uploading data to the server. If you do not see any files / changes you can force MiniDLNA to rescan the library via:

If you adapt / improve the MiniDLNA config, you have to reload the MiniDLNA server in order to take affect:

You can start, stop and restart the MiniDLNA via:

If inotify=yes the MiniDLNA will reindex the libraries after restart / start. This may take a while depending on the size of your library.

7. Testing

If not already happend, upload some music / videos via the Samba server onto the RaspberryPi. Check the MiniDLNA server page (<RaspberryIP>:8200) to see if the MiniDLNA indexed the files.

Then turn on a DLNA device and wait until the RaspberryPi Media Server shows up. Browse the library and play your favorite title.

8. Conclusion

It took us a while to get everything working smoothly, especially to have all the HDDs, RAID and server available atfter reboot. So if something is not working for you, invest some time, feel free to ask or comment and you will make it work.

Our setting here is very basic. Only the pi user is allowed to upload/delete data to the Samba server. Anybody is allowed to use the MiniDLNA streaming service though.

While uploading data we get an average of 10-12 MB/s which is almost the full bandwidth of a 100Mbit LAN. Streaming 1080p HD movies works flawlessly. We streamed two movies at the same time and the RaspberryPi had no problems.

All in all this is a really cheap NAS DLNA server. We spend about 240€ (~270$) for the two 5 TB HDDs and another 40€ (~45$) on the Raspberry. This system runs stable, our data is safe thanks to the RAID 1 system, you can store important data on the server without being troubled about data loss.

A year ago i bought a WD MyCloud system with 3 TB RAID 1 setup. I had multiple data losses there due to the crappy software, some weeks ago one HDD failed even if barely used, so i decided to build my own. Now i have 5 TB storage for under 300€ (~340$), while the 3 TB WD MyCloud was about 360€ (405$). Additonally the Pi is fast enough to run different apps, webserver etc., which makes it a cheap and in my opinion better alternative.

We hope this tutorial was understandable, if not please let us know.

9. Troubleshooting

Now we will show some errors / problems we stumbled over while configuring the RaspberryPi.

9.1 MDADM

The only troubles that occured here for us were the huge amount of time to sync (empty) HDDs. Took about 70h.

9.2 Samba

During the research for this tutorial we found several users having problems with Samba. They had the server running and the config seemed correct. Windows either did not show the network device or the users received errors when trying to connect via explorer. In our experience most of the cases are caused by wrong / bad permissions. Thats why we suggest to start with chmod -R 777 on your shared folder in order to avoid that.

9.3 MiniDLNA

For the MiniDLNA, several errors occured:

Make sure the created database (db_dir path in the config) is read and writeable by the user defined in the config. Default user is nobody. Adapt the user in the config or make sure that the default user can access the database file via chmod 777 /home/pi/.minidlna

The same problem occured here:

The MiniDLNA file scanner was not allowed to access the media folders of our server. Same as above, make sure the user in the MiniDLNA config has proper file permissions or make sure everybody can access the files via  chmod -R 777 /media/PI_NAS_5TB/Music etc.

Again, setting file permissions to 777 is considered unsafe.

Feel free to comment or ask if you have problems.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.