Quick thoughts on IT, fun projects, and the singularities I come across.

Raspberry Pi Project 1: NAS and XBMC

The Raspberry Pi makes a great HTPC or media PC, whatever you want to call it, because of its low cost, low power consumption, quiet, and small form factor. As I was getting ready to start my Raspberry Pi build, I realized that the Pi could take over a few other functions that my desktop does, including NAS, at a fraction of the cost (see my previous post about Powering a Pi and related utility savings). But could I modify a RaspXBMC build to also run Samba or some other network file sharing? Could XBMC be installed on Raspbian with less than a thousand calls to aptitude? Could XBMC and Samba run simultaneously on a Pi without making video playback unwatchable? That’s what I hoped to find out.

For context, I started with a Raspberry Pi starter kit from Amazon. For storage, I am using a Fantom 2TB external hard drive. After some preliminary research, I decided to start with Raspbian and try to install XBMC on top. For this walk-through, I’ll assume you’ve got your Raspberry Pi powered, connected to your router, and that you have gotten Raspbian installed. If you need help getting started, check out the Raspberry Pi Quick Start Guide.

Part 1 – NAS

Part 1.1 – NTFS-3G

If you don’t plan to use NTFS as your filesystem for your Pi storage, you can skip this step!

If you do want to use NTFS, and you want to be able to read AND write to the drive, you need to install NTFS support:

If you’re starting with a brand new external hard drive, you’ll need to create the partition and filesystem.

Part 1.2 – Automounting with fstab

In Raspbian (and every flavor of Linux I’ve worked with so far), all filesystems that should be mounted at boot are identified in the fstab file (/etc/fstab). The fstab file can be configured a million ways, so I’ll just share my configuration and explain.

The first four lines were created automatically by the Raspbian install, we’ll leave those alone. We’re only interested in the last line that starts with /dev/sda1, that’s the line I’ve manually added to the configuration.

BUT WHAT DOES IT ALL MEAN???  The columns of the fstab file (courtesy of the ArchLinux wiki) are

<file system> - the partition or storage device to be mounted. In my case, “/dev/sda1″
<dir> - the mountpoint where <file system> is mounted to. In my case, “/media/1TB”
<type> - the file system type of the partition or storage device to be mounted. In my case, it is “ntfs-3g”
<options> – mount options for the filesystem. In my case, I chose “defaults” which includes mounting the filesystem as read-write with no umasks for permissions/access
<dump> – not needed in our case, just use 0 (zero)
<pass> -
used by fsck to decide which order filesystems are to be checked. You can set this to 0 (zero)

So my configuration file says to mount /dev/sda1 on /media/1TB as an NTFS filesystem, using default mount options, and never run fsck on this drive.

Now that you have an example, implementing is pretty easy.

  1. Make a backup of your fstab file
    sudo cp /etc/fstab /var/fstab
  2. Open fstab in your favorite text editor
    sudo vim /etc/fstab
  3. Add the lines to the configuration (see example above) to automount your drives
  4. Save and close the fstab file
  5. Test your configuration by executing a mount all command
    mount -a

If your drive correctly mounted to the mount point you specified (/media/1TB) and it is mounted as read-write, you’re golden.

If you are using multiple drives, then simply add additional lines to the fstab to cover all of the drives you want to automount. Now our storage will automatically mount read-write at startup.

Part 1.3 – Samba

In my build, I wanted to share out any drives mounted in /media/ in case I added drives in the future. Additionally, accessibility rather than security was the priority for me. Because of that I set up Samba to allow anonymous access to any device on my LAN. If you’re cool with that approach, follow along. If not, you may have to do some research on how to set up Samba to your liking.

  1. Install samba
    sudo apt-get install samba
  2. Open the samba config file in your favorite text editor
    sudo vim /etc/samba/smb.conf
  3. Find the line that says “security = user” and update the file with the following lines
  4. Scroll to the bottom of the Samba configuration file and add a new section for your new share. See my configuration for example
  5. Test your configuration by executing ‘testparm’ and examining the output
  6. Assuming your test went well, reload samba
  7. On another device, try to mount the share. On Windows, you can just open the start menu and type “\\yourservername” or “\\your.server.IP.address” On a Mac, open Finder, go to the “Go” dropdown menu at the top, and choose connect to server

    Courtesy of Mobilefish.com

    and enter “smb://yourservername” or “smb://your.server.IP.address” If prompted for a user account, choose Guest.

OK, this is already over 1000 words long so let’s take a breather and continue with XBMC in Part 2.

Part 2 – XBMC

Part 2.1 – Installing XBMC

Installing XBMC is usually pretty easy, but since we’re using Raspbian we can’t use the normal packages. Fortunately for me and anyone who is trying to accomplish this, Michael Gorven has already figured this out for us and even hosts the packages we need. Go ahead and open a new window or tab and browse to http://michael.gorven.za.net/raspberrypi/xbmc . The author has already done a great job of documenting this process so I won’t duplicate it here. Once your installation is finished, test that you can start XBMC by executing ‘sudo xbmc-standalone’ and seeing if XBMC appears on your display.

This is perfect, except that XBMC is now tied to our session. Now if you’re directly connected to the Pi with a keyboard, this is irrelevant. If, like me, you’re doing all of your Pi work via SSH, XBMC will die once you terminate your SSH session. Let’s set up XBMC as a service so we can start/stop it gracefully and allow it to run after our SSH session ends. Additionally, if you’re having issues with XBMC not starting at boot, setting up a service can ensure that XBMC is started for the right run levels.

Part 2.2 – Setting up XBMC as a Service

The XBMC Wiki has a good article for how to set up XBMC as a service, but I want to expand on it a little bit.

  1. Create the init script file
  2. Paste the following code into your vim session:

    Save and quit out of vim. There are a few differences between my init script and the one provided on the XBMC wiki. For one, my “runas” user is set to “pi” – you’ll want to set this to whatever user you installed XBMC with to avoid permissions issues. Additionally, my init script will take “status” as an argument to the init script and will return information about whether or not XBMC is running.
  3. Change the script’s permissions to make it executable
  4. Test that the init script is working:
  5. If you want XBMC to start when the OS boots, update the rc.d scripts. Unless you have a specific use case, using the “defaults” argument will work for you.
  6. If you want to be thorough, restart your Raspberry Pi and verify that your disk has been mounted (via fstab from Part 1) and that XBMC has started.
  7. You’re done. Time to enjoy.

abed_cool_cool_cool

Part 2.3 – XBMC Remote

If you’re going to manage your XBMC/NAS/Pi remotely, I would also suggest downloading the XBMC Remote App for your Adroid or iPhone. To enable remote control on XBMC, go to System -> Settings -> Services -> Remote Control and enable both “Allow programs on this system to control XBMC” and “Allow programs on other systems to control XBMC.”

XBMC Settings->Services Screen

Courtesy of xbmc.org

Assuming you’re on the same wireless network, you should be able to connect to the XBMC with your remote app using your Pi’s IP address and the default username and password of ‘xbmc.’

Well at nearly 2000 words, this has been a whopper of a post. Hopefully this write up has helped you one way or another, and let me know if you have any questions. Enjoy your new combination NAS and HTPC.

Social tagging: > > > > > > >

Leave a Reply