0

Can anyone tell me how to pass an argument to the following script?

The author of this script has stated arguments are accept by entering a path to a directory on another hard drive (sdb).

I've tried, ./script /dev/sdb and ./script /media/user/data/test both fail. Updated below with error messages.

#!/bin/bash

LOOPS=5 #How many times to run each test
SIZE=1024 #Size of each test, multiples of 32 recommended for Q32 tests to give the most accurate results.
WRITEZERO=0 #Set whether to write zeroes or randoms to testfile (random is the default for both fio and crystaldiskmark); dd benchmarks typically only write zeroes which is why there can be a speed difference.

QSIZE=$(($SIZE / 32)) #Size of Q32Seq tests
SIZE+=m
QSIZE+=m

if [ -z $1 ]; then
    TARGET=$HOME
    echo "Defaulting to $TARGET for testing"
else
    TARGET="$1"
    echo "Testing in $TARGET"
fi

DRIVE=$(df $TARGET | grep /dev | cut -d/ -f3 | cut -d" " -f1 | rev | cut -c 2- | rev)
DRIVEMODEL=$(cat /sys/block/$DRIVE/device/model)
DRIVESIZE=$(($(cat /sys/block/$DRIVE/size)*512/1024/1024/1024))GB

echo "Configuration: Size:$SIZE Loops:$LOOPS Write Only Zeroes:$WRITEZERO
Running Benchmark on: /dev/$DRIVE, $DRIVEMODEL ($DRIVESIZE), please wait...
"

fio --loops=$LOOPS --size=$SIZE --filename=$TARGET/.fiomark.tmp --stonewall --ioengine=libaio --direct=1 --zero_buffers=$WRITEZERO --output-format=json \
  --name=Bufread --loops=1 --bs=$SIZE --iodepth=1 --numjobs=1 --rw=readwrite \
  --name=Seqread --bs=$SIZE --iodepth=1 --numjobs=1 --rw=read \
  --name=Seqwrite --bs=$SIZE --iodepth=1 --numjobs=1 --rw=write \
  --name=512kread --bs=512k --iodepth=1 --numjobs=1 --rw=read \
  --name=512kwrite --bs=512k --iodepth=1 --numjobs=1 --rw=write \
  --name=SeqQ32T1read --bs=$QSIZE --iodepth=32 --numjobs=1 --rw=read \
  --name=SeqQ32T1write --bs=$QSIZE --iodepth=32 --numjobs=1 --rw=write \
  --name=4kread --bs=4k --iodepth=1 --numjobs=1 --rw=randread \
  --name=4kwrite --bs=4k --iodepth=1 --numjobs=1 --rw=randwrite \
  --name=4kQ32T1read --bs=4k --iodepth=32 --numjobs=1 --rw=randread \
  --name=4kQ32T1write --bs=4k --iodepth=32 --numjobs=1 --rw=randwrite \
  --name=4kQ8T8read --bs=4k --iodepth=8 --numjobs=8 --rw=randread \
  --name=4kQ8T8write --bs=4k --iodepth=8 --numjobs=8 --rw=randwrite > $TARGET/.fiomark.txt

SEQR="$(($(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "Seqread"' | grep bw_bytes | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "Seqread"' | grep -m1 iops | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
SEQW="$(($(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "Seqwrite"' | grep bw_bytes | sed '2!d' | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "Seqwrite"' | grep iops | sed '7!d' | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
F12KR="$(($(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "512kread"' | grep bw_bytes | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "512kread"' | grep -m1 iops | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
F12KW="$(($(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "512kwrite"' | grep bw_bytes | sed '2!d' | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "512kwrite"' | grep iops | sed '7!d' | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
SEQ32R="$(($(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "SeqQ32T1read"' | grep bw_bytes | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "SeqQ32T1read"' | grep -m1 iops | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
SEQ32W="$(($(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "SeqQ32T1write"' | grep bw_bytes | sed '2!d' | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "SeqQ32T1write"' | grep iops | sed '7!d' | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
FKR="$(($(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "4kread"' | grep bw_bytes | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "4kread"' | grep -m1 iops | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
FKW="$(($(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "4kwrite"' | grep bw_bytes | sed '2!d' | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "4kwrite"' | grep iops | sed '7!d' | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
FK32R="$(($(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "4kQ32T1read"' | grep bw_bytes | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "4kQ32T1read"' | grep -m1 iops | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
FK32W="$(($(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "4kQ32T1write"' | grep bw_bytes | sed '2!d' | cut -d: -f2 | sed s:,::g)/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "4kQ32T1write"' | grep iops | sed '7!d' | cut -d: -f2 | cut -d. -f1 | sed 's: ::g')"
FK8R="$(($(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "4kQ8T8read"' | grep bw_bytes | sed 's/        "bw_bytes" : //g' | sed 's:,::g' | awk '{ SUM += $1} END { print SUM }')/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A15 '"name" : "4kQ8T8read"' | grep iops | sed 's/        "iops" : //g' | sed 's:,::g' | awk '{ SUM += $1} END { print SUM }' | cut -d. -f1)"
FK8W="$(($(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "4kQ8T8write"' | grep bw_bytes | sed 's/        "bw_bytes" : //g' | sed 's:,::g' | awk '{ SUM += $1} END { print SUM }')/1024/1024))MB/s IOPS=$(cat $TARGET/.fiomark.txt | grep -A80 '"name" : "4kQ8T8write"' | grep '"iops" '| sed 's/        "iops" : //g' | sed 's:,::g' | awk '{ SUM += $1} END { print SUM }' | cut -d. -f1)"

echo -e "
Results from /dev/$DRIVE, $DRIVEMODEL ($DRIVESIZE):  
\033[0;33m
Sequential Read: $SEQR
Sequential Write: $SEQW
\033[0;32m
512KB Read: $F12KR
512KB Write: $F12KW
\033[1;36m
Sequential Q32T1 Read: $SEQ32R
Sequential Q32T1 Write: $SEQ32W
\033[0;36m
4KB Read: $FKR
4KB Write: $FKW
\033[1;33m
4KB Q32T1 Read: $FK32R
4KB Q32T1 Write: $FK32W
\033[1;35m
4KB Q8T8 Read: $FK8R
4KB Q8T8 Write: $FK8W
"

rm $TARGET/.fiomark.txt $TARGET/.fiomark.tmp

The Errors are:

./script.sh /dev/sdb

Testing in /dev/sdb
cat: /sys/block/sd/device/model: No such file or directory
cat: /sys/block/sd/size: No such file or directory
./drive-benchmark.sh: line 21: *512/1024/1024/1024: syntax error: operand expected (error token is "*512/1024/1024/1024")
Configuration: Size:1024m Loops:5 Write Only Zeroes:0
Running Benchmark on: /dev/sd,  (), please wait...

./script /media/user/data/test

Testing in /media/user/data/test
cat: /sys/block/sd/device/model: No such file or directory
cat: /sys/block/sd/size: No such file or directory
./drive-benchmark.sh: line 21: *512/1024/1024/1024: syntax error: operand expected (error token is "*512/1024/1024/1024")
Configuration: Size:1024m Loops:5 Write Only Zeroes:0
Running Benchmark on: /dev/sd,  (), please wait...
  • 2
    How do they fail, is there a specific error being generated? Does it make it to the line that `echo`s "Configuration..." and if so does it contain all the information? Was the script designed to be used with whatever operating system you are using? – jesse_b Nov 14 '18 at 18:28
  • I've updated the post to include error output. Yes is it designed for Linux and works without using an argument, the script when run without arguments will use the user home drive/partition. – PrincessOfPower Nov 14 '18 at 18:41
  • Are you running it on virtual machine by chance? – jesse_b Nov 14 '18 at 18:58
  • Nope. But I believe I've found the issue. Line 19: cut -c 2- needs to be cut -c 1-. – PrincessOfPower Nov 14 '18 at 19:00
  • Spoke too soon. Changing line 19 works if stating an argument, but then breaks when ran with default (no arg). Error: line 21: *512/1024/1024/1024: syntax error: operand expected (error token is "*512/1024/1024/1024") – PrincessOfPower Nov 14 '18 at 19:10
  • Can you try replacing `df $TARGET | grep /dev | cut -d/ -f3 | cut -d" " -f1 | rev | cut -c 2- | rev` with `df $TARGET | awk 'NR > 1 {n = split($1, a, "/"); print a[n]}' | sed -e 's/[0-9]*//g'` – Jyotish P Nov 14 '18 at 21:13
  • @JyotishP Works with no Arg, using an Arg produces incorrect dev (should be /dev/nvme0n1) and errors: `Testing in /media/user/nvme-ssd/benchmark/ cat: /sys/block/nvmen/device/model: No such file or directory cat: /sys/block/nvmen/size: No such file or directory orig-benchmark.sh: line 21: *512/1024/1024/1024: syntax error: operand expected (error token is "*512/1024/1024/1024") Configuration: Size:1024m Loops:5 Write Only Zeroes:0 Running Benchmark on: /dev/nvmen, (), please wait...` – PrincessOfPower Nov 15 '18 at 03:20
  • It would be kind to link and credit the original source of your script to https://unix.stackexchange.com/a/480191 in your question... – Anon Nov 16 '18 at 07:11

1 Answers1

-1

The fio's are testing $TARGET (the filesystem) and should work regardless. The variable errors only affect the header & result info.

Assuming readlink & lsblk are also on your system, replacing DRIVE=, DRIVEMODEL=, and DRIVESIZE= with the following should work a little better (with raw & virtual devices and partitions).

DEVICE=$(readlink -e /sys/dev/block/$(lsblk -npo MAJ:MIN $(df $TARGET 2> /dev/null | grep ^/dev | awk '{print $1}') | awk '{print $1}'))
if [ ! -f ${DEVICE}/dev ]; then
    echo "error determining device of $TARGET"
    exit 1
fi

DRIVE=$(echo $DEVICE | awk -F\/ '{print $NF}')

if [ -f ${DEVICE}/device/model ]; then
    DRIVEMODEL=$(cat ${DEVICE}/device/model)
else
    if [ -f ${DEVICE}/partition ]; then
        if [ -f ${DEVICE}/../device/model ]; then
            DRIVEMODEL=" $(cat ${DEVICE}/../device/model) (partition)"
        else
            DRIVEMODEL="partition"
        fi
    else
       if [ -d ${DEVICE}/dm ]; then
           DRIVEMODEL="virtual"
       else
           DRIVEMODEL="unknown"
       fi
    fi
fi

if [ -f ${DEVICE}/size ]; then
    DRIVESIZE=$(cat ${DEVICE}/size)
else
    DRIVESIZE=0
fi
DRIVESIZE=$(($DRIVESIZE*512/1024/1024/1024))GB

echo "Configuration: Size:$SIZE Loops:$LOOPS Write Only Zeroes:$WRITEZERO
Running Benchmark on: $DEVICE, /dev/$DRIVE, $DRIVEMODEL ($DRIVESIZE), please wait...
"
Joseph Tingiris
  • 1,706
  • 12
  • 20
  • My reputation points do not allow me to comment to the original author to fix the script. I was able to bypass the issue with some simple modification and the script now runs with a directory argument, however, after about 5 mins stops with a new error on lines 53-64, all stating the following. `drive-benchmark.sh: line 53: /1024/1024: syntax error: operand expected (error token is "/1024/1024")` – PrincessOfPower Nov 15 '18 at 03:07
  • Your method runs with and without an Arg. Should "DRIVSIZE=0" be "DRIVESIZE=0"? Now that the script runs, it errors on lines 74~85 with these: `mod-benchmark.sh: line 74: /1024/1024: syntax error: operand expected (error token is "/1024/1024") mod-benchmark.sh: line 75: /1024/1024: syntax error: operand expected (error token is "/1024/1024")` – PrincessOfPower Nov 15 '18 at 03:38