#! /bin/bash
#

# Setup lustre with two mountpoints
MOUNT_2="true" lustre/tests/llmount.sh
echo "trace ioctl hsm" | sudo tee /sys/kernel/debug/lnet/debug || exit $?
lctl set_param mdt.*.hsm_control enabled

# Create a file on the first mountpoint
dd if=/dev/zero of=/mnt/lustre/blob bs=1M count=0 || exit 1

# Launch a copytool
lustre/utils/lhsmtool_posix --daemon --hsm-root /dev/shm /mnt/lustre
# Suspend it
pkill -STOP "lhsmtool_posix" || exit $?

# Empty debug logs
lctl dk > /dev/null

# Launch an ARCHIVE request using the second mountpoint
lfs hsm_archive /mnt/lustre2/blob
# Wait for it to be sent to the copytool
while ! lctl get_param mdt.*.hsm.actions | grep STARTED; do
    sleep 1
done

# Delete the file
rm /mnt/lustre2/blob || exit $?
sync

# Wake up the copytool (it seems important to wake it first before killing it)
pkill -CONT "lhsmtool_posix" || exit $?

# Store debug logs in a temporary file
tmpfile="$(mktemp --tmpdir=/tmp LU-10302-XXXXX.tmp)" && lctl dk > "$tmpfile"

# Umount (this should hang in an UNKILLABLE state)
lustre/tests/llmountcleanup.sh
