#!/bin/sh
# DF-0813 hammer2 flush NULL-deref setup (run as root, ONCE).
# Creates a vnode-backed hammer2 image, newfs's it, mounts it world-writable
# so the unprivileged user (maxx) can drive churn.
set -e
IMG=/root/h2.img
VN=vn0
MNT=/mnt/h2
SIZE=2g

echo "[setup] creating ${SIZE} sparse image ${IMG}"
rm -f ${IMG}
dd if=/dev/zero of=${IMG} bs=1m count=0 seek=${SIZE} 2>/dev/null

echo "[setup] vnconfig -c -T -S ${SIZE} ${VN} ${IMG}"
vnconfig -u ${VN} 2>/dev/null || true
vnconfig -c -T -S ${SIZE} ${VN} ${IMG}

echo "[setup] newfs_hammer2 /dev/${VN}"
newfs_hammer2 /dev/${VN} >/dev/null 2>&1 || newfs_hammer2 -f /dev/${VN}

echo "[setup] mount_hammer2 /dev/${VN}@DEFAULT ${MNT}"
mkdir -p ${MNT}
umount ${MNT} 2>/dev/null || true
mount_hammer2 /dev/${VN}@DEFAULT ${MNT}

echo "[setup] chmod 1777 ${MNT}"
chmod 1777 ${MNT}

echo "[setup] done. mount:"
mount | grep hammer2
