#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2023 Bruno Gonçalves <www.biglinux.com.br>
#
# Cap the dirty page cache of a USB disk at 16 MiB, unless it holds /.
# lsblk -s walks from the root file system down through LUKS or LVM to
# the disk. Matching df's source missed a USB disk under encryption,
# AtropOS's default: df names /dev/mapper/cr_root, never the disk.

root=$(findmnt -nvo SOURCE /)
if [ -n "$root" ] && lsblk -nsro NAME "$root" 2>/dev/null | grep -qxF "$1"; then
    exit 0
fi
echo 1 > "/sys/block/$1/bdi/strict_limit"
echo 16777216 > "/sys/block/$1/bdi/max_bytes"
