On Windows machines hard drives automatically sleep and spin down when they are not in use. This theoretically extends the life of your USB or SATA hard drive. On Linux systems you often have to configure power saving parameters for your hard drive manually.
At A Glance: Our Top 5 Picks For PI Units
- Our Top Pick: Raspberry Pi 3
- Raspberry Pi 2
- Raspberry Pi
- Banana Pi
- Banana Pi Pro
For the little Raspberry Pi and its relatives (Banana Pi, Orange Pi, Beaglebone etc) there is an array of programs that will assist you. Which program will work for you depends on the USB to SATA controller you have and the hard drive itself. I have included guides for hdparm, hd-idle and sdparm which should cover most users wanting to manage hard drive power consumption on the Raspberry Pi. I recommend using hdparm or hd-idle and using sdparm as a last resort. I tested these guides on Lubuntu, Raspbian and Minibian and should work on any Linux Debian/Ubuntu based system for devices like the Banana Pi, Orange Pi, Hummingboard, ODROID, Cubieboard and Hummingboard devices.
If you are trying to figure out which hardware would work best for you, consider reading the Pi benchmarks.
Our Top Pick | Raspberry Pi 3 |
| VIEW LATEST PRICE → |
Raspberry Pi 2 |
| VIEW LATEST PRICE → | |
Raspberry Pi |
| VIEW LATEST PRICE → | |
Banana Pi |
| VIEW LATEST PRICE → | |
Banana Pi Pro |
| VIEW LATEST PRICE → |
I want to see the top picks for PI Units
You can find out your device names on your Raspberry Pi, usually /dev/sda with this command
sudo blkid
Do update your repository list before installing any of these programs, you only need one to work so make sure to remove the others if they are not compatible.
sudo apt-get update
Install and Configure hdparm on Raspberry Pi
We are going to test hdparm first as it is widely used
Install hdparm
sudo apt-get install hdparm -y
Make sure your drive supports hd parm, if you have multiple hard drives it could be /dev/sdb or /dev/sdb – the command blkid will show you all disk drives connected.
sudo hdparm -y /dev/sda
sudo /usr/sbin/hdparm -y /dev/sda
You should get output like this indicating a successful standby command
/dev/sda:
issuing standby command
If you see output like this you will need to use hd-idle or sdparm
/dev/sda:
issuing standby command
SG_IO: bad/missing sense data, sb[]: 70 00 05 00 00 00 00 0a 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Check if your drive supports write cache
sudo hdparm -I /dev/sda | grep 'Write cache'
sudo /usr/sbin/hdparm -I /dev/sda | grep 'Write cache'
If you see a * (asterix) then you are good to go.
* Write cache
If you don't see a star (asterix) then write cache is not possible for your drive
Write cache
To make hdparm spindown the device you can use the -B flag
Values between 1 and 127 allow spinning down of the hard drive.
sudo hdparm -B127 /dev/sda
Time to make hdparm configurations permanent edit the configuration file
sudo nano /etc/hdparm.conf
The spindown_time value is multiplied by 5 and you have the total time in seconds. So a value of 120 yields 10 minutes (120*5=600).
Enable write cache and spindown time by adding this text to the bottom of the file
/dev/sda {
write_cache = on
spindown_time = 120
}
Restart the hdparm service
sudo service hdparm restart
More hdparm configurations are available here
Install and Configure hd-idle on Raspberry Pi
If hdparm didn't work or you just would rather use hd-idle, remove hdparm
sudo apt-get remove hdparm -y
hd-idle uses a special system file for detecting disk activity, if it doesn't exist it won't work
cat /proc/diskstats
You should see some output including the lines below, if you get no such file or directory you cannot use hd-idle
8 0 sda 342 0 2759 260 0 0 0 0 0 250 250
8 1 sda1 102 0 815 90 0 0 0 0 0 80 80
You have to build hd-idle so make sure you have compilation tools
sudo apt-get install build-essential fakeroot debhelper -y
Grab the hd-idle source
wget http://sourceforge.net/projects/hd-idle/files/hd-idle-1.05.tgz
Unpack hd-idle and enter the folder
tar -xvf hd-idle-1.05.tgz && cd hd-idle
Build the hd-idle package and install it
dpkg-buildpackage -rfakeroot
sudo dpkg -i ../hd-idle_*.deb
Double check hd-idle works with your hard drive
sudo hd-idle -i 0 -a sda -i 300 -d
You should see output like this
probing sda: reads: 2759, writes: 0
probing sda: reads: 2759, writes: 0
probing sda: reads: 2759, writes: 0
Use Ctrl+C to stop hd-idle in the terminal
Open the hd-idle configuration file to enable the service to automatically start and spin down drives
sudo nano /etc/default/hd-idle
Change this line to enable hd-idle
START_HD_IDLE=true
Adjust this line to enable sleeping on the drive every 10 minutes (60 seconds * 10)
HD_IDLE_OPTS="-i 0 -a sda -i 600"
Ctrl+X, Y and Enter to save.
Restart the service
sudo service hd-idle restart
By default hd-idle will spin down drives ever 10 minutes which should be sufficient for most users.
If all else fails you can use sdparm
Install and Configure sdparm on Raspberry Pi
First remove hdparm and remove hd-idle
sudo apt-get remove hdparm -y
sudo dpkg -r hd-idle
Install sdparm
sudo apt-get install sdparm -y
Test sdparm works
sdparm --flexible --command=stop /dev/sda
You should see output like this
/dev/sda: ATA FUJITSU MHW2160B 891F
Listen carefully, if it starts spinning up again immediately then you are going to need to find another option. Some users have had success with autofs for mounting and using the eject command.
If the hard drive stays stopped add the cronjob below.
Now we can set up a cron job to ask the usb hard drive to spin down ever hour at 5 past. This will run as the root user. /dev/sda is typically the first hard drive but you may have others, you will have to add additional cronjobs for /dev/sdb or whatever else you find by using the blkid command.
sudo crontab -l | { cat; echo "5 * * * * sdparm --command=stop /dev/sda"; } | sudo crontab -
If youw ant to run the same cronjob every 10 minutes you would add a / before the number, so this example will spin the drive down every 10 minutes
sudo crontab -l | { cat; echo "/10 * * * * sdparm --command=stop /dev/sda"; } | sudo crontab -
More about sdparm here
Now your Raspberry Pi device will spin down your usb hard drive to save power and hopefully extend the life of your hard drive.
These hard drives work with hd-parm and hd-idle:
USB Hard Drive Model | |||
---|---|---|---|
Western Digital My Passport Ultra Portable Hard Drive | |||
Western Digital My Passport Ultra Portable Hard Drive |