[systemd-devel] How to spin down a disc with systemd?

Lennart Poettering lennart at poettering.net
Wed Apr 30 04:02:56 PDT 2014


On Wed, 30.04.14 01:02, Marcin Kocur (marcin2006 at gmail.com) wrote:

> Hello all,
> 
> I wrote a service /etc/systemd/system/disable-2nd-hdd.service. I
> have 2 HDD's where one of them doesn't need to work all the time. So
> I thought it'd be cool to shut it down after 30 minutes of
> inactivity (the disc isn't mounted when system starts):
> /usr/bin/hdparm -S 241 /dev/sdb
> 
> Then I wrote a service:
> 
> [Unit]
> Description=Disable sdb
> Requires=local-fs.target
> After=local-fs.target

These two lines are implied and hence can be removed.

> 
> [Service]
> Type=oneshot
> ExecStart=/usr/bin/hdparm -S 241 /dev/sdb
> 
> [Install]
> WantedBy=multi-user.target
> 
> Which seems to work:
> 
> [root at linux mk]# systemctl status disable-2nd-hdd.service
> ● disable-2nd-hdd.service - Disable sdb
> Loaded: loaded (/etc/systemd/system/disable-2nd-hdd.service; enabled)
> Active: inactive (dead) since pon 2014-04-28 10:00:53 CEST; 21min ago
> Main PID: 248 (code=exited, status=0/SUCCESS)
> 
> kwi 28 10:00:53 linux hdparm[248]: /dev/sdb:
> kwi 28 10:00:53 linux hdparm[248]: setting standby to 241 (30 minutes)
> 
> But it doesn't work, like it had never happened, the disc is not spun down.
> 
> When I change the command to /usr/bin/hdparm -y /dev/sdb (spin down
> disc immediately), the disc is spun down while booting but is starts
> again right away.
> 
> When I manually start this service after system has already booted,
> it works as expected.

Note that devices are probed asynchronously, and a device not needed at
boot is not waited for. THis means that your service might run before
the harddisk showed up in the kernel, or possibly that your hdparm
command is invoked before the initializating/probing/identifying of the
hdd or its filesystems is compelted, thus possibly negating the effect
of your command.

YOu probably want to invoke your commandn with RUN from a udev rule so
that it is run when the hw is found.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list