can you write a script that checks all of these and provides output to the screen ? To…
- User
can you write a script that checks all of these and provides output to the screen ? To determine if data needs to be flushed to an SD card using direct kernel interfaces (without utilities), follow these steps:
1. Find the Device Identifier
First, identify your SD card's device name (e.g.,
/dev/mmcblk0or/dev/sdb):grep -l 'SD\|MMC' /sys/block/*/device/modelExample output:
/sys/block/mmcblk0/device/model
2. Check for Dirty Writeback Pages
The kernel tracks pages in memory that haven't been written to disk ("dirty" pages). Use:
cat /sys/block/<device>/bdi/*/dirty_bytesReplace
<device>with your SD card's name (e.g.,mmcblk0).
If the value is0, no data needs flushing.
Non-zero means data is pending.
3. Verify Kernel Request Queue State
Check if the device's I/O queue has pending operations:
cat /sys/block/<device>/statThe 9th field (counting from 1) shows current I/O operations in flight.
Non-zero = pending operations.Example to extract the 9th field:
awk '{print $9}' /sys/block/mmcblk0/stat
4. Check Page Cache State
Inspect the kernel's page cache for dirty pages targeting the device:
sudo grep -E 'dirty|writeback' /proc/meminfoDirty: Data not yet written to disk.Writeback: Data actively being written.
Non-zero values indicate pending operations (system-wide, not device-specific).
5. Force Flush & Verify
If you suspect pending writes:
- Force a sync:
sync - Re-check Steps 2–4. Persistent non-zero values mean data is still flushing.
Why This Works
/sys/block/<device>/bdi/*/dirty_bytes: Tracks unflushed bytes for the device's backing storage./sys/block/<device>/stat: Exposes live I/O statistics from the kernel's block layer./proc/meminfo: Shows system-wide page cache state (dirty/writeback pages).
Final Workflow
# 1. Find device DEVICE=$(grep -l 'SD\|MMC' /sys/block/*/device/model | cut -d/ -f4) # 2. Check dirty bytes echo "Dirty bytes: $(cat /sys/block/$DEVICE/bdi/*/dirty_bytes)" # 3. Check I/O in flight echo "I/O in flight: $(awk '{print $9}' /sys/block/$DEVICE/stat)" # 4. Check system page cache grep -E 'Dirty|Writeback' /proc/meminfoSafe to eject when:
dirty_bytes = 0I/O in flight = 0/proc/meminfoDirty/Writeback values are low/stable (aftersync).
⚠️ Note: The kernel doesn't expose per-device dirty page counts directly. The above combines the closest proxies. Always
syncand verify I/O completion before ejection. - Assistant
Below is a small Bash script (no external packages, just the shell plus the usual core tools that ship with every Debian install) that:
• auto-detects every SD / MMC block device
• shows, for each one, the kernel counters you asked for
• prints a single verdict: SAFE / WAIT
• optionally forcessyncbefore reporting (--sync flag)
• lets you override autodetection with ‑-device#!/usr/bin/env bash # ========================================================== # check_sd_flush.sh – Are my SD/MMC writes finished? # # Usage: # ./check_sd_flush.sh # auto-detect, just report # ./check_sd_flush.sh --sync # sync first, then report # ./check_sd_flush.sh --device mmcblk0 # ========================================================== set -euo pipefail SYNC_FIRST=0 USER_DEVICE="" print_usage() { echo "Usage: $0 [--sync] [--device <blockdev>]" exit 1 } while [[ $# -gt 0 ]]; do case "$1" in --sync|-s) SYNC_FIRST=1; shift ;; --device|-d) USER_DEVICE="$2"; shift 2 ;; -h|--help) print_usage ;; *) echo "Unknown option: $1"; print_usage ;; esac done # ---------------------------------------------------------- # 1. Build list of candidate devices # ---------------------------------------------------------- declare -a DEVICES=() if [[ -n "${USER_DEVICE}" ]]; then # trust caller if [[ ! -e "/sys/block/${USER_DEVICE}" ]]; then echo "ERROR: /sys/block/${USER_DEVICE} not found." >&2 exit 1 fi DEVICES+=("${USER_DEVICE}") else # heuristics: model string contains SD or MMC for path in /sys/block/*/device/model; do [[ -e "$path" ]] || continue model=$(tr -d '[:space:]' < "$path") if [[ "$model" =~ (SD|MMC|sd|mmc) ]]; then DEVICES+=("$(basename "$(dirname "$(dirname "$path")")")") fi done fi if [[ "${#DEVICES[@]}" -eq 0 ]]; then echo "No SD/MMC block devices found." exit 1 fi # ---------------------------------------------------------- # Optionally flush everything first # ---------------------------------------------------------- if (( SYNC_FIRST )); then echo "Syncing caches ... (may take a moment)" sync fi # ---------------------------------------------------------- # Convenience helpers # ---------------------------------------------------------- read_dirty_bytes() { local dev="$1" local file # new kernels keep dirty_bytes directly in bdi/ if [[ -f "/sys/block/$dev/bdi/dirty_bytes" ]]; then cat "/sys/block/$dev/bdi/dirty_bytes" return fi # older: /sys/block/<dev>/bdi/*/dirty_bytes file=$(ls /sys/block/"$dev"/bdi/*/dirty_bytes 2>/dev/null | head -n1 || true) [[ -n "$file" ]] && cat "$file" || echo "0" } global_dirty_kb() { awk '/^Dirty:/ {print $2}' /proc/meminfo; } global_write_kb() { awk '/^Writeback:/ {print $2}' /proc/meminfo; } # ---------------------------------------------------------- # Output header # ---------------------------------------------------------- printf "\n%-10s | %-12s | %-12s | %-8s\n" "DEVICE" "DIRTY_BYTES" "IO_IN_FLIGHT" "VERDICT" printf -- "-------------------------------------------------------------\n" # ---------------------------------------------------------- # Loop over every candidate # ---------------------------------------------------------- RET=0 # will be 1 if anything is still busy for dev in "${DEVICES[@]}"; do dirty_bytes=$(read_dirty_bytes "$dev") inflight=$(awk '{print $9}' "/sys/block/$dev/stat") global_dirty=$(global_dirty_kb) global_write=$(global_write_kb) verdict="SAFE" if [[ "$dirty_bytes" -ne 0 || "$inflight" -ne 0 || "$global_dirty" -ne 0 || "$global_write" -ne 0 ]]; then verdict="WAIT" RET=1 fi printf "%-10s | %-12s | %-12s | %-8s\n" "$dev" "$dirty_bytes" "$inflight" "$verdict" done # ---------------------------------------------------------- # If anything was busy, also show the global numbers # ---------------------------------------------------------- if (( RET )); then echo echo "System-wide cache state:" grep -E '^(Dirty|Writeback):' /proc/meminfo echo echo "At least one counter is non-zero – give it a moment and re-run." else echo -e "\nAll counters are zero – it should be safe to eject." fi exit $RETHow it works
-
Device discovery
Looks in/sys/block/*/device/modelfor strings containing “SD” or “MMC”.
You can override that with--device. -
dirty_bytes
Reads/sys/block/<dev>/bdi/dirty_bytes(or the older.../bdi/*/dirty_bytes).
Non-zero ⇒ kernel still has unwritten pages for that block device. -
I/O in flight
Field 9 of/sys/block/<dev>/statshows requests currently active in the block layer. -
Global cache counters
/proc/meminfo“Dirty” + “Writeback” lines give a system-wide sanity check. -
Verdict
If any of those four numbers are non-zero the script prints “WAIT”; otherwise “SAFE”.
Return value
0= safe to eject,1= still flushing.
That lets you use it in larger scripts:if ./check_sd_flush.sh --sync; then echo "Remove the card now." else echo "Hold on, still writing." fiCopy the snippet to a file,
chmod +x, and you’re good to go. Happy (and safe) ejecting! 🚀 -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.