From 24e1a572b8562a105f2c8504c66d0a1da4666a62 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Tue, 29 Aug 2023 15:47:14 +0000 Subject: [PATCH] Update README.md --- README.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cf060a9..fae1d07 100644 --- a/README.md +++ b/README.md @@ -19,38 +19,89 @@ In another place one user threw in an udev rule which would disable write-cache 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 +* Documentation for https://wiki.archlinux.org/title/udev -The final rule +The rule has gotten a major overhaul and now consist of the rule, a config file and a script +### The rule **/etc/udev/rules.d/99-usb-sync.rules** ``` # 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" +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" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/udev-usb-sync %k" +``` +### The config **/etc/usb-dev-sync/udev-usb-sync.conf** +``` +# default values +#use_tweaks=1 +#max_bytes=16777216 +#max_ratio=50 +#strict_limit=1 +``` +### The script **/usr/bin/udev-usb-sync** +``` +#!/usr/bin/bash +# +# script to tweak USB storage device filesystem sync +# +# sources /etc/usb-dev-sync/usb-dev-sync.conf +# + +use_tweaks=1 +max_bytes=16777216 +max_ration=50 +strict_limit=1 + +# read user config +source /etc/udev-usb-sync/udev-usb-sync.conf + +if [[ "$use_tweaks" = 0 ]]; then + exit 0 +fi + +if [[ -z "$1" ]]; then + exit 1 +fi + +echo "$max_bytes" > "/sys/block/$1/bdi/max_bytes" +echo "$max_ration" > "/sys/block/$1/bdi/max_ratio" +echo "$strict_limit" > "/sys/block/$1/bdi/strict_limit" ``` -The rule activates on +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’** -* execute **hdparm -W 0 /dev/%K** +* only if the device environment **ID_USB_TYPE=='disk'** +* run + - **hdparm -W 0 /dev/%k** (disable write cache if supported) + - **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) -Create a file in **/etc/udev/rules.d/99-usb-sync.rules** and paste above content into the file and save it. +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** 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. +Reload udev -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 + sudo udevadm control --reload + +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. + +For those preferring the package manager, I have created a [PKGBUILD][1] which will pull the **hdparm** dependency upon installation. pamac build udev-usb-sync