1d583b39bSJohn Wren Kennedy#!/usr/bin/ksh
2d583b39bSJohn Wren Kennedy
3d583b39bSJohn Wren Kennedy#
4d583b39bSJohn Wren Kennedy# This file and its contents are supplied under the terms of the
5d583b39bSJohn Wren Kennedy# Common Development and Distribution License ("CDDL"), version 1.0.
6d583b39bSJohn Wren Kennedy# You may only use this file in accordance with the terms of version
7d583b39bSJohn Wren Kennedy# 1.0 of the CDDL.
8d583b39bSJohn Wren Kennedy#
9d583b39bSJohn Wren Kennedy# A full copy of the text of the CDDL should have accompanied this
10d583b39bSJohn Wren Kennedy# source.  A copy of the CDDL is also available via the Internet at
11d583b39bSJohn Wren Kennedy# http://www.illumos.org/license/CDDL.
12d583b39bSJohn Wren Kennedy#
13d583b39bSJohn Wren Kennedy
14d583b39bSJohn Wren Kennedy#
1555a13001SPavel Zakharov# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
16cf988facSDan McDonald# Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
1766597161SYuri Pankov# Copyright 2021 Tintri by DDN, Inc. All rights reserved.
1878801af7SAndy Fiddaman# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
19d583b39bSJohn Wren Kennedy#
20d583b39bSJohn Wren Kennedy
211d32ba66SJohn Wren Kennedyexport PATH="/usr/bin"
22447b1e1fSJohn Wren Kennedyexport NOINUSE_CHECK=1
23d583b39bSJohn Wren Kennedyexport STF_SUITE="/opt/zfs-tests"
24b8052df9SRyan Moellerexport COMMON="$STF_SUITE/runfiles/common.run"
25d583b39bSJohn Wren Kennedyexport STF_TOOLS="/opt/test-runner/stf"
261d32ba66SJohn Wren Kennedyexport PATHDIR=""
27d583b39bSJohn Wren Kennedyrunner="/opt/test-runner/bin/run"
28d583b39bSJohn Wren Kennedyauto_detect=false
29d583b39bSJohn Wren Kennedy
3055a13001SPavel Zakharovif [[ -z "$TESTFAIL_CALLBACKS" ]] ; then
31ad42deeaSJohn Wren Kennedy	export TESTFAIL_CALLBACKS="$STF_SUITE/callbacks/zfs_dbgmsg"
3255a13001SPavel Zakharovfi
3355a13001SPavel Zakharov
34d583b39bSJohn Wren Kennedyfunction fail
35d583b39bSJohn Wren Kennedy{
36d583b39bSJohn Wren Kennedy	echo $1
37d583b39bSJohn Wren Kennedy	exit ${2:-1}
38d583b39bSJohn Wren Kennedy}
39d583b39bSJohn Wren Kennedy
40d583b39bSJohn Wren Kennedyfunction find_disks
41d583b39bSJohn Wren Kennedy{
421d32ba66SJohn Wren Kennedy	typeset all_disks=$(echo '' | sudo -k format | awk \
43d583b39bSJohn Wren Kennedy	    '/c[0-9]/ {print $2}')
441d32ba66SJohn Wren Kennedy	typeset used_disks=$(zpool status | awk \
4566597161SYuri Pankov	    '/c[0-9]+(t[0-9a-fA-F]+)?d[0-9]+/ {print $1}' | sed -E \
46ce326879SAttila Fülöp	    's/(s|p)[0-9]+//g')
47d583b39bSJohn Wren Kennedy
48d583b39bSJohn Wren Kennedy	typeset disk used avail_disks
49d583b39bSJohn Wren Kennedy	for disk in $all_disks; do
50d583b39bSJohn Wren Kennedy		for used in $used_disks; do
51d583b39bSJohn Wren Kennedy			[[ "$disk" = "$used" ]] && continue 2
52d583b39bSJohn Wren Kennedy		done
53d583b39bSJohn Wren Kennedy		[[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
54d583b39bSJohn Wren Kennedy		[[ -z $avail_disks ]] && avail_disks="$disk"
55d583b39bSJohn Wren Kennedy	done
56d583b39bSJohn Wren Kennedy
57d583b39bSJohn Wren Kennedy	echo $avail_disks
58d583b39bSJohn Wren Kennedy}
59d583b39bSJohn Wren Kennedy
60d583b39bSJohn Wren Kennedyfunction find_rpool
61d583b39bSJohn Wren Kennedy{
621d32ba66SJohn Wren Kennedy	typeset ds=$(mount | awk '/^\/ / {print $3}')
63d583b39bSJohn Wren Kennedy	echo ${ds%%/*}
64d583b39bSJohn Wren Kennedy}
65d583b39bSJohn Wren Kennedy
66d583b39bSJohn Wren Kennedyfunction find_runfile
67d583b39bSJohn Wren Kennedy{
68d583b39bSJohn Wren Kennedy	typeset distro=
69d583b39bSJohn Wren Kennedy	if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
70d583b39bSJohn Wren Kennedy		distro=delphix
71d583b39bSJohn Wren Kennedy	elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
72d583b39bSJohn Wren Kennedy		distro=openindiana
73cf988facSDan McDonald	elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
74cf988facSDan McDonald		distro=omnios
75d583b39bSJohn Wren Kennedy	fi
76d583b39bSJohn Wren Kennedy
77b8052df9SRyan Moeller	[[ -n $distro ]] && echo $COMMON,$STF_SUITE/runfiles/$distro.run
78d583b39bSJohn Wren Kennedy}
79d583b39bSJohn Wren Kennedy
80d583b39bSJohn Wren Kennedyfunction verify_id
81d583b39bSJohn Wren Kennedy{
82d583b39bSJohn Wren Kennedy	[[ $(id -u) = "0" ]] && fail "This script must not be run as root."
83d583b39bSJohn Wren Kennedy
84412db4e9SJohn Wren Kennedy	sudo -k -n id >/dev/null 2>&1
85d583b39bSJohn Wren Kennedy	[[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
86d583b39bSJohn Wren Kennedy}
87d583b39bSJohn Wren Kennedy
88d583b39bSJohn Wren Kennedyfunction verify_disks
89d583b39bSJohn Wren Kennedy{
90d583b39bSJohn Wren Kennedy	typeset disk
910dbcca9bSGordon Ross	typeset path
92*9164a50bSToomas Soome	typeset -lu expected_size
93*9164a50bSToomas Soome	typeset -lu size
94*9164a50bSToomas Soome
95*9164a50bSToomas Soome	# Ensure disks are large enough for the tests: no less than 10GB
96*9164a50bSToomas Soome	# and large enough for a crash dump plus overheads: the disk partition
97*9164a50bSToomas Soome	# table (about 34k), zpool with 4.5MB for pool label and 128k for pool
98*9164a50bSToomas Soome	# data, so we round up pool data + labels to 5MB.
99*9164a50bSToomas Soome	expected_size=$(sudo -k -n dumpadm -epH)
100*9164a50bSToomas Soome	(( expected_size = expected_size + 5 * 1024 * 1024 ))
101*9164a50bSToomas Soome
102*9164a50bSToomas Soome	if (( expected_size < 10 * 1024 * 1024 * 1024 )); then
103*9164a50bSToomas Soome		(( expected_size = 10 * 1024 * 1024 * 1024 ))
104*9164a50bSToomas Soome	fi
105*9164a50bSToomas Soome
106d583b39bSJohn Wren Kennedy	for disk in $DISKS; do
1070dbcca9bSGordon Ross		case $disk in
1080dbcca9bSGordon Ross		/*) path=$disk;;
1090dbcca9bSGordon Ross		*) path=/dev/rdsk/${disk}s0
1100dbcca9bSGordon Ross		esac
111*9164a50bSToomas Soome		set -A disksize $(sudo -k prtvtoc $path 2>&1 |
112*9164a50bSToomas Soome			awk '$3 == "bytes/sector" ||
113*9164a50bSToomas Soome			    ($3 == "accessible" && $4 == "sectors") {print $2}')
114*9164a50bSToomas Soome
115*9164a50bSToomas Soome		if [[ (-n "${disksize[0]}") && (-n "${disksize[1]}") ]]; then
116*9164a50bSToomas Soome			(( size = disksize[0] * disksize[1] ))
117*9164a50bSToomas Soome		else
118*9164a50bSToomas Soome			return 1
119*9164a50bSToomas Soome		fi
120*9164a50bSToomas Soome		if (( size <  expected_size )); then
121*9164a50bSToomas Soome			(( size = expected_size / 1024 / 1024 / 1024 ))
122*9164a50bSToomas Soome			fail "$disk is too small, need at least ${size}GB"
123*9164a50bSToomas Soome		fi
124d583b39bSJohn Wren Kennedy	done
125d583b39bSJohn Wren Kennedy	return 0
126d583b39bSJohn Wren Kennedy}
127d583b39bSJohn Wren Kennedy
1281d32ba66SJohn Wren Kennedyfunction create_links
1291d32ba66SJohn Wren Kennedy{
1301d32ba66SJohn Wren Kennedy	typeset dir=$1
1311d32ba66SJohn Wren Kennedy	typeset file_list=$2
132d583b39bSJohn Wren Kennedy
1331d32ba66SJohn Wren Kennedy	[[ -n $PATHDIR ]] || fail "PATHDIR wasn't correctly set"
1341d32ba66SJohn Wren Kennedy
1351d32ba66SJohn Wren Kennedy	for i in $file_list; do
1361d32ba66SJohn Wren Kennedy		[[ ! -e $PATHDIR/$i ]] || fail "$i already exists"
1371d32ba66SJohn Wren Kennedy		ln -s $dir/$i $PATHDIR/$i || fail "Couldn't link $i"
1381d32ba66SJohn Wren Kennedy	done
1391d32ba66SJohn Wren Kennedy
1401d32ba66SJohn Wren Kennedy}
1411d32ba66SJohn Wren Kennedy
1421d32ba66SJohn Wren Kennedyfunction constrain_path
1431d32ba66SJohn Wren Kennedy{
1441d32ba66SJohn Wren Kennedy	. $STF_SUITE/include/commands.cfg
1451d32ba66SJohn Wren Kennedy
1461d32ba66SJohn Wren Kennedy	PATHDIR=$(/usr/bin/mktemp -d /var/tmp/constrained_path.XXXX)
1471d32ba66SJohn Wren Kennedy	chmod 755 $PATHDIR || fail "Couldn't chmod $PATHDIR"
1481d32ba66SJohn Wren Kennedy
1491d32ba66SJohn Wren Kennedy	create_links "/usr/bin" "$USR_BIN_FILES"
1501d32ba66SJohn Wren Kennedy	create_links "/usr/sbin" "$USR_SBIN_FILES"
1511d32ba66SJohn Wren Kennedy	create_links "/sbin" "$SBIN_FILES"
1521d32ba66SJohn Wren Kennedy	create_links "/opt/zfs-tests/bin" "$ZFSTEST_FILES"
1531d32ba66SJohn Wren Kennedy
1541d32ba66SJohn Wren Kennedy	# Special case links
1551d32ba66SJohn Wren Kennedy	ln -s /usr/gnu/bin/dd $PATHDIR/gnu_dd
1561d32ba66SJohn Wren Kennedy}
1571d32ba66SJohn Wren Kennedy
1581d32ba66SJohn Wren Kennedyconstrain_path
1591d32ba66SJohn Wren Kennedyexport PATH=$PATHDIR
1601d32ba66SJohn Wren Kennedy
1611d32ba66SJohn Wren Kennedyverify_id
162b8052df9SRyan Moellerwhile getopts ac:l:qT: c; do
163d583b39bSJohn Wren Kennedy	case $c in
164d583b39bSJohn Wren Kennedy	'a')
165d583b39bSJohn Wren Kennedy		auto_detect=true
166d583b39bSJohn Wren Kennedy		;;
167d583b39bSJohn Wren Kennedy	'c')
1688b426108SToomas Soome		runfile=$OPTARG
1698b426108SToomas Soome		[[ -f $runfile ]] || fail "Cannot read file: $runfile"
1708b426108SToomas Soome		if [[ -z $runfiles ]]; then
1718b426108SToomas Soome			runfiles=$runfile
1728b426108SToomas Soome		else
1738b426108SToomas Soome			runfiles+=",$runfile"
1748b426108SToomas Soome		fi
175d583b39bSJohn Wren Kennedy		;;
17678801af7SAndy Fiddaman	'l')
17778801af7SAndy Fiddaman		logfile=$OPTARG
17878801af7SAndy Fiddaman		[[ -f $logfile ]] || fail "Cannot read file: $logfile"
17978801af7SAndy Fiddaman		xargs+=" -l $logfile"
18078801af7SAndy Fiddaman		;;
181d583b39bSJohn Wren Kennedy	'q')
18278801af7SAndy Fiddaman		xargs+=" -q"
183d583b39bSJohn Wren Kennedy		;;
184b8052df9SRyan Moeller	'T')
185b8052df9SRyan Moeller		xargs+=" -T $OPTARG"
186b8052df9SRyan Moeller		;;
187d583b39bSJohn Wren Kennedy	esac
188d583b39bSJohn Wren Kennedydone
189d583b39bSJohn Wren Kennedyshift $((OPTIND - 1))
190d583b39bSJohn Wren Kennedy
191d583b39bSJohn Wren Kennedy# If the user specified -a, then use free disks, otherwise use those in $DISKS.
192d583b39bSJohn Wren Kennedyif $auto_detect; then
193d583b39bSJohn Wren Kennedy	export DISKS=$(find_disks)
194d583b39bSJohn Wren Kennedyelif [[ -z $DISKS ]]; then
195d583b39bSJohn Wren Kennedy	fail "\$DISKS not set in env, and -a not specified."
196d583b39bSJohn Wren Kennedyelse
197d583b39bSJohn Wren Kennedy	verify_disks || fail "Couldn't verify all the disks in \$DISKS"
198d583b39bSJohn Wren Kennedyfi
199d583b39bSJohn Wren Kennedy
2008808ac5dSYuri Pankov# Add the root pool to $KEEP according to its contents.
2018808ac5dSYuri Pankov# It's ok to list it twice.
202d583b39bSJohn Wren Kennedyif [[ -z $KEEP ]]; then
2038808ac5dSYuri Pankov	KEEP="$(find_rpool)"
204d583b39bSJohn Wren Kennedyelse
2058808ac5dSYuri Pankov	KEEP+=" $(find_rpool)"
206d583b39bSJohn Wren Kennedyfi
207d583b39bSJohn Wren Kennedy
2088808ac5dSYuri Pankovexport __ZFS_POOL_EXCLUDE="$KEEP"
2098808ac5dSYuri Pankovexport KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
2108808ac5dSYuri Pankov
211b8052df9SRyan Moeller[[ -z $runfiles ]] && runfiles=$(find_runfile)
212b8052df9SRyan Moeller[[ -z $runfiles ]] && fail "Couldn't determine distro"
213d583b39bSJohn Wren Kennedy
214d583b39bSJohn Wren Kennedy. $STF_SUITE/include/default.cfg
215d583b39bSJohn Wren Kennedy
216d583b39bSJohn Wren Kennedynum_disks=$(echo $DISKS | awk '{print NF}')
217d583b39bSJohn Wren Kennedy[[ $num_disks -lt 3 ]] && fail "Not enough disks to run ZFS Test Suite"
218d583b39bSJohn Wren Kennedy
219412db4e9SJohn Wren Kennedy# Ensure user has only basic privileges.
220b8052df9SRyan Moellerppriv -s EIP=basic -e $runner -c $runfiles $xargs
2211d32ba66SJohn Wren Kennedyret=$?
222d583b39bSJohn Wren Kennedy
2231d32ba66SJohn Wren Kennedyrm -rf $PATHDIR || fail "Couldn't remove $PATHDIR"
2241d32ba66SJohn Wren Kennedy
2251d32ba66SJohn Wren Kennedyexit $ret
226