Just about any Single Board Computer (SBC) like a Raspberry Pi, Orange Pi, ODROID or NVIDIA Jetson can be used to create Network Attached Storage (NAS). Really the only prerequisites are that the board can run Linux, has a USB port, and has networking. After that, it just comes down to performance.
To build a Raspberry Pi-powered NAS is quite simple. Here is a step-by-step guide.
This guide concentrates on the NAS parts of the overall system build, therefore I assume that you have already:
- Installed a Debian/Ubuntu-based OS on your board. In the base of the Raspberry Pi than means Raspberry Pi OS, for something like the NVIDIA Jetson then you should install Jetpack.
- Configured a static IP address (this is optional, but recommended)
- Enabled SSH access to the board
- Set the board to boot to the command line and not the desktop (to save memory on smaller boards).
If you need help with vi or with the Linux commandline then please check these videos:
- Understanding Vi and Vim (Vi IMproved) in 10 Minutes
- Linux Directories Explained - including /etc /home /var /proc /usr
- 10 Linux Terminal Commands for Beginners
You need to be sure about how you connect a hard drive to the Raspberry Pi. Some hard drives draw the power directly from the USB port and depending on the drive and the model of Pi board then the power drawn maybe too much. Using an external hard drive with its own power supply is the best option or at least use a powered USB hub.
More information here:
You need to change the hostname of your board from the default so that you can uniquely identify it on your network.
sudo raspi-config
Goto "2 Network Options" and then "N1 Hostname". Set the host name (e.g. 'mypi0nas', exit from raspi-config using <FINISH> and then reboot as prompted.
Use SAMBA to share your files over the network. SAMBA is an open-source re-implementation of the SMB networking protocol (AKA Windows networking). To install SAMBA use:
sudo apt install samba
Now we can prepare, partition, format, and mount the hard drive.
- WARNING: ALL THE DATA WILL BE LOST ON THIS HARD DRIVE
- WARNING: PROCEED WITH CAUTION
- WARNING: IF YOU HAVE DATA ON THIS DRIVE THAT YOU NEED THEN STOP NOW
Connect the drive to your board. Find the name of the drive using lsblk
. It will likely be /dev/sda, but double-check and verify you have the correct drive name.
- From here on I will assume the drive is /dev/sda, please use the correct drive path according to your setup
You may find that the drive has been auto mounted, you need to unmount it. Do a df -h
to see if it is mounted then, if needed, sudo umount /dev/sda
- JUMP TO "Create the directories" IF YOU DON"T WANT TO PARTITION AND FORMAT THE DRIVE
Next, you need to partition the drive. I will use fdisk, there are alternatives. I have a whole video about how to do this: How to Partition and Format a Disk in Linux
- WARNING: ALL THE DATA ON THIS DRIVE WILL BE LOST
sudo fdisk /dev/sda
o
n
p
1
[ENTER]
[ENTER]
w
Key:
- o - create a new empty DOS partition table
- n - add a new partition
- p - primary or extended, p for primary
- 1 - partition number
- [ENTER] - first sector, use default
- [ENTER] - last sector, use default
- w - write table to disk and exit
Now create the filesystem:
sudo mkfs -t ext4 /dev/sda1
Create a top-level directory to mount the drive
sudo mkdir /myhd
Create a top-level directory that you can share on the network:
cd /myhd
sudo mkdir gary
Create a new user, for example, gary
sudo adduser gary
Follow the prompts and enter a new password, etc.
Change the ownership of the gary directory:
sudo chown gary:gary gary
ls -la /dev/disk/by-uuid
Note down the long number like 0603c209-795d-48d4-87e9-7f91390b1e6d
that points to /dev/sda1
Now edit the /etc/fstab file using sudo vi /etc/fstab
and add a line like this:
UUID=0603c209-795d-48d4-87e9-7f91390b1e6d /myhd ext4 nofail,auto 0 0
Now force a mount of all the drives listed and /etc/fstab and check the results:
sudo mount -a
df -h
Edit /etc/samba/smb.conf (with sudo vi /etc/samba/smb.conf
) and add this to the end of the file:
[gary]
comment = Gary
browseable = yes
path = /myhd/gary
guest ok = no
read only = no
writable = yes
valid users = gary
Also, make sure you comment out (using #) the [home] section. This will stop a conflict between the network share gary defined above and the gary home folder. Add an smb user
sudo smbpasswd -a gary
Set the password as promotped.
Restart the samba service (or reboot) using sudo service smbd restart
and you should have access to \\mypi0nas via Windows Explorer. If that doesn't work try the static IP address you set, e.g. \\192.168.1.42
You should also make sure that the hard drives spin down when idle. There are two methods.
sudo apt install hdparm
sudo hdparm -S 60 /dev/sda
(Values between 1 to 240 work in 5-second steps, so 60 is 5 minutes)
cd
wget https://downloads.sourceforge.net/project/hd-idle/hd-idle-1.04.tgz
tar -zxvf hd-idle-1.04.tgz
cd hd-idle
sudo apt install debhelper
dpkg-buildpackage -rfakeroot
sudo dpkg -i ../hd-idle_*.deb
sudo vi /etc/default/hd-idle
Set START_HD_IDLE=true
and uncomment this line: HD_IDLE_OPTS="-i 180 -l /var/log/hd-idle.log"