USB HDD Spinning down

Kernel, Main, Utilities & Applications, Miscellaneous Devices.
jca
Top Contributor
Posts: 1911
Joined: Wed May 27, 2020 1:59 pm
Has thanked: 145 times
Been thanked: 454 times

USB HDD Spinning down

Unread post by jca »

I am trying to prevent my HD spiining down but I cannot use hdparm as WD HD do not support commands to change the timeout.
Instead I want to use crontab as described in
https://askubuntu.com/questions/39760/h ... -down-time
near the bottom of the page (message 2).
when typing crontab -e I get crontab: can't change directory to '/var/spool/cron/crontabs': No such file or directory as there is no cron directory in /var/spool
My Linux administration skills are gone (25 years ago when Linux just came out) and I would need some help.
Thanks
jca
Top Contributor
Posts: 1911
Joined: Wed May 27, 2020 1:59 pm
Has thanked: 145 times
Been thanked: 454 times

Re: USB HDD Spinning down

Unread post by jca »

I also do not have a /etc/rsyslog.d directory.
jca
Top Contributor
Posts: 1911
Joined: Wed May 27, 2020 1:59 pm
Has thanked: 145 times
Been thanked: 454 times

Re: USB HDD Spinning down

Unread post by jca »

Great news: I finally managed to have my USB HD spin forever.
Disclaimer: I did not invent the wheel, I just tweaked the spokes in order for the wheel to spin smoothly under MISTer.
I used the following web page:
https://askubuntu.com/questions/39760/h ... -down-time
Using hdparm seems to be the best way to do it but the uSB HD has to support commands send by hdparm and mine does not.
In this case reading periodically a random block on the disk before it spins down will do.
As I did not want to mess up with the missing /var/spool/cron/crontabs and /etc/rsyslog.d I used scripts.
Note: this method will also work even if your drive supports the hdparm commands.
You need 2 things: how long it takes for the drive to spin down and the uuid of the disk.
My drive spins down after 30 minutes and I read the disk every 25 minutes.
Getting the uuid:
You can use the MISTer terminal or ssh. I prefer ssh as I can copy/paste the uuid.
Type blkid, you get something like
/dev/loop0: UUID="b160ae92-70fb-417d-af21-a7bec18ff171" BLOCK_SIZE="1024" TYPE="ext4"
/dev/mmcblk0p1: LABEL="MiSTer_Data" UUID="985A-D432" BLOCK_SIZE="512" TYPE="exfat" PARTUUID="5123f7c3-01"
/dev/mmcblk0p2: PARTUUID="5123f7c3-02"
/dev/loop8: LABEL="rootfs" UUID="356e4b65-6863-4872-9649-633c109c607d" BLOCK_SIZE="1024" TYPE="ext4"
/dev/sda1: LABEL="MISTer" UUID="fd9bdf29-f845-4b26-9aa8-0502d0cd9792" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c9214b85-01"
The HD is /dev/sda1
if you want to check if your drive supports hdparm commads type hdparm -I /dev/sda
if you get
/dev/sda:
hdparm: HDIO_DRIVE_CMD: Invalid argument
Your HD does not supports hdparm commands. If it does and want to use hdparm you are on your own.

The main script NoSpinDown.sh:

#!/bin/bash
while :
do
dd if=/dev/disk/by-uuid/fd9bdf29-f845-4b26-9aa8-0502d0cd9792 of=/dev/null count=1 skip=$RANDOM >/dev/null 2>&1
sleep 25m
done

As you can see this script loops forever and if you start it from MISTer menu it will never return. The script has to be run in the background using & and you have to prevent it to be killed when login out of the terminal by using nohup.

The launcher script Spin.sh

#!/bin/bash
nohup /media/fat/Scripts/NoSpinDown.sh </dev/null >/dev/null 2>/dev/null &

When you run this script it will not produce any output on screen, just Press any key to continue.

When you start your MISTer just run the Spin.sh script and your drive will spin like crazy et voilà!

Notes:
if you want to use these scripts do not forget to change the uuid and the sleep time in accordance to your HD.
If you are using Windows to create these scripts use Notepad++ and go to Edit -> EOL Conversion -> Unix (LF) before saving as Linux does not like at all DOS/Windows end of line. If you are using a Mac don't ask me as I won't touch Apple products even with a 10 foot pole.
User avatar
aberu
Core Developer
Posts: 1192
Joined: Tue Jun 09, 2020 8:34 pm
Location: Longmont, CO
Has thanked: 247 times
Been thanked: 411 times
Contact:

Re: USB HDD Spinning down

Unread post by aberu »

This is really helpful! Good job!

For those curious, one of the things that can ruin a hard disk drive is called "sticktion" in the IT world. Basically the spinning up can potentially wear them out sooner than spinning all day long (when tested in a vacuum, not accounting for other factors like heat, vibration, etc...)
birdybro~
User avatar
JWDog
Posts: 77
Joined: Sun Oct 25, 2020 6:54 pm
Has thanked: 65 times

Re: USB HDD Spinning down

Unread post by JWDog »

jca wrote: Tue Oct 06, 2020 12:22 pm The script has to be run in the background using & and you have to prevent it to be killed when login out of the terminal by using nohup.
what do you mean by this statement? what does it mean to run in the background with &?

Thanks for helping me understand.
jca
Top Contributor
Posts: 1911
Joined: Wed May 27, 2020 1:59 pm
Has thanked: 145 times
Been thanked: 454 times

Re: USB HDD Spinning down

Unread post by jca »

To explain in a concrete way we will not run the script via the OSD but by sshing into MISTer.
# cd /media/fat/Scripts
# ./NoSpinDown.sh
The script will never return as it is an infinite loop and because it has been started in the foreground you will not get a command prompt.
You have to start in in the background which is done using &:
# ./NoSpinDown.sh &
#
You get back the command prompt while the script is running in the background.
If you logout the script will be killed. In order to avoid the script being killed on logout you also have to start it by using the nohup command:
# nohup ./NoSpinDown.sh </dev/null &
The </dev/null is necessary to redirect the standard input to nothing. If it was not there nohup would be waiting for KB input.
This is the reason I used 2 scripts:
The launcher script Spin.sh and the "real" script NoSpinDown.sh.
All other stuff with > is to redirect standard output and standard error to nothing.
jca
Top Contributor
Posts: 1911
Joined: Wed May 27, 2020 1:59 pm
Has thanked: 145 times
Been thanked: 454 times

Re: USB HDD Spinning down

Unread post by jca »

A Xmas present :D

This is an updated version of my post explaining how to make your USB HDD forever.
With this version your HDD will be made to spin forever automatically upon bootup.
I changed the name of the 2 scripts: first I removed the .sh "extension" so the scripts do not show up in the OSD -> Scripts. Second the launcher script becomes S99spin and the main script becomes Spin.
The S99spin script will be copied to /etc/init.d in order to start automatically, the name was chosen so this script starts after all regular scripts as the last one is S91smb (it is not necessary for this script to be the last one but it is the way I did it).
The 2 scripts will be created in /media/fat/Scripts, even if you selected "Switch to USB" in the OSD.
The S99spin script will be copied to /etc/init.d and the original kept in the Scripts folder: if linux is updated the script is likely to have disappeared from the /etc/init.d directory and you can easily copy it back.

You need 2 things: how long it takes for the drive to spin down and the uuid of the disk.
My drive spins down after 5 minutes and I read the disk every 4 minutes.
Getting the uuid:
You can use the MISTer terminal or ssh. I prefer ssh as I can copy/paste the uuid.
Type blkid, you get something like
/dev/loop0: UUID="2d232a45-aeab-4b31-a8ad-ae0f27091c95" BLOCK_SIZE="1024" TYPE="ext4"
/dev/mmcblk0p1: LABEL="MiSTer_Data" UUID="017B-FF39" BLOCK_SIZE="512" TYPE="exfat" PTTYPE="dos" PARTUUID="3107eecb-01"
/dev/mmcblk0p2: PARTUUID="3107eecb-02"
/dev/loop8: LABEL="rootfs" UUID="356e4b65-6863-4872-9649-633c109c607d" BLOCK_SIZE="1024" TYPE="ext4"
/dev/sda1: UUID="993eedbb-30e9-410c-a363-560473d75e74" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="3c2d443f-01"
The HD is /dev/sda1

The main script Spin:

#!/bin/bash
while :
do
dd if=/dev/disk/by-uuid/993eedbb-30e9-410c-a363-560473d75e74 of=/dev/null count=1 skip=$RANDOM >/dev/null 2>&1
sleep 4m
done

The launcher script S99spin

#!/bin/bash
nohup /media/fat/Scripts/Spin </dev/null >/dev/null 2>/dev/null &

You can now test your scripts. At the console or via ssh:
/root# cd /media/fat/Scripts
/media/fat/Scripts# ./S99spin
/media/fat/Scripts# ps -ef | grep "Spin"
11664 root {Spin} /bin/bash /media/fat/Scripts/Spin
11737 root grep Spin

After 5 minutes (in my case) the HDD is still spinning. Time to copy S99spin to /etc/init.d and reboot.
/media/fat/Scripts# cp S99spin /etc/init.d

After reboot you can always check that the script is running with
ps -ef | grep "Spin"

Notes:
if you want to use these scripts do not forget to change the uuid and the sleep time in accordance to your HD.
If you are using Windows to create these scripts use Notepad++ and go to Edit -> EOL Conversion -> Unix (LF) before saving as Linux does not like at all DOS/Windows end of line. If you are using a Mac don't ask me as I won't touch Apple products even with a 10 foot pole.

If you already have these scripts from my previous post you can do a quick thing:
mv Spin.sh S99spin
cp S99spin /etc/init.d
After reboot your disk should be spinning, the main script will still be the original name and visible in the oSD Scripts menu.
Higgy
Posts: 92
Joined: Mon May 25, 2020 9:37 am
Has thanked: 7 times
Been thanked: 30 times

Re: USB HDD Spinning down

Unread post by Higgy »

@jca - thanks. I just got a HDD enclosure, fitted an old laptop HDD and applied your script, its all working sweet. 5mins was also my HDD shutdown.
User avatar
deltax0
Posts: 44
Joined: Sun May 24, 2020 8:17 pm
Been thanked: 3 times

Re: USB HDD Spinning down

Unread post by deltax0 »

does this do anything negative to the drive?
ThetaX55
MiSTer
4tb external hard drive, blister and io.
jca
Top Contributor
Posts: 1911
Joined: Wed May 27, 2020 1:59 pm
Has thanked: 145 times
Been thanked: 454 times

Re: USB HDD Spinning down

Unread post by jca »

It is hard to tell but it seems that stopping/starting a HDD puts a lot of stress on the device and having it run at all time is better. I think it is especially true for disk spinning down after a short time. I have a few laptop drives and they spin down after 5 minutes, om MISTer I have a WD Elements which spins down after 30 minutes.
See
https://www.reddit.com/r/hardware/comme ... more_from/
Post Reply