xref: /freebsd/tests/sys/geom/class/geom_subr.sh (revision 47dd1d1b619cc035b82b49a91a25544309ff95ae)
1#!/bin/sh
2# $FreeBSD$
3
4devwait()
5{
6	while :; do
7		if [ -c /dev/${class}/${name} ]; then
8			return
9		fi
10		sleep 0.2
11	done
12}
13
14attach_md()
15{
16	local test_md
17
18	test_md=$(mdconfig -a "$@") || exit
19	echo $test_md >> $TEST_MDS_FILE || exit
20	echo $test_md
21}
22
23detach_md()
24{
25	local test_md unit
26
27	test_md=$1
28	unit=${test_md#md}
29	mdconfig -d -u $unit || exit
30	sed -i '' "/^${test_md}$/d" $TEST_MDS_FILE || exit
31}
32
33geom_test_cleanup()
34{
35	local test_md
36
37	if [ -f "$TEST_MDS_FILE" ]; then
38		while read test_md; do
39			# The "#" tells the TAP parser this is a comment
40			echo "# Removing test memory disk: $test_md"
41			mdconfig -d -u $test_md
42		done < $TEST_MDS_FILE
43	fi
44	rm -f "$TEST_MDS_FILE"
45}
46
47if [ $(id -u) -ne 0 ]; then
48	echo '1..0 # SKIP tests must be run as root'
49	exit 0
50fi
51
52# If the geom class isn't already loaded, try loading it.
53if ! kldstat -q -m g_${class}; then
54	if ! geom ${class} load; then
55		echo "1..0 # SKIP could not load module for geom class=${class}"
56		exit 0
57	fi
58fi
59
60# Need to keep track of the test md devices to avoid the scenario where a test
61# failing will cause the other tests to bomb out, or a test failing will leave
62# a large number of md(4) devices lingering around
63: ${TMPDIR=/tmp}
64export TMPDIR
65if ! TEST_MDS_FILE=$(mktemp ${TMPDIR}/test_mds.XXXXXX); then
66	echo 'Failed to create temporary file for tracking the test md(4) devices'
67	echo 'Bail out!'
68	exit 1
69fi
70