Friday, May 20, 2022

Long USB drive unmounting after copying data to it


After copying significant amount of data to a USB drive the data get cached and need to be physically flushed to the drive before unmounting. 
By default, Ubuntu uses 20% of RAM for file caches.

Solution

There are some tunable settings that influence how the Linux kernel deals with the file system cache. 
All of them are connected with dirty data (or dirty memory) - data that is written into the cache but not saved on disk.
  • dirty_ratio - maximum percentage of dirty system memory
  • dirty_bytes - the same as dirty_ratio but specified in bytes
  • dirty_background_ratio - percentage of dirty system memory at which background writeback will start
  • dirty_background_bytes - the same as dirty_background_ratio but specified in bytes
$ sudo bash -c 'echo $((64*1024*1024)) > /proc/sys/vm/dirty_background_bytes'
$ sudo bash -c 'echo $((128*1024*1024)) > /proc/sys/vm/dirty_bytes'
In this case, coping progress shows correct speed, and unmounting takes only several seconds. 
To save this setting after reboot it’s required to add such a line into /etc/sysctl.conf:
vm.dirty_background_bytes = 67108864
vm.dirty_bytes = 134217728
This option is applied to all disks - external and internal ones what can reduce system performance.