You are not logged in.

#1 2016-12-21 09:25:37

ua4000
Member
Registered: 2015-10-14
Posts: 418

[SOLVED] poweroff external USB drive with system shutdown

I use an external USB Drive (174c:5106 ASMedia Technology Inc. ASM1051 SATA 3Gb/s bridge - with normal sata harddisk), and I want to poweroff the drive automatically, when shutting down the whole system.

I tried

hdparm -S 10 xxx 
systemctl poweroff

but this works only sometimes, it's not reliable.

Reliable is

hdparm -f -F -Y xxx
systemctl poweroff

But where do I put the hdparm command, so that the drive poweroff is done automatically at the right time?
Is there a right place in arch shutdown sequence to put such hdparm command?
I don't want to loose data, because I put the drive sleeping too early of course, e.g. before sync, un-mounting and drive cache flush.

Thanks very much.

Last edited by ua4000 (2016-12-25 08:44:07)

Online

#2 2016-12-21 18:05:07

d0cc0m
Member
Registered: 2016-07-25
Posts: 4

Re: [SOLVED] poweroff external USB drive with system shutdown

You should be able to accomplish this with a systemd unit file using the poweroff target. https://wiki.archlinux.org/index.php/Sy … unit_files

Offline

#3 2016-12-23 18:18:25

ua4000
Member
Registered: 2015-10-14
Posts: 418

Re: [SOLVED] poweroff external USB drive with system shutdown

Thanks very much! systemd ... of course :-)

But I didn't get it working. What I did:

# /etc/systemd/system/hdparm.service
[Unit]
Description=Shutdown external USB drive
Before=poweroff.target

[Service]
Type=oneshot
ExecStart=/usr/bin/hdparm -f -F -Y /dev/disk/by-id/ata-WDC_WD40EFRX-...

[Install]
WantedBy=poweroff.target

and then enabled it:

sudo systemctl daemon-reload
sudo systemctl enable hdparm.service

A manual start of the unit works fine, the external usb drive is powerd off, but it don't work automatically at system shutdown.

Is ExecStart right, or have I use ExecStop ? Also when googling for this problem (systemd + hdparm + poweroff.target) I found another location for a hdparm script: /usr/lib/systemd/system-shutdown - is this also I good idea to test?

Online

#4 2016-12-23 20:53:16

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: [SOLVED] poweroff external USB drive with system shutdown

Just place a file (+x) in /usr/lib/systemd/system-shutdown/, scripts/programs here are executed just before "deinitramfs".

For example I place the, to signal UPS in case of emergency shutdown.

$ cat /usr/lib/systemd/system-shutdown/apc_killpower 
#!/bin/sh

if [ -f /etc/apcupsd/powerfail -a "$1" = "poweroff" ]; then
    /etc/apcupsd/apccontrol killpower
fi

in your case you only need to check $1.

Offline

#5 2016-12-25 08:43:39

ua4000
Member
Registered: 2015-10-14
Posts: 418

Re: [SOLVED] poweroff external USB drive with system shutdown

thank you very much, this works!

#!/bin/sh
# sudo nano /usr/lib/systemd/system-shutdown/hdparm.shutdown
# sudo chmod a+x /usr/lib/systemd/system-shutdown/hdparm.shutdown
if [ "$1" = "poweroff" ]; then
	/usr/bin/hdparm -f -F -Y /dev/disk/by-id/ata-WDC_WD40EFRX-...
fi

Online

Board footer

Powered by FluxBB