Files
LEAF/scripts/provision-ids-tmpfs.sh
2026-11-15 17:42:55 +01:00

77 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# LEAF-PROTO sensor provisioning: RAM disk (tmpfs) for the suricata eve dir.
#
# eve.json under /var/log/suricata-leaf is rewritten constantly; on a
# journal/flash disk that burns write cycles and adds latency. This puts the
# directory on a size-capped tmpfs (RAM+swap backed), persisted via fstab.
# Controller state stays on persistent storage in /var/lib/leaf-controller.
# See docs/INSTALLATION.md for memory sizing and log retention.
#
# Usage: TMPFS_SIZE=6G sudo scripts/provision-ids-tmpfs.sh [mount_point]
# (default mount point /var/log/suricata-leaf, default size 8G)
set -euo pipefail
MOUNT_POINT="${1:-${MOUNT_POINT:-/var/log/suricata-leaf}}"
SIZE="${TMPFS_SIZE:-${SIZE:-8G}}"
usage() {
echo "usage: TMPFS_SIZE=8G sudo $0 [mount_point (default: /var/log/suricata-leaf)]"
}
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
echo "error: run as root (needs mount/fstab writes)" >&2
usage
exit 1
fi
if findmnt -rn -T "$MOUNT_POINT" | grep -q 'tmpfs'; then
echo "skip: $MOUNT_POINT is already on tmpfs:"
findmnt -rn -T "$MOUNT_POINT"
exit 0
fi
if ! [[ "$SIZE" =~ ^[0-9]+([MGK])?$ ]]; then
echo "error: TMPFS_SIZE must like 6G/8G/512M (got: $SIZE)" >&2
exit 1
fi
echo "warning: tmpfs is RAM-backed — $SIZE cap counts against RAM+swap."
free -h
echo "note: eve.json on this mount is lost on reboot by design;"
echo "note: controller block state lives on disk (/var/lib/leaf-controller), unaffected."
mkdir -p "$MOUNT_POINT"
# A pre-existing non-empty dir on another filesystem would be hidden under
# the tmpfs after mounting — warn, since old eve files become unreachable.
if [[ -n "$(findmnt -rn -T "$MOUNT_POINT" 2>/dev/null)" ]] &&
! findmnt -rn -T "$MOUNT_POINT" | grep -q 'tmpfs' &&
[[ -n "$(ls -A "$MOUNT_POINT" 2>/dev/null)" ]]; then
echo "warning: $MOUNT_POINT already has content on another filesystem;"
echo "warning: it will be hidden underneath the tmpfs — move it first if"
echo "warning: you need the old eve.json:"
ls -A "$MOUNT_POINT" | head -5
fi
FSTAB_LINE="tmpfs ${MOUNT_POINT} tmpfs defaults,size=${SIZE},nosuid,noexec,nodev 0 0"
if grep -qF " ${MOUNT_POINT} " /etc/fstab; then
echo "fstab already has an entry for $MOUNT_POINT — not appending:"
grep -F " ${MOUNT_POINT} " /etc/fstab
else
printf '\n# LEAF-PROTO: suricata eve on RAM disk (provision-ids-tmpfs.sh)\n%s\n' "$FSTAB_LINE" >> /etc/fstab
echo "fstab entry appended."
fi
systemctl daemon-reload
mount -a
if findmnt -rn -T "$MOUNT_POINT" | grep -q 'tmpfs'; then
echo "OK: mounted."
findmnt -rn -T "$MOUNT_POINT"
else
echo "error: mount did not come up — check /etc/fstab" >&2
exit 1
fi
df -h "$MOUNT_POINT"