Feed on

A little while ago I had a Netgear ReadyNAS unit which was faulty and wasn’t able to repair it however I thought it would be worth re-using the case to make my own NAS using the Raspberry Pi. I decided to build my own SMPS to provide 5V and 12V to the drives and the Pi and I’m using USB hub with 4x USB to SATA adapters to connect to the 4 hard drives.

(how it all looks at the moment)

After hooking up an 16×2 LCD display to the Pi, I made a start up script in Python to check the drives, assemble the array, mount it and then display the RAID status and free space (see the video above).

lcd.message("NAS4Pi v1.0\nBooting...")

# Check Disks at start up
hdds = ['/dev/sda', '/dev/sdb', '/dev/sdc', '/dev/sdd']

for hdd in hdds:
	checkdisk1 = subprocess.check_output("/usr/sbin/smartctl -a "+hdd+" | grep Reallocated_Sector_Ct | awk 'NR==1 {print $10}'", shell=True)
	print hdd+" - Reallocated_Sector_Ct is", checkdisk1[:-1]
	checkdisk1int = int(checkdisk1)
	checkdisk2 = subprocess.check_output("/usr/sbin/smartctl -a "+hdd+" | grep Current_Pending_Sector | awk 'NR==1 {print $10}'", shell=True)
	checkdisk2int = int(checkdisk2)
	print hdd+" - Current_Pending_Sector is", checkdisk2[:-1]
	checkdisk3 = subprocess.check_output("/usr/sbin/smartctl -a "+hdd+" | grep Offline_Uncorrectable | awk 'NR==1 {print $10}'", shell=True)
	checkdisk3int = int(checkdisk3)
	print hdd+" - Offline_Uncorrectable is", checkdisk3[:-1]

	lcd.message("Check "+hdd)
	if (checkdisk1int == 0 and checkdisk2int == 0 and checkdisk3int == 0):
        	print "- Passed"
        	print "- Failing"
        	lcd.message("\n*** Failing ***")

		lcd.message("Check "+hdd)
		lcd.message("\nRealloc: ")

	        lcd.message("Check "+hdd)
        	lcd.message("\nCurPend: ")

	        lcd.message("Check "+hdd)
        	lcd.message("\nOffUncorr: ")

# Assemble the array at boot
print "Assembling Array"
lcd.message("Assembling Array")
subprocess.call("sudo mdadm --assemble --scan", shell=True)

# Mount the array
print "Mounting Array"
lcd.message("Mounting Array")
subprocess.call("sudo mount /dev/md0 /media/raid", shell=True)

while (1):

	arraystatus = subprocess.check_output("grep 'level 5' /proc/mdstat | awk 'NR==1 {print $11}' | cut -d '/' -f 2 | cut -d ']' -f 1", shell=True)
	print "Disks in array: "+arraystatus[:-1]
	if (arraystatus.find("4") == 0):
        	print "Array ok"
        	lcd.message("Array ok\n")
        	print "Array failed"
        	arraybuild = subprocess.check_output("grep 'recovery' /proc/mdstat | awk 'NR==1 {print $4}'", shell=True)
        	print "Rebuilding: "+arraybuild[:-1]
        	lcd.message("Rebuild: "+arraybuild[:-1]+"\n")

	driveused = subprocess.check_output("df -h | grep /dev/md0 | awk 'NR==1 {print $3}'", shell=True)
	drivetotal = subprocess.check_output("df -h | grep /dev/md0 | awk 'NR==1 {print $2}'", shell=True)
	print driveused[:-1]+"/"+drivetotal[:-1]+" Used"
	lcd.message(driveused[:-1]+"/"+drivetotal[:-1]+" Used")

We schedule this boot up script in the crontab under the user root and firstly check the Reallocated Sectors, Current Pending Sector and Offline Uncorrectable in the SMART data for each drive. If any is more than 0, we say the drive is failing and print out the results. Next we assemble the array (it seems the USB to SATA doesn’t initialise before the array is trying to be assembled automatically) and then mount the array. Every 60 seconds afterwards, the array is checked to see that all drives are up and then we grab the used/free space. Download bootup.py

Next time I’ll be putting it NAS enclosure and hopefully with these improvements

  • Enable power button which would be independent of the Pi which would switch a relay on or off to power the drives and Pi
  • Making the front panel backup button able to run a backup script
  • The 4 front panel LEDs to indicate drive status and use iostat to show activity on “Act” LED
  • Connect the temperature sensor to both mosfets and power down if over temperature
  • Change the inductor to a thicker wire one as it’s reaching 84C when the array was rebuilding (hence why I had a fan on blowing on it)

Part 1
Part 2

6 Responses to “Building NAS4Pi: A NAS built using the Raspberry Pi – Part 1”

  1. G says:

    What kind of usb to sata cables did you use?

  2. dientuki says:

    Hi Alex, I’m tryng to do something like that using the raspberry pi B+ (4 usb port) without use the HUB (I want to connect the HD direct to the raspberry) using the cable that you use with you wifinas (https://www.insidegadgets.com/2014/08/30/building-wifipinas-a-single-bay-2-5-nas-using-the-raspberry-pi/).
    We need some help with the electronic ¿can you help me?


Leave a Reply