Saturday, 11 February 2012

LVM SCRIPT

{p06701csmp03}/software/scripts/IDD$ cat lvm.ksh
#!/usr/bin/ksh
######################################################################################################
#
#  Name: lvm.ksh
#
#  Purpose: To Create VG, LV, FS for both standalone and HACMP systems (excluding raw LV & ASM disks)
#
#  Authors: Prakash Rajendran
#
#  Change history
#
#      Date    | Author       | Version| Reason
#    ----------+--------------+--------+--------------------------
#    01/04/11  | PR           | 1.0    | Initial Version
#
#  More information can be found in the documentation as per below link:
#
#
#  NOTES:
#  This script is used to create VG, LV, FS on both standalone & HA systems with LBG standards
#
#
#######################################################################################################
# Common Variables
HOST=$(hostname)
MAP_FILE=/tmp/${HOST}.map
LOG_FILE=/tmp/lvm.log.`date +"%d%h%y-%H:%M"`
ERROR="ERROR: $(hostname) : `date +"%H:%M:%S"` :"
INFO="INFO : $(hostname) : `date +"%H:%M:%S"` :"
DEBUG=$2
# Variables for HA nodes
if [ -s /usr/es/sbin/cluster/clstat ] ; then
cl_util=/usr/es/sbin/cluster/utilities
cl_spoc=/usr/es/sbin/cluster/sbin
NODE_A=$($cl_util/cllsnode |grep NODE |awk '{print $2}' |cut -d: -f1 |head -1)
NODE_B=$($cl_util/cllsnode |grep NODE |awk '{print $2}' |cut -d: -f1 |tail -1)
fi
echo "$INFO Running script lvm.ksh" |tee $LOG_FILE
function vg_create
{
# Debugging check
if [ ! -z $DEBUG ] && [ $DEBUG = -d ] ; then
set -x
fi
echo "$INFO Calling Module vg" |tee $LOG_FILE
if [ -s $MAP_FILE ]; then
   vg_cnt=`grep  -p ^vg $MAP_FILE |sed 's/^$/d/' |grep -v ^vg |wc -l`
      if [ $vg_cnt -gt 0 ] ; then
         grep  -v "#" $MAP_FILE |grep -p ^vg |sed '$d' |grep -v ^vg |while read line
         do
         rm /tmp/asm_disks > /dev/null 2>&1 ; rm /tmp/hdisks > /dev/null 2>&1 ; rm /tmp/active_hdisks > /dev/null 2>&1
         touch /tmp/asm_disks ; touch /tmp/active_hdisks
         echo $line |awk -F":" '{print $1}' |read VG_NAME
         echo $line |awk -F":" '{print $2}' |read MGR_NUM
         echo $line |awk -F":" '{print $3}' |read MIRROR
         echo $line |awk -F":" '{print $4}' |read HDISK
         echo $line |awk -F":" '{print $4}' |tr ' ' '\n' >> /tmp/hdisks
         print "$INFO Processing line '$line'" |tee $LOG_FILE
         for PV in `cat /tmp/hdisks`
            do
               MAJ=`ls -ltr /dev |sed 's/,/ /'|grep -w $PV|awk '{print $5}'`
               MIN=`ls -ltr /dev |sed 's/,/ /'|grep -w $PV|awk '{print $6}'`
               ASM=`ls -ltr /dev |sed 's/,/ /'|awk -v MJ="$MAJ" '($5 == MJ) {print $0}'|awk -v MN="$MIN" '($6 == MN) {print $10}'`
               ASM_NEW=`echo "$ASM"|sed -e "s/^$PV//" -e "s/^r$PV//"`
               if [ ! -z $ASM_NEW ] ; then
                  echo $ASM_NEW >> /tmp/asm_disks
               fi
               lspv |grep -v None |grep -w $PV >> /tmp/active_hdisks
               if lspv |grep -w $PV > /dev/null 2>&1 ; then
                  hcheck=`lsattr -El $PV |grep hcheck_interval |awk '{print $2}'`
                     if [ $hcheck -ne 60 ] ; then
                        print "\033[1;31m$ERROR Attribute hcheck_interval is not set to 60 for $PV\033[m" |tee -a $LOG_FILE
                        return
                     fi
               else
                  print "\033[1;31m$ERROR $PV does not exist in the system\033[m" |tee -a $LOG_FILE
                  return
               fi
            done
               if lsvg |grep -w $VG_NAME > /dev/null 2>&1 ; then
                  print "\033[1;31m$ERROR $VG_NAME already present & will be ignored\033[m" |tee -a $LOG_FILE
               else
               if ls -lrt /dev |awk '{print $5}' |cut -d, -f1 |sort -u |grep $MGR_NUM > /dev/null 2>&1 ; then
               print "\033[1;31m$ERROR Major Number $MGR_NUM already used. Select another major number for $VG_NAME and try again\033[m" |tee -a $LOG_FILE
               else
               if [ -s /tmp/active_hdisks ]  || [ -s /tmp/asm_disks ] ; then
                  print "\033[1;31m$ERROR Some of the hdisks are part of existing VG or used as ASM disks. Correct them and retry\033[m" |tee -a $LOG_FILE
               else
               if echo $line |awk -F":" '{print $1}' |grep ^lo_ > /dev/null 2>&1; then
                  print "$INFO Creating Volume Group $VG_NAME" |tee -a $LOG_FILE
                  print "$INFO mkvg -B -s 256 -y $VG_NAME -V $MGR_NUM $HDISK" |tee -a $LOG_FILE
                  mkvg -B -s 256 -y $VG_NAME -V $MGR_NUM $HDISK > /dev/null 2>&1
                     if [ $? = 0 ] ; then
                        print "\033[1;32m$INFO Successfully created $VG_NAME\033[m" |tee -a $LOG_FILE
                        if  [ $MIRROR = y ] ; then
                           print "$INFO Changing Quorum Settings for $VG_NAME" |tee -a $LOG_FILE
                           chvg -Qn $VG_NAME > /dev/null 2>&1
                        fi
                     else
                        print "\033[1;31m$ERROR VG Creation failed\033[m" |tee -a $LOG_FILE
                     fi
               else
                  print "\033[1;31m$ERROR $VG_NAME is not starting with lo_ as this is local VG\033[m" |tee -a $LOG_FILE
               fi
               fi
               fi
               fi
        done
      else
      print "\033[1;31m$ERROR VG details not found on $MAP_FILE\033[m" |tee -a $LOG_FILE
      fi
else
print "\033[1;31m$ERROR Input file $MAP_FILE does not exist\033[m" |tee -a $LOG_FILE
fi
}
function lv_create
{
# Debugging check
if [ ! -z $DEBUG ] && [ $DEBUG = -d ] ; then
set -x
fi
echo "$INFO Calling Module lv" |tee $LOG_FILE
if [ -s $MAP_FILE ]; then
   lv_cnt=`grep  -p ^lv $MAP_FILE |sed '/^$/d' |grep -v ^lv |wc -l`
      if [ $lv_cnt -gt 0 ] ; then
      grep  -v "#" $MAP_FILE |grep -p ^lv |sed '/^$/d' |grep -v ^lv |while read lv_line
      do
         rm /tmp/lv_hdisks > /dev/null 2>&1
         echo $lv_line |awk -F":" '{print $1}' |read LV_VG_NAME
         echo $lv_line |awk -F":" '{print $2}' |read LV_NAME
         echo $lv_line |awk -F":" '{print $3}' |read NO_LVS
         echo $lv_line |awk -F":" '{print $4}' |read NO_COPIES
         echo $lv_line |awk -F":" '{print $5}' |read MNT_PT
         echo $lv_line |awk -F":" '{print $6}' |read SITE_A
         echo $lv_line |awk -F":" '{print $7}' |read SITE_B
         echo $lv_line |awk -F":" '{print $6}' |tr ' ' '\n' |sort -u >> /tmp/lv_hdisks ; cp -p /tmp/lv_hdisks /tmp/lv_hdisks_sitea
         echo $lv_line |awk -F":" '{print $7}' |tr ' ' '\n' |sort -u |tee /tmp/lv_hdisks_siteb >> /tmp/lv_hdisks
         print "$INFO Processing line '$lv_line' " |tee -a $LOG_FILE
         for PV in `cat /tmp/lv_hdisks`
            do
               if lsvg |grep $LV_VG_NAME  > /dev/null  2>&1 ; then
                  lsvg -o |grep $LV_VG_NAME > /dev/null  2>&1
                  if [ $? -ne 0 ] ; then
                     print "\033[1;31m$ERROR The volume group $LV_VG_NAME is not varied on\033[m" |tee -a $LOG_FILE
                     return
                  fi
               else
               print "\033[1;31m$ERROR The volume group $LV_VG_NAME does not exist\033[m" |tee -a $LOG_FILE
               return
               fi
               if lspv |grep -w $PV > /dev/null 2>&1 ; then
                  lsvg -Lp $LV_VG_NAME |grep -w $PV  > /dev/null 2>&1
                     if [ $? -ne 0 ] ; then
                        print "\033[1;31m$ERROR $PV does not belong to $LV_VG_NAME\033[m" |tee -a $LOG_FILE
                        return
                     fi
               else
                  print "\033[1;31m$ERROR $PV does not exist in the system\033[m" |tee -a $LOG_FILE
                  return
               fi
            done
            CNT_A=0
            for A_ in `cat /tmp/lv_hdisks_sitea`
            do
            lsvg -Lp $LV_VG_NAME |grep $A_ |awk '{print $4}' |read SIZE
            CNT_A=`expr $CNT_A + $SIZE`
            done
            CNT_B=0
            for B_ in `cat /tmp/lv_hdisks_siteb`
            do
            lsvg -Lp $LV_VG_NAME |grep $B_ |awk '{print $4}' |read SIZE
            CNT_B=`expr $CNT_B + $SIZE`
            done
            if [ $NO_COPIES -eq 2 ] ; then
               INT=1
               PVLIST=" "
               ADISKS=`cat /tmp/lv_hdisks_sitea|wc -l`
               BDISKS=`cat /tmp/lv_hdisks_siteb|wc -l`
               [ "$ADISKS" -ge "$BDISKS" ] && CHECK=$ADISKS || CHECK=$BDISKS
               until [ "$INT" -gt "$CHECK" ];
                 do
                 PV=`cat -n /tmp/lv_hdisks_sitea|grep -w $INT|awk '{print $2}'`
                 PV1=`cat -n /tmp/lv_hdisks_siteb|grep -w $INT|awk '{print $2}'`
                 PVLIST="$PVLIST $PV $PV1"
                 INT=`expr $INT + 1`
                 done
               CMD="mklv -y $LV_NAME -t jfs2 -c$NO_COPIES -ss $LV_VG_NAME $NO_LVS $PVLIST"
               if [ $CNT_A -gt $NO_LVS ] ; then
                  if [ $CNT_B -lt $NO_LVS ] ; then
                     print "\033[1;31m$ERROR There are not enough space in SITE B\033[m" |tee -a $LOG_FILE
                     return
                     fi
                  else
                     print "\033[1;31m$ERROR There are not enough space in SITE A\033[m" |tee -a $LOG_FILE
                     return
                  fi
            else
               CMD="mklv -y $LV_NAME -t jfs2 -c$NO_COPIES $LV_VG_NAME $NO_LVS $SITE_A"
               if [ $CNT_A -lt $NO_LVS ] ; then
                  print "\033[1;31m$ERROR There are not enough space to create the Filesystem $MNT_PT\033[m" |tee -a $LOG_FILE
                  return
               fi
            fi
          LV_COUNT=`echo $LV_NAME |wc -c`
          LV_COUNT=`expr $LV_COUNT - 1`
          if [ $LV_VG_NAME == rootvg ] ; then
             if [ $LV_COUNT -gt 11 ] ; then
                print "\033[1;31m$ERROR The LV $LV_NAME is exceeding 11 characters limit. Reduce no.of characters and try again\033[m" |tee -a $LOG_FILE
                return
             fi
          fi
          lsvg -o |lsvg -iLl |grep -w $LV_NAME  > /dev/null  2>&1
          if [[ $? -eq 0 ]] ; then
             print "\033[1;31m$ERROR The LV $LV_NAME is already present. Give another LV name\033[m" |tee -a $LOG_FILE
          else
          cat /etc/filesystems |grep -w $MNT_PT":" >/dev/null 2>&1
          if [[ $? -eq 0 ]] ; then
             print "\033[1;31m$ERROR The mount point $MNT_PT is already in use. Try another mount point\033[m" |tee -a $LOG_FILE
          else
          print "$INFO Creating Logical Volume $LV_NAME" |tee -a $LOG_FILE
          $CMD > /dev/null 2>&1
          if [ $? = 0 ] ; then
             print "$INFO Creating File system $MNT_PT" |tee -a $LOG_FILE
             crfs -v jfs2 -d $LV_NAME -a logname=INLINE -m $MNT_PT -Ay -t yes > /dev/null 2>&1
             if [ $? = 0 ] ; then
                print "$INFO Mounting file system $MNT_PT" |tee -a $LOG_FILE
                mount $MNT_PT > /dev/null 2>&1
                if [ $? = 0 ] ; then
                   print "\033[1;32m$INFO Successfully created file system $MNT_PT\033[m" |tee -a $LOG_FILE
                   print "\033[1;32m$INFO Please make sure to set the permission:ownership for $MNT_PT\033[m" |tee -a $LOG_FILE
                fi
             else
                print "\033[1;31m$ERROR File system creation failed for $MNT_PT\033[m" |tee -a $LOG_FILE
             fi
          else
             print "\033[1;31m$ERROR LV creation failed for $LV_NAME\033[m" |tee -a $LOG_FILE
          fi
          fi
          fi
      done
      else
         print "\033[1;31m$ERROR LV details not found on $MAP_FILE\033[m" |tee -a $LOG_FILE
      fi
else
print "\033[1;31m$ERROR Input file $MAP_FILE does not exist\033[m" |tee -a $LOG_FILE
fi
}
function havg_create
{
# Debugging check
if [ ! -z $DEBUG ] && [ $DEBUG = -d ] ; then
set -x
fi
echo "\033[1;31mAre you sure newly assigned LUNs are having PVID & added them to specific sites in HA config & synced ? [y/n] :\033[m"
until [ "$rep" == "y" -o "$rep" == "n" ];do
   echo "Reply: \c"
   read rep
done
echo "$INFO Calling Module havg" |tee $LOG_FILE
if [ "$rep" == "y" ] ; then
   if [ -s /usr/es/sbin/cluster/clstat ] ; then
      if [ -s $MAP_FILE ]; then
         havg_cnt=`grep  -p ^havg $MAP_FILE |sed 's/^$/d/' |grep -v ^havg |wc -l`
         if [ $havg_cnt -gt 0 ] ; then
            grep  -v "#" $MAP_FILE |grep -p ^havg |sed '$d' |grep -v ^havg |while read line
            do
               rm /tmp/asm_disks > /dev/null 2>&1 ; rm /tmp/hdisks > /dev/null 2>&1 ; rm /tmp/active_hdisks > /dev/null 2>&1
               touch /tmp/asm_disks ; touch /tmp/active_hdisks
               echo $line |awk -F":" '{print $1}' |read VG_NAME
               echo $line |awk -F":" '{print $2}' |read MGR_NUM
               echo $line |awk -F":" '{print $4}' |read HDISK
               echo $line |awk -F":" '{print $4}' |tr ' ' '\n' >> /tmp/hdisks
               echo $line |awk -F":" '{print $3}' |read MIRROR
               print "$INFO Processing line '$line'" |tee -a $LOG_FILE
               for PV in `cat /tmp/hdisks`
               do
                  MAJ=`ls -ltr /dev |sed 's/,/ /'|grep -w $PV|awk '{print $5}'`
                  MIN=`ls -ltr /dev |sed 's/,/ /'|grep -w $PV|awk '{print $6}'`
                  ASM=`ls -ltr /dev |sed 's/,/ /'|awk -v MJ="$MAJ" '($5 == MJ) {print $0}'|awk -v MN="$MIN" '($6 == MN) {print $10}'`
                  ASM_NEW=`echo "$ASM"|sed -e "s/^$PV//" -e "s/^r$PV//"`
                  if [ ! -z $ASM_NEW ] ; then
                     echo $ASM_NEW >> /tmp/asm_disks
                  fi
                  lspv |grep -v None |grep -w $PV >> /tmp/active_hdisks
                  if lspv |grep -w $PV > /dev/null 2>&1 ; then
                     hcheck=`lsattr -El $PV |grep hcheck_interval |awk '{print $2}'`
                     if [ $hcheck -ne 60 ] ; then
                        print "\033[1;31m$ERROR Attribute hcheck_interval is not set to 60 for $PV\033[m" |tee -a $LOG_FILE
                        return
                     fi
                  else
                     print "\033[1;31m$ERROR $PV does not exist in the system\033[m" |tee -a $LOG_FILE
                     return
                  fi
               done
                  if lsvg |grep -w $VG_NAME > /dev/null 2>&1 ; then
                     print "$INFO $VG_NAME already present & will be ignored" |tee -a $LOG_FILE
                  else
                  if ls -lrt /dev |awk '{print $5}' |cut -d, -f1 |sort -u |grep $MGR_NUM > /dev/null 2>&1 ; then
                  print "\033[1;31m$ERROR Major Number $MGR_NUM already used. Select another major number for $VG_NAME and try again\033[m" |tee -a $LOG_FILE
                  else
                  if [ -s /tmp/active_hdisks ]  || [ -s /tmp/asm_disks ] ; then
                    print "\033[1;31m$ERROR Some of the hdisks are part of existing VG or used as ASM disks. Correct them and retry\033[m"  |tee -a $LOG_FILE
                  else
                  if echo $line |awk -F":" '{print $1}' |grep ^sh_ > /dev/null 2>&1; then
                     print "$INFO Creating Volume Group $VG_NAME" |tee -a $LOG_FILE
                     $cl_spoc/cl_mkvg -cspoc "-n$NODE_A,$NODE_B" -C -B -s 256 -l true -V $MGR_NUM -y $VG_NAME $HDISK > /dev/null 2>&1
                     if [ $? = 0 ] ; then
                        print "\033[1;32m$INFO Successfully created $VG_NAME\033[m" |tee -a $LOG_FILE
                        print "\033[1;32m$INFO Please make sure to add VG $VG_NAME into HACMP RG configuration and sync\033[m" |tee -a $LOG_FILE
                        if [ $MIRROR = y ] ; then
                           $cl_spoc/cl_chvg -cspoc "-n$NODE_A,$NODE_B" -Qn $VG_NAME  > /dev/null 2>&1
                        fi
                     else
                     print "\033[1;31m$ERROR VG creation failed for $VG_NAME\033[m" |tee -a $LOG_FILE
                     fi
                  else
                     print "\033[1;31m$ERROR $VG_NAME is not starting with sh_ as this is shared VG\033[m" |tee -a $LOG_FILE
                  fi
                  fi
                  fi
                  fi
            done
         else
         print "\033[1;31m$ERROR VG details not found on $MAP_FILE\033[m" |tee -a $LOG_FILE
         fi
      else
      print "\033[1;31m$ERROR Input file $MAP_FILE does not exist\033[m" |tee -a $LOG_FILE
      fi
   else
   print "\033[1;31m$ERROR This is not a HACMP node\033[m" |tee -a $LOG_FILE
   fi
else
continue
fi
}
function halv_create
{
# Debugging check
if [ ! -z $DEBUG ] && [ $DEBUG = -d ] ; then
set -x
fi
echo "\033[1;31mAre you sure newly assigned LUNs are having PVID & added them to specific sites in HA config & synced ? [y/n] :\033[m"
rep=0
until [ "$rep" == "y" -o "$rep" == "n" ];do
   echo "Reply: \c"
   read rep
done
echo "$INFO Calling Module halv" |tee $LOG_FILE
if [ "$rep" == "y" ] ; then
   if [ -s /usr/es/sbin/cluster/clstat ] ; then
      if [ -s $MAP_FILE ]; then
         halv_cnt=`grep  -p ^halv $MAP_FILE |sed '/^$/d' |grep -v ^halv |wc -l`
         if [ $halv_cnt -gt 0 ] ; then
            grep  -v "#" $MAP_FILE |grep -p ^halv |sed '/^$/d' |grep -v ^halv |while read halv_line
            do
               rm /tmp/halv_hdisks > /dev/null 2>&1
               echo $halv_line |awk -F":" '{print $1}' |read LV_VG_NAME
               echo $halv_line |awk -F":" '{print $2}' |read LV_NAME
               echo $halv_line |awk -F":" '{print $3}' |read NO_LVS
               echo $halv_line |awk -F":" '{print $4}' |read NO_COPIES
               echo $halv_line |awk -F":" '{print $5}' |read MNT_PT
               echo $halv_line |awk -F":" '{print $6}' |read SITE_A
               echo $halv_line |awk -F":" '{print $7}' |read SITE_B
               echo $halv_line |awk -F":" '{print $6}' |tr ' ' '\n' |sort -u >> /tmp/halv_hdisks ; cp -p /tmp/halv_hdisks /tmp/halv_hdisks_sitea
               echo $halv_line |awk -F":" '{print $7}' |tr ' ' '\n' |sort -u |tee /tmp/halv_hdisks_siteb >> /tmp/halv_hdisks
               print "$INFO Processing line '$halv_line' " |tee -a $LOG_FILE
               for PV in `cat /tmp/halv_hdisks`
                  do
                     if lsvg |grep $LV_VG_NAME  > /dev/null  2>&1 ; then
                        lsvg -o |grep $LV_VG_NAME > /dev/null  2>&1
                        if [ $? -ne 0 ] ; then
                           print "\033[1;31m$ERROR The volume group $LV_VG_NAME is not varied on as active\033[m" |tee -a $LOG_FILE
                           return
                        fi
                     else
                     print "\033[1;31m$ERROR The volume group $LV_VG_NAME does not exist\033[m" |tee -a $LOG_FILE
                     return
                     fi
                     if lspv |grep -w $PV > /dev/null 2>&1 ; then
                        lsvg -Lp $LV_VG_NAME |grep -w $PV  > /dev/null 2>&1
                        if [ $? -ne 0 ] ; then
                           print "\033[1;31m$ERROR $PV does not belong to $LV_VG_NAME\033[m" |tee -a $LOG_FILE
                           return
                        fi
                     else
                        print "\033[1;31m$ERROR $PV does not exist in the system\033[m" |tee -a $LOG_FILE
                        return
                     fi
                  done
                  CNT_A=0
                  for A_ in `cat /tmp/halv_hdisks_sitea`
                  do
                  lsvg -Lp $LV_VG_NAME |grep $A_ |awk '{print $4}' |read SIZE
                  CNT_A=`expr $CNT_A + $SIZE`
                  done
                  CNT_B=0
                  for B_ in `cat /tmp/halv_hdisks_siteb`
                  do
                  lsvg -Lp $LV_VG_NAME |grep $B_ |awk '{print $4}' |read SIZE
                  CNT_B=`expr $CNT_B + $SIZE`
                  done
                  if [ $NO_COPIES -eq 2 ] ; then
                     INT=1
                     PVLIST=" "
                     ADISKS=`cat /tmp/halv_hdisks_sitea|wc -l`
                     BDISKS=`cat /tmp/halv_hdisks_siteb|wc -l`
                     [ "$ADISKS" -ge "$BDISKS" ] && CHECK=$ADISKS || CHECK=$BDISKS
                     until [ "$INT" -gt "$CHECK" ];
                        do
                        PV=`cat -n /tmp/halv_hdisks_sitea|grep -w $INT|awk '{print $2}'`
                        PV1=`cat -n /tmp/halv_hdisks_siteb|grep -w $INT|awk '{print $2}'`
                        PVLIST="$PVLIST $PV $PV1"
                        INT=`expr $INT + 1`
                        done
                     CMD="$cl_spoc/cl_mklv -cspoc "-n$NODE_A,$NODE_B" -R$HOST -tjfs2 -c$NO_COPIES -ss -y$LV_NAME $LV_VG_NAME $NO_LVS $PVLIST"
                     if [ $CNT_A -gt $NO_LVS ] ; then
                        if [ $CNT_B -lt $NO_LVS ] ; then
                           print "\033[1;31m$ERROR There are not enough space in SITE B\033[m" |tee -a $LOG_FILE
                           return
                        fi
                     else
                        print "\033[1;31m$ERROR There are not enough space in SITE A\033[m" |tee -a $LOG_FILE
                        return
                     fi
                  else
                     CMD="$cl_spoc/cl_mklv -cspoc "-n$NODE_A,$NODE_B" -R$HOST -tjfs2 -c$NO_COPIES -y$LV_NAME $LV_VG_NAME $NO_LVS $SITE_A"
                     if [ $CNT_A -lt $NO_LVS ] ; then
                        print "\033[1;31m$ERROR There are not enough space to create the Filesystem $MNT_PT\033[m" |tee -a $LOG_FILE
                        return
                     fi
                  fi
                  LV_COUNT=`echo $LV_NAME |wc -c`
                  LV_COUNT=`expr $LV_COUNT - 1`
                  if [ $LV_VG_NAME == rootvg ] ; then
                     if [ $LV_COUNT -gt 11 ] ; then
                        print "\033[1;31m$ERROR LV $LV_NAME is exceeding 11 characters limit. Reduce no.of characters and try again\033[m" |tee -a $LOG_FILE
                        return
                     fi
                  fi
                  lsvg -o |lsvg -iLl |grep -w $LV_NAME  > /dev/null  2>&1
                  if [[ $? -eq 0 ]] ; then
                     print "\033[1;31m$ERROR The LV $LV_NAME is already present. Give another LV name\033[m" |tee -a $LOG_FILE
                  else
                  cat /etc/filesystems |grep -w $MNT_PT":" >/dev/null 2>&1
                  if [[ $? -eq 0 ]] ; then
                     print "\033[1;31m$ERROR The mount point $MNT_PT is already in use. Try another mount point\033[m" |tee -a $LOG_FILE
                  else
                  print "$INFO Creating Logical Volume $LV_NAME" |tee -a $LOG_FILE
                  $CMD > /dev/null 2>&1
                  if [ $? = 0 ] ; then
                     print "$INFO Creating File system $MNT_PT" |tee -a $LOG_FILE
                     $cl_spoc/cl_crfs -cspoc "-n$NODE_A,$NODE_B" -v jfs2 -d $LV_NAME -a logname=INLINE -m $MNT_PT -t yesstr > /dev/null 2>&1
                     if [ $? = 0 ] ; then
                        print "\033[1;32m$INFO Successfully created file system $MNT_PT\033[m" |tee -a $LOG_FILE
                        print "\033[1;32m$INFO Please make sure to set the permission:ownership for $MNT_PT\033[m" |tee -a $LOG_FILE
                     else
                        print "\033[1;31m$ERROR Shared FS creation failed for $MNT_PT\033[m" |tee -a $LOG_FILE
                     fi
                  else
                  print "$\033[1;31m$ERROR Shared LV creation failed for $LV_NAME\033[m" |tee -a $LOG_FILE
                  fi
                  fi
                  fi
            done
         else
         print "\033[1;31m$ERROR LV details not found on $MAP_FILE\033[m" |tee -a $LOG_FILE
         fi
      else
      print "\033[1;31m$ERROR Input file $MAP_FILE does not exist\033[m" |tee -a $LOG_FILE
      fi
   else
   print "\033[1;31m$ERROR This is not a HACMP node\033[m" |tee -a $LOG_FILE
   fi
else
continue
fi
}
function syntax
{
        echo "Usage: lvm.ksh <argument>"
        Echo "Valid arguments : vg, lv, havg, halv, ALL"
}

case $1 in
        vg)
        vg_create
                ;;
        lv)
        lv_create
                ;;
        havg)
        havg_create
                ;;
        halv)
        halv_create
                ;;
        ALL)
        vg_create; lv_create; havg_create; halv_create
                ;;
        *)
        syntax
                ;;
esac

Friday, 10 February 2012

LINUX USEFUL LINKS

SNAPSHOT LINUX LV

LVM Snapshots
Snapshot Creation
All Pages

LVM Snapshots Overview


Continuing with our LVM how to series, in this KB, we will build on the knowledge from our LVM Configuration KB, and explore logical volume (LV) snapshots. We will show you how to create a LV snapshot which can then be mounted for a backup or some other purpose.
Linux administrators typically use LV snapshots for backups. Often times, running a backup can take anywhere from a couple of minutes to several hours (depending on the amount of data to backup.) Depending on how busy your system, from the time the backup begins to the time it completes, some of your files could have changed with omissions to open files. Snapshots provide a means of obtaining a consistent volume (free of changes) in a matter of seconds. The snapshot (which is a exact copy of the LV taken at the time the snapshot command was run) can then be backed-up without the worry of changes to the file system or need to shutdown running databases or close open files.
LVM snapshots can only be taken from logical volumes. Non LV partitions cannot have a snapshot taken of them.
Snapshots require only a fraction of the space required by the source LV. Behind the scenes, when a snapshot is taken, the source LV is frozen while the changes to the LV are written to the LV snapshot. Thus, the size of the snapshot only needs to be large enough for the anticipated changes to the file system over the lifetime of the snapshot. So in other words, the size of the source LV has no bearing on the size requirements for the snapshot LV, but rather how many changes will be made to the source LV throughout the lifetime of the existence of the snapshot LV.
Once the snapshot is removed, the changes logged in the LV snapshot are written back to the source LV. Should a snapshot LV run out of space, the snapshot will be released.
One cautionary note with LV snapshots, they are I/O intensive. I have seen I/O performance hits by more than 50% of the snapshot LV while running with a snapshot. Therefore, you should ensure that you do not run with a snapshot LV any longer than absolutely necessary.



Snapshot Creation


Creating a logical volume (LV) snapshot is much the same process as creating a LV (see our LVM Configuration KB for details on how to create a LV.) However, when creating a LV snapshot you must use a -s command line switch in conjunction with the LV you wish to snapshot. The syntax is as follows:
  • lvcreate -L <SIZE_OF_SNAPSHOT> -s -n <NAME_OF_SNAPSHOT> <LV_TO_SNAPSHOT>
In the following example, we will create a 500MB snapshot LV of an existing LV, lets being:
  1. Use lvdisplay to find the name of the logical volume you wish to snapshot

    [root@Linux01 ~]# lvdisplay
    --- Logical volume ---
    LV Name/dev/TCPDumpVolGRP/TCPDumpLV
    VG NameTCPDumpVolGRP
    LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Accessread/write
    LV Statusavailable
    # open1
    LV Size1.50 GB
    Current LE48
    Segments1
    Allocationinherit
    Read ahead sectorsauto
    - currently set to256
    Block device253:5

    ... OUTPUT TRUNCATED
  2. Create a new 500MB snapshot from the source LV /dev/TCPDumpVolGRP/TCPDumpLV

    [root@Linux01 /]# lvcreate -L 500M -s -n BackupLV /dev/TCPDumpVolGRP/TCPDumpLV
    Rounding up size to full physical extent 512.00 MB
    Logical volume "BackupLV" created
    [root@Linux01 ~]#
    Note: Our snapshot LV (500MB) is about 1/3 the size of the source LV (1.5 GB). As we have already said, the snapshot LV does not need to be the same size of the source. Because the snapshot LV will only contain the changes made to the source LV while its snapshot, and we know there is not a high rate of change on the source LV, we are fine using 1/3 the size (and would have probably been safe making it far less.)
  3. Verify the snapshot has been created

    [root@Linux01 ~]# lvdisplay
    --- Logical volume ---
    LV Name/dev/TCPDumpVolGRP/TCPDumpLV
    VG NameTCPDumpVolGRP
    LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Accessread/write
    LV Statusavailable
    # open1
    LV Size1.50 GB
    Current LE48
    Segments1
    Allocationinherit
    Read ahead sectorsauto
    - currently set to256
    Block device253:5

    --- Logical volume ---
    LV Name/dev/TCPDumpVolGRP/BackupLV
    VG NameTCPDumpVolGRP
    LV UUIDmSWMF0-5JtO-GkAd-plBb-YIf8-1HOg-JRfV34
    LV Write Accessread/write
    LV snapshot statusactive destination for /dev/TCPDumpVolGRP/TCPDumpLV
    LV Statusavailable
    # open0
    LV Size1.50 GB
    Current LE48
    COW-table size512.00 MB
    COW-table LE16
    Allocated to snapshot0.00%
    Snapshot chunk size4.00 KB
    Segments1
    Allocationinherit
    Read ahead sectorsauto
    - currently set to256
    Block device253:7
    ... OUTPUT TRUNCATED
    Note: You will notice on the BackupLV that although the LV size says that it's 1.5GB, the copy-on-write (COW) table informs us that it's actually only 512MB (the source LV is 1.5GB.) Also, the percentage allocated to the snapshot is currently at 0%. As updates are made to the source LV, you'll notice this percentage will increase.
  4. We can now mount the snapshot so that it may be backed-up

    [root@Linux01 /]# mkdir -p /mnt/backup
    [root@Linux01 /]# mount /dev/TCPDumpVolGRP/BackupLV /mnt/backup/
    [root@Linux01 /]# df -kh

    FilesystemSizeUsedAvailUse%Mounted on
    /dev/mapper/VolGroup00-LogVol043.9G2.2G1.6G59%/
    /dev/sda199M12M82M13%/boot
    tmpfs1006M01006M0%/dev/shm
    /dev/mapper/VolGroup00-LogVol00992M41M901M5%/home
    /dev/mapper/VolGroup00-LogVol02992M69M872M8%/tmp
    /dev/mapper/VolGroup00-LogVol032.0G150M1.7G8%/var
    /dev/mapper/TCPDumpVolGRP-4GLV4.0G137M3.7G4%/4GLV
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV1.5G920M497M65%/TCPDumpLV
    /dev/mapper/TCPDumpVolGRP-BackupLV1.5G920M497M65%/mnt/backup
  5. Once the snapshot has been backed-up, unmount the LV and remove it

    [root@Linux01 /]# umount /mnt/backup/
    [root@Linux01 /]# lvremove /dev/mapper/TCPDumpVolGRP-BackupLV
    Do you really want to remove active logical volume "BackupLV"? [y/n]: y
    Logical volume "BackupLV" successfully removed
    [root@Linux01 /]#
Nice work, you now know how-to snapshot a LV. Good luck!

SHRINK EXT3 filesystem

Ex:

  1. First, check to see how much space is available

    [root@Linux01 ~]# pwd
    /TCPDumpLV
    [root@Linux01 TCPDumpLV]# df -kh .
    FilesystemSizeUsedAvailUse%Mounted on
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV6.1G922M4.9G16%/TCPDumpLV
    Note: The disk free command shows that we are using 922MB and have 4.9G available on our file system. Therefore, we can safely shrink the volume to 1.5G (leaving a little bit for overhead) without any issue.
  2. Unmount the file system

    [root@Linux01 TCPDumpLV]# cd /
    [root@Linux01 /]# umount /TCPDumpLV
  3. Check the file system for errors

    [root@Linux01 /]# e2fsck -f /dev/mapper/TCPDumpVolGRP-TCPDumpLV
    e2fsck 1.39 (29-May-2006)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV: 13/802816 files (7.7% non-contiguous), 261017/1605632 blocks
    [root@Linux01 /]#

  4. Shrink the file system to 1.5GB

    [root@Linux01 /]# resize2fs /dev/mapper/TCPDumpVolGRP-TCPDumpLV 1500M
    resize2fs 1.39 (29-May-2006)
    Resizing the filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV to 384000 (4k) blocks.
    The filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV is now 384000 blocks long.

    [root@Linux01 /]#
  5. Shrink the logical file system to 1.5GB

    [root@Linux01 /]# lvresize -L 1.5G /dev/TCPDumpVolGRP/TCPDumpLV
    WARNING: Reducing active logical volume to 1.50 GB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce TCPDumpLV? [y/n]: y
    Reducing logical volume TCPDumpLV to 1.50 GB
    Logical volume TCPDumpLV successfully resized
    [root@Linux01 /]#
    Note: Special precaution should be taken with this step. It's possible to reduce the logical volume size by more than the size of the file system. If you do reduce the LV size by more than what you resized the file system to (from step #4), this will almost certainly end very badly for you. Ensure the LV is large enough for the file system and that you make a backup before hand!
  6. Verify the new size of the logical volume

    [root@Linux01 ~]# lvdisplay /dev/TCPDumpVolGRP/TCPDumpLV
    --- Logical volume ---
    LV Name/dev/TCPDumpVolGRP/TCPDumpLV
    VG NameTCPDumpVolGRP
    LV UUIDhYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Accessread/write
    LV Statusavailable
    # open0
    LV Size1.50 GB
    Current LE48
    Segments1
    Allocationinherit
    Read ahead sectorsauto
    - currently set to256
    Block device253:5
    1. Remount the file system and verify the new size
    1. [root@Linux01 /]# mount /dev/TCPDumpVolGRP/TCPDumpLV
      [root@Linux01 /]# cd /TCPDumpLV/
      <>
    1. [root@Linux01 TCPDumpLV]# df -kh .
    1. Filesystem
    1. Size
    1. Used
    1. Avail
    1. Use%
    1. Mounted on
    1. /dev/mapper/TCPDumpVolGRP-TCPDumpLV
    1. 1.5G
    1. 920M
    1. 497M
    1. 65%
    1. /TCPDumpLV