Page 1 of 1

An easy way to disable cache just for removable devices

Posted: 19 Jul 2017, 14:49
by fulalas
Has anyone noticed that if you copy some big file/folder to a removable device (for instance, an USB stick), the progress bar goes to 100% very quick and then the dialog disappears? However, the copying process is still in progress because Linux is pushing the cache to the actual removable unit -- you can notice this with USB sticks that have an external light that blinks when it's being accessed. This means that:

1- you don't really know when the copying process finishes, so the progress bar is pretty useless;
2- if you unplug the removable device you'll end with corrupt files;

So, although cache is good for non-removable devices, it's pretty useless/unsafe for removable devices -- I can't understand why this is set by default on Linux. Does anyone know a solution to this?

Re: An easy way to disable cache just for removable devices

Posted: 19 Jul 2017, 15:25
by jssouza
fulalas wrote:Does anyone know a solution to this?
Hi fulalas,

If you mean a solution to wait till the complete copy has finished, what I usually do is after a copy I open a terminal and type sync, and wait till the sync command exits before unplugging the USB device. By the way, it is not just a gui copy with a progress bar, I've seen cp and mv from the command line also exit much earlier than the actual completion of the transfer to a USB stick.

Have no idea about the cache funda. :unknown:

Re: An easy way to disable cache just for removable devices

Posted: 19 Jul 2017, 18:09
by donald
fulalas wrote: 2- if you unplug the removable device you'll end with corrupt files;
..it might sound antiquated:
"one should get used to unmount and eject a (usb) device"
to avoid data loss.. :)

-- write cache --
you could experiment with hdparm -- be carefully
eg.

Code: Select all

hdparm -W 0 /dev/sdX
detailed device info

Code: Select all

hdparm -I /dev/sdX
...but i'm not quite sure if it will work with usb devices.. :unknown:

Re: An easy way to disable cache just for removable devices

Posted: 20 Jul 2017, 04:29
by fulalas
@jssouza, thanks for the tip. Isn't exactly what I'm looking for though :(

@donald, I'm aware of the eject 'trick', but I'm looking for something more straightforward. Take a look at how Windows works: https://www.tenforums.com/attachments/t ... ache-2.png . And unfortunately these hdparm commands don't work with USB devices. Thanks anyway :good:

An easy way to disable cache just for removable devices

Posted: 30 Jul 2017, 16:38
by brokenman
Not all devices perhaps. It works on a USB device I just picked up and plugged in. Silicon Power 4Gb flash drive.
hdparm -W0K1 /dev/sdb

This turned off cache writing for the device. Once you can successfully turn off cache writing you can use a udev rule to turn it off every time the device is plugged in. Another option (without getting into the kernel) is to use the flush argument in fstab. Unfortunately all control panel settings are just too basic.

An easy way to disable cache just for removable devices

Posted: 01 Aug 2017, 00:59
by fulalas

Code: Select all

root@porteus:/home/guest# hdparm -W0K1 /dev/sdd1

/dev/sdd1:
 setting drive keep features to 1 (on)
SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 0a 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 setting drive write-caching to 0 (off)
SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 0a 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 0a 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 0a 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 0a 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 0a 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 write-caching = not supported
After doing that, if I copy a big amount of files to an USB stick, the file copying dialog finishes before the actual files are copied (confirmed by calling 'sync'). :(