#!/bin/sh -x

cache_dev=`lsblk | awk '$4=="186.3G" { print "/dev/"$1"1"; }'`
echo "# cache_dev=${cache_dev}"
raid_members=`lsblk | awk '$4=="1.8T" { print "/dev/"$1"1"; }'`
echo "# raid_members=${raid_members}"
disks=`lsblk | awk '$4=="186.3G" || $4=="1.8T" { print "/dev/"$1; }'`
echo "# disks=${disks}"

read -p "RETURN to continue:" bla

for d in ${disks}; do hdparm -W 0 $d; done
#for d in ${disks}; do hdparm -W 1 $d; done

mdadm --create /dev/md127 --level=raid5 --chunk=512 --raid-devices=4 ${raid_members}
#mdadm --create /dev/md127 --level=raid10 --chunk=512 --raid-devices=4 ${raid_members}
sleep 3; udevadm settle
echo "1" >/sys/block/md127/md/sync_speed_min
echo "1" >/sys/block/md127/md/sync_speed_max
sleep 1; udevadm settle

#make-bcache --data_offset 2048 --wipe-bcache -w 4k -C ${cache_dev} -B /dev/md127
#make-bcache --data_offset 2048 --wipe-bcache -w 2k -C ${cache_dev} -B /dev/md127
#make-bcache --data_offset 2048 --wipe-bcache -w 1k -C ${cache_dev} -B /dev/md127
#make-bcache --data_offset 2048 --wipe-bcache -w 512 -C ${cache_dev} -B /dev/md127
#make-bcache --data_offset 2048 --wipe-bcache -C ${cache_dev} -B /dev/md127
make-bcache --wipe-bcache -C ${cache_dev} -B /dev/md127

sleep 3; udevadm settle
echo "${cache_dev}" >/sys/fs/bcache/register  # sometimes needs explicit register
sleep 3; udevadm settle
echo 'writeback' > /sys/block/md127/bcache/cache_mode
#echo 'writethrough' > /sys/block/md127/bcache/cache_mode
sleep 3; udevadm settle


for n in $(seq 1 8); do
    fio --filename=/dev/bcache0 --direct=1 --sync=1 --rw=write --bs=4k --numjobs=${n} --iodepth=1 --runtime=30 --time_based --group_reporting --name=journal-test
done
