Make a hard disk sleep and only wake up when needed
I want to connect another hard disk to my computer, which I want to sleep 99% of the time. I will only use it for a few things, but I need it to be mounted at all times.
To achieve this I would like to know:
- How do I log which processes accesses a device? I need the logging to be able to tell what is causing the hard disk to wake up if it does, so I may act on it.
- Are there any special kernel settings I need to make so that the device may sleep longer?
- How do I set the sleep intervals of the hard disk?
Sleep interval is called "APM" (Automatic Power Management) and spindown_time. This is controlled with hdparm
like this:
hdparm -B 50 -S 36 /dev/disk/by-label/BACKUP-HDD
It will make your HDD to spindown on ~3min inactivity.
As you are on linux, you can use the new fatrace
utility, which logs every file access and tells you which process is responsible:
https://launchpad.net/fatrace
More information here:
http://www.piware.de/2012/02/fatrace-report-system-wide-file-access-events/
It makes use of the linux fanotify API (more details) available since linux kernel 2.6.37.
fatrace
isn't packaged by all distributions as of July 2014 (it entered debian testing recently, so should ship in 'jessie'), but is easy to install from source.
I have similar issue. I have SSD, /dev/sdb
, with OS (Linux Mint 18.1 based on Ubuntu Xenial) and HDD, /dev/sda
, with data, which I use time to time. Both disks are encrypted. HDD's partitions are not mounted. And anyway in periods of several minutes HDD wakes up, then sleeps, then wakes up again. Mess.
Here is a duplicate question with helpful answer, which suggests auditd
to find the bad-behaving process.
apt-get install auditd
auditctl -w /dev/sda -p rwa
Then I force HDD to sleep with hdparm -Y /dev/sda
. Then wait until I hear HDD spinning up again. Then run ausearch -f /dev/sda
. In my case it shows entries like the following.
time->Sat Feb 25 12:38:17 2017
type=PROCTITLE msg=audit(1488022697.651:1744): proctitle=2F7573722F6C69622F756469736B73322F756469736B7364002D2D6E6F2D6465627567
type=PATH msg=audit(1488022697.651:1744): item=0 name="/dev/sda" inode=376 dev=00:06 mode=060660 ouid=0 ogid=6 rdev=08:00 nametype=NORMAL
type=CWD msg=audit(1488022697.651:1744): cwd="/"
type=SYSCALL msg=audit(1488022697.651:1744): arch=c000003e syscall=2 success=yes exit=12 a0=f3fb90 a1=800 a2=7f4745221f64 a3=30 items=1 ppid=1 pid=18520 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="pool" exe="/usr/lib/udisks2/udisksd" key=(null)
Relevant part is exe="/usr/lib/udisks2/udisksd". Though I also had smartmontools
whose smartd
was also the culprit. I uninstalled smartmontools
and stopped udisk2
service with service udisks2 stop
. After that HDD sleeps as expected.
Note that udisks2
will automatically start when I, for instance, open Disks application, so I have to stop it again. Another downside is that SMART parameters are not monitored for both disk, which is not good but as a workaround it fits.
Also one thing that is not clear, is that this bug report says udisks2
doesn't do polling disks which is now done by kernel. But the evidence seems to indicate the contrary.