Skip to content

Commit

Permalink
Implementation of script and service unit
Browse files Browse the repository at this point in the history
  • Loading branch information
oreinert committed Jun 12, 2020
1 parent 09d0122 commit abf8cb2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
29 changes: 29 additions & 0 deletions hd-standby
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /bin/bash -eu

# set rotational disks to spin down after a timeout

DISK_NAME=${1:?disk name required}

# default delay: 10 mins
DELAY=${2:-120}

function resolve_disk {
for devpath in "$1" "disk/by-id/$1" "disk/by-path/$1"; do
if [ -b "/dev/$devpath" ]; then
abspath=$(readlink -f "/dev/$devpath")
echo ${abspath#/dev/}
return
fi
done
echo "No disk named '$1' found" >/dev/stderr
return 1
}

DISK=$(resolve_disk "$DISK_NAME")

if [ $(cat /sys/block/$DISK/queue/rotational) == 0 ]; then
echo "Not a rotating disk: $DISK" >/dev/stderr
exit 1
fi

hdparm -S $DELAY /dev/$DISK
13 changes: 13 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Spindown timeout for rotating disk %i
After=suspend.target hibernate.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/hd-standby %i
ExecStop=/usr/sbin/hd-standby %i 0

[Install]
WantedBy=basic.target suspend.target hibernate.target

0 comments on commit abf8cb2

Please sign in to comment.