xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/devices/devices_common.kshlib (revision 31aa620247ae407b2bee2dccd71693d1938f54d6)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29# Copyright 2020 Joyent, Inc.
30#
31
32. $STF_SUITE/tests/functional/devices/devices.cfg
33. $STF_SUITE/include/libtest.shlib
34
35#
36# Create block file or charactor file according to parameter.
37#
38# $1 device file type
39# $2 file name
40#
41function create_dev_file
42{
43	typeset filetype=$1
44	typeset filename=$2
45
46	# On illumos we need access to the root zpool to get the major/minor
47	# numbers here.
48	export __ZFS_POOL_EXCLUDE=""
49
50	case $filetype in
51		b)
52			devtype=$(df -n / | awk '{print $3}')
53			case $devtype in
54				zfs)
55					rootpool=$(df / | \
56						awk '{print $2}')
57					rootpool=${rootpool#\(}
58					rootpool=${rootpool%%/*}
59
60					devstr=$(get_disklist $rootpool)
61					devstr=$(echo "$devstr" | \
62						awk '{print $1}')
63					[[ -z $devstr ]] && \
64						log_fail "Can not get block device file."
65					devstr=/dev/dsk/${devstr}
66					;;
67				ufs)
68			#
69			# Get the existing block device file in current system.
70			# And bring out the first one.
71			#
72					devstr=$(df -lhF ufs | \
73						grep "^/dev/dsk" | \
74						awk '{print $1}')
75					devstr=$(echo "$devstr" | \
76						awk '{print $1}')
77					[[ -z $devstr ]] && \
78						log_fail "Can not get block device file."
79					;;
80				*)
81					log_unsupported "Unsupported fstype " \
82						"for / ($devtype)," \
83						"only ufs|zfs is supported."
84					;;
85			esac
86
87			#
88			# Get the device file information. i.e:
89			# /dev/dsk/c0t0d0s0:      block special (28/768)
90			#
91			devstr=$(file $devstr)
92
93			#
94			# Bring out major and minor number.
95			#
96			major=${devstr##*\(}
97			major=${major%%/*}
98			minor=${devstr##*/}
99			minor=${minor%\)}
100
101			log_must mknod $filename b $major $minor
102			;;
103		c)
104			#
105			# Create device file '/dev/null'
106			#
107			log_must mknod $filename c $(getmajor mm) 2
108			;;
109		*)
110			log_fail "'$filetype' is wrong."
111			;;
112	esac
113
114	return 0
115}
116
117function cleanup
118{
119	log_must zfs set devices=on $TESTPOOL/$TESTFS
120	log_must rm -f $TESTDIR/$TESTFILE1
121	log_must rm -f $TESTDIR/$TESTFILE2
122	log_must rm -f $TESTDIR/$TESTFILE1.out
123	log_must rm -f $TESTDIR/$TESTFILE2.out
124}
125