#!/sbin/sh -
# $Id: vxmksdpart.sh,v 1.4.45.2 2003/03/04 00:01:10 kirubak Exp $
#ident "$Source: /project/unixvm-cvs/src/sol/cmd/vxvm/support/vxmksdpart.sh,v $"

# Copyright (c) 2001 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
# 
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF VERITAS SOFTWARE.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF VERITAS SOFTWARE.
# 
#               RESTRICTED RIGHTS LEGEND
# USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT IS
# SUBJECT TO RESTRICTIONS AS SET FORTH IN SUBPARAGRAPH
# (C) (1) (ii) OF THE RIGHTS IN TECHNICAL DATA AND
# COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013.
#               VERITAS SOFTWARE
# 1600 PLYMOUTH STREET, MOUNTAIN VIEW, CA 94043


: ${VOLROOT_DIR:=$__VXVM_ROOT_DIR}
. ${VOL_SCRIPTS_LIB:-$VOLROOT_DIR/usr/lib/vxvm/lib}/vxcommon

usage="`egettxt \"Usage: vxmksdpart [-f] [-g diskgroup] subdisk sliceno [tag flags]\" vxvmshm:462`"

exec 4>&2

# doit - execute a command, printing the command in verbose mode
doit() {
	[ "$verbose" ] && cat <<-% >&4
			! $*
			%
	"$@"
}

quit()
{
	rm -f "$tmpfile1"
	exit $1
}

trap "quit 1" INT HUP QUIT TERM

#
# set OS specific variables
#
dgoptarg=
dgargs=-A
verbose=
force=
while getopts g:vf c
do
	case $c in
	g)	dgoptarg=-g; dgargs=$OPTARG;;
	v)	verbose=yes;;
	f)	force=yes;;
	?)	echo "$usage" >&2; quit 1;;
	esac
done
shift `expr $OPTIND - 1`
if [ $# -ne 2 ] && [ $# -ne 4 ]
then
	echo "$usage" >&2
	quit 1
fi

eval `vxparms`
if [ -z "$V_USR" ] || [ -z "$VOL_PUB_SLICE_TAG" ]
then
	egettxt "vxmksdpart: Unexpected output from vxparms" vxvmshm:564
	quit 1
fi

sdname=$1
sdsliceno=$2
if [ $# -gt 2 ]
then
	sdslicetag=$3
	sdsliceflags=$4
else
	sdslicetag=$V_USR
	sdsliceflags=0x200
fi

sdinfo="`vxprint -Qq $dgoptarg "$dgargs" -sF '%dev_offset %len %device' \
	$sdname`"
if [ `list_count $sdinfo` -eq 0 ]
then
	quit 1
fi
if [ `list_count $sdinfo` -ne 3 ]
then
	sdname="$sdname" egettxt \
		"vxmksdpart: $sdname is not unique within all disk groups"
		vxvmshm:559 >&2
	quit 1
fi
sd_dev_offset="`list_item 1 $sdinfo`"
sd_len="`list_item 2 $sdinfo`"
sd_device="`list_item 3 $sdinfo`"
active_path=`/usr/sbin/vxdmpadm getsubpaths dmpnodename=$sd_device 2>/dev/null |tail +3 | grep ENABLED | head -1 | awk '{print $1}'`
if [ -z "$active_path" ]
then
	export daname ; egettxt \
	"vxmksdpart: No enabled path found for device $sd_device" \
	vxvmshm:0 >&2
	quit 1
fi
sd_device=$active_path
if [ "X$sd_device" = X- ]
then
	sdname="$sdname" egettxt \
		"vxmksdpart: There is no device for subdisk $sdname" \
		vxvmshm:563 >&2
	quit 1
fi


if vxcheckda $sd_device > /dev/null 2> /dev/null
then
	if not dogi_is_whole_slice $sd_device
	then
		invalid_sd_device=yes
	else
		invalid_sd_device=no
	fi
else
		invalid_sd_device=yes
fi

if [ "${invalid_sd_device}" = "yes" ]
then
	sd_device="$sd_device" egettxt \
	"vxmksdpart: Device $sd_device is not a standard disk device" \
	vxvmshm:561 >&2
	quit 1
fi

dogi_slice_rawpath $sd_device sd_rawpath
pub_slice_offset=
$PRTVTOC -f $tmpfile1 "$sd_rawpath"
exec 3<&0 < $tmpfile1
while read slice tag flags start size
do
	if [ "X$tag" = "X$VOL_PUB_SLICE_TAG" ]
	then
		pub_slice_offset=$start
	fi
done
exec <&3 3<&-

if [ -z "$pub_slice_offset" ]
then
	sd_device="$sd_device" egettxt \
		"vxmksdpart: Device $sd_device has no public region partition" vxvmshm:560 >&2
	quit 1
fi

sd_disk_offset="`expr $sd_dev_offset + $pub_slice_offset`"

# make sure that there isn't a partition of the number we want that
# is incompatible with the partition we want to make.  If -f was specified,
# then don't bother with this check.

exec 3<&0 < $tmpfile1
while read slice tag flags start size
do
	if [ "X$slice" = "X$sdsliceno" ]
	then
		[ "X$tag" = "X0x0" ] && continue
		if [ -n "$force" ]
		then
			if [ "X$tag" != "X$sdslicetag" ] &&
			   [ "X$tag" != "X$V_USR" ]
			then
				sdsliceno="$sdsliceno" sd_device="$sd_device" egettxt \
  "vxmksdpart: Partition $sdsliceno is not available on device $sd_device" vxvmshm:562 >&2
				quit 1
			fi
		elif [ "X$tag" != "X$sdslicetag" ] ||
		     [ "X$start" != "X$sd_disk_offset" ]
		then
			sdsliceno="$sdsliceno" sd_device="$sd_device" egettxt \
  "vxmksdpart: Partition $sdsliceno is not available on device $sd_device" vxvmshm:562 >&2
			quit 1
		fi
		break
	fi
done
exec <&3 3<&-
#
# Only the real sub disk associated with this vol is passed.
# This sd starts VOL_GHOST_LEN blocks away from the actual partition.
# So reduce the offset and increase the len by VOL_GHOST_LEN
#
if [ $sd_disk_offset -eq $VOL_GHOST_LEN ]
then
	sd_disk_offset="`expr $sd_disk_offset - $VOL_GHOST_LEN`"
	sd_len="`expr $sd_len + $VOL_GHOST_LEN`"
fi
doit vxpartadd "$sd_rawpath" $sdsliceno $sdslicetag $sdsliceflags \
	$sd_disk_offset $sd_len

quit 0
