1eda14cbcSMatt Macy#!/usr/bin/env bash 2eda14cbcSMatt Macy 3eda14cbcSMatt Macy# 4eda14cbcSMatt Macy# CDDL HEADER START 5eda14cbcSMatt Macy# 6eda14cbcSMatt Macy# This file and its contents are supplied under the terms of the 7eda14cbcSMatt Macy# Common Development and Distribution License ("CDDL"), version 1.0. 8eda14cbcSMatt Macy# You may only use this file in accordance with the terms of version 9eda14cbcSMatt Macy# 1.0 of the CDDL. 10eda14cbcSMatt Macy# 11eda14cbcSMatt Macy# A full copy of the text of the CDDL should have accompanied this 12eda14cbcSMatt Macy# source. A copy of the CDDL is also available via the Internet at 13eda14cbcSMatt Macy# http://www.illumos.org/license/CDDL. 14eda14cbcSMatt Macy# 15eda14cbcSMatt Macy# CDDL HEADER END 16eda14cbcSMatt Macy# 17eda14cbcSMatt Macy 18eda14cbcSMatt Macy# 19eda14cbcSMatt Macy# Copyright (c) 2015 by Delphix. All rights reserved. 20eda14cbcSMatt Macy# Copyright (C) 2016 Lawrence Livermore National Security, LLC. 217877fdebSMatt Macy# Copyright (c) 2017, Intel Corporation. 22eda14cbcSMatt Macy# 23eda14cbcSMatt Macy 24eda14cbcSMatt MacyBASE_DIR=$(dirname "$0") 25eda14cbcSMatt MacySCRIPT_COMMON=common.sh 26eda14cbcSMatt Macyif [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then 27eda14cbcSMatt Macy . "${BASE_DIR}/${SCRIPT_COMMON}" 28eda14cbcSMatt Macyelse 29eda14cbcSMatt Macy echo "Missing helper script ${SCRIPT_COMMON}" && exit 1 30eda14cbcSMatt Macyfi 31eda14cbcSMatt Macy 32eda14cbcSMatt Macy# shellcheck disable=SC2034 33eda14cbcSMatt MacyPROG=zloop.sh 34eda14cbcSMatt MacyGDB=${GDB:-gdb} 35eda14cbcSMatt Macy 36eda14cbcSMatt MacyDEFAULTWORKDIR=/var/tmp 37eda14cbcSMatt MacyDEFAULTCOREDIR=/var/tmp/zloop 38eda14cbcSMatt Macy 39eda14cbcSMatt Macyfunction usage 40eda14cbcSMatt Macy{ 41*3f9d360cSMartin Matuska cat >&2 <<EOF 42*3f9d360cSMartin Matuska 43*3f9d360cSMartin Matuska$0 [-hl] [-c <dump directory>] [-f <vdev directory>] 44*3f9d360cSMartin Matuska [-m <max core dumps>] [-s <vdev size>] [-t <timeout>] 45*3f9d360cSMartin Matuska [-I <max iterations>] [-- [extra ztest parameters]] 46*3f9d360cSMartin Matuska 47*3f9d360cSMartin Matuska This script runs ztest repeatedly with randomized arguments. 48*3f9d360cSMartin Matuska If a crash is encountered, the ztest logs, any associated 49*3f9d360cSMartin Matuska vdev files, and core file (if one exists) are moved to the 50*3f9d360cSMartin Matuska output directory ($DEFAULTCOREDIR by default). Any options 51*3f9d360cSMartin Matuska after the -- end-of-options marker will be passed to ztest. 52*3f9d360cSMartin Matuska 53*3f9d360cSMartin Matuska Options: 54*3f9d360cSMartin Matuska -c Specify a core dump directory to use. 55*3f9d360cSMartin Matuska -f Specify working directory for ztest vdev files. 56*3f9d360cSMartin Matuska -h Print this help message. 57*3f9d360cSMartin Matuska -l Create 'ztest.core.N' symlink to core directory. 58*3f9d360cSMartin Matuska -m Max number of core dumps to allow before exiting. 59*3f9d360cSMartin Matuska -s Size of vdev devices. 60*3f9d360cSMartin Matuska -t Total time to loop for, in seconds. If not provided, 61*3f9d360cSMartin Matuska zloop runs forever. 62*3f9d360cSMartin Matuska -I Max number of iterations to loop before exiting. 63*3f9d360cSMartin Matuska 64*3f9d360cSMartin MatuskaEOF 65eda14cbcSMatt Macy} 66eda14cbcSMatt Macy 67eda14cbcSMatt Macyfunction or_die 68eda14cbcSMatt Macy{ 69eda14cbcSMatt Macy # shellcheck disable=SC2068 7016038816SMartin Matuska if ! $@; then 7116038816SMartin Matuska echo "Command failed: $*" 72eda14cbcSMatt Macy exit 1 73eda14cbcSMatt Macy fi 74eda14cbcSMatt Macy} 75eda14cbcSMatt Macy 76eda14cbcSMatt Macycase $(uname) in 77eda14cbcSMatt MacyFreeBSD) 78eda14cbcSMatt Macy coreglob="z*.core" 79eda14cbcSMatt Macy ;; 80eda14cbcSMatt MacyLinux) 81eda14cbcSMatt Macy # core file helpers 82eda14cbcSMatt Macy origcorepattern="$(cat /proc/sys/kernel/core_pattern)" 83eda14cbcSMatt Macy coreglob="$(grep -E -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*" 84eda14cbcSMatt Macy 85eda14cbcSMatt Macy if [[ $coreglob = "*" ]]; then 86eda14cbcSMatt Macy echo "Setting core file pattern..." 87eda14cbcSMatt Macy echo "core" > /proc/sys/kernel/core_pattern 88eda14cbcSMatt Macy coreglob="$(grep -E -o '^([^|%[:space:]]*)' \ 89eda14cbcSMatt Macy /proc/sys/kernel/core_pattern)*" 90eda14cbcSMatt Macy fi 91eda14cbcSMatt Macy ;; 92eda14cbcSMatt Macy*) 93eda14cbcSMatt Macy exit 1 94eda14cbcSMatt Macy ;; 95eda14cbcSMatt Macyesac 96eda14cbcSMatt Macy 97eda14cbcSMatt Macyfunction core_file 98eda14cbcSMatt Macy{ 9916038816SMartin Matuska # shellcheck disable=SC2012,SC2086 10016038816SMartin Matuska ls -tr1 $coreglob 2>/dev/null | head -1 101eda14cbcSMatt Macy} 102eda14cbcSMatt Macy 103eda14cbcSMatt Macyfunction core_prog 104eda14cbcSMatt Macy{ 105eda14cbcSMatt Macy prog=$ZTEST 106eda14cbcSMatt Macy core_id=$($GDB --batch -c "$1" | grep "Core was generated by" | \ 107eda14cbcSMatt Macy tr \' ' ') 10816038816SMartin Matuska if [[ "$core_id" == *"zdb "* ]]; then 109eda14cbcSMatt Macy prog=$ZDB 110eda14cbcSMatt Macy fi 111eda14cbcSMatt Macy printf "%s" "$prog" 112eda14cbcSMatt Macy} 113eda14cbcSMatt Macy 114eda14cbcSMatt Macyfunction store_core 115eda14cbcSMatt Macy{ 116eda14cbcSMatt Macy core="$(core_file)" 117eda14cbcSMatt Macy if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then 118eda14cbcSMatt Macy df -h "$workdir" >>ztest.out 119eda14cbcSMatt Macy coreid=$(date "+zloop-%y%m%d-%H%M%S") 120eda14cbcSMatt Macy foundcrashes=$((foundcrashes + 1)) 121eda14cbcSMatt Macy 122eda14cbcSMatt Macy # zdb debugging 123eda14cbcSMatt Macy zdbcmd="$ZDB -U "$workdir/zpool.cache" -dddMmDDG ztest" 124eda14cbcSMatt Macy zdbdebug=$($zdbcmd 2>&1) 125eda14cbcSMatt Macy echo -e "$zdbcmd\n" >>ztest.zdb 126eda14cbcSMatt Macy echo "$zdbdebug" >>ztest.zdb 127eda14cbcSMatt Macy 128eda14cbcSMatt Macy dest=$coredir/$coreid 129eda14cbcSMatt Macy or_die mkdir -p "$dest" 130eda14cbcSMatt Macy or_die mkdir -p "$dest/vdev" 131eda14cbcSMatt Macy 132eda14cbcSMatt Macy if [[ $symlink -ne 0 ]]; then 133eda14cbcSMatt Macy or_die ln -sf "$dest" ztest.core.$foundcrashes 134eda14cbcSMatt Macy fi 135eda14cbcSMatt Macy 136eda14cbcSMatt Macy echo "*** ztest crash found - moving logs to $dest" 137eda14cbcSMatt Macy 138eda14cbcSMatt Macy or_die mv ztest.history "$dest/" 139eda14cbcSMatt Macy or_die mv ztest.zdb "$dest/" 140eda14cbcSMatt Macy or_die mv ztest.out "$dest/" 141eda14cbcSMatt Macy or_die mv "$workdir/ztest*" "$dest/vdev/" 142eda14cbcSMatt Macy 143eda14cbcSMatt Macy if [[ -e "$workdir/zpool.cache" ]]; then 144eda14cbcSMatt Macy or_die mv "$workdir/zpool.cache" "$dest/vdev/" 145eda14cbcSMatt Macy fi 146eda14cbcSMatt Macy 147eda14cbcSMatt Macy # check for core 148eda14cbcSMatt Macy if [[ -f "$core" ]]; then 149eda14cbcSMatt Macy coreprog=$(core_prog "$core") 150eda14cbcSMatt Macy coredebug=$($GDB --batch --quiet \ 151eda14cbcSMatt Macy -ex "set print thread-events off" \ 152eda14cbcSMatt Macy -ex "printf \"*\n* Backtrace \n*\n\"" \ 153eda14cbcSMatt Macy -ex "bt" \ 154eda14cbcSMatt Macy -ex "printf \"*\n* Libraries \n*\n\"" \ 155eda14cbcSMatt Macy -ex "info sharedlib" \ 156eda14cbcSMatt Macy -ex "printf \"*\n* Threads (full) \n*\n\"" \ 157eda14cbcSMatt Macy -ex "info threads" \ 158eda14cbcSMatt Macy -ex "printf \"*\n* Backtraces \n*\n\"" \ 159eda14cbcSMatt Macy -ex "thread apply all bt" \ 160eda14cbcSMatt Macy -ex "printf \"*\n* Backtraces (full) \n*\n\"" \ 161eda14cbcSMatt Macy -ex "thread apply all bt full" \ 162eda14cbcSMatt Macy -ex "quit" "$coreprog" "$core" 2>&1 | \ 163eda14cbcSMatt Macy grep -v "New LWP") 164eda14cbcSMatt Macy 165eda14cbcSMatt Macy # Dump core + logs to stored directory 166eda14cbcSMatt Macy echo "$coredebug" >>"$dest/ztest.gdb" 167eda14cbcSMatt Macy or_die mv "$core" "$dest/" 168eda14cbcSMatt Macy 169eda14cbcSMatt Macy # Record info in cores logfile 170eda14cbcSMatt Macy echo "*** core @ $coredir/$coreid/$core:" | \ 171eda14cbcSMatt Macy tee -a ztest.cores 172eda14cbcSMatt Macy fi 173eda14cbcSMatt Macy 174eda14cbcSMatt Macy if [[ $coremax -gt 0 ]] && 175eda14cbcSMatt Macy [[ $foundcrashes -ge $coremax ]]; then 176eda14cbcSMatt Macy echo "exiting... max $coremax allowed cores" 177eda14cbcSMatt Macy exit 1 178eda14cbcSMatt Macy else 179eda14cbcSMatt Macy echo "continuing..." 180eda14cbcSMatt Macy fi 181eda14cbcSMatt Macy fi 182eda14cbcSMatt Macy} 183eda14cbcSMatt Macy 184eda14cbcSMatt Macy# parse arguments 185eda14cbcSMatt Macy# expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args] 186eda14cbcSMatt Macycoredir=$DEFAULTCOREDIR 187eda14cbcSMatt Macybasedir=$DEFAULTWORKDIR 188eda14cbcSMatt Macyrundir="zloop-run" 189eda14cbcSMatt Macytimeout=0 190eda14cbcSMatt Macysize="512m" 191eda14cbcSMatt Macycoremax=0 192eda14cbcSMatt Macysymlink=0 193*3f9d360cSMartin Matuskaiterations=0 194*3f9d360cSMartin Matuskawhile getopts ":ht:m:I:s:c:f:l" opt; do 195eda14cbcSMatt Macy case $opt in 196eda14cbcSMatt Macy t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;; 197eda14cbcSMatt Macy m ) [[ $OPTARG -gt 0 ]] && coremax=$OPTARG ;; 198*3f9d360cSMartin Matuska I ) [[ $OPTARG ]] && iterations=$OPTARG ;; 199eda14cbcSMatt Macy s ) [[ $OPTARG ]] && size=$OPTARG ;; 200eda14cbcSMatt Macy c ) [[ $OPTARG ]] && coredir=$OPTARG ;; 201eda14cbcSMatt Macy f ) [[ $OPTARG ]] && basedir=$(readlink -f "$OPTARG") ;; 202eda14cbcSMatt Macy l ) symlink=1 ;; 203eda14cbcSMatt Macy h ) usage 204eda14cbcSMatt Macy exit 2 205eda14cbcSMatt Macy ;; 206eda14cbcSMatt Macy * ) echo "Invalid argument: -$OPTARG"; 207eda14cbcSMatt Macy usage 208eda14cbcSMatt Macy exit 1 209eda14cbcSMatt Macy esac 210eda14cbcSMatt Macydone 211eda14cbcSMatt Macy# pass remaining arguments on to ztest 212eda14cbcSMatt Macyshift $((OPTIND - 1)) 213eda14cbcSMatt Macy 214eda14cbcSMatt Macy# enable core dumps 215eda14cbcSMatt Macyulimit -c unlimited 216eda14cbcSMatt Macyexport ASAN_OPTIONS=abort_on_error=1:disable_coredump=0 217eda14cbcSMatt Macy 218eda14cbcSMatt Macyif [[ -f "$(core_file)" ]]; then 219eda14cbcSMatt Macy echo -n "There's a core dump here you might want to look at first... " 220eda14cbcSMatt Macy core_file 221eda14cbcSMatt Macy echo 222eda14cbcSMatt Macy exit 1 223eda14cbcSMatt Macyfi 224eda14cbcSMatt Macy 225eda14cbcSMatt Macyif [[ ! -d $coredir ]]; then 226eda14cbcSMatt Macy echo "core dump directory ($coredir) does not exist, creating it." 227eda14cbcSMatt Macy or_die mkdir -p "$coredir" 228eda14cbcSMatt Macyfi 229eda14cbcSMatt Macy 230eda14cbcSMatt Macyif [[ ! -w $coredir ]]; then 231eda14cbcSMatt Macy echo "core dump directory ($coredir) is not writable." 232eda14cbcSMatt Macy exit 1 233eda14cbcSMatt Macyfi 234eda14cbcSMatt Macy 235eda14cbcSMatt Macyor_die rm -f ztest.history 236eda14cbcSMatt Macyor_die rm -f ztest.zdb 237eda14cbcSMatt Macyor_die rm -f ztest.cores 238eda14cbcSMatt Macy 239eda14cbcSMatt Macyztrc=0 # ztest return value 240eda14cbcSMatt Macyfoundcrashes=0 # number of crashes found so far 241eda14cbcSMatt Macystarttime=$(date +%s) 242eda14cbcSMatt Macycurtime=$starttime 243*3f9d360cSMartin Matuskaiteration=0 244eda14cbcSMatt Macy 245eda14cbcSMatt Macy# if no timeout was specified, loop forever. 246*3f9d360cSMartin Matuskawhile (( timeout == 0 )) || (( curtime <= (starttime + timeout) )); do 247*3f9d360cSMartin Matuska if (( iterations > 0 )) && (( iteration++ == iterations )); then 248*3f9d360cSMartin Matuska break 249*3f9d360cSMartin Matuska fi 250*3f9d360cSMartin Matuska 251eda14cbcSMatt Macy zopt="-G -VVVVV" 252eda14cbcSMatt Macy 253eda14cbcSMatt Macy # start each run with an empty directory 254eda14cbcSMatt Macy workdir="$basedir/$rundir" 255eda14cbcSMatt Macy or_die rm -rf "$workdir" 256eda14cbcSMatt Macy or_die mkdir "$workdir" 257eda14cbcSMatt Macy 2587877fdebSMatt Macy # switch between three types of configs 2597877fdebSMatt Macy # 1/3 basic, 1/3 raidz mix, and 1/3 draid mix 2607877fdebSMatt Macy choice=$((RANDOM % 3)) 2617877fdebSMatt Macy 2627877fdebSMatt Macy # ashift range 9 - 15 263eda14cbcSMatt Macy align=$(((RANDOM % 2) * 3 + 9)) 2647877fdebSMatt Macy 2657877fdebSMatt Macy # randomly use special classes 2667877fdebSMatt Macy class="special=random" 2677877fdebSMatt Macy 2687877fdebSMatt Macy if [[ $choice -eq 0 ]]; then 2697877fdebSMatt Macy # basic mirror only 2707877fdebSMatt Macy parity=1 2717877fdebSMatt Macy mirrors=2 2727877fdebSMatt Macy draid_data=0 2737877fdebSMatt Macy draid_spares=0 2747877fdebSMatt Macy raid_children=0 2757877fdebSMatt Macy vdevs=2 2767877fdebSMatt Macy raid_type="raidz" 2777877fdebSMatt Macy elif [[ $choice -eq 1 ]]; then 2787877fdebSMatt Macy # fully randomized mirror/raidz (sans dRAID) 2797877fdebSMatt Macy parity=$(((RANDOM % 3) + 1)) 2807877fdebSMatt Macy mirrors=$(((RANDOM % 3) * 1)) 2817877fdebSMatt Macy draid_data=0 2827877fdebSMatt Macy draid_spares=0 2837877fdebSMatt Macy raid_children=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2))) 2847877fdebSMatt Macy vdevs=$(((RANDOM % 3) + 3)) 2857877fdebSMatt Macy raid_type="raidz" 2867877fdebSMatt Macy else 2877877fdebSMatt Macy # fully randomized dRAID (sans mirror/raidz) 2887877fdebSMatt Macy parity=$(((RANDOM % 3) + 1)) 2897877fdebSMatt Macy mirrors=0 2907877fdebSMatt Macy draid_data=$(((RANDOM % 8) + 3)) 2917877fdebSMatt Macy draid_spares=$(((RANDOM % 2) + parity)) 2927877fdebSMatt Macy stripe=$((draid_data + parity)) 2937877fdebSMatt Macy extra=$((draid_spares + (RANDOM % 4))) 2947877fdebSMatt Macy raid_children=$(((((RANDOM % 4) + 1) * stripe) + extra)) 2957877fdebSMatt Macy vdevs=$((RANDOM % 3)) 2967877fdebSMatt Macy raid_type="draid" 2977877fdebSMatt Macy fi 2987877fdebSMatt Macy 2997877fdebSMatt Macy zopt="$zopt -K $raid_type" 300eda14cbcSMatt Macy zopt="$zopt -m $mirrors" 3017877fdebSMatt Macy zopt="$zopt -r $raid_children" 3027877fdebSMatt Macy zopt="$zopt -D $draid_data" 3037877fdebSMatt Macy zopt="$zopt -S $draid_spares" 304eda14cbcSMatt Macy zopt="$zopt -R $parity" 305eda14cbcSMatt Macy zopt="$zopt -v $vdevs" 306eda14cbcSMatt Macy zopt="$zopt -a $align" 3077877fdebSMatt Macy zopt="$zopt -C $class" 308eda14cbcSMatt Macy zopt="$zopt -s $size" 309eda14cbcSMatt Macy zopt="$zopt -f $workdir" 310eda14cbcSMatt Macy 31116038816SMartin Matuska cmd="$ZTEST $zopt $*" 312eda14cbcSMatt Macy desc="$(date '+%m/%d %T') $cmd" 313eda14cbcSMatt Macy echo "$desc" | tee -a ztest.history 314eda14cbcSMatt Macy echo "$desc" >>ztest.out 315eda14cbcSMatt Macy $cmd >>ztest.out 2>&1 316eda14cbcSMatt Macy ztrc=$? 317eda14cbcSMatt Macy grep -E '===|WARNING' ztest.out >>ztest.history 318eda14cbcSMatt Macy 319eda14cbcSMatt Macy store_core 320eda14cbcSMatt Macy 321eda14cbcSMatt Macy curtime=$(date +%s) 322eda14cbcSMatt Macydone 323eda14cbcSMatt Macy 324eda14cbcSMatt Macyecho "zloop finished, $foundcrashes crashes found" 325eda14cbcSMatt Macy 326eda14cbcSMatt Macy# restore core pattern. 327eda14cbcSMatt Macycase $(uname) in 328eda14cbcSMatt MacyLinux) 329eda14cbcSMatt Macy echo "$origcorepattern" > /proc/sys/kernel/core_pattern 330eda14cbcSMatt Macy ;; 331eda14cbcSMatt Macy*) 332eda14cbcSMatt Macy ;; 333eda14cbcSMatt Macyesac 334eda14cbcSMatt Macy 335eda14cbcSMatt Macyuptime >>ztest.out 336eda14cbcSMatt Macy 337eda14cbcSMatt Macyif [[ $foundcrashes -gt 0 ]]; then 338eda14cbcSMatt Macy exit 1 339eda14cbcSMatt Macyfi 340