#!/bin/sh
# DF-4800 PoC setup (ASYNC backing): mirrored vinum volume on two GPT
# partitions of a second virtio disk (/dev/vbd1).  virtio-blk completes
# asynchronously (interrupt-driven), so revive_block()'s biowait really
# sleeps; the LWKT scheduler passively releases the MP lock (held by the
# non-MPSAFE vinum ioctl) while the thread sleeps, which opens the
# concurrent-revive interleaving window that the inline md backend
# closes.
DATA_SKIP=265
DATA_CNT=6144
kldload vinum >/dev/null 2>&1
# zero the ENTIRE disk: a GPT backup header at the end of the disk
# survives a partial zero and corrupts the re-created slice table
dd if=/dev/zero of=/dev/vbd1 bs=1m count=512 >/dev/null 2>&1
gpt create -f vbd1 >/dev/null 2>&1
gpt add -t vinum -s 65536 vbd1 >/dev/null 2>&1
gpt add -t vinum -s 65536 vbd1 >/dev/null 2>&1
ls /dev/vbd1*
printf "drive d1 device /dev/vbd1s0\ndrive d2 device /dev/vbd1s1\nvolume vol\n plex org concat\n  sd length 3m drive d1\n plex org concat\n  sd length 3m drive d2\n" > /tmp/vc.txt
vinum create -f /tmp/vc.txt >/dev/null 2>&1
for o in vol.p0.s0 vol.p1.s0 vol.p0 vol.p1 vol; do
  vinum setstate -f up $o >/dev/null 2>&1
done
sleep 1
dd if=/dev/urandom of=/tmp/pat bs=1m count=3 2>/dev/null
dd if=/tmp/pat of=/dev/vinum/vol/vol bs=1m conv=notrunc 2>/dev/null
dd if=/dev/vbd1s0 of=/tmp/ref0 bs=512 skip=$DATA_SKIP count=$DATA_CNT 2>/dev/null
dd if=/dev/vbd1s1 of=/tmp/ref1 bs=512 skip=$DATA_SKIP count=$DATA_CNT 2>/dev/null
cmp -s /tmp/ref0 /tmp/ref1 && echo "SETUP: plex data windows identical before revive" || echo "SETUP: WARNING data windows differ already"
vinum setstate stale vol.p1.s0 >/dev/null 2>&1
dd if=/dev/zero of=/dev/vbd1s1 bs=512 seek=$DATA_SKIP count=$DATA_CNT 2>/dev/null
echo "== state before concurrent revive =="
vinum list | tr -d "\r" | grep -E "^V |^P |^S "
