Path: blob/main/sys/contrib/openzfs/scripts/zloop.sh
48266 views
#!/usr/bin/env bash1# SPDX-License-Identifier: CDDL-1.023#4# CDDL HEADER START5#6# This file and its contents are supplied under the terms of the7# Common Development and Distribution License ("CDDL"), version 1.0.8# You may only use this file in accordance with the terms of version9# 1.0 of the CDDL.10#11# A full copy of the text of the CDDL should have accompanied this12# source. A copy of the CDDL is also available via the Internet at13# http://www.illumos.org/license/CDDL.14#15# CDDL HEADER END16#1718#19# Copyright (c) 2015 by Delphix. All rights reserved.20# Copyright (C) 2016 Lawrence Livermore National Security, LLC.21# Copyright (c) 2017, Intel Corporation.22#2324BASE_DIR=${0%/*}25SCRIPT_COMMON=common.sh26if [[ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]]; then27. "${BASE_DIR}/${SCRIPT_COMMON}"28else29echo "Missing helper script ${SCRIPT_COMMON}" && exit 130fi3132# shellcheck disable=SC203433PROG=zloop.sh34GDB=${GDB:-gdb}3536DEFAULTWORKDIR=/var/tmp37DEFAULTCOREDIR=/var/tmp/zloop3839function usage40{41cat >&2 <<EOF4243$0 [-hl] [-c <dump directory>] [-f <vdev directory>]44[-m <max core dumps>] [-s <vdev size>] [-t <timeout>]45[-I <max iterations>] [-- [extra ztest parameters]]4647This script runs ztest repeatedly with randomized arguments.48If a crash is encountered, the ztest logs, any associated49vdev files, and core file (if one exists) are moved to the50output directory ($DEFAULTCOREDIR by default). Any options51after the -- end-of-options marker will be passed to ztest.5253Options:54-c Specify a core dump directory to use.55-f Specify working directory for ztest vdev files.56-h Print this help message.57-l Create 'ztest.core.N' symlink to core directory.58-m Max number of core dumps to allow before exiting.59-s Size of vdev devices.60-t Total time to loop for, in seconds. If not provided,61zloop runs forever.62-I Max number of iterations to loop before exiting.6364EOF65}6667function or_die68{69if ! "$@"; then70echo "Command failed: $*"71exit 172fi73}7475case $(uname) in76FreeBSD)77coreglob="z*.core"78;;79Linux)80# core file helpers81read -r origcorepattern </proc/sys/kernel/core_pattern82coreglob="$(grep -E -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*"8384if [[ $coreglob = "*" ]]; then85echo "Setting core file pattern..."86echo "core" > /proc/sys/kernel/core_pattern87coreglob="$(grep -E -o '^([^|%[:space:]]*)' \88/proc/sys/kernel/core_pattern)*"89fi90;;91*)92exit 193;;94esac9596function core_file97{98# shellcheck disable=SC2012,SC208699ls -tr1 $coreglob 2>/dev/null | head -1100}101102function core_prog103{104# shellcheck disable=SC2154105prog=$ZTEST106core_id=$($GDB --batch -c "$1" | grep "Core was generated by" | \107tr \' ' ')108if [[ "$core_id" == *"zdb "* ]]; then109# shellcheck disable=SC2154110prog=$ZDB111fi112printf "%s" "$prog"113}114115function store_core116{117core="$(core_file)"118if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then119df -h "$workdir" >>ztest.out120coreid=$(date "+zloop-%y%m%d-%H%M%S")121foundcrashes=$((foundcrashes + 1))122123# zdb debugging124zdbcmd="$ZDB -U "$workdir/zpool.cache" -dddMmDDG ztest"125zdbdebug=$($zdbcmd 2>&1)126echo -e "$zdbcmd\n" >>ztest.zdb127echo "$zdbdebug" >>ztest.zdb128129dest=$coredir/$coreid130or_die mkdir -p "$dest/vdev"131132if [[ $symlink -ne 0 ]]; then133or_die ln -sf "$dest" "ztest.core.${foundcrashes}"134fi135136echo "*** ztest crash found - moving logs to $dest"137138or_die mv ztest.history ztest.zdb ztest.out "$dest/"139or_die mv "$workdir/"ztest* "$dest/vdev/"140141if [[ -e "$workdir/zpool.cache" ]]; then142or_die mv "$workdir/zpool.cache" "$dest/vdev/"143fi144145# check for core146if [[ -f "$core" ]]; then147coreprog=$(core_prog "$core")148coredebug=$($GDB --batch --quiet \149-ex "set print thread-events off" \150-ex "printf \"*\n* Backtrace \n*\n\"" \151-ex "bt" \152-ex "printf \"*\n* Libraries \n*\n\"" \153-ex "info sharedlib" \154-ex "printf \"*\n* Threads (full) \n*\n\"" \155-ex "info threads" \156-ex "printf \"*\n* Backtraces \n*\n\"" \157-ex "thread apply all bt" \158-ex "printf \"*\n* Backtraces (full) \n*\n\"" \159-ex "thread apply all bt full" \160-ex "quit" "$coreprog" "$core" 2>&1 | \161grep -v "New LWP")162163# Dump core + logs to stored directory164echo "$coredebug" >>"$dest/ztest.gdb"165or_die mv "$core" "$dest/"166167# Record info in cores logfile168echo "*** core @ $coredir/$coreid/$core:" | \169tee -a ztest.cores170fi171172if [[ $coremax -gt 0 ]] &&173[[ $foundcrashes -ge $coremax ]]; then174echo "exiting... max $coremax allowed cores"175exit 1176else177echo "continuing..."178fi179fi180}181182# parse arguments183# expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args]184coredir=$DEFAULTCOREDIR185basedir=$DEFAULTWORKDIR186rundir="zloop-run"187timeout=0188size="512m"189coremax=0190symlink=0191iterations=0192while getopts ":ht:m:I:s:c:f:l" opt; do193case $opt in194t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;;195m ) [[ $OPTARG -gt 0 ]] && coremax=$OPTARG ;;196I ) [[ -n $OPTARG ]] && iterations=$OPTARG ;;197s ) [[ -n $OPTARG ]] && size=$OPTARG ;;198c ) [[ -n $OPTARG ]] && coredir=$OPTARG ;;199f ) [[ -n $OPTARG ]] && basedir=$(readlink -f "$OPTARG") ;;200l ) symlink=1 ;;201h ) usage202exit 2203;;204* ) echo "Invalid argument: -$OPTARG";205usage206exit 1207esac208done209# pass remaining arguments on to ztest210shift $((OPTIND - 1))211212# enable core dumps213ulimit -c unlimited214export ASAN_OPTIONS=abort_on_error=true:halt_on_error=true:allocator_may_return_null=true:disable_coredump=false:detect_stack_use_after_return=true215export UBSAN_OPTIONS=abort_on_error=true:halt_on_error=true:print_stacktrace=true216217if [[ -f "$(core_file)" ]]; then218echo -n "There's a core dump here you might want to look at first... "219core_file220echo221exit 1222fi223224if [[ ! -d $coredir ]]; then225echo "core dump directory ($coredir) does not exist, creating it."226or_die mkdir -p "$coredir"227fi228229if [[ ! -w $coredir ]]; then230echo "core dump directory ($coredir) is not writable."231exit 1232fi233234or_die rm -f ztest.history ztest.zdb ztest.cores235236ztrc=0 # ztest return value237foundcrashes=0 # number of crashes found so far238starttime=$(date +%s)239curtime=$starttime240iteration=0241242# if no timeout was specified, loop forever.243while (( timeout == 0 )) || (( curtime <= (starttime + timeout) )); do244if (( iterations > 0 )) && (( iteration++ == iterations )); then245break246fi247248zopt="-G -VVVVV"249250# start each run with an empty directory251workdir="$basedir/$rundir"252or_die rm -rf "$workdir"253or_die mkdir "$workdir"254255# ashift range 9 - 15256align=$(((RANDOM % 2) * 3 + 9))257258# choose parity value259parity=$(((RANDOM % 3) + 1))260261draid_data=0262draid_spares=0263264# randomly use special classes265class="special=random"266267# choose between four types of configs268# (basic, raidz mix, raidz expansion, and draid mix)269case $((RANDOM % 4)) in270271# basic mirror configuration2720) parity=1273mirrors=2274raid_children=0275vdevs=2276raid_type="raidz"277;;278279# fully randomized mirror/raidz (sans dRAID)2801) mirrors=$(((RANDOM % 3) * 1))281raid_children=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2)))282vdevs=$(((RANDOM % 3) + 3))283raid_type="raidz"284;;285286# randomized raidz expansion (one top-level raidz vdev)2872) mirrors=0288vdevs=1289# derive initial raidz disk count based on parity choice290# P1: 3 - 7 disks291# P2: 5 - 9 disks292# P3: 7 - 11 disks293raid_children=$(((RANDOM % 5) + (parity * 2) + 1))294295# 1/3 of the time use a dedicated '-X' raidz expansion test296if [[ $((RANDOM % 3)) -eq 0 ]]; then297zopt="$zopt -X -t 16"298raid_type="raidz"299else300raid_type="eraidz"301fi302;;303304# fully randomized dRAID (sans mirror/raidz)3053) mirrors=0306draid_data=$(((RANDOM % 8) + 3))307draid_spares=$(((RANDOM % 2) + parity))308stripe=$((draid_data + parity))309extra=$((draid_spares + (RANDOM % 4)))310raid_children=$(((((RANDOM % 4) + 1) * stripe) + extra))311vdevs=$((RANDOM % 3))312raid_type="draid"313;;314*)315# avoid shellcheck SC2249316;;317esac318319zopt="$zopt -K $raid_type"320zopt="$zopt -m $mirrors"321zopt="$zopt -r $raid_children"322zopt="$zopt -D $draid_data"323zopt="$zopt -S $draid_spares"324zopt="$zopt -R $parity"325zopt="$zopt -v $vdevs"326zopt="$zopt -a $align"327zopt="$zopt -C $class"328zopt="$zopt -s $size"329zopt="$zopt -f $workdir"330331cmd="$ZTEST $zopt $*"332echo "$(date '+%m/%d %T') $cmd" | tee -a ztest.history ztest.out333$cmd >>ztest.out 2>&1334ztrc=$?335grep -E '===|WARNING' ztest.out >>ztest.history336337store_core338339curtime=$(date +%s)340done341342echo "zloop finished, $foundcrashes crashes found"343344# restore core pattern.345case $(uname) in346Linux)347echo "$origcorepattern" > /proc/sys/kernel/core_pattern348;;349*)350;;351esac352353uptime >>ztest.out354355if [[ $foundcrashes -gt 0 ]]; then356exit 1357fi358359360