An easy way to disable cache just for removable devices

Technical issues/questions of an intermediate or advanced nature.
fulalas
DEV Team
DEV Team
Posts: 2050
Joined: 26 Oct 2016, 15:34
Distribution: Porteus
Location: Brazil

An easy way to disable cache just for removable devices

Post#1 by fulalas » 19 Jul 2017, 14:49

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?

jssouza
Legendary
Legendary
Posts: 1165
Joined: 09 Jul 2015, 14:17
Distribution: Porteus x86 arm

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

Post#2 by jssouza » 19 Jul 2017, 15:25

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:

donald
Full of knowledge
Full of knowledge
Posts: 2064
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

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

Post#3 by donald » 19 Jul 2017, 18:09

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:

fulalas
DEV Team
DEV Team
Posts: 2050
Joined: 26 Oct 2016, 15:34
Distribution: Porteus
Location: Brazil

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

Post#4 by fulalas » 20 Jul 2017, 04:29

@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:

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

An easy way to disable cache just for removable devices

Post#5 by brokenman » 30 Jul 2017, 16:38

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.
How do i become super user?
Wear your underpants on the outside and put on a cape.

fulalas
DEV Team
DEV Team
Posts: 2050
Joined: 26 Oct 2016, 15:34
Distribution: Porteus
Location: Brazil

An easy way to disable cache just for removable devices

Post#6 by fulalas » 01 Aug 2017, 00:59

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'). :(

Post Reply