Compare commits

..

3 commits

Author SHA1 Message Date
Frede H
0130785971
update kernel check 2024-09-13 17:31:48 +02:00
Frede H
e1ec0b47c3
fix kernel minor check 2024-09-13 15:42:33 +02:00
Frede H
1fe2ddca75
Added kernel minor to the factor
Only kernels above 6.1 supports strict_limit and max_bytes
2024-09-13 15:07:11 +02:00
4 changed files with 37 additions and 60 deletions

View file

@ -1,19 +0,0 @@
pkgbase = udev-usb-sync
pkgdesc = Fine tune write cache and impose buffer limits when USB storage device is plugged
pkgver = 0.9
pkgrel = 3
url = https://codeberg.org/wonky/udev-usb-sync
install = udev-usb-sync.install
arch = any
license = MIT
depends = hdparm
depends = bc
backup = etc/udev-usb-sync/udev-usb-sync.conf
source = 99-usb-sync.rules
source = udev-usb-sync
source = udev-usb-sync.conf
sha256sums = fde345c047bbb3fb51cb2a66b6106a883a413638b760e0e8087c692a30f87514
sha256sums = bdc35135e6f8074890a911a501ca4fb3e6a7888963b51c3a1edb98c23bb62ec2
sha256sums = ec26baede73e94f9cfab77cd5aa6e0ffebcc413ff657a4e98eae6c9e2145655e
pkgname = udev-usb-sync

View file

@ -31,13 +31,17 @@
# : @linux-aarhus (Manjaro Forum)
# inspired by : @kwg (EndeavourOS Forum)
KERNEL!="sd[a-z]", GOTO="usb_limit_write_cache_end"
KERNEL!="sd[a-z]|sd[a-z][0-9]", 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"
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"
PROGRAM+="/usr/bin/uname -r"
LABEL="usb_limit_write_cache_end"
RESULT=="4*|5*|6.1.*", ENV{ID_FS_USAGE}=="filesystem", ENV{UDISKS_MOUNT_OPTIONS_DEFAULTS}+="sync", ENV{UDISKS_MOUNT_OPTIONS_ALLOW}+="sync", GOTO="usb_limit_write_cache_end"
RESULT!="4*|5*|6.1.*", ATTRS{bDeviceClass}!="09", ATTRS{speed}=="10000", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end"
RESULT!="4*|5*|6.1.*", ATTRS{bDeviceClass}!="09", ATTRS{speed}=="5000", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end"
RESULT!="4*|5*|6.1.*", ATTRS{bDeviceClass}!="09", ATTRS{speed}=="480", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end"
RESULT!="4*|5*|6.1.*", 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"

View file

@ -1,17 +1,17 @@
# Maintainer: root.nix.dk
pkgname='udev-usb-sync'
pkgver=0.9
pkgrel=3
pkgver=0.12
pkgrel=1
pkgdesc='Fine tune write cache and impose buffer limits 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' 'bc')
backup=("etc/${pkgname}/${pkgname}.conf")
install="${pkgname}.install"
source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf')
sha256sums=('fde345c047bbb3fb51cb2a66b6106a883a413638b760e0e8087c692a30f87514'
'bdc35135e6f8074890a911a501ca4fb3e6a7888963b51c3a1edb98c23bb62ec2'
sha256sums=('b1463dc2f102b31d16b8e9b3ba39356bdf6ec791b6b2034f3ded5553f4c31f8a'
'741fbc305c151c88dad3bdb2203289855c7dc2a2a7d581c8e325dd8ed286c6dc'
'ec26baede73e94f9cfab77cd5aa6e0ffebcc413ff657a4e98eae6c9e2145655e')
package() {

View file

@ -41,38 +41,30 @@ CONFIG='/etc/udev-usb-sync/udev-usb-sync.conf'
BLOCKDEVICE="$1"
SPEED="$2"
KERNEL_MAJOR_VERSION=$(uname -r | awk -F'.' '{print $1}')
# disable write cache for device if possible
[[ -n $(which hdparm) ]] && $(which hdparm) -W 0 /dev/$BLOCKDEVICE
[[ -n $(which hdparm) ]] && $(which hdparm) -W 0 /dev/$BLOCKDEVICE > /dev/null
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
# the following rules is introduced with kernel 6.2
# https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-max-bytes
# 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
case $AUTOCALC in
0) # 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
;;
1) 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
;;
esac
# https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-strict-limit
# apply strict limit
echo 1 > /sys/block/$BLOCKDEVICE/bdi/strict_limit