title: External USB 3.0 HDD Spin down and power off when powering off or rebooting the Raspberry Pi 4B
body: <p>An external USB 3.0 HDD Seagate Expantion Drive 1TB is connected to my Raspberry Pi 4B on the Raspbian Buster.
  The hard drive rarely works, so the first thing I would like it to shut down during idle time (rotation stopped).
  I must say right away that I tried the "hdparm" utility, but it absolutely does not work for me. On my old Raspberry Pi 3B +, it did its job perfectly, but it doesn’t work at all on the new one.
  I tried doing </p>
  
  <pre><code>sudo hdparm -S 10 / dev / sda1
  </code></pre>
  
  <p>and </p>
  
  <pre><code>sudo hdparm -Y / dev / sda1
  </code></pre>
  
  <p>But all he gives out is </p>
  
  <pre><code>/dev/sda:
  issuing sleep command
  SG_IO: bad / missing sense data, sb []: 70 00 05 00 00 00 00 0a 00 00 00 00 24 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  </code></pre>
  
  <p>At the same time, the disk continues to rotate and the LED on the case is on, no matter how much I wait
  I also tried to register the following settings in hdparm.conf: </p>
  
  <pre><code>/dev/sda1 {
      spindown_time = 20
  }
  </code></pre>
  
  <p>But this also did not give any effect, the disk continues to rotate.
  Also, knowing that udisks2 is installed in the raspbian buster, I tried to create a configuration file Seagate_Expansion_NA4B2GTR-0: 0.conf (this is exactly what the command "udevadm info / dev / sda" gave me) in the directory /etc/udisks2/ with the contents </p>
  
  <pre><code>[ATA]
  StandbyTimeout = 20
  </code></pre>
  
  <p>But it didn't work either.</p>
  
  <p>The fact that the disk does not stop spinning is my first problem. The second problem is that when you turn off or restart Raspberry, the disk does not park its heads and its power is interrupted abruptly, which causes a very unpleasant rattle. I found similar queries on the Internet, this is a very common problem.
  I would like that during the procedure of shutting down the OS, the disk should be correctly unmounted and its power turned off before power is lost on the USB 3.0 port.
  Fortunately, I was able to write a similar script and it works great:</p>
  
  <p>hddshutdown.sh </p>
  
  <pre><code>sudo umount -f /dev/sda1
  sudo udisksclt power-off -b /dev/sda1
  sleep 5
  </code></pre>
  
  <p>If I run this script just while the OS is running, it does exactly what I need: The disk is unmounted, the disk rotation stops and the LED on the case goes out. However, I cannot get this script to work the same way during the OS shutdown procedure.</p>
  
  <p>I wrote this unit for systemd:
  hddshutdown.service in /usr/lib/systemd/system/ </p>
  
  <pre><code>[Unit]
  Description=[=======USB-HDD-spindown============]
  DefaultDependencies=no
  After=umount.target
  Before=shutdown.target poweroff.target reboot.target halt.target
  
  [Service]
  ExecStart=/bin/sh  /home/pi/hddshutdown.sh
  Type=oneshot
  RemainAfterExit=yes
  
  [Install]
  WantedBy=reboot.target
  </code></pre>
  
  <p>I did "sudo systemctl enable hddshutdown", tried several different configuration options, played with different Before, After, and WantedBy values, but none of the options worked, just one, the drive is successfully unmounted, but the power does not turn off, the drive continues to spin and LED is on until the moment the Pi is turned off. Moreover, I know for sure that the sh-script starts exactly, the "sleep" delay, which I set at the end of the script, really works.</p>
  
  <p>I do not know Linux very well and this was my first experience writing units for systemd. I found information that udisksctl needs DBus to work, but it doesn’t exist during shutdown, and that’s why the drive’s power cannot turn off. I could not find a way to execute this unit as early as possible during the shutdown procedure when DBus is still available. (I don’t even know what it is, maybe I said stupidity)
  Please help me solve 2 of these problems, thanks in advance.</p>