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