3

I'm trying to get my HDD to spin down after boot, as I only use it occasionally for backing up and long term storage. I'm running Ubuntu 17.10.

I've been searching for a while and found that I can edit the hdparm.conf file to include this:

command_line {
sudo hdparm -S 1 /dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H
}

I've also tried all sorts of variants of it, with indentation, without the "command_line" option, and instead just done:

/dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H {
spindown_time 1
}

None of it has worked so far. I have disabled "mount on boot" option for the drive.

The thing that confuses me the most, is that when I run

sudo hdparm -S 1 /dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H

the disk will spin down after 5 seconds, just like it is supposed to do.

So, is there any possibility that the hdparm-conf settings aren't applied at boot, and if so, how do I fix it?

Trish
  • 41
  • 1
  • 5

6 Answers6

3

Ubuntu 20.04 LTS has a bug that causes the command-line arguments to not be returned correctly. The fix is to change the end of the hdparm_options() function in /usr/lib/hdparm/hdparm-functions to read:

if [ -n "$OPTIONS" ]; then
    echo $OPTIONS
    return 0
elif [ -n "$DEFAULT" ]; then
    echo $DEFAULT
    return 0
fi

Details

In Ubuntu 20.04 LTS, there is a bug in /usr/lib/hdparm/hdparm-functions, which /usr/lib/udev/hdparm uses to parse /etc/hdparm.conf. The udev/hdparm script calls hdparm_options() in hdparm/hdparm-functions to parse the config file, and it expects the resulting command line arguments to be printed to stdout. However, hdparm_options() never writes to stdout except when the requested disk was not found in the config file, in which case it echos the default command line arguments.

TainToTain
  • 261
  • 1
  • 4
2

Its an old post but timdiels answer helped me to investigate the same problem on Slax/Debian 9. It turns out that an arbitrary script in /lib/hdparm overrides certain settings (-S and -B) based purely on whether the device is on mains or battery power. You need to change the hdparm-functions script to prevent this.

acer
  • 21
  • 2
  • 1
    A little more details on this would really be nice. This way your answer is not quite useful. – divB Dec 05 '20 at 19:32
1

Note: based on my experience with Debian stretch (deb9u1). This may not entirely apply to your distro.

Not sure whether you may omit =, try:

/dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H {
spindown_time = 1
}

Note you may use indents, even on comments, so this should be fine too:

/dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H {
    # Sleep after 5 sec (unit = 5 sec)
    spindown_time = 1
}

To check whether it's run at all, see if you can find anything about hdparm in sudo journalctl -u systemd-udevd.

Another thing you could try is checking hdparm -B /dev/yourdisk, if this is unsupported, try adding apm =; that's right, leave it blank. This hack causes it to call hdparm -B ... instead of the default hdparm -B254 ....

If adding = doesn't fix it, you can debug the issue by editing /lib/udev/hdparm (copy it first so you can restore the original later). Redirect some of the output to >> ~root/hdparm.log 2>&1 and especially add an echo to see the $OPTIONS used for each $DEVNAME. /lib/udev/hdparm is a script ran by udev, probably when it discovers a new device. It does so asynchronously, so if you were to reboot and you have multiple disks, the log messages may 'interrupt' each other. You don't need to reboot to test, try DEVNAME=/dev/...YOURDISK sudo /lib/udev/hdparm.

Tim Diels
  • 3,246
  • 2
  • 19
  • 22
0

If you have USB disks, it's probably this: https://bugs.launchpad.net/ubuntu/+source/hdparm/+bug/515023

Specifically I had to edit hdparm_try_apm() in /lib/hdparm/hdparm-functions:

hdparm_try_apm()
{
    return 0
...

Not a great work around, very fragile (actually this entire thing seems a bit brittle to me!)

Instead, I'll try a udev script

See: https://unix.stackexchange.com/questions/28548/how-to-run-custom-scripts-upon-usb-device-plug-in

Also, I noticed the following:

                apm)
                    if ! hdparm_is_on_battery; then
                        hdparm_set_option -B$VALUE
                    fi
                    ;;
                apm_battery)
                    if hdparm_is_on_battery; then
                        hdparm_set_option -B$VALUE
                    fi

So make sure that's not getting you, IE, you might want to set "apm_battery" as well.

Matt V
  • 1
0

Run

hdparm -B /dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H

to see how APM (Advanced Power Management) is configured. According to the man page (see description of -B argument) lower values allow more aggressive power management while higher values correspond to higher performance. Values 128 through 254 do not allow spin down. On my machine this value defaults to 254. The following configuration should ensure your disk spins down after 5 s:

/dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H {
    apm = 1
    spindown_time = 1
}
Martin Konrad
  • 1,075
  • 1
  • 10
  • 20
0

There is a bug in /usr/lib/hdparm/hdparm-functions, which does not handle disk names like /dev/disk/by-id/ata-ST2000DM006-2DM164_Z4ZAL35H correctly. The solution is to use regular disk names like /dev/sda. It is less than ideal though because disk names can change.

Tao
  • 1
  • 1