From ea7f43726a450a128ddf107e4f8bb63f17c1a723 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sun, 5 Mar 2023 05:57:39 +0000 Subject: [PATCH 01/43] Initial commit --- .gitignore | 15 +++++++++++++++ LICENSE | 9 +++++++++ README.md | 2 ++ 3 files changed, 26 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce8d4d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# ---> ArchLinuxPackages +*.tar +*.tar.* +*.jar +*.exe +*.msi +*.zip +*.tgz +*.log +*.log.* +*.sig + +pkg/ +src/ + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ebe10ae --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# udev-usb-sync + From 9e017c0465f54425bd53931548c7b52045ea009c Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Sun, 5 Mar 2023 07:12:21 +0100 Subject: [PATCH 02/43] initial commit --- 99-usb-sync.rules | 3 +++ PKGBUILD | 17 +++++++++++++++++ README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 99-usb-sync.rules create mode 100644 PKGBUILD diff --git a/99-usb-sync.rules b/99-usb-sync.rules new file mode 100644 index 0000000..8c4369e --- /dev/null +++ b/99-usb-sync.rules @@ -0,0 +1,3 @@ +# 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" diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..01e1ba3 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,17 @@ +# Maintainer: +pkgname='udev-usb-sync' +pkgver=0.1 +pkgrel=1 +pkgdesc='Disable writecache when USB storage device is plugged' +arch=('any') +url='https://codeberg.org/wonky/udev-usb-sync' +license=('MIT') +depends=('hdparm') +provides=("${pkgname}") +source=('99-usb-sync.rules') +sha256sums=('ed0253bbbd5b64529efd752978ba17176e3ff23d3f7f220bbacd3fa3a3691df9') + +package() { + install -d -m755 $pkgdir/etc/udev/rules.d + cp ${startdir}/99-usb-sync.rules "${pkgdir}/etc/udev/rules.d" +} diff --git a/README.md b/README.md index ebe10ae..7f9dc8d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,43 @@ # udev-usb-sync +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 + +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. + +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 +* kernel event involvinig disk devices sd[a-z] +* only if the device environment ID_USB_TYPE==‘disk’ +* run 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 amout of files to the device - when the copy is done - click eject in the file manager - and the device ejects immediately. + +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 From f7df8de06e1db2ca04b45dc36caf309c43617ffd Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Sun, 5 Mar 2023 07:18:04 +0100 Subject: [PATCH 03/43] add @megavolt rule --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 7f9dc8d..6cc811f 100644 --- a/README.md +++ b/README.md @@ -41,3 +41,13 @@ Then plug an usb device - open in your file manager - copy a huge amout of files 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 + +Another idea by [@megavolt][1] at [Manjaro Forum][2] which does not require hdparm. + +> ``` +> SUBSYSTEMS=="usb", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", ENV{UDISKS_MOUNT_OPTIONS_DEFAULTS}+="sync", ENV{UDISKS_MOUNT_OPTIONS_ALLOW}+="sync" +> ``` +> This udev rule is just an example. Adjuste it if needed. + +[1]: https://forum.manjaro.org/u/megavolt +[2]: https://forum.manjaro.org/t/decrease-dirty-bytes-for-more-reliable-usb-transfer/120798/4 \ No newline at end of file From a18a7af9ea4c666cb34fbf62eae903ca8e98938c Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Sun, 5 Mar 2023 07:21:22 +0100 Subject: [PATCH 04/43] rephrase to link to forum topic --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 6cc811f..9edeeb9 100644 --- a/README.md +++ b/README.md @@ -44,10 +44,5 @@ For those preferring that I have created a PKGBUILD 2 for it which will pull the Another idea by [@megavolt][1] at [Manjaro Forum][2] which does not require hdparm. -> ``` -> SUBSYSTEMS=="usb", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", ENV{UDISKS_MOUNT_OPTIONS_DEFAULTS}+="sync", ENV{UDISKS_MOUNT_OPTIONS_ALLOW}+="sync" -> ``` -> This udev rule is just an example. Adjuste it if needed. - [1]: https://forum.manjaro.org/u/megavolt [2]: https://forum.manjaro.org/t/decrease-dirty-bytes-for-more-reliable-usb-transfer/120798/4 \ No newline at end of file From c49169e1d601983fb70b564ccdc4f8f627a3fff0 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Sun, 5 Mar 2023 07:24:52 +0100 Subject: [PATCH 05/43] formatting --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9edeeb9..75f17e1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # udev-usb-sync -Linux filesystem cache +## Linux filesystem cache Linux agressively caches files in order to improve overall performance. @@ -11,6 +11,7 @@ How long you have to wait depends on your system and the quality of the USB stor 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. @@ -26,17 +27,17 @@ ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bi The rule activates on * add or change -* kernel event involvinig disk devices sd[a-z] -* only if the device environment ID_USB_TYPE==‘disk’ -* run hdparm -W 0 /dev/%K +* kernel event involvinig 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. +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. +Install **hdparm package**. sudo pacman -Syu hdparm -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 - and the device ejects immediately. +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 that I have created a PKGBUILD 2 for it which will pull the hdparm dependency upon installation. From ac6abb214be23404c92590c9891b7baf6d444742 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Sun, 5 Mar 2023 07:54:25 +0100 Subject: [PATCH 06/43] update --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 75f17e1..3261299 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# udev-usb-sync +# Bypass write cache for usb storage devices + +[Manjaro Forum topic][0] ## Linux filesystem cache @@ -45,5 +47,6 @@ For those preferring that I have created a PKGBUILD 2 for it which will pull the 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 \ No newline at end of file From 2a58dacaaf9ce3d2ba0440a3aea3ae33f9aef623 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Sun, 5 Mar 2023 08:03:13 +0100 Subject: [PATCH 07/43] added link to arch wiki --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 3261299..8bb29ec 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,9 @@ Numerous issues regarding this has resulted in various suggestions involving sys udev rule to disable write-cache ## udev rule to disable write-cache + +Arch wiki documentation on https://wiki.archlinux.org/title/udev + 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. From e72877db7e4d2d7121b2608bdda2a98d8a640a85 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Sun, 5 Mar 2023 08:08:19 +0100 Subject: [PATCH 08/43] update --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8bb29ec..a87fb7a 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ udev rule to disable write-cache ## udev rule to disable write-cache -Arch wiki documentation on https://wiki.archlinux.org/title/udev - 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 ``` From 31bb14f56c732bf0e7fd7959efe28063f09881b6 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sun, 5 Mar 2023 11:13:16 +0000 Subject: [PATCH 09/43] Update 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a87fb7a..509c03b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Bypass write cache for usb storage devices +# Disable write cache for usb storage devices [Manjaro Forum topic][0] From fd893edb42817a31cca573825f7e812aef3a4de0 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sun, 5 Mar 2023 11:15:41 +0000 Subject: [PATCH 10/43] Update 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 509c03b..e3e6d23 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Install **hdparm package**. sudo pacman -Syu hdparm -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. +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 that I have created a PKGBUILD 2 for it which will pull the hdparm dependency upon installation. From 4a4836043c96ddf95b4a8f3f9819512be45487af Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sun, 5 Mar 2023 11:17:08 +0000 Subject: [PATCH 11/43] Update 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e3e6d23..0baa575 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ The rule activates on 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**. +Install **hdparm** package. sudo pacman -Syu hdparm From a3409833efdd1eedfe973e355c6fabd5f5d59d5d Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Mon, 6 Mar 2023 14:53:49 +0000 Subject: [PATCH 12/43] Update 'README.md' --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0baa575..421e634 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Install **hdparm** package. 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 that I have created a PKGBUILD 2 for it which will pull the hdparm dependency upon installation. +For those preferring the package manager, I have created a [PKGBUILD][4] which will pull the hdparm dependency upon installation. pamac build udev-usb-sync @@ -52,4 +52,5 @@ Another idea by [@megavolt][1] at [Manjaro Forum][2] which does not require hdp [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 \ No newline at end of file +[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 \ No newline at end of file From 6a9d8c9f9dfe5ae3132299d4caafcbe8bbb458c6 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Mon, 6 Mar 2023 14:56:10 +0000 Subject: [PATCH 13/43] Update 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 421e634..6330c72 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Install **hdparm** package. 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][4] which will pull the hdparm dependency upon installation. +For those preferring the package manager, I have created a [PKGBUILD](./PKGBUILD) which will pull the hdparm dependency upon installation. The same PGBUILD can found within AUR (Arch User Repository) https://aur.archlinux.org/packages/udev-usb-sync pamac build udev-usb-sync From e1df1fc2c4777552e1ace90da691c1833608fddb Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Mon, 6 Mar 2023 14:57:27 +0000 Subject: [PATCH 14/43] Update 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6330c72..03980a7 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Install **hdparm** package. 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 PGBUILD can found within AUR (Arch User Repository) https://aur.archlinux.org/packages/udev-usb-sync +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 From 192d841837ce6f4f8ddc1fa7f1fa1c6ebb0c188a Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Mon, 6 Mar 2023 15:01:22 +0000 Subject: [PATCH 15/43] Update 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 03980a7..0a29de5 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bi The rule activates on * add or change -* kernel event involvinig disk devices **sd[a-z]** +* kernel event for disk devices **sd[a-z]** * only if the device environment **ID_USB_TYPE==‘disk’** * execute **hdparm -W 0 /dev/%K** From bc866e2552915f21c6e9cdcc257e872a0aee5e38 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Fri, 21 Jul 2023 06:48:51 +0000 Subject: [PATCH 16/43] Update '99-usb-sync.rules' --- 99-usb-sync.rules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 8c4369e..51fe822 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,3 +1,3 @@ # 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" From 6005616c9327c25d5b550b90263741a619268bf2 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sun, 23 Jul 2023 06:26:28 +0000 Subject: [PATCH 17/43] Update '99-usb-sync.rules' --- 99-usb-sync.rules | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 51fe822..21ec69d 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,3 +1,13 @@ # 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" + +# 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" From d0dbd3fa3e14db9c972a50562dc915138657dde0 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sun, 23 Jul 2023 06:27:26 +0000 Subject: [PATCH 18/43] Update 'README.md' --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 0a29de5..cf060a9 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ 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 From ebbea9dc2086bfb230e3b4e5b462fce765ddf775 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sun, 23 Jul 2023 06:27:46 +0000 Subject: [PATCH 19/43] Update '99-usb-sync.rules' --- 99-usb-sync.rules | 4 ---- 1 file changed, 4 deletions(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 21ec69d..7909e06 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,10 +1,6 @@ # 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" - -# 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 From e5f51a6d2f4a9da5260476a589ae2d228661a1b7 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Wed, 23 Aug 2023 12:01:01 +0000 Subject: [PATCH 20/43] Update 99-usb-sync.rules make hdparm conditional - only execute on kernel > 6.1 --- 99-usb-sync.rules | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 7909e06..0e5dca0 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,8 +1,9 @@ # 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+="[[ $(uname -r | awk -F'.' '{print $1 $2}') > 61 ]] && /usr/bin/hdparm -W 0 /dev/%K" # -# the following rules is introduced with kernel 6.2 +# rules to impose buffer limits on USB devices +# requires kernel > 6.1 # 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 From f985c58c059429dabd7d872379a02e8aa3aaca10 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Wed, 23 Aug 2023 12:04:25 +0000 Subject: [PATCH 21/43] Update 99-usb-sync.rules revert direction --- 99-usb-sync.rules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 0e5dca0..85f0116 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,6 +1,6 @@ # 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+="[[ $(uname -r | awk -F'.' '{print $1 $2}') > 61 ]] && /usr/bin/hdparm -W 0 /dev/%K" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && /usr/bin/hdparm -W 0 /dev/%K" # # rules to impose buffer limits on USB devices # requires kernel > 6.1 From 541bc1b1149fc9c9c2171ea7315acef7a5891e6d Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Wed, 23 Aug 2023 12:11:35 +0000 Subject: [PATCH 22/43] Update 99-usb-sync.rules condition complete --- 99-usb-sync.rules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 85f0116..f44cb35 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,6 +1,6 @@ # 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+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && /usr/bin/hdparm -W 0 /dev/%K" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && [[ $(which hdparm) =~ (hdparm) ]] && && /usr/bin/hdparm -W 0 /dev/%K" # # rules to impose buffer limits on USB devices # requires kernel > 6.1 From 0d6474d2343103997a9a0ce78c021061a7e0acb7 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Thu, 24 Aug 2023 13:16:54 +0000 Subject: [PATCH 23/43] Update 99-usb-sync.rules --- 99-usb-sync.rules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index f44cb35..1098350 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,6 +1,6 @@ # 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+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && [[ $(which hdparm) =~ (hdparm) ]] && && /usr/bin/hdparm -W 0 /dev/%K" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && [[ $(which hdparm) =~ (hdparm) ]] && /usr/bin/hdparm -W 0 /dev/%K" # # rules to impose buffer limits on USB devices # requires kernel > 6.1 From 9c7ba8eeb430028f8cc956c96c85d74c70f975d0 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Sat, 26 Aug 2023 04:41:13 +0000 Subject: [PATCH 24/43] Update 99-usb-sync.rules revert kernel check --- 99-usb-sync.rules | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 1098350..06bbfcf 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,6 +1,7 @@ # 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+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && [[ $(which hdparm) =~ (hdparm) ]] && /usr/bin/hdparm -W 0 /dev/%K" +#ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && [[ $(which hdparm) =~ (hdparm) ]] && /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" # # rules to impose buffer limits on USB devices # requires kernel > 6.1 From 5e74eb782568a72048170110150ffa3283f222f8 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Tue, 29 Aug 2023 10:36:21 +0000 Subject: [PATCH 25/43] Update 99-usb-sync.rules block variable must be %K instead of %k --- 99-usb-sync.rules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 06bbfcf..b377f0b 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -8,4 +8,4 @@ ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bi # 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/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" From eab2abcb4aef6ed36942710e0f0cd0c9e4c01e7a Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Tue, 29 Aug 2023 11:04:21 +0000 Subject: [PATCH 26/43] Update 99-usb-sync.rules removed test rule --- 99-usb-sync.rules | 1 - 1 file changed, 1 deletion(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index b377f0b..3c2aac8 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,6 +1,5 @@ # 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+="[[ $(uname -r | awk -F'.' '{print $1 $2}') < 62 ]] && [[ $(which hdparm) =~ (hdparm) ]] && /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" # # rules to impose buffer limits on USB devices From 45d7307d1d2911fab1df16c311075876516f09c6 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Tue, 29 Aug 2023 16:25:04 +0200 Subject: [PATCH 27/43] update with config and script --- 99-usb-sync.rules | 6 ++++-- PKGBUILD | 22 +++++++++++++++------- udev-usb-sync.conf | 5 +++++ udev-usb-sync.install | 11 +++++++++++ udev-usb-sync.sh | 26 ++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 udev-usb-sync.conf create mode 100644 udev-usb-sync.install create mode 100755 udev-usb-sync.sh diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 3c2aac8..b1d8fb4 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,10 +1,12 @@ # 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" # # rules to impose buffer limits on USB devices # requires kernel > 6.1 # 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" +# implemented using /usr/bin/udev-usb-tweak.sh +# optional configuration in /etc/udev-usb-tweak/udev-usb-tweak.sh +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+=/usr/bin/udev-usb-sync.sh %k" diff --git a/PKGBUILD b/PKGBUILD index 01e1ba3..4a7952a 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,17 +1,25 @@ -# Maintainer: +# Maintainer: root.nix.dk pkgname='udev-usb-sync' -pkgver=0.1 -pkgrel=1 -pkgdesc='Disable writecache when USB storage device is plugged' +pkgver=0.3 +pkgrel=6 +pkgdesc='Fine tune write cache when USB storage device is plugged' arch=('any') url='https://codeberg.org/wonky/udev-usb-sync' license=('MIT') depends=('hdparm') provides=("${pkgname}") -source=('99-usb-sync.rules') -sha256sums=('ed0253bbbd5b64529efd752978ba17176e3ff23d3f7f220bbacd3fa3a3691df9') +backup=("etc/${pkgname}/${pkgname}.conf") +install=${pkgname}.install +source=('99-usb-sync.rules' 'udev-usb-sync.sh' 'udev-usb-sync.conf') +sha256sums=('39e714df856c8d47fe14365b854b9a1b36ed3afa300383d8329452394a01a1c4' + '2669e043722b33009798c8c30f48414e1eaad0f001631587f502b19a2f11feb7' + 'bc902da2c453355e1dbc3a970a564daf71d41c6278dc771a930aa3badb90e453') package() { install -d -m755 $pkgdir/etc/udev/rules.d - cp ${startdir}/99-usb-sync.rules "${pkgdir}/etc/udev/rules.d" + install -d -m755 $pkgdir/etc/$pkgname + install -d -m755 $pkgdir/usr/bin + cp ${srcdir}/99-usb-sync.rules "${pkgdir}/etc/udev/rules.d" + cp ${srcdir}/${pkgname}.sh "${pkgdir}/usr/bin" + cp ${srcdir}/${pkgname}.conf "${pkgdir}/etc/${pkgname}" } diff --git a/udev-usb-sync.conf b/udev-usb-sync.conf new file mode 100644 index 0000000..31683ee --- /dev/null +++ b/udev-usb-sync.conf @@ -0,0 +1,5 @@ +# default values +#use_tweaks=1 +#max_bytes=16777216 +#max_ratio=50 +#strict_limit=1 diff --git a/udev-usb-sync.install b/udev-usb-sync.install new file mode 100644 index 0000000..7b7452a --- /dev/null +++ b/udev-usb-sync.install @@ -0,0 +1,11 @@ +post_install() { + udevadm control --reload +} + +post_upgrade() { + post_install +} + +post_remove() { + post_install +} diff --git a/udev-usb-sync.sh b/udev-usb-sync.sh new file mode 100755 index 0000000..392a1a1 --- /dev/null +++ b/udev-usb-sync.sh @@ -0,0 +1,26 @@ +#!/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" > "/dev/block/$1/bdi/max_bytes" +echo "$max_ration" > "/dev/block/$1/bdi/max_ratio" +echo "$strict_limit" > "/dev/block/$1/bdi/strict_limit" From 4c3619aeb144b87383487eeb7ce8ab7b4df5bb64 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Tue, 29 Aug 2023 16:37:48 +0200 Subject: [PATCH 28/43] update install --- PKGBUILD | 4 ++-- udev-usb-sync.sh => udev-usb-sync | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename udev-usb-sync.sh => udev-usb-sync (100%) diff --git a/PKGBUILD b/PKGBUILD index 4a7952a..ef84a0a 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -10,7 +10,7 @@ depends=('hdparm') provides=("${pkgname}") backup=("etc/${pkgname}/${pkgname}.conf") install=${pkgname}.install -source=('99-usb-sync.rules' 'udev-usb-sync.sh' 'udev-usb-sync.conf') +source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf') sha256sums=('39e714df856c8d47fe14365b854b9a1b36ed3afa300383d8329452394a01a1c4' '2669e043722b33009798c8c30f48414e1eaad0f001631587f502b19a2f11feb7' 'bc902da2c453355e1dbc3a970a564daf71d41c6278dc771a930aa3badb90e453') @@ -20,6 +20,6 @@ package() { install -d -m755 $pkgdir/etc/$pkgname install -d -m755 $pkgdir/usr/bin cp ${srcdir}/99-usb-sync.rules "${pkgdir}/etc/udev/rules.d" - cp ${srcdir}/${pkgname}.sh "${pkgdir}/usr/bin" + cp ${srcdir}/${pkgname} "${pkgdir}/usr/bin" cp ${srcdir}/${pkgname}.conf "${pkgdir}/etc/${pkgname}" } diff --git a/udev-usb-sync.sh b/udev-usb-sync similarity index 100% rename from udev-usb-sync.sh rename to udev-usb-sync From 9d5f0f68aa65794b625cede6baac12b182924f64 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Tue, 29 Aug 2023 16:42:04 +0200 Subject: [PATCH 29/43] update package --- 99-usb-sync.rules | 2 +- PKGBUILD | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index b1d8fb4..68c3a3d 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -9,4 +9,4 @@ ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bi # https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-max-bytes # implemented using /usr/bin/udev-usb-tweak.sh # optional configuration in /etc/udev-usb-tweak/udev-usb-tweak.sh -ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+=/usr/bin/udev-usb-sync.sh %k" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/udev-usb-sync %k" diff --git a/PKGBUILD b/PKGBUILD index ef84a0a..d9ce1e3 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,7 +1,7 @@ # Maintainer: root.nix.dk pkgname='udev-usb-sync' -pkgver=0.3 -pkgrel=6 +pkgver=0.4 +pkgrel=1 pkgdesc='Fine tune write cache when USB storage device is plugged' arch=('any') url='https://codeberg.org/wonky/udev-usb-sync' @@ -11,7 +11,7 @@ provides=("${pkgname}") backup=("etc/${pkgname}/${pkgname}.conf") install=${pkgname}.install source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf') -sha256sums=('39e714df856c8d47fe14365b854b9a1b36ed3afa300383d8329452394a01a1c4' +sha256sums=('be08224041008876efb09e6b2a119d6f42b911efecf0563be413f9ff2d33a7b3' '2669e043722b33009798c8c30f48414e1eaad0f001631587f502b19a2f11feb7' 'bc902da2c453355e1dbc3a970a564daf71d41c6278dc771a930aa3badb90e453') From bbf27a0e335323cd039e9885c87cc5c3239bf38a Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Tue, 29 Aug 2023 16:55:12 +0200 Subject: [PATCH 30/43] update script --- PKGBUILD | 4 ++-- udev-usb-sync | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index d9ce1e3..2ff593e 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,6 +1,6 @@ # Maintainer: root.nix.dk pkgname='udev-usb-sync' -pkgver=0.4 +pkgver=0.5 pkgrel=1 pkgdesc='Fine tune write cache when USB storage device is plugged' arch=('any') @@ -12,7 +12,7 @@ backup=("etc/${pkgname}/${pkgname}.conf") install=${pkgname}.install source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf') sha256sums=('be08224041008876efb09e6b2a119d6f42b911efecf0563be413f9ff2d33a7b3' - '2669e043722b33009798c8c30f48414e1eaad0f001631587f502b19a2f11feb7' + '0c60b841dd18521130eb22e18bf62bb5bf3dcf124e64dd7a6764aaf5af972eea' 'bc902da2c453355e1dbc3a970a564daf71d41c6278dc771a930aa3badb90e453') package() { diff --git a/udev-usb-sync b/udev-usb-sync index 392a1a1..73895e3 100755 --- a/udev-usb-sync +++ b/udev-usb-sync @@ -21,6 +21,6 @@ if [[ -z "$1" ]]; then exit 1 fi -echo "$max_bytes" > "/dev/block/$1/bdi/max_bytes" -echo "$max_ration" > "/dev/block/$1/bdi/max_ratio" -echo "$strict_limit" > "/dev/block/$1/bdi/strict_limit" +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" From 24e1a572b8562a105f2c8504c66d0a1da4666a62 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Tue, 29 Aug 2023 15:47:14 +0000 Subject: [PATCH 31/43] 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 From af6547a9e91b57da80ea043a136b1ac0f3a58dbe Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Tue, 29 Aug 2023 15:48:25 +0000 Subject: [PATCH 32/43] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fae1d07..19e8d4d 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bi use_tweaks=1 max_bytes=16777216 -max_ration=50 +max_ratio=50 strict_limit=1 # read user config @@ -70,7 +70,7 @@ if [[ -z "$1" ]]; then fi echo "$max_bytes" > "/sys/block/$1/bdi/max_bytes" -echo "$max_ration" > "/sys/block/$1/bdi/max_ratio" +echo "$max_ratio" > "/sys/block/$1/bdi/max_ratio" echo "$strict_limit" > "/sys/block/$1/bdi/strict_limit" ``` From c3a59498e100c68f10eb4410f6cbe7938df60a62 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Tue, 29 Aug 2023 15:48:55 +0000 Subject: [PATCH 33/43] Update udev-usb-sync --- udev-usb-sync | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/udev-usb-sync b/udev-usb-sync index 73895e3..ccaab7e 100755 --- a/udev-usb-sync +++ b/udev-usb-sync @@ -7,7 +7,7 @@ use_tweaks=1 max_bytes=16777216 -max_ration=50 +max_ratio=50 strict_limit=1 # read user config @@ -22,5 +22,5 @@ if [[ -z "$1" ]]; then fi echo "$max_bytes" > "/sys/block/$1/bdi/max_bytes" -echo "$max_ration" > "/sys/block/$1/bdi/max_ratio" +echo "$max_ratio" > "/sys/block/$1/bdi/max_ratio" echo "$strict_limit" > "/sys/block/$1/bdi/strict_limit" From 44ecc46333e76e60d2e9b43224f66c1bcfcdf878 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Mon, 16 Oct 2023 04:34:36 +0000 Subject: [PATCH 34/43] Update PKGBUILD --- PKGBUILD | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 2ff593e..8b3edbf 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,25 +1,24 @@ # Maintainer: root.nix.dk pkgname='udev-usb-sync' -pkgver=0.5 -pkgrel=1 +pkgver=0.6 +pkgrel=2 pkgdesc='Fine tune write cache when USB storage device is plugged' arch=('any') url='https://codeberg.org/wonky/udev-usb-sync' license=('MIT') depends=('hdparm') -provides=("${pkgname}") backup=("etc/${pkgname}/${pkgname}.conf") -install=${pkgname}.install +install="${pkgname}.install" source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf') sha256sums=('be08224041008876efb09e6b2a119d6f42b911efecf0563be413f9ff2d33a7b3' - '0c60b841dd18521130eb22e18bf62bb5bf3dcf124e64dd7a6764aaf5af972eea' + '8d16ac2e912fa12c2ea79a8de4b94588b8f9e658fd8a3bf1e65bc8e5318c3140' 'bc902da2c453355e1dbc3a970a564daf71d41c6278dc771a930aa3badb90e453') package() { - install -d -m755 $pkgdir/etc/udev/rules.d - install -d -m755 $pkgdir/etc/$pkgname - install -d -m755 $pkgdir/usr/bin - cp ${srcdir}/99-usb-sync.rules "${pkgdir}/etc/udev/rules.d" - cp ${srcdir}/${pkgname} "${pkgdir}/usr/bin" - cp ${srcdir}/${pkgname}.conf "${pkgdir}/etc/${pkgname}" + install -d -m755 "$pkgdir/etc/udev/rules.d" + install -d -m755 "$pkgdir/etc/$pkgname" + install -d -m755 "$pkgdir/usr/bin" + cp "${srcdir}/99-usb-sync.rules" "${pkgdir}/etc/udev/rules.d" + cp "${srcdir}/${pkgname}" "${pkgdir}/usr/bin" + cp "${srcdir}/${pkgname}.conf" "${pkgdir}/etc/${pkgname}" } From 00c3f0005ad5a1930f721d1186a9f28f2db92ed0 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Mon, 16 Oct 2023 04:38:27 +0000 Subject: [PATCH 35/43] Update 99-usb-sync.rules --- 99-usb-sync.rules | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 68c3a3d..e71d7d9 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -7,6 +7,6 @@ ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bi # 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 -# implemented using /usr/bin/udev-usb-tweak.sh -# optional configuration in /etc/udev-usb-tweak/udev-usb-tweak.sh +# implemented using /usr/bin/udev-usb-sync +# optional configuration in /etc/udev-usb-sync/udev-usb-sync.conf ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/udev-usb-sync %k" From 0287d3301838b5703cc06caa9368a581872b6c60 Mon Sep 17 00:00:00 2001 From: Grumpy Coder Date: Mon, 16 Oct 2023 04:40:01 +0000 Subject: [PATCH 36/43] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 19e8d4d..5ba5302 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ Reload udev 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. +For those preferring the package manager, I have created a [PKGBUILD][4] which will pull the **hdparm** dependency upon installation. pamac build udev-usb-sync From 63c258c37abef30b71c3133ee2f7e0e5e88e21b8 Mon Sep 17 00:00:00 2001 From: wonky Date: Tue, 27 Aug 2024 08:53:08 +0000 Subject: [PATCH 37/43] Update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 2071b23..9112a7a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2023 Frede Hundewadt (@linux-aarhus) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From 21f9ed99496f6a3491d2a36a0bee7b655a47332b Mon Sep 17 00:00:00 2001 From: wonky Date: Tue, 27 Aug 2024 09:03:20 +0000 Subject: [PATCH 38/43] Update udev-usb-sync.conf --- udev-usb-sync.conf | 52 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/udev-usb-sync.conf b/udev-usb-sync.conf index 31683ee..5e98857 100644 --- a/udev-usb-sync.conf +++ b/udev-usb-sync.conf @@ -1,5 +1,53 @@ -# default values +# The configuration file is part of udev-usb-sync package +# +# MIT License +# +# Copyright (c) 2023 Frede Hundewadt (@linux-aarhus) +# +# Permission is hereby granted, free of charge,to any person obtaining a copy +# of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# @linux-aarhus - root.nix.dk +# +# configuration : /etc/usb-dev-sync/usb-dev-sync.conf +# triggered by : /usr/lib/udev/rules.d/99-usb-sync.rules +# +# contributors: @megavolt (Manjaro Forum) +# : @linux-aarhus (Manjaro Forum) +# inspired by : @kwg (EndeavourOS Forum) +# +# The values used can be modified if you need to do so +# The commented default usually works well #use_tweaks=1 -#max_bytes=16777216 +#use_bandwith=1 + #max_ratio=50 +# strict limit requires kernel >= 6.2 #strict_limit=1 + +# a sane default bandwidth value +#max_bytes=16777216 + +# bandwith based limitation requires >= 6.1 +# values as reported by the device ATTRS{speed} +# These values seem to be good and stable. +# It buffers 0,01s of the current bandwidth. +#max_bytes_12=10485 +#max_bytes_480=629145 +#max_bytes_5000=655369 +#max_bytes_10000=13107200 From 475e5094ab79a3ed1ff27f72ddd6f386cb35b3d7 Mon Sep 17 00:00:00 2001 From: wonky Date: Tue, 27 Aug 2024 09:05:29 +0000 Subject: [PATCH 39/43] Update udev-usb-sync --- udev-usb-sync | 59 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/udev-usb-sync b/udev-usb-sync index ccaab7e..b14ce65 100755 --- a/udev-usb-sync +++ b/udev-usb-sync @@ -1,18 +1,53 @@ #!/usr/bin/bash # -# script to tweak USB storage device filesystem sync +# The script is part of udev-usb-sync package # -# sources /etc/usb-dev-sync/usb-dev-sync.conf +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Affero GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# @linux-aarhus - root.nix.dk +# +# configuration : /etc/usb-dev-sync/usb-dev-sync.conf +# triggered by : /usr/lib/udev/rules.d/99-usb-sync.rules +# +# contributors: @megavolt (Manjaro Forum) +# : @linux-aarhus (Manjaro Forum) +# inspired by : @kwg (EndeavourOS Forum) +# +# Arguments provided by udev rule +# $1: usb block device +# $2: usb bandwidth reported by device +# +# default values (override in configuration file) # +# block device is passed in $1 +# speed is passed as $2 + +# defaults use_tweaks=1 -max_bytes=16777216 +use_bandwith=1 max_ratio=50 strict_limit=1 +max_bytes=16777216 +# speed defined values +max_bytes_12=10485 +max_bytes_480=629145 +max_bytes_5000=655369 +max_bytes_10000=13107200 -# read user config +# source configuratoin to override default values source /etc/udev-usb-sync/udev-usb-sync.conf - if [[ "$use_tweaks" = 0 ]]; then exit 0 fi @@ -21,6 +56,18 @@ if [[ -z "$1" ]]; then exit 1 fi -echo "$max_bytes" > "/sys/block/$1/bdi/max_bytes" +# if speed value is not present use default max_bytes +[[ -z $2 ]] && use_bandwith=0 + +# apply max_ratio echo "$max_ratio" > "/sys/block/$1/bdi/max_ratio" +# apply strict limit echo "$strict_limit" > "/sys/block/$1/bdi/strict_limit" +# apply max_bytes depending on speed value +if [[ use_bandwith == 0 ]]; then + # apply default value + echo "$max_bytes" > "/sys/block/$1/bdi/max_bytes" +else + # apply bandwidth defined value + echo "$max_bytes_$2" > "/sys/block/%1/bdi/max_bytes" +fi From e27bf71fce5387f1c6ab0291af36a9ab42ea9b69 Mon Sep 17 00:00:00 2001 From: wonky Date: Tue, 27 Aug 2024 09:06:03 +0000 Subject: [PATCH 40/43] Update 99-usb-sync.rules --- 99-usb-sync.rules | 50 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index e71d7d9..8b97fd0 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,12 +1,46 @@ +# This rules file is part of udev-usb-sync package +# +# MIT License +# +# Copyright (c) 2023 Frede Hundewadt (@linux-aarhus) +# +# Permission is hereby granted, free of charge,to any person obtaining a copy +# of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# @linux-aarhus - root.nix.dk +# +# configuration : /etc/usb-dev-sync/usb-dev-sync.conf +# triggered by : /usr/lib/udev/rules.d/99-usb-sync.rules +# +# contributors: @megavolt (Manjaro Forum) +# : @linux-aarhus (Manjaro Forum) +# inspired by : @kwg (EndeavourOS Forum) + # 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" -# + # rules to impose buffer limits on USB devices -# requires kernel > 6.1 -# 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 -# implemented using /usr/bin/udev-usb-sync -# optional configuration in /etc/udev-usb-sync/udev-usb-sync.conf -ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/udev-usb-sync %k" +# implemented using: /usr/bin/udev-usb-sync +# see configuration: /etc/udev-usb-sync/udev-usb-sync.conf +# Works since Linux 6.2 +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="12", RUN+="/usr/bin/udev-usb-sync %k 12" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="480", RUN+="/usr/bin/udev-usb-sync %k 480" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="5000", RUN+="/usr/bin/udev-usb-sync %k 5000" +ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="10000", RUN+="/usr/bin/udev-usb-sync %k 10000" + From 14bb3197dec4850b2464f741376556955c1c4779 Mon Sep 17 00:00:00 2001 From: wonky Date: Tue, 27 Aug 2024 09:06:48 +0000 Subject: [PATCH 41/43] Update udev-usb-sync.conf --- udev-usb-sync.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/udev-usb-sync.conf b/udev-usb-sync.conf index 5e98857..42d8b2f 100644 --- a/udev-usb-sync.conf +++ b/udev-usb-sync.conf @@ -1,4 +1,4 @@ -# The configuration file is part of udev-usb-sync package +# # The configuration file is part of udev-usb-sync package # # MIT License # From 7abeeadb9866061f50910852d27fbfa962ecb4a9 Mon Sep 17 00:00:00 2001 From: wonky Date: Tue, 27 Aug 2024 09:09:12 +0000 Subject: [PATCH 42/43] Update PKGBUILD --- PKGBUILD | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 8b3edbf..a5c659d 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,7 +1,7 @@ # Maintainer: root.nix.dk pkgname='udev-usb-sync' -pkgver=0.6 -pkgrel=2 +pkgver=0.8 +pkgrel=1 pkgdesc='Fine tune write cache when USB storage device is plugged' arch=('any') url='https://codeberg.org/wonky/udev-usb-sync' @@ -10,15 +10,15 @@ depends=('hdparm') backup=("etc/${pkgname}/${pkgname}.conf") install="${pkgname}.install" source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf') -sha256sums=('be08224041008876efb09e6b2a119d6f42b911efecf0563be413f9ff2d33a7b3' - '8d16ac2e912fa12c2ea79a8de4b94588b8f9e658fd8a3bf1e65bc8e5318c3140' - 'bc902da2c453355e1dbc3a970a564daf71d41c6278dc771a930aa3badb90e453') +sha256sums=('4f5888647d9be47a8992a7f5ea52eadd5baac0295a39751baa496815bddb065f' + 'b514e3bf1ea55f5e1dc4f5af46da3b5f9f2409da1efe7b36a11647704faf0a8f' + '7cf194b2e3767f8ce4dcbf98b665e46d67624034d5ec0f4cb89f359b677c3687') package() { install -d -m755 "$pkgdir/etc/udev/rules.d" install -d -m755 "$pkgdir/etc/$pkgname" install -d -m755 "$pkgdir/usr/bin" cp "${srcdir}/99-usb-sync.rules" "${pkgdir}/etc/udev/rules.d" - cp "${srcdir}/${pkgname}" "${pkgdir}/usr/bin" + cp "${srcdir}/${pkgname}" "${pkgdir}/usr/bin/${pkgname}" cp "${srcdir}/${pkgname}.conf" "${pkgdir}/etc/${pkgname}" -} +} \ No newline at end of file From 2f98c317457fc905e9e4bb5da130c5d06bd1255f Mon Sep 17 00:00:00 2001 From: Frede H Date: Wed, 11 Sep 2024 16:14:01 +0200 Subject: [PATCH 43/43] update rules - use auto calc for buffers based on hardware and speed --- 99-usb-sync.rules | 19 ++++------ LICENSE | 2 +- PKGBUILD | 16 ++++---- README.md | 94 ++++++++++++++++------------------------------ udev-usb-sync | 81 ++++++++++++++++++++------------------- udev-usb-sync.conf | 30 ++++++--------- 6 files changed, 104 insertions(+), 138 deletions(-) diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 8b97fd0..292bdd6 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -31,16 +31,13 @@ # : @linux-aarhus (Manjaro Forum) # inspired by : @kwg (EndeavourOS Forum) -# 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" +KERNEL!="sd[a-z]", GOTO="usb_limit_write_cache_end" +ENV{ID_USB_TYPE}!="disk", GOTO="usb_limit_write_cache_end" +ACTION!="add|change", GOTO="usb_limit_write_cache_end" -# rules to impose buffer limits on USB devices -# implemented using: /usr/bin/udev-usb-sync -# see configuration: /etc/udev-usb-sync/udev-usb-sync.conf -# Works since Linux 6.2 -ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="12", RUN+="/usr/bin/udev-usb-sync %k 12" -ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="480", RUN+="/usr/bin/udev-usb-sync %k 480" -ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="5000", RUN+="/usr/bin/udev-usb-sync %k 5000" -ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="10000", RUN+="/usr/bin/udev-usb-sync %k 10000" +ATTRS{bDeviceClass}!="09", ATTRS{speed}=="10000", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end" +ATTRS{bDeviceClass}!="09", ATTRS{speed}=="5000", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end" +ATTRS{bDeviceClass}!="09", ATTRS{speed}=="480", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end" +ATTRS{bDeviceClass}!="09", ATTRS{speed}=="12", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end" +LABEL="usb_limit_write_cache_end" diff --git a/LICENSE b/LICENSE index 9112a7a..2071b23 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Frede Hundewadt (@linux-aarhus) +Copyright (c) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/PKGBUILD b/PKGBUILD index a5c659d..edb93ce 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,18 +1,18 @@ # Maintainer: root.nix.dk pkgname='udev-usb-sync' -pkgver=0.8 +pkgver=0.9 pkgrel=1 -pkgdesc='Fine tune write cache when USB storage device is plugged' +pkgdesc='Fine tune write cache and impose buffer limites when USB storage device is plugged' arch=('any') -url='https://codeberg.org/wonky/udev-usb-sync' +url='https://gitlab.manjaro.org/fhdk/udev-usb-sync' license=('MIT') -depends=('hdparm') +depends=('hdparm' 'bc') backup=("etc/${pkgname}/${pkgname}.conf") install="${pkgname}.install" source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf') -sha256sums=('4f5888647d9be47a8992a7f5ea52eadd5baac0295a39751baa496815bddb065f' - 'b514e3bf1ea55f5e1dc4f5af46da3b5f9f2409da1efe7b36a11647704faf0a8f' - '7cf194b2e3767f8ce4dcbf98b665e46d67624034d5ec0f4cb89f359b677c3687') +sha256sums=('14ff9fa9783f72cb321792ea8a44051d0eb0aaf244edec773d7fa16fdf8dc023' + 'd2987ed0e88d027024ea6c060e42e5557505216d97029930620bd60fccc22b8b' + '4666e62ef38fe4c9c982411f92a99e8e8e5cf73eb08e83252afaf5ec0b527acc') package() { install -d -m755 "$pkgdir/etc/udev/rules.d" @@ -21,4 +21,4 @@ package() { cp "${srcdir}/99-usb-sync.rules" "${pkgdir}/etc/udev/rules.d" cp "${srcdir}/${pkgname}" "${pkgdir}/usr/bin/${pkgname}" cp "${srcdir}/${pkgname}.conf" "${pkgdir}/etc/${pkgname}" -} \ No newline at end of file +} diff --git a/README.md b/README.md index 5ba5302..dca3cb9 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,30 @@ -# Disable write cache for usb storage devices - -[Manjaro Forum topic][0] - ## Linux filesystem cache Linux agressively caches files in order to improve overall performance. +* https://linuxatemyram.com + 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 + +Examples: + +* https://forum.manjaro.org/t/unbelievably-slow-file-transfer-speeds/123222 +* https://forum.manjaro.org/t/decrease-dirty-bytes-for-more-reliable-usb-transfer/120798 +* https://forum.manjaro.org/t/slow-usb-transfers/146863 +* https://forum.manjaro.org/t/usb-transfer-speed-5mb-sec-dropping/146777 +* https://forum.manjaro.org/t/extremely-slow-file-transfer-speeds-from-usb-3-0-to-external-hdd/39339 +* https://forum.manjaro.org/t/transfer-speed-slows-down-to-a-crawl-after-10gb-transfered/43453 +* https://forum.manjaro.org/t/usb-transfer-data-drops-drastically-and-maximum-number-of-clients-are-reached/93278 +* https://forum.manjaro.org/t/file-copy-on-a-usb-2-slows-down-to-a-halt/36316 +* https://forum.manjaro.org/t/dolphin-large-file-transfer-to-usb-stick-indicates-completed-after-caching-is-done-transfer-still-ongoing/132634 ## 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. +In [another place][2] 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. @@ -23,57 +32,13 @@ I dug into the intricacies of udev and found a method to only target USB storage 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" +https://gitlab.manjaro.org/fhdk/udev-usb-sync/-/blob/master/99-usb-sync.rules -# -# 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/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 -# +https://gitlab.manjaro.org/fhdk/udev-usb-sync/-/blob/master/udev-usb-sync.conf -use_tweaks=1 -max_bytes=16777216 -max_ratio=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_ratio" > "/sys/block/$1/bdi/max_ratio" -echo "$strict_limit" > "/sys/block/$1/bdi/strict_limit" -``` +https://gitlab.manjaro.org/fhdk/udev-usb-sync/-/blob/master/udev-usb-sync +## How it works The rule activates when udev detects * add or change @@ -86,7 +51,9 @@ The rule activates when udev detects - 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. @@ -99,15 +66,20 @@ Reload udev 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. +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. -For those preferring the package manager, I have created a [PKGBUILD][4] which will pull the **hdparm** dependency upon installation. +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 -Another idea by [@megavolt][1] at [Manjaro Forum][2] which does not require hdparm. +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 -[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 \ No newline at end of file +https://gitlab.com/cscs/maxperfwiz + +https://gitlab.manjaro.org/fhdk/udev-usb-sync + +* Cross posted - https://root.nix.dk/en/system-configuration/disable-write-cache-for-usb-devices +* Related topic with tests - [Slow USB transfers - #12 by linux-aarhus](https://forum.manjaro.org/t/slow-usb-transfers/146863/12) + +[1]: https://aur.archlinux.org/packages/udev-usb-sync +[2]: https://forum.endeavouros.com/t/how-come-everything-is-written-when-i-decide-to-unmount-a-usbdrive/37178/10 \ No newline at end of file diff --git a/udev-usb-sync b/udev-usb-sync index b14ce65..46a1df2 100755 --- a/udev-usb-sync +++ b/udev-usb-sync @@ -18,7 +18,7 @@ # @linux-aarhus - root.nix.dk # # configuration : /etc/usb-dev-sync/usb-dev-sync.conf -# triggered by : /usr/lib/udev/rules.d/99-usb-sync.rules +# triggered by : /etc/udev/rules.d/99-usb-sync.rules # # contributors: @megavolt (Manjaro Forum) # : @linux-aarhus (Manjaro Forum) @@ -28,46 +28,51 @@ # $1: usb block device # $2: usb bandwidth reported by device # -# default values (override in configuration file) -# -# block device is passed in $1 -# speed is passed as $2 +set -euo pipefail -# defaults -use_tweaks=1 -use_bandwith=1 -max_ratio=50 -strict_limit=1 -max_bytes=16777216 -# speed defined values -max_bytes_12=10485 -max_bytes_480=629145 -max_bytes_5000=655369 -max_bytes_10000=13107200 +LANG=C +LC_NUMERIC=C -# source configuratoin to override default values -source /etc/udev-usb-sync/udev-usb-sync.conf -if [[ "$use_tweaks" = 0 ]]; then - exit 0 -fi +AUTOCALC=${AUTOCALC:-1} +CONFIG='/etc/udev-usb-sync/udev-usb-sync.conf' -if [[ -z "$1" ]]; then - exit 1 -fi +[[ -f $CONFIG ]] && source $CONFIG -# if speed value is not present use default max_bytes -[[ -z $2 ]] && use_bandwith=0 +BLOCKDEVICE="$1" +SPEED="$2" +KERNEL_MAJOR_VERSION=$(uname -r | awk -F'.' '{print $1}') -# apply max_ratio -echo "$max_ratio" > "/sys/block/$1/bdi/max_ratio" -# apply strict limit -echo "$strict_limit" > "/sys/block/$1/bdi/strict_limit" -# apply max_bytes depending on speed value -if [[ use_bandwith == 0 ]]; then - # apply default value - echo "$max_bytes" > "/sys/block/$1/bdi/max_bytes" -else - # apply bandwidth defined value - echo "$max_bytes_$2" > "/sys/block/%1/bdi/max_bytes" -fi +# disable write cache for device if possible +[[ -n $(which hdparm) ]] && $(which hdparm) -W 0 /dev/$BLOCKDEVICE + +if [[ $KERNEL_MAJOR_VERSION -le 5 ]]; then + # the following rules is introduced with kernel 2.6 + # https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-max-ratio + + # 1% of available RAM -> 8046522kB -> 80.465kB -> 80MB + echo 1 > /sys/block/$BLOCKDEVICE/bdi/max_ratio +elif [[ $KERNEL_MAJOR_VERSION -ge 6 ]]; then + # the following rules is introduced with kernel 6.1 + # 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-bytes + + # apply strict limit + echo 1 > /sys/block/$BLOCKDEVICE/bdi/strict_limit + + if [[ ${AUTOCALC} == 0 ]]; then + # apply 16M as max_bytes + echo 16777216 > /sys/block/$BLOCKDEVICE/bdi/max_bytes + elif [[ ${AUTOCALC} == 1 ]]; then + BUFFER_TIME=${BUFFER_TIME:-"0.05"} + SAFETY_FACTOR=${SAFETY_FACTOR:-"1.3"} + BUFFER_SIZE=$(printf '%.0f' `echo "( ($SPEED / 8) * $BUFFER_TIME * $SAFETY_FACTOR) * 1024 * 1024" | bc`) + # for x in 12 480 5000 10000; do echo -n "$x -> " ;printf "%.0f\n" ` echo "(($x / 8) * 0.05 * 1.3) * 1024 * 1024" | bc`; done + # 62915 + # 4089446 + # 42593157 + # 85196800 + # apply calculated buffer size + echo "$BUFFER_SIZE" > /sys/block/$BLOCKDEVICE/bdi/max_bytes + fi +fi \ No newline at end of file diff --git a/udev-usb-sync.conf b/udev-usb-sync.conf index 42d8b2f..ee3beab 100644 --- a/udev-usb-sync.conf +++ b/udev-usb-sync.conf @@ -1,4 +1,4 @@ -# # The configuration file is part of udev-usb-sync package +# The configuration file is part of udev-usb-sync package # # MIT License # @@ -24,8 +24,8 @@ # # @linux-aarhus - root.nix.dk # -# configuration : /etc/usb-dev-sync/usb-dev-sync.conf -# triggered by : /usr/lib/udev/rules.d/99-usb-sync.rules +# trigger script: /usr/bin/usb-dev-sync +# triggered by : /etc/udev/rules.d/99-usb-sync.rules # # contributors: @megavolt (Manjaro Forum) # : @linux-aarhus (Manjaro Forum) @@ -33,21 +33,13 @@ # # The values used can be modified if you need to do so # The commented default usually works well -#use_tweaks=1 -#use_bandwith=1 +# +# +## Let it calculate based on given bandwidth +#AUTOCALC=1 -#max_ratio=50 -# strict limit requires kernel >= 6.2 -#strict_limit=1 +## The time in seconds to hold data in RAM +#BUFFER_TIME='0.05' -# a sane default bandwidth value -#max_bytes=16777216 - -# bandwith based limitation requires >= 6.1 -# values as reported by the device ATTRS{speed} -# These values seem to be good and stable. -# It buffers 0,01s of the current bandwidth. -#max_bytes_12=10485 -#max_bytes_480=629145 -#max_bytes_5000=655369 -#max_bytes_10000=13107200 +## multiplicator for safety reasons. +#SAFETY_FACTOR='1.3'