#!/bin/ksh 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003,2006 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)24 1.16 src/avs/fs/mmfs/ts/admin/mmvaryonvg.sh, mmfs, avs_rgpfs24, rgpfs240610b 12/9/05 09:56:42
###################################################################
#
#  Usage:
#    mmvaryonvg [diskName]
#
#  If diskName is passed, find the file system to which the disk
#  belongs and activate all disks that belong to that file system.
#  Otherwise, activate all disks in all file systems in the nodeset.
#
#  This script is used by the daemon's devOpen() function when
#  a disk cannot be opened.
#
#  The script is a no-op in Linux.
#  The script should not be used for VSD type disks.
#
###################################################################

# Include global declarations and service routines.
. /usr/lpp/mmfs/bin/mmglobfuncs
. /usr/lpp/mmfs/bin/mmsdrfsdef
. /usr/lpp/mmfs/bin/mmfsfuncs

[[ -n $DEBUG || -n $DEBUGmmvaryonvg ]] && set -x
$mmTRACE_ENTER "$*"

diskName=""
fsName=""
integer found=0


#######################################################################
#                                                                     #
# This function activates a volume group when disk leasing is in use. #
#                                                                     #
#######################################################################
function activateVg
{
  typeset vgname=$1

  # Activate the disk.
  $varyonvg -u $vgname
  if [[ $? = 0 ]]
  then
    # The varyon of the volume group succeeded.
    printErrorMsg 520 $mmcmd "$(date)" $vgname
  else
    # We were unable to varyon the volume group.
    printErrorMsg 535 $mmcmd "$(date)" $vgname
  fi

}    # ---- end of function activateVg ----------


#####################################################################
#
# Main function
#
#####################################################################

# Get disk name from input, if any.
diskName=$arg1

# Set up trap exception handling.
trap pretrap2 HUP INT QUIT KILL

# Get the node number for this node.
[[ -z $MMMODE ]] && determineMode
getLocalNodeData

# This function is not needed in Linux.
[[ $osName = Linux ]] &&  \
  cleanupAndExit 0

# Get a list containing the physical disk name, pvid, and vgname
# for all known volume groups on this node.
LC_ALL=C $lspv > $volGroupFile

# Get the name of the filesystem to which the disk belongs.
if [[ -n $diskName ]]
then
  fsName=$($mmcommon getSGDevice $diskName)
fi

# Create a temp file containing the disks (along with their
# types, subtypes, pvids, and name sources) to be varied on.
getDiskData $tmpfile $fsName


exec 3<&-
exec 3< $tmpfile
while read -u3 diskLine
do
  # Extract the lvname, disk type, disk subtype, pvid, name source,
  # and underlying lv name (if applicable) for each disk.
  IFS=':'
  set -f ; set -- $diskLine ; set +f
  lvname=$1
  diskType=$2
  diskSubtype=$3
  pvid=$4
  nameSource=$5
  nsdSubtype=$6
  nsdSubtypeDiskname=$7
  IFS="$IFS_sv"

  # If the underlying disk is not a logical volume, skip it.
  [[ $nsdSubtype != lv ]] && continue

  # This is an nsd with an underlying logical volume;
  # use that logical volume name.
  [[ -n $nsdSubtypeDiskname ]] && lvname=$nsdSubtypeDiskname

  # Try to determine the vg name for this logical volume.
  vgname=""
  if [[ $nameSource = $userSpecified ]]
  then
    # The disk name was specified by the user; in this case,
    # obtain the vgname by appending "vg" to the lvname.
    vgname=${lvname}vg
  elif [[ $lvname = gpfs*lv ]]
  then
    # The disk name appears to have been created by mmcrlv;
    # obtain the vgname by changing the "lv" suffix to "vg".
    vgname=${lvname%lv}vg
  else
    # The disk was not created by mmcrlv;
    # try to obtain the vgname by using the lslv command.
    vgname=$(LC_ALL=C $lslv -L $lvname 2>/dev/null | $awk '/VOLUME GROUP:/ {print $6}')
  fi

  # If the vg name is not known, skip the disk.
  # Note that further down we will try one more time to process the input disk.
  if [[ -z $vgname ]]
  then
    [[ $diskName != $lvname ]] &&  \
      printErrorMsg 547 $mmcmd $lvname $ourNodeName
    continue
  fi

  # If the disk is not known on this node, try to import it.
  lspvLine=$($grep -w $vgname $volGroupFile)
  if [[ $? = 1 ]]
  then
    importDisk $lvname $vgname $pvid $volGroupFile
    # If we couldn't make the disk known on this node, skip to the next disk.
    if [[ $? = 1 ]]
    then
      printErrorMsg 547 $mmcmd $lvname $ourNodeName
      continue
    fi
    lspvLine=$($grep -w $vgname $volGroupFile)
  fi

  # Use the lslv command to check whether the volume group is active or not.
  vgstate=$(LC_ALL=C $lslv -L $lvname 2>/dev/null |  \
     $awk '/VG STATE:/ { {split($3,state,"/")} {print state[1]} }')

  [[ $diskName = $lvname ]] && found=1

  # Activate the volume group if needed.
  [[ $vgstate != "active" ]] && activateVg $vgname
done

# If a disk was passed but it was not found in the mmsdrfs file,
# then try to activate that disk also.  We will not be able to handle
# the case where the disk has not been imported yet, since we have no way
# to obtain the pvid of the disk if it is not listed in the mmsdrfs file.
if [[ -n $diskName && $found = 0 ]]
then
  # Determine the name of the volume group and whether it is active or not.
  lslvLine=$(LC_ALL=C $lslv -L $diskName 2>/dev/null | \
    $awk '                                            \
      /VOLUME GROUP:/  { group = $6 }                 \
      /VG STATE:/      { split($3, state, "/") }      \
      END              { print group " " state[1] }   \
    ')
  set -f ; set -- $lslvLine ; set +f
  vgname=$1
  vgstate=$2
  if [[ -n $vgstate ]]
  then
    # Activate the volume group if needed.
    [[ $vgstate != "active" ]] && activateVg $vgname
  else
    print -u2 "Disk $diskName is not known on node $ourNodeName."
  fi  # end if [[ -n $vgstate ]]
fi  # end if [[ -n $diskName && $found = 0 ]]

# Always return zero.
cleanupAndExit 0

