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 by Delphix. All rights reserved.
16# Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
17#
18
19export STF_SUITE="/opt/zfs-tests"
20export STF_TOOLS="/opt/test-runner/stf"
21runner="/opt/test-runner/bin/run"
22auto_detect=false
23
24function fail
25{
26	echo $1
27	exit ${2:-1}
28}
29
30function find_disks
31{
32	typeset all_disks=$(echo '' | sudo /usr/sbin/format | awk \
33	    '/c[0-9]/ {print $2}')
34	typeset used_disks=$(/sbin/zpool status | awk \
35	    '/c[0-9]*t[0-9a-f]*d[0-9]/ {print $1}' | sed 's/s[0-9]//g')
36
37	typeset disk used avail_disks
38	for disk in $all_disks; do
39		for used in $used_disks; do
40			[[ "$disk" = "$used" ]] && continue 2
41		done
42		[[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
43		[[ -z $avail_disks ]] && avail_disks="$disk"
44	done
45
46	echo $avail_disks
47}
48
49function find_rpool
50{
51	typeset ds=$(/usr/sbin/mount | awk '/^\/ / {print $3}')
52	echo ${ds%%/*}
53}
54
55function find_runfile
56{
57	typeset distro=
58	if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
59		distro=delphix
60	elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
61		distro=openindiana
62	elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
63		distro=omnios
64	fi
65
66	[[ -n $distro ]] && echo $STF_SUITE/runfiles/$distro.run
67}
68
69function verify_id
70{
71	[[ $(id -u) = "0" ]] && fail "This script must not be run as root."
72
73	sudo -n id >/dev/null 2>&1
74	[[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
75
76	typeset -i priv_cnt=$(ppriv $$ | egrep -v \
77	    ": basic$|	L:| <none>|$$:" | wc -l)
78	[[ $priv_cnt -ne 0 ]] && fail "User must only have basic privileges."
79}
80
81function verify_disks
82{
83	typeset disk
84	for disk in $DISKS; do
85		sudo /usr/sbin/prtvtoc /dev/rdsk/${disk}s0 >/dev/null 2>&1
86		[[ $? -eq 0 ]] || return 1
87	done
88	return 0
89}
90
91verify_id
92
93while getopts ac:q c; do
94	case $c in
95	'a')
96		auto_detect=true
97		;;
98	'c')
99		runfile=$OPTARG
100		[[ -f $runfile ]] || fail "Cannot read file: $runfile"
101		;;
102	'q')
103		quiet='-q'
104		;;
105	esac
106done
107shift $((OPTIND - 1))
108
109# If the user specified -a, then use free disks, otherwise use those in $DISKS.
110if $auto_detect; then
111	export DISKS=$(find_disks)
112elif [[ -z $DISKS ]]; then
113	fail "\$DISKS not set in env, and -a not specified."
114else
115	verify_disks || fail "Couldn't verify all the disks in \$DISKS"
116fi
117
118# Add the rpool to $KEEP according to its contents. It's ok to list it twice.
119if [[ -z $KEEP ]]; then
120	export KEEP="^$(find_rpool)\$"
121else
122	export KEEP="^$(echo $KEEP | sed 's/ /|$/')\$"
123	KEEP+="|^$(find_rpool)\$"
124fi
125
126[[ -z $runfile ]] && runfile=$(find_runfile)
127[[ -z $runfile ]] && fail "Couldn't determine distro"
128
129. $STF_SUITE/include/default.cfg
130
131num_disks=$(echo $DISKS | awk '{print NF}')
132[[ $num_disks -lt 3 ]] && fail "Not enough disks to run ZFS Test Suite"
133
134$runner $quiet -c $runfile
135
136exit $?
137