Simplest NAS - Rasberry Pi + Others & HDD Power Management
I recently wanted to consolidate various media and backup files onto a new external hard drive. It was very clumsy to swap USB cables around so why not stick it on the network? Data transfer speed will suffer but I'm more inclined to actually get things organized if I can do it remotely in my free time. And the upshot is getting to stream my music and video library around the house without taking up storage on the phone.
For this all you need is storage media and the Pi, clone, SBC, or whatever Debian based machine attached to your router - wifi or ethernet, doesn't matter, just be mindful of your connection speed and go for gigabit lan if you can.
To start, I'm assuming your connection is already set up and you can SSH in.
We're going to mount the storage media then go in over sftp.
Yeah, that's all. If you can get to a shell on these systems you're already set up to map it as a network dive via SSH file transfer protocol. Makes file & folder manipulation way easier than renaming and moving things around in terminal.
Ideally your server (Pi) should have the same user account(s) as your client machine. That'll keep all the permissions consistent if you decide to swap the external drive between computers.
Ready? Plug in your storage media, power up, and SSH in.
Take a look at mounted filesystems
df -h
response:
Filesystem Size Used Avail Use% Mounted on
udev 922M 0 922M 0% /dev
tmpfs 199M 5.9M 194M 3% /run
/dev/mmcblk0p1 29G 1.3G 28G 5% /
tmpfs 995M 0 995M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 995M 0 995M 0% /tmp
/dev/zram1 49M 2.3M 43M 6% /var/log
tmpfs 199M 0 199M 0% /run/user/1000
If your system auto mounted the media, congratulations! Skip to connecting/power management. I'm going to have to manually mount my HDD.
Find available disks and partitions
sudo fdisk -l
My drive appears at the bottom
Disk /dev/sda: 1.82 TiB, 2000365289472 bytes, 3906963456 sectors
Disk model: Game Drive
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 40213016-EDA1-4564-9CAE-9F59B1AD1DF9
Device Start End Sectors Size Type
/dev/sda1 2048 500002815 500000768 238.4G Microsoft basic data
/dev/sda2 500002816 3906961407 3406958592 1.6T Linux filesystem
Note the disk is /dev/sda but it has two partitions at /dev/sda1 & /dev/sda2. I configured this portable drive with a 256G NTFS partition for cross platform compatibility - might need to hand some files off to a windows user at some point. The partition I care about here is the 1.6T EXT4 sda2.
Lets grab some more info about it
sudo blkid /dev/sda2
response:
/dev/sda2: LABEL="DELTA" UUID="8097e42d-316c-4663-9aa7-e96b65471a4a" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="a881af27-f88e-44c2-95e9-c4c854247080"
We're going to use identifiers to mount the right partition. If what's plugged into the server ever changes I don't want to see whatever randomly gets assigned sda2 or if another drive also happens to be named "DELTA." The UUID is perfect for this, note down the UUID and TYPE.
Now we need a place to mount this. I'm going with the traditional /mnt/sda1, sda2,...sdaX scheme, but you could do a folder in your home directory or anywhere else that makes sense
sudo mkdir -p /mnt/sda2
Configure fstab
sudo nano /etc/fstab
append the following
UUID=[YOUR_UUID] [YOUR_MOUNT_DIRECTORY] [YOUR_FS] defaults,auto,users,rw,nofail,noatime 0 0
As an example, mine looks like this:
UUID=8097e42d-316c-4663-9aa7-e96b65471a4a /mnt/sda2 ext4 defaults,auto,users,rw,nofail,noatime 0 0
exit, save
evoke auto mounting
sudo mount -a
check if it's mounted
df -h
respone:
Filesystem Size Used Avail Use% Mounted on
udev 922M 0 922M 0% /dev
tmpfs 199M 5.9M 194M 3% /run
/dev/mmcblk0p1 29G 1.3G 28G 5% /
tmpfs 995M 0 995M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 995M 0 995M 0% /tmp
/dev/zram1 49M 2.3M 43M 6% /var/log
tmpfs 199M 0 199M 0% /run/user/1000
/dev/sda2 1.6T 241G 1.3T 16% /mnt/sda2 <----gotem
I'm going to take recursive ownership of the mount point and all the contents of the partition (command could take a while, just trust it)
sudo chown -R [USER]:[USER] /mnt/sda2
where [USER] is the actual user you log in with on this machine. So if you ssh pi@192.168.1.200
it goes
sudo chown -R pi:pi /mnt/sda2
That way permissions are maintained when you add files remotely. As mentioned before, ideally your client machine has the same user so if you transfer in a file you made locally things stay consistent.
Now if you did this with solid state media you're done, go to connecting.
HDD Power Management
Being that I have a mechanical hard drive, I don't want it to spin 24/7 if I'm not accessing it. This can be managed with the packages hd-idle or hdparm. They have varying compatibility and work a bit different but chances are one or the other will work for you.
I'm going over hdparm
sudo apt install hdparm
Here's some brief usage of hdparm - and here's the manual:
! careful, hdparm options are case sensitive !
They aren't persistent until you edit the config file, though. So if you really mess up a reboot should bring you back.
Drive info
sudo hdparm -I /dev/sda
Check drive state
sudo hdparm -C /dev/sda
Force standby mode now ! -y forces standby, -Y forces sleep mode ! Do not be writing, risk of data corruption !
sudo hdparm -y /dev/sda
Benchmark
sudo hdparm -Tt /dev/sda
Power Management Settings
Three that take values 0-255:
hdparm -B
Used by itself, queries Advanced Power Management setting. Otherwise used to set APM. Values 1 - 127 permit spindown, 128 - 254 do not. 255 disables APM. Values are disk dependent. Eg, disks vary in their behavior, requires experimentation and may not be supported.
hdparm -S
Used by itself, enter idle mode. Otherwise used to set standby timeout. 0 disables, 1 - 240 specify multiples of 5 seconds (eg 120 = 600 sec or 10 min), 241 - 251 specify multiples of 30 minutes (eg 241 = 30 min, 251 = 330 min, min = 30(S-240) ).
hdparm -M
Automatic Acoustic Management feature. Set head movement speed to reduce noise output. Usually disable = 0, quiet = 128, fast = 254, but compatibility is disk dependent, may not be supported.
Query APM
sudo hdparm -B /dev/sda
Setting a 5 minute standby timeout
sudo hdparm -S 60 /dev/sda
Mostly you can't do too much damage playing around with these non-persistent settings. When you're not writing anything you can check if hdparm will spindown your drive with sudo hdparm -y /dev/sda. Mine spins down right way. Although, I don't have perfect compatibility, (the drive state returns unknown) but the -I query has plenty of info and it's doing what it's suppose to otherwise. Play with -B to find a suitable standby timeout, or set -S to a specific time using the formula. While you're adjusting APM run the benchmark and confirm your speeds aren't suffering.
I arrived at setting the APM to a value of 124 (default was 128) for some advanced power management that permits spindown after about 10 minutes. That was set with:
sudo hdparm -B 124 /dev/sda
Edit /etc/hdparm.conf to make settings persist.
sudo nano /etc/hdparm.conf
Take a look at example entry for syntax
#/dev/disk/by-id/ata-WDC_WD10EFRX-68PJCN0_WD-WCC4J0998391 {
# apm = 128
# acoustic_management = 128
# spindown_time = 240}
So for my settings
/dev/disk/by-uuid/8097e42d-316c-4663-9aa7-e96b65471a4a {apm = 124}
exit, save
That concludes the server setup.
Connecting
On Linux in your file browser: File > connect to server
- server is the hostname or ip address of the server
- type is SSH or SFTP (look for it to default on port 22)
- folder is the mount point you made (tip: try leaving it on "/" , assuming the credentials you are about to pass have permission, you'll land in the root directory, can be useful for managing the server or if you'd like to access your home folder in the same session)
- user name and password are the same ones you use to SSH in: same protocol, remember
Example to get media on smartphone
- install VLC
- tap Browse at the bottom
- tap 3 dots in the top right
- tap Add a server favorite
- select SFTP, port 22 defaults in
- same server, user, and folder as before
- choose the name you'd like it to appear as
- ok, it gets added to the top row of brower folders
- select it and you'll be prompted for your password