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