No description
Find a file
Frede H 82f6390114 refactor: fix rules to work with kernel 7.0
Added logging option to configuration and script
2026-04-05 10:08:15 +00:00
.gitignore initial commit (copied from @fhdk) 2025-03-15 07:35:18 +01:00
99-usb-sync.rules refactor: fix rules to work with kernel 7.0 2026-04-05 10:08:15 +00:00
LICENSE initial commit (copied from @fhdk) 2025-03-15 07:35:18 +01:00
README.md Edit README.md 2025-03-15 07:17:14 +00:00
udev-usb-sync refactor: fix rules to work with kernel 7.0 2026-04-05 10:08:15 +00:00
udev-usb-sync.conf refactor: fix rules to work with kernel 7.0 2026-04-05 10:08:15 +00:00

Linux filesystem cache

Linux agressively caches files in order to improve overall performance.

When copying large amount of files to an USB storage this often results in some wait time until the device can be safely removed.

How long you have to wait depends on your system and the quality of the USB storage device.

Numerous issues regarding this has resulted in various suggestions involving sysctl tweaks and trim.

Examples:

udev rule to disable write-cache

In another place one user threw in an udev rule which would disable write-cache for devices when they were added and while it was just an idea - it triggered my curiosity.

I dug into the intricacies of udev and found a method to only target USB storage devices.

How it works

The rule activates when udev detects

  • add or change
  • kernel event for disk devices sd[a-z]
  • only if the device environment ID_USB_TYPE=='disk'
  • run
    • hdparm -W 0 /dev/%k (disable write cache if supported by the device)
    • udev-usb-sync %k
      • applies defaults
      • read config and apply user values
      • if use_tweaks=0 the script exits
      • if use_tweaks=1 the applies the values (default or config)
      • set a max_bytes value based on device speed

How to setup manually

Create a file in /etc/udev/rules.d/99-usb-sync.rules and paste the rule into it. Create a file in /etc/udev-usb-sync/udev-usb-sync.conf and paste the default values. Create a file in /usr/bin/udev-usb-sync and paste the script content.

Install hdparm and bc package.

sudo pacman -Syu hdparm bc

Reload udev

sudo udevadm control --reload

Then plug an usb device - open in your file manager - copy a huge amount of files to the device - when the copy is done - click eject in the file manager - note how quick the device is ejected.

Another fine utility script provided by @cscs fine-tunes a number of system parameters with the option to input your own values when the script is run

https://gitlab.com/cscs/maxperfwiz