xref: /freebsd/tests/sys/cddl/zfs/bin/zpool_smi.ksh (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1#!/usr/local/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# $FreeBSD$
24
25#
26# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)zpool_smi.ksh	1.2	09/01/13 SMI"
30#
31
32function labelvtoc
33{
34	typeset disk=$1
35	if [[ -z $disk ]]; then
36		print "no disk is given."
37		return 1
38	fi
39
40	/usr/sbin/format $disk << _EOF >/dev/null 2>&1
41label
42yes
43_EOF
44
45	labeltype=$(/usr/sbin/prtvtoc -fh /dev/${disk}s2 | \
46		awk '{print $1}' | awk -F= '{print $2}' )
47	if [[ -z $labeltype ]]; then
48		print "${disk} not exist."
49		return 1
50	fi
51
52	if [[ $labeltype == "34" ]]; then
53
54		typeset label_file=$TMPDIR/labelvtoc.${TESTCASE_ID:-$$}
55		typeset arch=$(uname -p)
56
57		if [[ $arch == "i386" ]]; then
58			print "label" > $label_file
59			print "0" >> $label_file
60	       		print "" >> $label_file
61		 	print "q" >> $label_file
62		 	print "q" >> $label_file
63
64		 	fdisk -B /dev/${disk}p0 >/dev/null 2>&1
65		 	# wait a while for fdisk finishes
66			/usr/sbin/devfsadm > /dev/null 2>&1
67		elif [[ $arch == "sparc" ]]; then
68			print "label" > $label_file
69			print "0" >> $label_file
70			print "" >> $label_file
71			print "" >> $label_file
72			print "" >> $label_file
73			print "q" >> $label_file
74		else
75	   		print "unknow arch type : $arch"
76			return 1
77		fi
78
79		format -e -s -d $disk -f $label_file
80		typeset -i ret_val=$?
81		rm -f $label_file
82		#
83		# wait the format to finish
84		#
85		/usr/sbin/devfsadm > /dev/null 2>&1
86		if (( ret_val != 0 )); then
87			print "unable to label $disk as VTOC."
88			return 1
89		fi
90	fi
91
92	return 0
93}
94
95cmd=$1
96
97if [[ -z $cmd ]]; then
98	return 0
99fi
100
101shift
102
103
104typeset option
105case $cmd in
106	create|add|attach|detach|replace|remove|online|offline|clear)
107		for arg in $@; do
108			if [[ $arg == "/dev/"* ]]; then
109				arg=${arg#/dev/}
110			fi
111
112			print $arg | egrep "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
113
114			if [[ $? -eq 0 ]] ; then
115				labelvtoc $arg
116				if [[ $? -eq 0 ]] ; then
117					arg=${arg}s2
118				fi
119			fi
120			option="${option} $arg"
121		done
122		;;
123	*)
124		option="$@"
125		;;
126esac
127
128case $cmd in
129	create|add|attach|replace)
130		if [[ $option != *"-f"* ]]; then
131			cmd="${cmd} -f"
132		fi
133		;;
134	*)
135		;;
136esac
137
138print $cmd $option
139