Archive for March 2023
USB drive going to sleep on Linux? - How to prevent that?
Posted on Mon, Mar 20, 2023 at 14:40 by Hubertus A. Haniel
I have recently been experimenting with some stuff that needed quite a bit of storage but was not throughput intensive so I thought I use some old drives I had lying around with some USB to SATA adapters. It was working like a charm until I discovered that the application was complaining about latency which I eventually pinned down to the drives spinning down if not used for a few minutes. So any time I wanted to read data in short moments but stretched apart for a few minutes I had a delay while the drive was spinning up of about 5 to 10 seconds.
Normally I disable this in the BIOS for all drives because I don't think it actually is very healthy for the drive constantly spinning up and down but that is another debate...
It turned out that the disks actually have a kind of a BIOS setting on their own that is used when they are on USB or not controlled via some other way. - The tools that will be of help here are smartctl, hdparm and sdparm. Unfortunately there is no telling of which tools will work best in your scenario since some of the tools work differently on different models of drives and even more so if they are hanging of USB.
In some cases sdparm is your friend:
sdparm --all /dev/sda | grep STANDBY STANDBY_Y 0 [cha: n, def: 0, sav: 0] STANDBY 1 [cha: y, def: 1, sav: 0]
The above has power saving enabled - to disable this we can do:
sdparm --clear=STANDBY /dev/sda
Which should set the previous to:
sdparm --all /dev/sda | grep STANDBY STANDBY_Y 0 [cha: n, def: 0, sav: 0] STANDBY 0 [cha: y, def: 1, sav: 0]
I have also seen in some cases a STANDBY_Z variable that was set to 1 and clearing that did the trick.
The more elegant way is using hdparm but it would not work on all of my drives
hdparm -B /dev/sdb /dev/sdb: APM_level = 128
The lower the number of APM_level is the more aggressive is the power management vs I/O numbers below 128 permit spin down and any higher numbers up to 254 prevent spin down - 255 will disable Power Management:
hdparm -B255 /dev/sdb /dev/sdb: setting Advanced Power Management level to disabled SG_IO: bad/missing sense data, sb[]: 70 00 01 00 00 00 00 0a 00 00 00 00 00 1d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 APM_level = off
There is also an hdparm -C and -S option - The -C is used to query the APM_level and -S 0 should diable power saving which did not work for me though - although take a look at the man page with the -Z option for Seagate drives.
I could not get smartcl to work reliably on my USB converter so I can not comment on the usage much as smartctl -g apm on any of my devices would just return unavailable.
Edited on: Mon, Mar 20, 2023 15:39Posted in HowTo (RSS), System - Linux (RSS)