udev-usb-sync/README.md
2023-07-23 06:27:26 +00:00

62 lines
No EOL
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Disable write cache for usb storage devices
[Manjaro Forum topic][0]
## 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.
udev rule to disable write-cache
## 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.
* Documentation on https://wiki.archlinux.org/title/udev
The final rule
```
# rule to disable write cache for usb storage
# requires hdparm to be installed
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/hdparm -W 0 /dev/%k"
#
# the following rules is introduced with kernel 6.2
# https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-strict-limit
# https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-max-ratio
# https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-max-bytes
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/echo 1 > /sys/block/%k/bdi/strict_limit", RUN+="/usr/bin/echo 50 > /sys/block/%k/bdi/max_ratio", RUN+="/usr/bin/echo 16777216 > /sys/block/%k/bdi/max_bytes"
```
The rule activates on
* add or change
* kernel event for disk devices **sd[a-z]**
* only if the device environment **ID_USB_TYPE==disk**
* execute **hdparm -W 0 /dev/%K**
Create a file in **/etc/udev/rules.d/99-usb-sync.rules** and paste above content into the file and save it.
Install **hdparm** package.
sudo pacman -Syu hdparm
Then plug an usb device - open in your file manager - copy a huge amount of file data to the device - when the copy is done - click eject in the file manager - note how quick the device is ejected.
For those preferring the package manager, I have created a [PKGBUILD](./PKGBUILD) which will pull the hdparm dependency upon installation. The same PKGBUILD can found within AUR (Arch User Repository) at https://aur.archlinux.org/packages/udev-usb-sync
pamac build udev-usb-sync
Another idea by [@megavolt][1] at [Manjaro Forum][2] which does not require hdparm.
[0]: https://forum.manjaro.org/t/root-tip-how-to-bypass-write-cache-for-usb-storage-devices/135566
[1]: https://forum.manjaro.org/u/megavolt
[2]: https://forum.manjaro.org/t/decrease-dirty-bytes-for-more-reliable-usb-transfer/120798/4
[4]: https://aur.archlinux.org/packages/udev-usb-sync