#! /bin/bash -e

DRIVE="/dev/sdb"
FILENAME="asdf"

printf "Drive Information\r\n"
hdparm -I $DRIVE | grep "Model\|Firmware"

hdparm -W1 $DRIVE
printf "###############################################\r\n"
printf "Ensure that write back is enabled via the speed\r\n"
printf "###############################################\r\n"
dd if=/dev/zero bs=128M count=10 oflag=direct,sync of=$DRIVE 2>&1| grep bytes

printf "\r\nRAW Write:\r\n"
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes

FILE="/mnt/${DRIVE}/${FILENAME}"
DIR="/mnt/${DRIVE}"
mkdir -p $DIR
umount $DIR || true

# Setup for Ext4
wipefs -t xfs,ext4 $DRIVE
mkfs.ext4 $DRIVE > /dev/null

printf "\r\nExt4, Trim\r\n"
mount -o discard $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

printf "\r\nExt4, No Trim\r\n"
mount $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

# Setup for xfs
wipefs -t ext4,xfs $DRIVE
mkfs.xfs -f $DRIVE > /dev/null

printf "\r\nxfs, Trim\r\n"
mount -o discard $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

printf "\r\nxfs, No Trim\r\n"
mount $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

hdparm -W0 $DRIVE
printf "################################################\r\n"
printf "Ensure that write back is DISABLED via the speed\r\n"
printf "################################################\r\n"
dd if=/dev/zero bs=128M count=10 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes

printf "\r\nRAW Write:\r\n"
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes

# Setup for Ext4
wipefs -t xfs,ext4 $DRIVE
mkfs.ext4 $DRIVE > /dev/null

printf "\r\nExt4, Trim\r\n"
mount -o discard $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

printf "\r\nExt4, No Trim\r\n"
mount $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

# Setup for xfs
wipefs -t xfs,ext4 $DRIVE
mkfs.xfs -f $DRIVE > /dev/null

printf "\r\nxfs, Trim\r\n"
mount -o discard $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

printf "\r\nxfs, No Trim\r\n"
mount $DRIVE $DIR
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
dd if=/dev/zero bs=512 count=1000 oflag=direct,sync of=$FILE 2>&1 | grep bytes
umount $DIR

rmdir $DIR
hdparm -W1 $DRIVE
printf "###############################################\r\n"
printf "Ensure that write back is enabled via the speed\r\n"
printf "###############################################\r\n"
dd if=/dev/zero bs=128M count=10 oflag=direct,sync of=$DRIVE 2>&1 | grep bytes