xref: /illumos-gate/usr/src/test/zfs-tests/cmd/scripts/zfstest.ksh (revision ce8560eeb961d528e27685fcdd2ffb03e9478dbf)
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 2016 Nexenta Systems, Inc.
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 STF_TOOLS="/opt/test-runner/stf"
25export PATHDIR=""
26runner="/opt/test-runner/bin/run"
27auto_detect=false
28
29if [[ -z "$TESTFAIL_CALLBACKS" ]] ; then
30	export TESTFAIL_CALLBACKS="$STF_SUITE/callbacks/zfs_dbgmsg"
31fi
32
33function fail
34{
35	echo $1
36	exit ${2:-1}
37}
38
39function find_disks
40{
41	typeset all_disks=$(echo '' | sudo -k format | awk \
42	    '/c[0-9]/ {print $2}')
43	typeset used_disks=$(zpool status | awk \
44	    '/c[0-9]+(t[0-9a-f]+)?d[0-9]+/ {print $1}' | sed -E \
45	    's/(s|p)[0-9]+//g')
46
47	typeset disk used avail_disks
48	for disk in $all_disks; do
49		for used in $used_disks; do
50			[[ "$disk" = "$used" ]] && continue 2
51		done
52		[[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
53		[[ -z $avail_disks ]] && avail_disks="$disk"
54	done
55
56	echo $avail_disks
57}
58
59function find_rpool
60{
61	typeset ds=$(mount | awk '/^\/ / {print $3}')
62	echo ${ds%%/*}
63}
64
65function find_runfile
66{
67	typeset distro=
68	if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
69		distro=delphix
70	elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
71		distro=openindiana
72	elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
73		distro=omnios
74	fi
75
76	[[ -n $distro ]] && echo $STF_SUITE/runfiles/$distro.run
77}
78
79function verify_id
80{
81	[[ $(id -u) = "0" ]] && fail "This script must not be run as root."
82
83	sudo -k -n id >/dev/null 2>&1
84	[[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
85}
86
87function verify_disks
88{
89	typeset disk
90	typeset path
91	for disk in $DISKS; do
92		case $disk in
93		/*) path=$disk;;
94		*) path=/dev/rdsk/${disk}s0
95		esac
96		sudo -k prtvtoc $path >/dev/null 2>&1
97		[[ $? -eq 0 ]] || return 1
98	done
99	return 0
100}
101
102function create_links
103{
104	typeset dir=$1
105	typeset file_list=$2
106
107	[[ -n $PATHDIR ]] || fail "PATHDIR wasn't correctly set"
108
109	for i in $file_list; do
110		[[ ! -e $PATHDIR/$i ]] || fail "$i already exists"
111		ln -s $dir/$i $PATHDIR/$i || fail "Couldn't link $i"
112	done
113
114}
115
116function constrain_path
117{
118	. $STF_SUITE/include/commands.cfg
119
120	PATHDIR=$(/usr/bin/mktemp -d /var/tmp/constrained_path.XXXX)
121	chmod 755 $PATHDIR || fail "Couldn't chmod $PATHDIR"
122
123	create_links "/usr/bin" "$USR_BIN_FILES"
124	create_links "/usr/sbin" "$USR_SBIN_FILES"
125	create_links "/sbin" "$SBIN_FILES"
126	create_links "/opt/zfs-tests/bin" "$ZFSTEST_FILES"
127
128	# Special case links
129	ln -s /usr/gnu/bin/dd $PATHDIR/gnu_dd
130}
131
132constrain_path
133export PATH=$PATHDIR
134
135verify_id
136while getopts ac:l:q c; do
137	case $c in
138	'a')
139		auto_detect=true
140		;;
141	'c')
142		runfile=$OPTARG
143		[[ -f $runfile ]] || fail "Cannot read file: $runfile"
144		;;
145	'l')
146		logfile=$OPTARG
147		[[ -f $logfile ]] || fail "Cannot read file: $logfile"
148		xargs+=" -l $logfile"
149		;;
150	'q')
151		xargs+=" -q"
152		;;
153	esac
154done
155shift $((OPTIND - 1))
156
157# If the user specified -a, then use free disks, otherwise use those in $DISKS.
158if $auto_detect; then
159	export DISKS=$(find_disks)
160elif [[ -z $DISKS ]]; then
161	fail "\$DISKS not set in env, and -a not specified."
162else
163	verify_disks || fail "Couldn't verify all the disks in \$DISKS"
164fi
165
166# Add the root pool to $KEEP according to its contents.
167# It's ok to list it twice.
168if [[ -z $KEEP ]]; then
169	KEEP="$(find_rpool)"
170else
171	KEEP+=" $(find_rpool)"
172fi
173
174export __ZFS_POOL_EXCLUDE="$KEEP"
175export KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
176
177[[ -z $runfile ]] && runfile=$(find_runfile)
178[[ -z $runfile ]] && fail "Couldn't determine distro"
179
180. $STF_SUITE/include/default.cfg
181
182num_disks=$(echo $DISKS | awk '{print NF}')
183[[ $num_disks -lt 3 ]] && fail "Not enough disks to run ZFS Test Suite"
184
185# Ensure user has only basic privileges.
186ppriv -s EIP=basic -e $runner -c $runfile $xargs
187ret=$?
188
189rm -rf $PATHDIR || fail "Couldn't remove $PATHDIR"
190
191exit $ret
192