udev-usb-sync/README.md

55 lines
2.1 KiB
Markdown
Raw Normal View History

2023-03-05 07:54:25 +01:00
# Bypass write cache for usb storage devices
[Manjaro Forum topic][0]
2023-03-05 06:57:39 +01:00
2023-03-05 07:24:52 +01:00
## Linux filesystem cache
2023-03-05 07:12:21 +01:00
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
2023-03-05 07:24:52 +01:00
## udev rule to disable write-cache
2023-03-05 08:03:13 +01:00
2023-03-05 07:12:21 +01:00
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.
2023-03-05 08:08:19 +01:00
* Documentation on https://wiki.archlinux.org/title/udev
2023-03-05 07:12:21 +01:00
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 rule activates on
* add or change
2023-03-05 07:24:52 +01:00
* kernel event involvinig disk devices **sd[a-z]**
* only if the device environment **ID_USB_TYPE==disk**
* execute **hdparm -W 0 /dev/%K**
2023-03-05 07:12:21 +01:00
2023-03-05 07:24:52 +01:00
Create a file in **/etc/udev/rules.d/99-usb-sync.rules** and paste above content into the file and save it.
2023-03-05 07:12:21 +01:00
2023-03-05 07:24:52 +01:00
Install **hdparm package**.
2023-03-05 07:12:21 +01:00
sudo pacman -Syu hdparm
2023-03-05 07:24:52 +01:00
Then plug an usb device - open in your file manager - copy a huge amout of files to the device - when the copy is done - click eject in the file manager - note how quick the device is ejected.
2023-03-05 07:12:21 +01:00
For those preferring that I have created a PKGBUILD 2 for it which will pull the hdparm dependency upon installation.
pamac build udev-usb-sync
2023-03-05 07:18:04 +01:00
Another idea by [@megavolt][1] at [Manjaro Forum][2] which does not require hdparm.
2023-03-05 07:54:25 +01:00
[0]: https://forum.manjaro.org/t/root-tip-how-to-bypass-write-cache-for-usb-storage-devices/135566
2023-03-05 07:18:04 +01:00
[1]: https://forum.manjaro.org/u/megavolt
[2]: https://forum.manjaro.org/t/decrease-dirty-bytes-for-more-reliable-usb-transfer/120798/4