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