Sunday, March 29, 2015

Setting Up Raspberry Pi as a File Server

Last time we finished building an RC car with Arduino and the Wii Remote.

There are a few more directions I could take the Arduino RC car, but for now I'm going to step away from Arduino and write about Raspberry Pi for a while.



There are currently five Raspberry Pi models available.

Ideally you'd pick one to match your use case, but they're quite a bit pricier than Arduino.

If you're only buying one, the Raspberry Pi 2 Model B is probably the all-around best choice.


In this post I'll turn a Raspberry Pi 2 Model B into a NAS (file server).


Setting Up the Raspberry Pi 2


The usual approach is to install NOOBS (the bootloader), hook up a monitor and keyboard, and go from there — but digging out an HDMI cable and USB keyboard just for initial setup is a pain.

Here's how to get Raspbian running without a monitor or keyboard at all.



Before you start, you'll need:

  • A Windows or Mac computer.
  • A network with DHCP.

    (You can work around this by connecting directly to your PC with an Ethernet cable and manually assigning an IP address.)
  • An Ethernet connection for the Pi.

First, download the Raspbian image archive from the Raspberry Pi website.

Unzip the downloaded file to get the disk image.



Install Win32 Disk Imager on your Windows PC,

then write the disk image to a micro SD card.



Insert the micro SD card into the Raspberry Pi 2, plug in an Ethernet cable,

and power it on.

Once it boots, it should get an IP address from DHCP.

Check your DHCP server (e.g. your router's admin page) — you should see a device named "Raspberry Pi" with an assigned address.



Use an SSH client like Tera Term to connect to that IP address.

Username: pi, Password: raspberry



Installing Required Packages


Run the following commands to update the OS and reboot.

pi@raspberrypi ~ $ sudo -s
root@raspberrypi:~# rpi-update
root@raspberrypi:~# apt-get update
root@raspberrypi:~# apt-get upgrade
root@raspberrypi:~# reboot

Then install Samba, the Windows file-sharing server.

# apt-get install samba

Connecting an External HDD


An SD card alone won't give you enough storage for a NAS,

so you'll need to attach an external HDD or SSD via USB.



I recommend a self-powered HDD (one with its own AC adapter).

The Raspberry Pi 2 is powered by micro USB, so a bus-powered HDD puts extra load on the power supply and can cause problems.



Once connected, use fdisk -l to find the device path.

root@raspberrypi:~# fdisk -l

Disk /dev/mmcblk0: 15.6 GB, 15577645056 bytes
4 heads, 16 sectors/track, 475392 cylinders, total 30425088 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009bf4f

Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          122880    30425087    15151104   83  Linux

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000130cd

Device Boot      Start         End      Blocks   Id  System



Here, "Disk /dev/sda: 1000.2 GB" tells us the 1 TB HDD is recognised as /dev/sda.



Time to format it.

I'll skip the detailed fdisk options — search for "fdisk" and you'll find plenty of good explanations.

root@raspberrypi ~ # fdisk /dev/sda
Command (m for help): n
# Answer the prompts (just press Enter through them) to create a new partition
Command (m for help): w
# Write the partition table and exit.
root@raspberrypi ~ # mkfs.ext4 /dev/sda1



Now find the UUID and mount the disk.

Be careful not to use > instead of >> — using the wrong one will overwrite your fstab.

root@raspberrypi ~ # mkdir /mnt/hdd
root@raspberrypi ~ # mount -t ext4 /dev/sda1 /mnt/hdd
# Verify it mounts correctly
root@raspberrypi ~ # blkid /dev/sda1
root@raspberrypi ~ # cp -p /etc/fstab /etc/fstab.org
root@raspberrypi ~ # echo 'UUID="" /mnt/hdd ext4    0 2' >> /etc/fstab



Configuring Samba


Now let's set up the file server.

There are plenty of great Samba guides online, so I'll keep this brief.

The following config is enough to get a basic file server running.



Replace 192.168.1. with the network prefix your Raspberry Pi is on.

vim /etc/samba/smb.conf
[global]
interfaces = 192.168.1. 127.0.0.0/8 eth0
[public]
comment = Public
path = /mnt/hdd/
public = yes
read only = no
browsable = yes



vim is a keyboard-only text editor.

Use k, j, h, l to move up/down/left/right.

Press i to enter insert mode, ESC to leave it.

:wq saves and exits.



Restart Samba when you're done.

root@raspberrypi ~ # service samba restart



You should now be able to access the drive from Windows by navigating to \\<RaspberryPi-IP>\pi.





That's it — the file server is ready.

Next I'll cover adding a VNC server and setting up development environments like Scratch and the Arduino IDE, but that's for another post.

No comments:

Post a Comment