#!/bin/bash

set -xv

MNTPT=/mnt/test

umount $MNTPT
vgchange -an
sleep 1
multipath -F
/etc/init.d/multipathd stop

modprobe -r scsi_debug
sleep 1

# 4K/4K -- UNMAP
modprobe scsi_debug dev_size_mb=100 unmap_max_desc=16 unmap_granularity=2048 sector_size=4096
# 512K
#modprobe scsi_debug dev_size_mb=100 unmap_max_desc=16 unmap_granularity=2048

/etc/init.d/multipathd restart
sleep 3
multipath -ll

DEVICE=/dev/mapper/`multipath -ll | grep scsi_debug | cut -d' ' -f1`

lvremove discard_vg/lv
vgremove discard_vg
pvremove $DEVICE

pvcreate $DEVICE
vgcreate discard_vg $DEVICE
lvcreate -L 96M -n lv discard_vg
lvchange -ay discard_vg/lv
mkfs.ext4 -b 4096 /dev/discard_vg/lv 
mount -o discard /dev/discard_vg/lv $MNTPT

sync

dd if=/dev/zero of=${MNTPT}/hmm bs=384k oflag=direct count=1
sync; sync
filefrag -v ${MNTPT}/hmm

rm -f ${MNTPT}/hmm; sync; sync

sleep 5
