Page 1 of 1

[Solved] USB HDD poweroff (HDD abruptly stop spinning when running the shutdown command)

Posted: Sun Jul 28, 2019 11:08 am
by vladimir.k
I have external 2TB WD usb HDD drive attached to my RP2/3. HDD works file. However its abruptly stop spinning during the shutdown procedure. IT produce specific scratch sound. The seems disk dont park the heads before cut off. For most modern HDD it safe, but sound still annoying.
I tried to use udisksctl to detach disk after umount procedure.

Code: Select all

#umount 
/bin/umount -f /dev/sda1
/bin/sleep 4
#detach
/usr/bin/udisksctl power-off -b /dev/sda
/bin/sleep 10
It works fine until logout, but not later. Unfortunately udisksctl require D-Bus. Thereafter it was not possible to use udisksctl after umount.target during shutdown procedure.

As i understood "udisksctl power-off " do 2 things: put hdd in sleep mode (ka stop spinning) and remove /dev/sda from list.
During shutdown I think its possible to replace udisksctl via hdparm (or etc). HDD will stop spinning. Rest will be done by OS automatically.
So i implemented here small properpoweroff sctipt and run it after umount.target :

Code: Select all

 sudo nano /etc/systemd/system/properpoweroff.service
===================================================
[Unit]
Description=[=======USB-HDD-spindown============]
DefaultDependencies=no
After=umount.target
Before=shutdown.target reboot.target halt.target

[Service]
ExecStart=/bin/bash  /my/hddspindown
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target
===================================================
systemctl daemon-reload
sudo systemctl stop  properpoweroff.service  
sudo systemctl start  properpoweroff.service

sudo systemctl disable properpoweroff.service
sudo systemctl enable properpoweroff.service

nano /my/hddspindown
===================================================
# it comes after umount.target, and drives already unmounted but can spinning
# replace /dev/sda with your drive
/sbin/hdparm -Y /dev/sda
/bin/sleep 5
===================================================
sudo chmod 744 /my/hddspindown
No specific scratch sound anymore. Power off log:

Code: Select all

sudo poweroff
[  OK  ] Started Show Plymouth Power Off Screen.
[  OK  ] Stopped D-Bus System Message Bus.
[  OK  ] Stopped dhcpcd on all interfaces.
[  OK  ] Stopped LSB: Manage my cool stuff.
[  OK  ] Stopped LSB: Manage my UPS.
[  OK  ] Stopped target Basic System.
[  OK  ] Stopped Forward Password Requests to Plymouth Directory Watch.
[  OK  ] Stopped target Paths.
[  OK  ] Stopped target Slices.
[  OK  ] Removed slice User and Session Slice.
[  OK  ] Stopped target Sockets.
[  OK  ] Closed triggerhappy.socket.
[  OK  ] Closed Syslog Socket.
[  OK  ] Closed D-Bus System Message Bus Socket.
[  OK  ] Closed Avahi mDNS/DNS-SD Stack Activation Socket.
[  OK  ] Stopped target System Initialization.
[  OK  ] Stopped target Swap.
         Stopping Network Time Synchronization...
[  OK  ] Stopped target Encrypted Volumes.
[  OK  ] Stopped Forward Password Requests to Wall Directory Watch.
         Stopping Update UTMP about System Boot/Shutdown...
[  OK  ] Stopped Apply Kernel Variables.
         Stopping Load/Save Random Seed...
[  OK  ] Stopped Load Kernel Modules.
[  OK  ] Stopped Load/Save Random Seed.
[  OK  ] Stopped Network Time Synchronization.
[  OK  ] Stopped Update UTMP about System Boot/Shutdown.
[  OK  ] Stopped Create Volatile Files and Directories.
[  OK  ] Stopped target Local File Systems.
         Unmounting /run/user/0...
         Unmounting /boot...
[  OK  ] Unmounted /run/user/0.
[  OK  ] Unmounted /boot.
[  OK  ] Reached target Unmount All Filesystems.
         Starting [=======USB-HDD-spindown============]...
[  OK  ] Stopped File System Check on /dev/disk/by-partuuid/9b7bbdf9-01.
[  OK  ] Stopped target Local File Systems (Pre).
[  OK  ] Stopped Remount Root and Kernel File Systems.
[  OK  ] Stopped Create Static Device Nodes in /dev.
[  OK  ] Removed slice system-systemd\x2dfsck.slice.
[  OK  ] Started [=======USB-HDD-spindown============].
[  OK  ] Reached target Shutdown.
[ 3281.364761] reboot: Power down
HDD smart status possible to check by smartctl utulity. It show same values as official WD windows utility.
I think that "Power-Off_Retract_Count" should not grow after save poweroff

Code: Select all

sudo apt-get install -y smartmontools --no-install-recommends
sudo smartctl  -ax --device=sat /dev/sda| grep "K "
  1 Raw_Read_Error_Rate     POSR-K   200   200   051    -    0
  3 Spin_Up_Time            POS--K   208   206   021    -    4591
  4 Start_Stop_Count        -O--CK   100   100   000    -    740
  5 Reallocated_Sector_Ct   PO--CK   200   200   140    -    0
  7 Seek_Error_Rate         -OSR-K   100   253   000    -    0
  9 Power_On_Hours          -O--CK   099   099   000    -    1164
 10 Spin_Retry_Count        -O--CK   100   100   000    -    0
 11 Calibration_Retry_Count -O--CK   100   100   000    -    0
 12 Power_Cycle_Count       -O--CK   100   100   000    -    447
192 Power-Off_Retract_Count -O--CK   200   200   000    -    101
193 Load_Cycle_Count        -O--CK   199   199   000    -    5080
194 Temperature_Celsius     -O---K   120   101   000    -    32
196 Reallocated_Event_Count -O--CK   200   200   000    -    0
197 Current_Pending_Sector  -O--CK   200   200   000    -    0
198 Offline_Uncorrectable   ----CK   100   253   000    -    0
199 UDMA_CRC_Error_Count    -O--CK   200   200   000    -    0
                            ||||||_ K auto-keep
[2019-07-28 14:48:10] 318ms
PS: I used 5V10A UPS for Pi, so HDD can take as mach power as it need.
PS: I did this topic because i did not find solution and it can be useful for others. If someone have comments you are welcome.

Re: [Solved] USB HDD poweroff (HDD abruptly stop spinning when running the shutdown command)

Posted: Sun Jul 28, 2019 2:39 pm
by epoch1970
For data disks, another solution might be to use an automounter.
With autofs5, systemd stops the automounter relatively early in the shutdown process so (spin-up), unmount and spin-down occur before complete power off.
No idea how systemd's own automount service would fare.