xref: /illumos-gate/usr/src/test/zfs-tests/cmd/scripts/zfstest.ksh (revision 068cf9dc70b00292d5e5ff1ab17f1b046215d6f6)
1#!/usr/bin/ksh
2
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
16# Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
17# Copyright 2021 Tintri by DDN, Inc. All rights reserved.
18# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
19#
20
21export PATH="/usr/bin"
22export NOINUSE_CHECK=1
23export STF_SUITE="/opt/zfs-tests"
24export COMMON="$STF_SUITE/runfiles/common.run"
25export STF_TOOLS="/opt/test-runner/stf"
26export PATHDIR=""
27runner="/opt/test-runner/bin/run"
28auto_detect=false
29
30if [[ -z "$TESTFAIL_CALLBACKS" ]] ; then
31	export TESTFAIL_CALLBACKS="$STF_SUITE/callbacks/zfs_dbgmsg"
32fi
33
34function fail
35{
36	echo $1
37	exit ${2:-1}
38}
39
40function find_disks
41{
42	typeset all_disks=$(echo '' | sudo -k format | awk \
43	    '/c[0-9]/ {print $2}')
44	typeset used_disks=$(zpool status | awk \
45	    '/c[0-9]+(t[0-9a-fA-F]+)?d[0-9]+/ {print $1}' | sed -E \
46	    's/(s|p)[0-9]+//g')
47
48	typeset disk used avail_disks
49	for disk in $all_disks; do
50		for used in $used_disks; do
51			[[ "$disk" = "$used" ]] && continue 2
52		done
53		[[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
54		[[ -z $avail_disks ]] && avail_disks="$disk"
55	done
56
57	echo $avail_disks
58}
59
60function find_rpool
61{
62	typeset ds=$(mount | awk '/^\/ / {print $3}')
63	echo ${ds%%/*}
64}
65
66function find_runfile
67{
68	typeset distro=
69	if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
70		distro=delphix
71	elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
72		distro=openindiana
73	elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
74		distro=omnios
75	fi
76
77	[[ -n $distro ]] && echo $COMMON,$STF_SUITE/runfiles/$distro.run
78}
79
80function verify_id
81{
82	[[ $(id -u) = "0" ]] && fail "This script must not be run as root."
83
84	sudo -k -n id >/dev/null 2>&1
85	[[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
86}
87
88function verify_disks
89{
90	typeset disk
91	typeset path
92	typeset -lu expected_size
93	typeset -lu size
94
95	# Ensure disks are large enough for the tests: no less than 10GB
96	# and large enough for a crash dump plus overheads: the disk partition
97	# table (about 34k), zpool with 4.5MB for pool label and 128k for pool
98	# data, so we round up pool data + labels to 5MB.
99	expected_size=$(sudo -k -n dumpadm -epH)
100	(( expected_size = expected_size + 5 * 1024 * 1024 ))
101
102	if (( expected_size < 10 * 1024 * 1024 * 1024 )); then
103		(( expected_size = 10 * 1024 * 1024 * 1024 ))
104	fi
105
106	for disk in $DISKS; do
107		case $disk in
108		/*) path=$disk;;
109		*) path=/dev/rdsk/${disk}s0
110		esac
111		set -A disksize $(sudo -k prtvtoc $path 2>&1 |
112			awk '$3 == "bytes/sector" ||
113			    ($3 == "accessible" && $4 == "sectors") {print $2}')
114
115		if [[ (-n "${disksize[0]}") && (-n "${disksize[1]}") ]]; then
116			(( size = disksize[0] * disksize[1] ))
117		else
118			return 1
119		fi
120		if (( size <  expected_size )); then
121			(( size = expected_size / 1024 / 1024 / 1024 ))
122			fail "$disk is too small, need at least ${size}GB"
123		fi
124	done
125	return 0
126}
127
128function create_links
129{
130	typeset dir=$1
131	typeset file_list=$2
132
133	[[ -n $PATHDIR ]] || fail "PATHDIR wasn't correctly set"
134
135	for i in $file_list; do
136		[[ ! -e $PATHDIR/$i ]] || fail "$i already exists"
137		ln -s $dir/$i $PATHDIR/$i || fail "Couldn't link $i"
138	done
139
140}
141
142function constrain_path
143{
144	. $STF_SUITE/include/commands.cfg
145
146	PATHDIR=$(/usr/bin/mktemp -d /var/tmp/constrained_path.XXXX)
147	chmod 755 $PATHDIR || fail "Couldn't chmod $PATHDIR"
148
149	create_links "/usr/bin" "$USR_BIN_FILES"
150	create_links "/usr/sbin" "$USR_SBIN_FILES"
151	create_links "/sbin" "$SBIN_FILES"
152	create_links "/opt/zfs-tests/bin" "$ZFSTEST_FILES"
153
154	# Special case links
155	ln -s /usr/gnu/bin/dd $PATHDIR/gnu_dd
156}
157
158constrain_path
159export PATH=$PATHDIR
160
161verify_id
162while getopts ac:l:qT: c; do
163	case $c in
164	'a')
165		auto_detect=true
166		;;
167	'c')
168		runfile=$OPTARG
169		[[ -f $runfile ]] || fail "Cannot read file: $runfile"
170		if [[ -z $runfiles ]]; then
171			runfiles=$runfile
172		else
173			runfiles+=",$runfile"
174		fi
175		;;
176	'l')
177		logfile=$OPTARG
178		[[ -f $logfile ]] || fail "Cannot read file: $logfile"
179		xargs+=" -l $logfile"
180		;;
181	'q')
182		xargs+=" -q"
183		;;
184	'T')
185		xargs+=" -T $OPTARG"
186		;;
187	esac
188done
189shift $((OPTIND - 1))
190
191# If the user specified -a, then use free disks, otherwise use those in $DISKS.
192if $auto_detect; then
193	export DISKS=$(find_disks)
194elif [[ -z $DISKS ]]; then
195	fail "\$DISKS not set in env, and -a not specified."
196else
197	verify_disks || fail "Couldn't verify all the disks in \$DISKS"
198fi
199
200# Add the root pool to $KEEP according to its contents.
201# It's ok to list it twice.
202if [[ -z $KEEP ]]; then
203	KEEP="$(find_rpool)"
204else
205	KEEP+=" $(find_rpool)"
206fi
207
208export __ZFS_POOL_EXCLUDE="$KEEP"
209export KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
210
211[[ -z $runfiles ]] && runfiles=$(find_runfile)
212[[ -z $runfiles ]] && fail "Couldn't determine distro"
213
214. $STF_SUITE/include/default.cfg
215
216num_disks=$(echo $DISKS | awk '{print NF}')
217[[ $num_disks -lt 3 ]] && fail "Not enough disks to run ZFS Test Suite"
218
219# Ensure user has only basic privileges.
220ppriv -s EIP=basic -e $runner -c $runfiles $xargs
221ret=$?
222
223rm -rf $PATHDIR || fail "Couldn't remove $PATHDIR"
224
225exit $ret
226