Turn off write cache on all USB External Drives (Debian / Ubuntu / Linux)

12,854

You need to use the -K option. From man hdparm

   -k     Get/set the "keep_settings_over_reset" flag for the drive.  When this flag is set, the drive will preserve the -dmu settings
          over a soft reset, (as done during the error recovery sequence).  This option defaults to off, to prevent drive reset  loops
          which  could  be caused by combinations of -dmu settings.  The -k option should therefore only be set after one has achieved
          confidence in correct system operation with a chosen set of configuration settings.  In practice, all that is typically nec‐
          essary  to  test a configuration (prior to using -k) is to verify that the drive can be read/written, and that no error logs
          (kernel messages) are generated in the process (look in /var/adm/messages on most systems).

   -K     Set the drive´s "keep_features_over_reset" flag.  Setting this enables the drive to retain the settings for -APSWXZ  over  a
          soft reset (as done during the error recovery sequence).  Not all drives support this feature.
Share:
12,854

Related videos on Youtube

Mikeweb49
Author by

Mikeweb49

Updated on September 18, 2022

Comments

  • Mikeweb49
    Mikeweb49 over 1 year

    How do you turn off write cache permanently for external USB drives in Linux (I'm using Ubuntu)?

    I've tried:

    Command line method : hdparm -W0 /dev/sdb <---- that works but only for the current drive and resets after reboot.

    The hdparm "-k1" flag should force it to keep the setting, but it says:

    HDIO_SET_KEEPSETTINGS failed: Invalid argument

    HDIO_GET_KEEPSETTINGS failed: Invalid argument

    Configuration file method (seems to be another way to do it permanently) : The next thing I found was modifying the "hdparm.conf" file located in /etc. However, that seems to require that you know ahead of time the drive letter, "sdb, sdc..." etc.

    How do you just blanket cover ALL drives no matter what, or specifically all USB externals?

    The key problem is.. I'm not sure of the order I will connect these USB external drives, therefore the drive letter will change, so I can't just name "sdb" specifically since it might not be "sdb"...

    Is this a situation where hdparm.conf can have an entry multiple times over even for drives letters that aren't even connected? That way, no matter what drive is connected it will just turn the write_cache off preemptively. Or will that mess something up?

    Possible /etc/hdparm.conf example (will this work?):

    /dev/sda {
            write_cache = off
    }
    /dev/sdb {
            write_cache = off
    }
    /dev/sdc {
            write_cache = off
    }
    /dev/sdd {
            write_cache = off
    }
    

    Again, those drives may not even exist yet... Just covering as many letters of the alphabet preemptively so no matter what, the write_cache is off. I would think there's got to be a better way than this though..

    Any other ideas? Or better approaches? Thank you!