#!/bin/sh
# setup_image.sh - Create a hammer2 filesystem image with enough entries
# to force an INDIRECT block in testdir's inode blockset.  Run as root
# in the guest.  Produces /root/h2.img.
set -e
IMG=/root/h2.img

rm -f "$IMG"
dd if=/dev/zero of="$IMG" bs=1m count=64 2>&1 | tail -1

vnconfig -c vn0 "$IMG"
newfs_hammer2 /dev/vn0 2>&1 | tail -2

mkdir -p /mnt/h2test
mount_hammer2 /dev/vn0@DATA /mnt/h2test

# Create testdir with enough entries that its inode's blockset spills
# into an INDIRECT block.  hammer2 inodes have a 4-entry blockset; one
# is reserved for atime, leaving 3 slots.  Adding 4+ entries forces an
# INDIRECT.  Add files of varying name lengths.
mkdir -p /mnt/h2test/testdir
for i in 1 2 3 4 5 6 7 8 9 10 11 12; do
    echo "file$i" > /mnt/h2test/testdir/file$i.txt
done
# >64-byte filename forces a DIRENT with a separate data block too
touch "/mnt/h2test/testdir/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.txt"

ls /mnt/h2test/testdir/
sync
umount /mnt/h2test
vnconfig -u vn0
ls -la "$IMG"
echo "SETUP_DONE"
