diff --git a/.SRCINFO b/.SRCINFO index d0c9955..e1d8868 100644 --- a/.SRCINFO +++ b/.SRCINFO @@ -1,7 +1,7 @@ pkgbase = udev-usb-sync - pkgdesc = Fine tune write cache when USB storage device is plugged - pkgver = 0.6 - pkgrel = 2 + pkgdesc = Fine tune write cache and impose buffer limites when USB storage device is plugged + pkgver = 0.8 + pkgrel = 1 url = https://codeberg.org/wonky/udev-usb-sync install = udev-usb-sync.install arch = any @@ -11,8 +11,8 @@ pkgbase = udev-usb-sync source = 99-usb-sync.rules source = udev-usb-sync source = udev-usb-sync.conf - sha256sums = be08224041008876efb09e6b2a119d6f42b911efecf0563be413f9ff2d33a7b3 - sha256sums = 8d16ac2e912fa12c2ea79a8de4b94588b8f9e658fd8a3bf1e65bc8e5318c3140 - sha256sums = bc902da2c453355e1dbc3a970a564daf71d41c6278dc771a930aa3badb90e453 + sha256sums = 4f5888647d9be47a8992a7f5ea52eadd5baac0295a39751baa496815bddb065f + sha256sums = b514e3bf1ea55f5e1dc4f5af46da3b5f9f2409da1efe7b36a11647704faf0a8f + sha256sums = 7cf194b2e3767f8ce4dcbf98b665e46d67624034d5ec0f4cb89f359b677c3687 pkgname = udev-usb-sync diff --git a/99-usb-sync.rules b/99-usb-sync.rules index 68c3a3d..06ad118 100644 --- a/99-usb-sync.rules +++ b/99-usb-sync.rules @@ -1,12 +1,45 @@ +# 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-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 %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" diff --git a/PKGBUILD b/PKGBUILD index 8b3edbf..2d1246c 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,8 +1,8 @@ # Maintainer: root.nix.dk pkgname='udev-usb-sync' -pkgver=0.6 -pkgrel=2 -pkgdesc='Fine tune write cache when USB storage device is plugged' +pkgver=0.8 +pkgrel=1 +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' license=('MIT') @@ -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}" } diff --git a/udev-usb-sync b/udev-usb-sync index ccaab7e..1192dcb 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 \ No newline at end of file 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