A udev rule, /etc/udev/rules.d/99-adjust-writeback-cache.rules
:
ACTION=="add", KERNEL=="sd?", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", \
RUN+="/usr/local/lib/adjust-writeback-cache.sh $major $minor"
cat /usr/local/lib/adjust-writeback-cache.sh
#! /bin/bash
devroot=/sys/class/bdi
max_bytes=134217728 # must be divisible by 4096
dev=$1:$2
logme="logger --tag basename $0
"
test "$TERM" = "xterm-256color" && logme=echo
test "$UID" -ne "0" && $logme "Must be run under root" && exit 1
test -z "$2" && $logme "Need two arguments: major minor" && exit 2
$logme "Adjusting writeback cache for the device [$dev] to $max_bytes bytes ..."
test ! -d "$devroot/$dev" && $logme "The device [$dev] is not found in $devroot. Bailing out!" && exit 3
echo $max_bytes > "$devroot/$dev/max_bytes"
res=cat "$devroot/$dev/max_bytes"
test "$res" = "$max_bytes" && $logme "All good!" || $logme "The operation has failed."
Don't forget to make it executable, e.g. sudo chmod 755 /usr/local/lib/adjust-writeback-cache.sh
vm.dirty_bytes
andvm.dirty_background_bytes
look simply insane for systems with more than 16GB of RAM. – Artem S. Tashkinov Mar 01 '24 at 09:16