xref: /titanic_50/usr/src/test/zfs-tests/cmd/scripts/zfstest.ksh (revision 5ffb0c9b03b5149ff4f5821a62be4a52408ada2a)
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, 2015 by Delphix. All rights reserved.
16# Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
17# Copyright 2016 Nexenta Systems, Inc.
18#
19
20export STF_SUITE="/opt/zfs-tests"
21export STF_TOOLS="/opt/test-runner/stf"
22runner="/opt/test-runner/bin/run"
23auto_detect=false
24
25function fail
26{
27	echo $1
28	exit ${2:-1}
29}
30
31function find_disks
32{
33	typeset all_disks=$(echo '' | sudo -k /usr/sbin/format | awk \
34	    '/c[0-9]/ {print $2}')
35	typeset used_disks=$(/sbin/zpool status | awk \
36	    '/c[0-9]*t[0-9a-f]*d[0-9]/ {print $1}' | sed 's/s[0-9]//g')
37
38	typeset disk used avail_disks
39	for disk in $all_disks; do
40		for used in $used_disks; do
41			[[ "$disk" = "$used" ]] && continue 2
42		done
43		[[ -n $avail_disks ]] && avail_disks="$avail_disks $disk"
44		[[ -z $avail_disks ]] && avail_disks="$disk"
45	done
46
47	echo $avail_disks
48}
49
50function find_rpool
51{
52	typeset ds=$(/usr/sbin/mount | awk '/^\/ / {print $3}')
53	echo ${ds%%/*}
54}
55
56function find_runfile
57{
58	typeset distro=
59	if [[ -d /opt/delphix && -h /etc/delphix/version ]]; then
60		distro=delphix
61	elif [[ 0 -ne $(grep -c OpenIndiana /etc/release 2>/dev/null) ]]; then
62		distro=openindiana
63	elif [[ 0 -ne $(grep -c OmniOS /etc/release 2>/dev/null) ]]; then
64		distro=omnios
65	fi
66
67	[[ -n $distro ]] && echo $STF_SUITE/runfiles/$distro.run
68}
69
70function verify_id
71{
72	[[ $(id -u) = "0" ]] && fail "This script must not be run as root."
73
74	sudo -k -n id >/dev/null 2>&1
75	[[ $? -eq 0 ]] || fail "User must be able to sudo without a password."
76}
77
78function verify_disks
79{
80	typeset disk
81	for disk in $DISKS; do
82		sudo -k /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 root pool to $KEEP according to its contents.
116# It's ok to list it twice.
117if [[ -z $KEEP ]]; then
118	KEEP="$(find_rpool)"
119else
120	KEEP+=" $(find_rpool)"
121fi
122
123export __ZFS_POOL_EXCLUDE="$KEEP"
124export KEEP="^$(echo $KEEP | sed 's/ /$|^/g')\$"
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# Ensure user has only basic privileges.
135/usr/bin/ppriv -s EIP=basic -e $runner $quiet -c $runfile
136
137exit $?
138