#!/bin/sh
PVNAME=$1

source /etc/sysconfig/lvm

if [[ x$LVM_ACTIVATED_ON_DISCOVERED == xdisable ]]; then
  echo COLLECT=1
  exit 0
fi

ulimit -n 128
count=0
/sbin/lvm vgs --config "devices { ignore_suspended_devices = 1 scandevice = [ \"$PVNAME\" ] filter = [ \"a|$PVNAME|\", \"r/.*/\" ] }" --partial -o vg_name,vg_attr,vg_uuid,pv_uuid,lv_count,pv_count,pv_name --nolock --ignorelockingfailure --noheadings 2>/dev/null | while read vgname vgattr vguuid pvuuid lvcount pvcount pvname others; do 
  if [[ $vgattr =~ mode. ]]; then
    continue
  fi

  let count=$count+1

  # ignore clustered VG
  if [[ $vgattr =~ .....c ]]; then
    echo COLLECT=1
    exit 0
  fi
  # ignore VG without LV
  if [[ $lvcount = 0 ]]; then
    echo COLLECT=1
    exit 0
  fi
  # uuid of this pv
  if [[ x"$pvname" != xunknown ]]; then
    PVUUID=$pvuuid
    echo PVUUID=$pvuuid
  fi

  pvlist="$pvlist $pvuuid"

  if [[ $count == $pvcount ]]; then
    echo VGUUID=$vguuid
    echo VGNAME=$vgname

    if [[ -n $LVM_VGS_ACTIVATED_ON_BOOT ]]; then
    found=0
    for autovg in $LVM_VGS_ACTIVATED_ON_BOOT; do
      if [[ $vgname == $autovg ]]; then
        found=1
        break
      fi
    done
    if [[ $found == 0 ]]; then
      echo COLLECT=1
      exit 0
    fi
    fi

    collected=0
    for pv in $pvlist; do
    if [[ x$pv = x$PVUUID ]]; then
	  continue
	  fi
	  if [[ ! -h /dev/disk/by-id/lvm2-pvuuid-$pv ]]; then
	  echo COLLECT=1
	  collected=1
	  break
	  fi
    done
  if [[ $collected = 0 ]]; then
  echo COLLECT=0
  fi

  fi
done


