xref: /freebsd/tests/sys/cddl/zfs/bin/zpool_smi.ksh (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
12fae26bdSAlan Somers#!/usr/local/bin/ksh93 -p
22fae26bdSAlan Somers#
32fae26bdSAlan Somers# CDDL HEADER START
42fae26bdSAlan Somers#
52fae26bdSAlan Somers# The contents of this file are subject to the terms of the
62fae26bdSAlan Somers# Common Development and Distribution License (the "License").
72fae26bdSAlan Somers# You may not use this file except in compliance with the License.
82fae26bdSAlan Somers#
92fae26bdSAlan Somers# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
102fae26bdSAlan Somers# or http://www.opensolaris.org/os/licensing.
112fae26bdSAlan Somers# See the License for the specific language governing permissions
122fae26bdSAlan Somers# and limitations under the License.
132fae26bdSAlan Somers#
142fae26bdSAlan Somers# When distributing Covered Code, include this CDDL HEADER in each
152fae26bdSAlan Somers# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
162fae26bdSAlan Somers# If applicable, add the following below this CDDL HEADER, with the
172fae26bdSAlan Somers# fields enclosed by brackets "[]" replaced with your own identifying
182fae26bdSAlan Somers# information: Portions Copyright [yyyy] [name of copyright owner]
192fae26bdSAlan Somers#
202fae26bdSAlan Somers# CDDL HEADER END
212fae26bdSAlan Somers#
222fae26bdSAlan Somers
232fae26bdSAlan Somers#
242fae26bdSAlan Somers# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
252fae26bdSAlan Somers# Use is subject to license terms.
262fae26bdSAlan Somers
272fae26bdSAlan Somersfunction labelvtoc
282fae26bdSAlan Somers{
292fae26bdSAlan Somers	typeset disk=$1
302fae26bdSAlan Somers	if [[ -z $disk ]]; then
312fae26bdSAlan Somers		print "no disk is given."
322fae26bdSAlan Somers		return 1
332fae26bdSAlan Somers	fi
342fae26bdSAlan Somers
352fae26bdSAlan Somers	/usr/sbin/format $disk << _EOF >/dev/null 2>&1
362fae26bdSAlan Somerslabel
372fae26bdSAlan Somersyes
382fae26bdSAlan Somers_EOF
392fae26bdSAlan Somers
40*5a2fc464SAndriy Gapon	labeltype=$(/usr/sbin/prtvtoc -fh /dev/${disk}s2 | \
412fae26bdSAlan Somers		awk '{print $1}' | awk -F= '{print $2}' )
422fae26bdSAlan Somers	if [[ -z $labeltype ]]; then
432fae26bdSAlan Somers		print "${disk} not exist."
442fae26bdSAlan Somers		return 1
452fae26bdSAlan Somers	fi
462fae26bdSAlan Somers
472fae26bdSAlan Somers	if [[ $labeltype == "34" ]]; then
482fae26bdSAlan Somers
492fae26bdSAlan Somers		typeset label_file=$TMPDIR/labelvtoc.${TESTCASE_ID:-$$}
502fae26bdSAlan Somers		typeset arch=$(uname -p)
512fae26bdSAlan Somers
522fae26bdSAlan Somers		if [[ $arch == "i386" ]]; then
532fae26bdSAlan Somers			print "label" > $label_file
542fae26bdSAlan Somers			print "0" >> $label_file
552fae26bdSAlan Somers	       		print "" >> $label_file
562fae26bdSAlan Somers		 	print "q" >> $label_file
572fae26bdSAlan Somers		 	print "q" >> $label_file
582fae26bdSAlan Somers
59*5a2fc464SAndriy Gapon		 	fdisk -B /dev/${disk}p0 >/dev/null 2>&1
602fae26bdSAlan Somers		 	# wait a while for fdisk finishes
612fae26bdSAlan Somers			/usr/sbin/devfsadm > /dev/null 2>&1
622fae26bdSAlan Somers		elif [[ $arch == "sparc" ]]; then
632fae26bdSAlan Somers			print "label" > $label_file
642fae26bdSAlan Somers			print "0" >> $label_file
652fae26bdSAlan Somers			print "" >> $label_file
662fae26bdSAlan Somers			print "" >> $label_file
672fae26bdSAlan Somers			print "" >> $label_file
682fae26bdSAlan Somers			print "q" >> $label_file
692fae26bdSAlan Somers		else
702fae26bdSAlan Somers	   		print "unknow arch type : $arch"
712fae26bdSAlan Somers			return 1
722fae26bdSAlan Somers		fi
732fae26bdSAlan Somers
742fae26bdSAlan Somers		format -e -s -d $disk -f $label_file
752fae26bdSAlan Somers		typeset -i ret_val=$?
762fae26bdSAlan Somers		rm -f $label_file
772fae26bdSAlan Somers		#
782fae26bdSAlan Somers		# wait the format to finish
792fae26bdSAlan Somers		#
802fae26bdSAlan Somers		/usr/sbin/devfsadm > /dev/null 2>&1
812fae26bdSAlan Somers		if (( ret_val != 0 )); then
822fae26bdSAlan Somers			print "unable to label $disk as VTOC."
832fae26bdSAlan Somers			return 1
842fae26bdSAlan Somers		fi
852fae26bdSAlan Somers	fi
862fae26bdSAlan Somers
872fae26bdSAlan Somers	return 0
882fae26bdSAlan Somers}
892fae26bdSAlan Somers
902fae26bdSAlan Somerscmd=$1
912fae26bdSAlan Somers
922fae26bdSAlan Somersif [[ -z $cmd ]]; then
932fae26bdSAlan Somers	return 0
942fae26bdSAlan Somersfi
952fae26bdSAlan Somers
962fae26bdSAlan Somersshift
972fae26bdSAlan Somers
982fae26bdSAlan Somers
992fae26bdSAlan Somerstypeset option
1002fae26bdSAlan Somerscase $cmd in
1012fae26bdSAlan Somers	create|add|attach|detach|replace|remove|online|offline|clear)
1022fae26bdSAlan Somers		for arg in $@; do
103*5a2fc464SAndriy Gapon			if [[ $arg == "/dev/"* ]]; then
104*5a2fc464SAndriy Gapon				arg=${arg#/dev/}
1052fae26bdSAlan Somers			fi
1062fae26bdSAlan Somers
1072fae26bdSAlan Somers			print $arg | egrep "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
1082fae26bdSAlan Somers
1092fae26bdSAlan Somers			if [[ $? -eq 0 ]] ; then
1102fae26bdSAlan Somers				labelvtoc $arg
1112fae26bdSAlan Somers				if [[ $? -eq 0 ]] ; then
1122fae26bdSAlan Somers					arg=${arg}s2
1132fae26bdSAlan Somers				fi
1142fae26bdSAlan Somers			fi
1152fae26bdSAlan Somers			option="${option} $arg"
1162fae26bdSAlan Somers		done
1172fae26bdSAlan Somers		;;
1182fae26bdSAlan Somers	*)
1192fae26bdSAlan Somers		option="$@"
1202fae26bdSAlan Somers		;;
1212fae26bdSAlan Somersesac
1222fae26bdSAlan Somers
1232fae26bdSAlan Somerscase $cmd in
1242fae26bdSAlan Somers	create|add|attach|replace)
1252fae26bdSAlan Somers		if [[ $option != *"-f"* ]]; then
1262fae26bdSAlan Somers			cmd="${cmd} -f"
1272fae26bdSAlan Somers		fi
1282fae26bdSAlan Somers		;;
1292fae26bdSAlan Somers	*)
1302fae26bdSAlan Somers		;;
1312fae26bdSAlan Somersesac
1322fae26bdSAlan Somers
1332fae26bdSAlan Somersprint $cmd $option
134