Recently I have set up an SSD as my primary drive, and in my CD laptop bay I have put my old 500GBs HDD drive. Some time ago I had the same set up with 2 drives for a while, but now the 2nd drive is not going to be used so much. I can make a hardware switch (with a double switch on the sata +5v and +12v) or... put it to sleep with LinuxFu! Lets see how.
For putting the hdd to sleep we are going to use hdparm:
sudo hdparm -Y /dev/sdb
We can check the state with:
sudo hdparm -C /dev/sdb
To have the hdd on sleep after every startup we need to add the command at /etc/rc.local:
And to have it to sleep when coming back of suspend, we need to add a script at /etc/pm/sleep.d. We name the file starting with 10 to execute in the lasts moments of resume, the ensure the HDD is put to sleep even it has been read when resuming. So the filename will look as 10sleephdd and contain:
And then we give execute permissions to that file:
sudo chmod +x 10_sleep_hdd
If we mount/read a file from it, then it will wake up. To put it to sleep again easily we can create a little script, sleep_hdd.sh, that I've put on ~/bin/ (I have it on the path):
To be able to execute it without the need of the sudo password, we need to add an exception to sudoers. This will make a hole in your security: someone rewriting that ~/bin/sleep_hdd.sh could have a shell and be inside!. Anyways, as an exercise, here is how to do it. You can add the exception to /etc/sudoers, or do it in a new file inside /etc/sudoers.d/, it gets more elegant without touching the sudoers file for easier upgrades.
sudo visudo -f /etc/sudoers.d/sleep_hdd
the line to add should look as user host = (root) NOPASSWD: /path/to/command. Remember, as the shell script would be able to be executed with root privileges, change the ownership and modes to root to not leave silly security holes. And just to make it clean, better move it to /usr/local/bin or /usr/local/sbin.
Víctor Cuadrado Juan
I'm Víctor Cuadrado Juan, a developer and FOSS enthusiast, in love with Linux. Currently living in Nürnberg, Germany. Feel free to waste your precious time around here, or to contact me: me at viccuad.me .