# Spin down idle disks on a Raspberry Pi

Here is how to stop an external hard disk after it has been idle for a while.

I use external hard disks with Raspberry Pis that are accessed only
occasionally. Keeping them spinning all day wastes power and makes noise.

`hdparm` could not control my disk through its USB adapter. It returned
`SG_IO: bad/missing sense data`. [`hd-idle`][hd-idle] worked instead.

[hd-idle]: https://github.com/adelolmo/hd-idle

## Install hd-idle

On current versions of Raspberry Pi OS, install `hd-idle` with APT:

```shell
$ sudo apt update
$ sudo apt install hd-idle
```

Find the external disk's name:

```shell
$ lsblk --nodeps --output NAME,MODEL,SIZE,TYPE
```

Use the whole-disk name, such as `sda`, not a partition such as `sda1`. Check
the model and size carefully so you do not target the wrong disk.

Test the disk by asking it to enter standby immediately:

```shell
$ sudo hd-idle -t /dev/sda
```

Replace `/dev/sda` with your disk. Activity may wake it again immediately, and
some disks or USB adapters do not support the command.

## Configure hd-idle

Open the configuration file:

```shell
$ sudoedit /etc/default/hd-idle
```

Enable the service and set a 10-minute idle timeout for `sda`:

```shell
START_HD_IDLE=true
HD_IDLE_OPTS="-i 0 -a sda -i 600"
```

The first `-i 0` disables spin-down for every disk. `-a sda -i 600` then enables
it for `sda` after 600 seconds of inactivity. The `-a` value omits `/dev/`.

For two disks with different timeouts:

```shell
HD_IDLE_OPTS="-i 0 -a sda -i 600 -a sdb -i 1200"
```

Apply the configuration and enable the service at boot:

```shell
$ sudo systemctl enable hd-idle
$ sudo systemctl restart hd-idle
$ systemctl status hd-idle
```

Avoid very short timeouts: repeated spin-up cycles can wear out a hard disk. Ten
minutes is a sensible starting point. Disk-monitoring tools such as
`smartmontools` can also keep waking the disk.
