#!/usr/bin/perl
## @file rmtSdcUpgrade
# @brief   Script to update GRUB configuration file (grub.conf) to allow for the
#          automatic "upgrade" of the target upon reboot
#
# @details Note that all the output to the console is intensional to allow the
#          UCM/DM ssh calls to capture the error output.
#
# @param hostname - target host name to deploy (key parameter to find the data
#                   related to the deployment operation).
# @param IP       - ip-address of the target host
# @author
# @date Janurary 2010
# @version 1.00 - Initial version
# @note none

#
#
use lib "/opt/nortel/base/lib";

use warnings;
use strict;

use common_variables;
use common_functions;
use CommonInstallUtils;
use baselog;
use base_system;
use hwRecognition;
use base_deploy;

use Getopt::Long;

sub validate_linuxbase_version();
sub validate_nfs($);
sub get_ifconfig($);
sub get_userinfo($);
sub update_grub_conf($);  #update GRUB configuration file grub.conf
sub back_it_up();

sub rmtSdcUpgrade();      # main entry
exit(rmtSdcUpgrade());


###############################################################################

###############################################################################
## @fn validate_linuxbase_version()
# @brief   Validate that the installed Linuxbase RPM is of the minimum verion
#          that contains the...
#          ...hmm if I am being called then I am the correct version! :-)
#
#          For now, this is used by "appSdcUpgrade" to determine if the
#          function can be called thus implying that the DC Upgrade
#          capability is in place and it confirms that the eth/ip connection
#          is up and running.
# @param
# @param
# @return   $OK    - operation successful
# @return   $ERROR - some error happened
#
###############################################################################
sub validate_linuxbase_version()
{
   baselog($LOG_INFO, "Validating Linuxbase Version Successfull", $TRUE);

   # Inform anyone currently on the remote to get lost
   do_system([$WALL,"A Server Upgrade is being performed, the system will be REBOOTED shortly"]);
   return $OK;
}

###############################################################################
## @fn validate_nfs()
# @brief    Determine if the NFS services are enabled on the SDS
#
# @param    $nfsIP - NFS Server IP Address
# @param
# @return   $OK    - operation successful
# @return   $ERROR - some error happened
#
###############################################################################
sub validate_nfs($)
{
   my $nfsIP  = shift(@_);
   my $retVal = $OK;

   # Counts of expected NFS services available on the UCM/DS server.
   my $PORTMAPPER_CNT = 2;
   my $STATUS_CNT     = 2;
   my $NFS_CNT        = 6;
   my $NLOCKMGR_CNT   = 6;
   my $MOUNTD_CNT     = 6;

   baselog($LOG_INFO, "Validating ($nfsIP) NFS Availability", $TRUE);

   #
   # Perform an RPC call to the SDS server and report what services are found.
   # - if not reachable can take about 3 min. to timeout.
   #
   my $rc;
   my @out;
   if (not do_system([$RPCINFO, "-p", "$nfsIP"], {$dsOUT => \@out, $dsRETVAL => \$rc}))
   {
      baselog($LOG_ERROR, "$RPCINFO -p $nfsIP Error rc=$rc", $TRUE);
      return $ERROR;
   }
   if (scalar(@out) <= 1)
   {
      baselog($LOG_ERROR, "Unable to determine RPCINFO on $nfsIP", $TRUE);
      return $ERROR;
   }
   foreach my $tmp (@out)
   {
      baselog($LOG_INFO, "RPCINFO: $tmp");
   }
   
   my @list = ();
   @list = grep (/portmapper/i, @out);
   if (scalar(@list) != $PORTMAPPER_CNT)
   {
      baselog($LOG_ERROR, "Incorrect number of \"portmapper\" services detected on $nfsIP", $TRUE);
      $retVal = $ERROR;
   }

   @list = ();
   @list = grep (/STATUS/i, @out);
   if (scalar(@list) != $STATUS_CNT)
   {
      baselog($LOG_ERROR, "Incorrect number of \"status\" services detected on $nfsIP", $TRUE);
      $retVal = $ERROR;
   }

   @list = ();
   @list = grep (/NFS/i, @out);
   if (scalar(@list) != $NFS_CNT)
   {
      baselog($LOG_ERROR, "Incorrect number of \"nfs\" services detected on $nfsIP", $TRUE);
      $retVal = $ERROR;
   }

   @list = ();
   @list = grep (/NLOCKMGR/i, @out);
   if (scalar(@list) != $NLOCKMGR_CNT)
   {
      baselog($LOG_ERROR, "Incorrect number of \"nlockmgr\" services detected on $nfsIP", $TRUE);
      $retVal = $ERROR;
   }

   @list = ();
   @list = grep (/MOUNTD/i, @out);
   if (scalar(@list) != $MOUNTD_CNT)
   {
      baselog($LOG_ERROR, "Incorrect number of \"mountd\" services detected $nfsIP", $TRUE);
      $retVal = $ERROR;
   }
   baselog($LOG_INFO, "Validating ($nfsIP) NFS Availability - Done", $TRUE);
   return $retVal;
} ## end sub validate_nfs($)

###############################################################################
## @fn get_ifconfig()
# @brief     This function obtains the eth0 & eth1 IP addresses of the
#            current host; using "ifconfig"
#
# @param    $ifIP  - returned hash containinf the IP addesses
# @param
# @return   $OK    - operation successful
# @return   $ERROR - some error happened
#
###############################################################################
sub get_ifconfig($)
{
   my $ifIP = shift(@_);

   $ifIP->{'ELAN_IP'} = '';
   $ifIP->{'TLAN_IP'} = '';

   #
   # We will make use of the "ifconfig" utility to determine the IP addresses
   # of the current target.
   #
   # Retrieve the ETH0 I/F IP address
   #
   my ($tmp, $rc);
   my @out;
   if (not do_system([$IFCONFIG, "eth0"], {$dsOUT => \@out, $dsRETVAL => \$rc}))
   {
      baselog($LOG_ERROR, "$IFCONFIG eth0 Error rc=$rc", $TRUE);
      return $ERROR;
   }
   $tmp = join(" ", @out);

   if ($tmp =~ /.*inet addr:(.+)\s.Bcast*/)
   {
      $ifIP->{'ELAN'} = $1;
      baselog($LOG_INFO, "ELAN/ETH0 IP = \"$ifIP->{'ELAN_IP'}\" ");
   }
   else
   {
      baselog($LOG_ERROR, "$IFCONFIG eth0 output parsing failure", $TRUE);
      return $ERROR;
   }

   #
   # Retrieve the ETH1 I/F IP address
   #
   if (not do_system([$IFCONFIG, "eth1"], {$dsOUT => \@out, $dsRETVAL => \$rc}))
   {
      baselog($LOG_ERROR, "$IFCONFIG eth1 Error rc=$rc", $TRUE);
      return $ERROR;
   }
   $tmp = join(" ", @out);

   if ($tmp =~ /.*inet addr:(.+)\s.Bcast*/)
   {
      $ifIP->{'TLAN'} = $1;
      baselog($LOG_INFO, "TLAN/ETH1 IP = \"$ifIP->{'TLAN_IP'}\" ");
   }
   else
   {
      baselog($LOG_ERROR, "$IFCONFIG eth1 output parsing failure", $TRUE);
      return $ERROR;
   }
   return $OK;
} ## end sub get_ifconfig($)

###############################################################################
## @fn get_userinfo()
# @brief     This function obtains the eth0 & eth1 IP addresses of the
#            current target
#
# @param    $ifIP  - returned hash containing the IP address info
# @param
# @return   $OK    - operation successful
# @return   $ERROR - some error happened
#
###############################################################################
sub get_userinfo($)
{
   my $ifIP = shift(@_);

   baselog($LOG_INFO, "Retrieving target $USERINFO Information", $TRUE);

   # read-in the userfinfo.txt file into a local array
   unless (open(UINFO, "<", "$USERINFO"))
   {
      baselog($LOG_ERROR, "Cannot open $USERINFO for reading\n", $TRUE);
      return $ERROR;
   }
   my @list = <UINFO>;
   unless (close(UINFO))
   {
      baselog($LOG_WARNING, "Failed to close $USERINFO \n", $TRUE);
   }

   #
   # Retrieve the reguired eth0/1 I/F information
   #
   my $retVal = $OK;
   foreach my $line (@list)
   {
      if ($line =~ /^machineIP=(.*)$/)
      {

         # Validate IP for correct syntax.
         if (validateIP($1))
         {
            $ifIP->{'ELAN_IP'} = $1;
         }
         else
         {
            baselog($LOG_ERROR, "Incorrect IP syntax: machineIP=$1", $TRUE);
            $retVal = $ERROR;
         }
      } ## end if ($line =~ /^machineIP=(.*)$/...
      elsif ($line =~ /^gateway=(.*)$/)
      {
         if (validateIP($1))
         {
            $ifIP->{'ELAN_GW'} = $1;
         }
         else
         {
            baselog($LOG_ERROR, "Incorrect IP syntax: gateway=$1", $TRUE);
            $retVal = $ERROR;
         }
      } ## end elsif ($line =~ /^gateway=(.*)$/...
      elsif ($line =~ /^netmask=(.*)$/)
      {
         if (validateIP($1))
         {
            $ifIP->{'ELAN_NM'} = $1;
         }
         else
         {
            baselog($LOG_ERROR, "Incorrect IP syntax: netmask=$1", $TRUE);
            $retVal = $ERROR;
         }
      } ## end elsif ($line =~ /^netmask=(.*)$/...
      elsif ($line =~ /^machineIPTlan=(.*)$/)
      {
         if (validateIP($1))
         {
            $ifIP->{'TLAN_IP'} = $1;
         }
         else
         {
            baselog($LOG_ERROR, "Incorrect IP syntax: machineIPTlan=$1", $TRUE);
            $retVal = $ERROR;
         }
      } ## end elsif ($line =~ /^machineIPTlan=(.*)$/...
      elsif ($line =~ /^gatewayTlan=(.*)$/)
      {
         if (validateIP($1))
         {
            $ifIP->{'TLAN_GW'} = $1;
         }
         else
         {
            baselog($LOG_ERROR, "Incorrect IP syntax: gatewayTlan=$1", $TRUE);
            $retVal = $ERROR;
         }
      } ## end elsif ($line =~ /^gatewayTlan=(.*)$/...
      elsif ($line =~ /^netmaskTlan=(.*)$/)
      {
         if (validateIP($1))
         {
            $ifIP->{'TLAN_NM'} = $1;
         }
         else
         {
            baselog($LOG_ERROR, "Incorrect IP syntax: netmaskTlan=$1", $TRUE);
            $retVal = $ERROR;
         }
      } ## end elsif ($line =~ /^netmaskTlan=(.*)$/...
   } ## end foreach my $line (@list)
   baselog($LOG_INFO, "Retrieving target $USERINFO Information - Done", $TRUE);
   return $retVal;
} ## end sub get_userinfo($)

###############################################################################
## @fn update_grub_conf()
# @brief     This function performs the updating of grub.conf file such that
#            a reboot will cause the target to perform a DS/NFS installation.
# @details   Backup operation includes the following steps:
# @details   1. Update the "/boot/grub/grub.conf" file with an "SDC INSTALL" entry
# @details   2. Configure grub to boot off of the new "SDC INSTALL" entry (once)
# @param    $kernel_params - kernel parameters
# @param
# @return   $OK  - operation successful
# @return   $ERROR - some error happens
#
###############################################################################
sub update_grub_conf($)
{
   my $line;

   my $kernel_params = shift(@_);

   my $kernel_count            = 0;   # Count of "title" entries in grub.conf
   my $found                   = 0;   # flag to indicate grub.conf changes
   my $sds_default_entry       = -1;  # "SDC INSTALL" entry location in grub.conf
   my $sds_install_title_count = 0;   # Count of "SDC INSTALL" title entries found
   my $sds_install_title_found = 0;   # Flag indicating "SDC INSTALL" entry context

   if (not defined $kernel_params)
   {
      baselog($LOG_ERROR, "Grub Kernel parameters need to be specified\n", $TRUE);
      return $ERROR;
   }
   # print "$kernel_params \n";

   #
   # Before we try to modify the grub config file, ensure that the kernel images
   # (installation images) exist on the target.
   #
   my $VMLINUZ   = "$RMT_SDS_PATH/$DMS_SDS_PATH/isolinux/vmlinuz";
   my $VMLINUZ_I = "/boot/vmlinuz-install";

   my $INITRD    = "$RMT_SDS_PATH/$DMS_SDS_PATH/isolinux/initrd.img";
   my $INITRD_I  = "/boot/initrd-install.img";

   baselog($LOG_INFO, "Initial Installation kernel images being copied", $TRUE);
   unless (-e $VMLINUZ)
   {
      baselog($LOG_ERROR, "Grub $VMLINUZ installation kernel image not found\n", $TRUE);
      return $ERROR;
   }
   unless (-e $INITRD)
   {
      baselog($LOG_ERROR, "Grub $INITRD installation image not found\n", $TRUE);
      return $ERROR;
   }

   unless (do_system([$CP, '-f', $VMLINUZ, $VMLINUZ_I]))
   {
      baselog($LOG_ERROR, "$VMLINUZ_I backup failed");
      return $ERROR;
   }
   unless (do_system([$CP, '-f', $INITRD, $INITRD_I]))
   {
      baselog($LOG_ERROR, "$INITRD_I backup failed");
      return $ERROR;
   }

   #
   # Create a backup of the grub.conf file
   #
   baselog($LOG_INFO, "Modifying $GRUBCONF for automatic target installation", $TRUE);
   if (-e $GRUBCONF)
   {
      baselog($LOG_INFO, "File $GRUBCONF backed-up to $GRUBCONF.bak");
      unless (do_system([$CP, $GRUBCONF, "$GRUBCONF.bak"]))
      {
         baselog($LOG_ERROR, "$GRUBCONF backup failed");
         return $ERROR;
      }
   } ## end if (-e $GRUBCONF)
   else
   {
      baselog($LOG_ERROR, "File $GRUBCONF does not exist\n", $TRUE);
      return $ERROR;
   }

   # read-in the grub.conf file into a local array
   my $grub_h = undef;
   unless (open($grub_h, "<", "$GRUBCONF"))
   {
      baselog($LOG_ERROR, "Cannot open $GRUBCONF for reading\n", $TRUE);
      return $ERROR;
   }
   my @list = <$grub_h>;
   unless (close($grub_h))
   {
      baselog($LOG_WARNING, "Failed to close $GRUBCONF\n", $TRUE);
   }
   if (scalar(@list) == 0)
   {
      baselog($LOG_ERROR, "Cannot retrieve $GRUBCONF data\n", $TRUE);
      return $ERROR;
   }

   #
   # Process the grub.conf content
   #
   my @grubconf;
   foreach $line (@list)
   {
      if ($line =~ /^\s*#.*/)
      {

         # Ignore comment lines
      }
      elsif ($line =~ /^\s*title(.*)/)
      {
         if ($1 =~ /SDC INSTALL/)
         {
            $sds_install_title_found = 1;
            $sds_install_title_count++;

            if ($sds_install_title_count > 1)
            {  #
               # Throw away any other DS titles/lines found
               next;
            }
            else
            {  #
               # Keep count of all title entries found to allow us to determine the
               # "SDC INSTALL" block to which to boot to.
               #
               $sds_default_entry = $kernel_count;
            } ## end else [ if ($sds_install_title_count...
            $found = 1;
         } ## end if ($1 =~ /SDC INSTALL/...
         else
         {  #
            # Keep count of title/kernel blocks found
            $sds_install_title_found = 0;
            $kernel_count++;
         } ## end else [ if ($1 =~ /SDC INSTALL/...
      } ## end elsif ($line =~ /^\s*title(.*)/...
      elsif ($sds_install_title_found == 1)
      {  #
         # If a "SDC INSTALL" entry is found determine if modification
         # is required or not
         #
         if ($sds_install_title_count == 1)
         {  #
            # If an initial "SDC INSTALL" title is found then ensure that all the
            # parameters are reset to their expected default values.
            #
            if ($line =~ /(^\s*root\s*)(.*)/)
            {
               $line = "\troot (hd0,0)\n";
            }
            elsif ($line =~ /(^\s*kernel\s*)(.*)/)
            {
               $line = "\tkernel /vmlinuz-install $kernel_params\n";
            }
            elsif ($line =~ /(^\s*initrd\s*)(.*)/)
            {
               $line = "\tinitrd /initrd-install.img\n";
            }
            else
            {

               # Throw away any other lines found in the "DC INSTALL" entry.
               next;
            } ## end else [ if ($line =~ /(^\s*root\s*)(.*)/...
         } ## end if ($sds_install_title_count...
         else
         {

            # Throw away any other "SDC INSTALL" titles/lines found.
            # - we only expect one entry
            next;
         } ## end else [ if ($sds_install_title_count...
         $found = 1;
      } ## end elsif ($sds_install_title_found...

      # Save each line read and/or modified from the orig grub config file
      push(@grubconf, $line);
   } ## end foreach $line (@list)

   #
   # If a DS entry is not found then create an "SDC INSTALL" entry
   #
   if ($sds_install_title_count == 0)
   {
      $line = "title SDC INSTALL\n";
      $line = $line . "\troot (hd0,0)\n";
      $line = $line . "\tkernel /vmlinuz-install $kernel_params\n";
      $line = $line . "\tinitrd /initrd-install.img\n";

      push(@grubconf, $line);
      $sds_default_entry = $kernel_count;
      $found             = 1;
   } ## end if ($sds_install_title_count...

   #
   # If there are any changes reguired to be made to the grub.conf file
   # then update the file.
   #
   baselog($LOG_INFO, "Titles found:     $kernel_count\n",        $TRUE);
   baselog($LOG_INFO, "DC Default Entry: $sds_default_entry\n\n", $TRUE);

   if ($found == 1)
   {
      # Put updates into a tmp file first
      my $fh = undef;
      unless (open($fh, ">", "$GRUBCONF.tmp"))
      {
         baselog($LOG_ERROR, "Cannot open $GRUBCONF.tmp for writing\n", $TRUE);
         return $ERROR;
      }
      foreach $line (@grubconf)
      {
         print $fh $line;
         baselog($LOG_INFO, "   $line", $TRUE);
      }
      unless (close($fh))
      {
         baselog($LOG_WARNING, "Cannot close $GRUBCONF.tmp\n", $TRUE);
      }

      # Since everything is okay copy the tmp file over the orig file.
      unless (do_system([$MV, "$GRUBCONF.tmp", $GRUBCONF]))
      {
         baselog($LOG_ERROR, "Updating $GRUBCONF failed");
         return $ERROR;
      }
      unless (do_system([$CHMOD, '600', $GRUBCONF]))
      {
         baselog($LOG_ERROR, "Updating $GRUBCONF mode failed");
         return $ERROR;
      }
   } ## end if ($found == 1)

   #
   # Configure grub to boot off of the "SDC INSTALL" entry just added/modified
   # - we will configure GRUB to bypass (only once) the "timeout" and "default" settings
   #   from the /boot/grub/grub.conf, and boot off the sdc install entry.
   #   This will allow us to recover should there be an issue in booting the "SDC INSTALL"
   #   kernel entry.
   #
   baselog($LOG_INFO, "   $ECHO 'savedefault --default=$kernel_count --once' | $GRUB --batch", $TRUE);
   if (system("$ECHO 'savedefault --default=$kernel_count --once' | $GRUB --batch") != 0)
   {
      baselog($LOG_ERROR, "Unable to configure GRUB to boot off of \"SDC INSTALL\" ", $TRUE);
      return $ERROR;
   }

   if (system("$SHUTDOWN -r +1 \"A Server Upgrade is being performed, the system will be REBOOTED shortly\" &") != $OK)
   {
      baselog($LOG_WARNING, "Cannot perform Server REBOOT to perform a Server DC Upgrade.\n", $TRUE);
      return $ERROR;
   }
   return $OK;
} ## end sub update_grub_conf($)

###############################################################################
## @fn back_it_up()
# @brief    This function performs a local sysbackup of the target server.
#           The intension is that on the upgrade/reboot that the backup file
#           "upgrade_backup.tar.gz" is re-used when the applications are installed.
# @details
# @param    none
# @return   $OK  - operation successful
# @return   $ERROR - some error happens
#
###############################################################################
sub back_it_up()
{
   my $ret = $ERROR;

   my $command = $SYSBACKUP;
   $command .= " --upgrade --noninteractive";

   baselog($LOG_INFO, "System Backup Started... Please wait", $TRUE);

   my @cmdarr = @{prepare_command_to_run_as($command, $NORTEL_ACCT)};
   if (scalar(@cmdarr) != 0)
   {
      baselog($LOG_INFO, "@cmdarr");
      if (not do_system(\@cmdarr, {$dsINTERACT => $TRUE}))
      {
         baselog($LOG_ERROR, "System Backup failed", $TRUE);
         return $ERROR;
      }
      else
      {
         baselog($LOG_INFO, "System Backup Completed", $TRUE);
      }
   } ## end if (scalar(@cmdarr) !=...
   else
   {
      baselog($LOG_ERROR, "System Backup cannot be executed.", $TRUE);
      return $ERROR;
   }
   return $OK;
} ## end sub back_it_up()

###############################################################################
## @fn un_archive()
# @brief    This function will take the specified archive file from the UCM/DM
#           server and untar the file into the local temporary SDS distribution
#           folder to allow for a "local" upgrade
#          
# @details
# @param    $archive
# @return   $OK  - operation successful
# @return   $ERROR - some error happens
###############################################################################
sub un_archive($)
{
   my $archive = shift(@_);

   my $retval = $OK;

   #
   # Confirm the existance of the specified archive.
   #
   if (!-e $archive)
   {
      baselog($LOG_ERROR, "No archive data $archive has been found.", $TRUE);
      return  $ERROR;
   }

   # Create the Temporary SDS Distribution folder if it does not exist
   if (!-e $RMT_SDS_PATH)
   {  
      if ( !do_system([$MKDIR, "-p", $RMT_SDS_PATH]) )
      {
         baselog($LOG_ERROR, "Unable to create $RMT_SDS_PATH", $TRUE);
         return  $ERROR;
      }
   }

   # Expand the archive into the Temporary SDS Distribution folder
   if ( get_rmt_archive_data($archive, $RMT_SDS_PATH) != $TRUE )
   {
      baselog($LOG_ERROR, "Unable to Un-Archive $archive", $TRUE);
      return  $ERROR;
   }
   else
   {
      baselog($LOG_INFO, "Successful Un-Archive of $archive", $TRUE);

      # Remove the archive as we no longer need it
      if ( !do_system([$RM, "-f", $archive]) )
      {
         baselog($LOG_ERROR, "Unable to remove $archive", $TRUE);
         return  $ERROR;
      }
   }
   return $retval;
} ## end un_archive


###############################################################################
## @fn create_sdcparameters()
# @brief    This function will "/var/sds/sdcparameters" file for use during
#           the PreInstall stage.
#          
# @details
# @param    $tlanip
# @return   $OK  - operation successful
# @return   $ERROR - some error happens
###############################################################################
sub create_sdcparameters($)
{
   my $tlanip = shift(@_);

   my $fh = undef;
   unless (open($fh, ">", "$RMT_SDS_PATH/var/sds/sdcparameters"))
   {
      baselog($LOG_ERROR, "Cannot open $RMT_SDS_PATH/var/sds/sdcparameters for writing\n", $TRUE);
      return $ERROR;
   }
   print $fh "ip=$tlanip";

   unless (close($fh))
   {
      baselog($LOG_WARNING, "Cannot close $RMT_SDS_PATH/var/sds/sdcparameters file\n", $TRUE);
   }
   unless (do_system([$CHOWN, 'nortel.nortel', "$RMT_SDS_PATH/var/sds/sdcparameters"]))
   {
      baselog($LOG_ERROR, "Updating $RMT_SDS_PATH/var/sds/sdcparameters ownership failed");
      return $ERROR;
   }

   baselog($LOG_INFO, "$RMT_SDS_PATH/var/sds/sdcparameters created ($tlanip)\n", $TRUE);
   return $OK;
} ## end create_sdcparameters

###############################################################################
## @fn create_fspart()
# @brief    This function will generate the "/admin/fspart.txt" which will contain
#           mount point to harddrive partition information. THe current assumption
#           is that there is only one system device and thus only the partition 
#           number is stored
#          
# @details
# @param    
# @return   $OK    - operation successful
# @return   $ERROR - some error happens
###############################################################################
sub create_fspart()
{
   my %mnt_part = ();
   my @tmp;		# work variable
   my $retval = $OK;

   #
   # Read in the fstab file to determine the mount points for each file system
   # defined on the target.
   #
   # /etc/fstab content is of the following format:
   #
   #    /dev/sda11 /                      ext3    defaults        1 1
   #    /dev/sda6  /tmp                   ext3    defaults        1 2
   #    /dev/sda2  /admin                 ext3    defaults,nosuid 1 2
   #    /dev/sda8  /opt                   ext3    defaults        1 2
   #    /dev/sda10 /home                  ext3    defaults,nosuid 1 2
   #    /dev/sda5  /var                   ext3    defaults        1 2
   #    /dev/sda7  /var/log               ext3    defaults,nosuid 1 2
   #    /dev/sda9  /var/coredump          ext3    defaults,nosuid 1 2
   #    /dev/sda1  /boot                  ext3    defaults,nosuid 1 2
   #
   unless (open(FHI, "<", "/etc/fstab"))
   {
      baselog($LOG_ERROR, "Cannot open fstab for reading\n", $TRUE);
      return $ERROR;
   }
   @tmp = ();
   @tmp = <FHI>;
   unless (close(FHI))
   {
      baselog($LOG_WARNING, "Cannot close fstab \n", $TRUE);
   }

   my ($mnt, $dsk, $par);

   # For each mount point parse out the disk name and partition number
   foreach my $line (@tmp)
   {
      if ( $line =~ /^\/dev\/(\D+)(\d+)\s+([\w|\/]+)\s+ext.+/ )
      {
         $dsk = $1;	# system disk
         $par = $2;	# partition number
         $mnt = $3;	# mount point

         $mnt =~ s/\s+//g;

         $mnt_part{$mnt} = $par; # save partition number for the mount point

         baselog($LOG_INFO," '$mnt' => $dsk $par ... $mnt_part{$mnt}");
      }
   } ## foreach my $line

   #
   # Read the /proc/swaps file to determine the swap partition since
   # it is not part of the static information about the filesystems
   # ie. not specified in the fstab file
   #
   unless (open(FHI, "<", "/proc/swaps"))
   {
      baselog($LOG_ERROR, "Cannot open swaps for reading\n", $TRUE);
      return $ERROR;
   }
   @tmp = ();
   @tmp = <FHI>;
   unless (close(FHI))
   {
      baselog($LOG_WARNING, "Cannot close swaps \n", $TRUE);
   }

   #
   # Expect only two lines; a title line and the swap partition info
   #
   # For example:
   #   Filename       Type           Size    Used    Priority
   #   /dev/sda3      partition      2112536 96      -1
   #
   my $i = scalar(@tmp);
   if ( $i eq '2' )
   {
      foreach my $line (@tmp)
      {
         if ( $line =~ /^\/dev\/(\D+)(\d+)\s+.+/ )
         {
            $dsk = $1;
            $par = $2;

            $mnt_part{$SWAP_K} = $par;
            baselog($LOG_INFO,"* '$SWAP_K'  =>  $dsk $par ... $mnt_part{$SWAP_K} \n");
         }
      }
   }
   else
   {
      baselog($LOG_INFO, "Unable to determine the swap mount - none or more than one", $TRUE);
      return $ERROR;
   }

   #
   # NEED TO CONFIRM THAT ALL EXPECTED PARTIONS WERE FOUND
   #
   @tmp = ("$BOOT_K","$ADMIN_K","$ROOT_K","$HOME_K","$OPT_K","$TMP_K","$VAR_K","$LOG_K","$SWAP_K","$COREDUMP_K");
   foreach $par (@tmp)
   {
      if (defined $mnt_part{$par} )
      {
         baselog($LOG_INFO, "Partition '$par' = $mnt_part{$par} Found", $TRUE);
      }
      else
      {
         baselog($LOG_ERROR, "Partition '$par' NOT Found ***", $TRUE);
         $retval = $ERROR;
      }
   }
   if ($retval == $ERROR) { return $ERROR; }

   #
   # Dump fs partition information into a file to be picked up during
   # the kickstart stage when defining the partitions on the system disk
   #
   # File format of the form:
   #    <mnt point>:<partition number>
   #
   # For example:
   #    /var/coredump:9
   #
   unless ( open(FHO, ">", "$RMT_SDS_PATH/$DMS_SDS_PATH/$FSPART_TXT") )
   {
      baselog($LOG_ERROR, "Cannot open $RMT_SDS_PATH/$DMS_SDS_PATH/$FSPART_TXT for writing", $TRUE);
      return $ERROR;
   }
   foreach my $key (keys (%mnt_part))
   {
      print FHO "$key:$mnt_part{$key}\n";
   }
   unless (close(FHO))
   {
      baselog($LOG_WARNING, "Cannot close $RMT_SDS_PATH/$DMS_SDS_PATH/$FSPART_TXT", $TRUE);
   }
   baselog($LOG_INFO, "$RMT_SDS_PATH/$DMS_SDS_PATH/$FSPART_TXT created", $TRUE);

   return $OK;
} ## end create_fspart

###############################################################################
## @fn get_partition_dev()
# @brief    This function will return the HD partition number for the specified
#           mount point.
#          
# @details
# @param    $mnt_str         - mount point 
# @return   partition number - operation successful
# @return   undef            - some error happens
###############################################################################
sub get_partition_dev($)
{
   # String identifying the mount for which we need to determine the drive partition
   my $mnt_str = shift(@_);

   my @out;
   unless ( do_system(["/bin/mount"], {$dsOUT => \@out}) )
   {
      baselog($LOG_INFO, "Unable to assertain the mounts available", $TRUE);
      return undef;
   }
   
   my @log = grep (/on\s+$mnt_str\s+type/, @out);
   if (scalar(@log) != 1)
   {
      baselog($LOG_ERROR, "More than one (or no) log device found", $TRUE);
      if (scalar(@out) > 0)
      {
         # Dump out what we did see
         foreach my $line (@out)
         {
            baselog($LOG_INFO, "   $line", $TRUE);
         }
      }
      return undef;
   }
   else
   {
      baselog($LOG_INFO, "Found $log[0] as the '$mnt_str' partition");
   }
   
   # Extract the "drive" identifier for the "$mnt_str" partition
   if ( $log[0] =~ m/^\/dev\/(.+)\s+on\s.+/ )
   {
      my $logdev = $1;
      baselog($LOG_INFO, "Found $logdev as the device for the '$mnt_str' partition");
      return $logdev;
   }
   else
   {
      baselog($LOG_ERROR, "No device determined for '$mnt_str' partition");
      return undef;
   }
   # should never get here
   return undef;
} ## end get_partition_dev

###############################################################################
## @fn update_ks_cfg()
# @brief    This function will modify the specified kickstart with the specified
#           mount point.
#          
# @details
# @param    $cfg   - kickstart file
# @param    $o_dev - original device string
# @param    $n_dev - new device string to sub with
# @return   $OK  - operation successful
# @return   $ERROR - some error happens
###############################################################################
sub update_ks_cfg($$$)
{
   my $cfg    = shift(@_);	# Kickstart configuration file
   my $o_dev  = shift(@_);	# Old partition device in ks file
   my $n_dev  = shift(@_);	# New partition device in ks file
  
   #
   # Create a backup of the kickstart configuration file
   #
   if (-e $cfg)
   {
      baselog($LOG_INFO, "File $cfg backed-up to $cfg.bak");
      unless (do_system([$CP, $cfg, "$cfg.bak"]))
      {
         baselog($LOG_ERROR, "$cfg backup failed");
         return $ERROR;
      }
   } ## end if (-e $cfg)
   else
   {
      baselog($LOG_ERROR, "File $cfg does not exist\n", $TRUE);
      return $ERROR;
   }

   #
   # Create the "new" kickstart file with the correct "/var/log" hd partition
   #
 
   # Grab the content of the orginal kickstart file
   my $fh;
   unless (open($fh, "<", $cfg))
   {
      baselog($LOG_ERROR, "Cannot open $cfg for reading", $TRUE);
      return $ERROR;
   }
   my @list = <$fh>;
   unless (close($fh))
   {
      baselog($LOG_WARNING, "Cannot close $cfg", $TRUE);
   }

   # Create the new kickstart file
   unless (open($fh, ">", "$cfg.new"))
   {
      baselog($LOG_ERROR, "Cannot open $cfg.new for reading\n", $TRUE);
      return $ERROR;
   }
   
   print $fh "#\n# THIS FILE HAS BEEN AUTOMATICALLY CREATED TO REFLECT THE /VAR/LOG PARTITION\n";
   print $fh "# PLEASE DO NOT MODIFY\n#\n";
   
   foreach my $line (@list)
   {
      # Replace the $o_dev string with the proper log device value
      # - Ensure that the xxx_hd.cfg file references to the "hd" does not
      #   change unless this is modified as well.
      $line =~ s/$o_dev/$n_dev/g;
      print $fh $line;
   }
   unless (close($fh))
   {
      baselog($LOG_WARNING, "Cannot close $cfg.new \n", $TRUE);
   }


   # Since everything is okay copy the tmp file over the orig file.
   unless (do_system([$MV, "$cfg.new", $cfg]))
   {
      baselog($LOG_ERROR, "Updating $cfg failed");
      return $ERROR;
   }
   unless (do_system([$CHMOD, '440', $cfg]))
   {
      baselog($LOG_ERROR, "Updating $cfg mode failed");
      return $ERROR;
   }
   unless (do_system([$CHOWN, 'nortel.nortel', $cfg]))
   {
      baselog($LOG_ERROR, "Updating $cfg ownership failed");
      return $ERROR;
   }

   baselog($LOG_INFO, "New $cfg generated", $TRUE);
   return $OK;
} ## end update_ks_cfg


###############################################################################
sub rmtSdcUpgrade()
{
   #
   # GRUB "SDC INSTALL" kernel entry default parameters
   #
   my $COTS_KERN_PARAMS =
     "ramdisk_size=8192 text selinux=0 console=ttyS0,9600n8 installation_stage ks=hd:sda8:/var/sds/ks_hd.cfg";

   # Kernel parameter line for the CPPM cards
   my $CPPM_KERN_PARAMS =
     "ramdisk_size=8192 root=/dev/ram ide1=noprobe ide1=nodma text selinux=0 acpi=off console=ttyS0,9600n8 installation_stage ks=hd:hda9:/var/sds/ks_cppm_hd.cfg";

   # Kernel parameter line for the CPDC & CPMG cards
   my $CPDC_KERN_PARAMS =
     "ramdisk_size=8192 root=/dev/ram text selinux=0 acpi=off console=ttyS0,9600n8 installation_stage ks=hd:sda8:/var/sds/ks_cppm_hd.cfg";

   #
   # Hash to hold the target IP info
   #
   my %myIfInfo = (
                   'NFS_IP'  => "",
                   'ELAN_IP' => "",
                   'ELAN_GW' => "",
                   'ELAN_NM' => "",
                   'TLAN_IP' => "",
                   'TLAN_GW' => "",
                   'TLAN_NM' => ""
                  );

   my $retVal = $OK;

   #
   # Parse and validate the input parameters
   #
   my $nfs_server_hostname         = undef;
   my $nfs_server_ip               = undef;
   my $validate_linuxbase_ver_flag = undef;
   my $validate_nfs_flag           = undef;
   my $backup_flag                 = undef;
   my $update_grub_flag            = undef;
   my $reboot_flag                 = undef;
   my $clear_temp_repository_flag  = undef;
   my $archive_full_path           = undef;
   my $help                        = undef;

   my $res =
     GetOptions(
                "nfs_hostname:s"        => \$nfs_server_hostname,
                "nfs_ip:s"              => \$nfs_server_ip,
                "validate_base_version" => \$validate_linuxbase_ver_flag,
##              "validate_nfs"          => \$validate_nfs_flag,             # obsoleted
                "backup"                => \$backup_flag,
                "update_grub"           => \$update_grub_flag,
                "clear_temp_repository" => \$clear_temp_repository_flag,
                "expand_archive:s"      => \$archive_full_path,
                "help"                  => \$help
               );

   #Parameter checking

   # - the nfs_hostname & nfs_ip represent the Deployment Server hostname & IP (resp).
   #   The module was originally developed to be a NFS solution and thus
   #   the various reference to NFS base variable & function names throughout.

   if (!$res)
   {
      print "Unknown  options provided\n";
      print "*** rmtSdcUpgrade -nfs_hostname cppm -nfs_ip 47.11.68.51\n";
      return $ERROR;
   } ## end if (!$res)

   if (defined $help)
   {
      print "*** rmtSdcUpgrade -nfs_hostname cppm -nfs_ip 47.11.68.51 [-help | -validate_nfs | -backup | - ...]\n";
      return $OK;
   }

   # Validate IP syntax
   if ((!defined $nfs_server_ip) || !validateIP($nfs_server_ip))
   {
      baselog($LOG_ERROR, "Supplied NFS Server IP syntax is invalid", $TRUE);
      return $ERROR;
   } ## end if ((!defined $nfs_server_ip...
   $myIfInfo{'NFS_IP'} = $nfs_server_ip;

   #######################################################################################
   #
   # Perform actions based on input options (big else-if :-)
   # - other than "nfs_hostname" & "nfs_ip" only one option at a time is expected
   #
   if (defined $validate_linuxbase_ver_flag)
   {  #
      # Validate that the correct Linuxbase version is installed.
      #
      if (validate_linuxbase_version() != $OK)
      {
         baselog($LOG_ERROR, "Linuxbase Version Validation failed", $TRUE);
         return $ERROR;
      } ## end if (validate_linuxbase_version...
   } ## end if (defined $validate_linuxbase_ver_flag...
   #######################################################################################
   elsif (defined $validate_nfs_flag)
   {  #
      # Validate the availability of the SDS Server NFS services
      #
      if (validate_nfs($myIfInfo{'NFS_IP'}) != $OK)
      {
         baselog($LOG_ERROR, "Unable to validate the availability of NFS services on UCM/DS Server ($myIfInfo{'NFS_IP'})", $TRUE);
         return $ERROR;
      } ## end if (validate_nfs($myIfInfo...
   } ## end elsif (defined $validate_nfs_flag...
   #######################################################################################
   elsif (defined $backup_flag)
   {  #
      # Perform a local backup of the target applications
      # - the local backup will create a "/admin/nortel/upgrade_backup.tar.gz"
      #   file which would get picked up by the when the target reboots after
      #   being upgraded.
      #
      if (back_it_up() != $OK)
      {
         baselog($LOG_ERROR, "Unable to backup target server information", $TRUE);
         return $ERROR;
      } ## end if (back_it_up() != $OK...
   } ## end elsif (defined $backup_flag...
   #######################################################################################
   elsif (defined $update_grub_flag)
   {  #
      # Retrieve the target IP information
      #
      if (get_userinfo(\%myIfInfo) != $OK)
      {
         baselog($LOG_ERROR, "Unable to determine target server ELAN/TLAN IP information\n", $TRUE);
         return $ERROR;
      } ## end if (get_userinfo(\%myIfInfo...

      print "*** NFS IP = \"$myIfInfo{'NFS_IP'}\" \n";
      print "*** ELAN   = \"$myIfInfo{'ELAN_IP'}\" \n";
      print "*** TLAN   = \"$myIfInfo{'TLAN_IP'}\" \n";
      print "\n";
      
      # 
      # Create the "sdcparameters" to be used during the PreInstall stage
      #
      if ( create_sdcparameters( $myIfInfo{'TLAN_IP'} ) != $OK )
      {
         baselog($LOG_ERROR, "Unable to create sdcparameters configuration file", $TRUE);
         return $ERROR;
      }

      #
      # Create the "create_fspart.txt" file which will contain the target fs/partition information
      # to be used by "SetupPartitions.pm::buildPartInfoStrings()" during the PreInstall stage.
      # The intension is that during a SSH/HD target upgrade, the hard drive partitioning is maintained
      # 
      if ( create_fspart() )
      {
         baselog($LOG_ERROR, "Unable to create fspart configuration file", $TRUE);
         return $ERROR;
      }

      #
      # Determine the device partition that the "/var/log" is on since that
      # will become the temporary DM distribution folder on the target for the
      # HD installation. We will then modify the ks_hd.cfg and ks_cppm_hd.cfg to
      # reflect the corrected partition.
      #
      # Determine the "/var/log" ($LOG_K) partition 
      my $part = get_partition_dev($LOG_K);
      if ( not defined $part )
      {
         baselog($LOG_ERROR, "Unable to determine '$LOG_K' partition", $TRUE);
         return $ERROR;
      }

      #
      # Update/Create the GRUB "SDC INSTALL" Entry
      # - the grub entry will ensure that the target boots from the DM server
      #   distribution (temporary copy) that was copied over to the target
      #
      if ( is_cpdc() | is_cpmg() )
      {
         baselog($LOG_INFO, "This is a CPDC/CPMG Server", $TRUE);

         # Update the ks configuration file with the above partition for '/var/log'
         # - ensure that hdaX syncs up with the "default" ks_cppm_hd.cfg version
         #   (yes we are using the cppm hd kickstart for the cpdc & cpmg)
         #
         # - all references to 'hdaX' in the kickstart file should be changed to the
         #   discovered '/var/log' partition
         #
         my $KS_USB_HD_CFG = "$RMT_SDS_PATH/$DMS_SDS_PATH/ks_cppm_hd.cfg";

         # Change all 'hdaX' references to discovered '/var/log' partition number
         if ( update_ks_cfg($KS_USB_HD_CFG, ,'hdaX', $part) !=  $OK )
         {
            baselog($LOG_ERROR, "Failed to update $KS_USB_HD_CFG", $TRUE);
            return $ERROR;
         }

         # Update the $CPDC_KERN_PARAMS with the above partition
         if ( $CPDC_KERN_PARAMS =~ s/hd:(.+):/hd:$part:/ )
         {
            baselog($LOG_INFO, "Update \"$CPDC_KERN_PARAMS\"  with $part partition device", $TRUE);
         }
         else
         {
            baselog($LOG_ERROR, "Unable to update \"$CPDC_KERN_PARAMS\" with $part partition device", $TRUE);
            return $ERROR;
         }

         #
         # Update the GRUB configuration file with a INSTALL boot entry
         #
         if (update_grub_conf($CPDC_KERN_PARAMS) != $OK)
         {
            baselog($LOG_ERROR, "Unable to modify GRUB Configuration file", $TRUE);
            $retVal = $ERROR;
         }
      }
      elsif ( is_cppm() )
      {
         baselog($LOG_INFO, "This is a CPPM Server", $TRUE);

         # Update the ks configuration file with the above partition
         # - ensure that hdaX syncs up with the "default" ks_cppm_hd.cfg version
         my $KS_CPPM_HD_CFG = "$RMT_SDS_PATH/$DMS_SDS_PATH/ks_cppm_hd.cfg";

         # Change all 'hdaX' references to discovered '/var/log' partition number
         if ( update_ks_cfg($KS_CPPM_HD_CFG, ,'hdaX', $part) !=  $OK )
         {
            baselog($LOG_ERROR, "Failed to update $KS_CPPM_HD_CFG", $TRUE);
            return $ERROR;
         }

         # Update the $CPPM_KERN_PARAMS with the above partition
         if ( $CPPM_KERN_PARAMS =~ s/hd:(.+):/hd:$part:/ )
         {
            baselog($LOG_INFO, "Update \"$CPPM_KERN_PARAMS\"  with $part partition device", $TRUE);
         }
         else
         {
            baselog($LOG_ERROR, "Unable to update \"$CPPM_KERN_PARAMS\" with $part partition device", $TRUE);
            return $ERROR;
         }

         #
         # Update the GRUB configuration file with a INSTALL boot entry
         #
         if (update_grub_conf($CPPM_KERN_PARAMS) != $OK)
         {
            baselog($LOG_ERROR, "Unable to modify GRUB Configuration file", $TRUE);
            $retVal = $ERROR;
         }
      } ## end if (is_cppm())
      else
      {
         baselog($LOG_INFO, "This is a COTS Server", $TRUE);

         # Update the ks configuration file with the above partition
         # - ensure that sdaX syncs up with the "default" ks_hd.cfg version
         my $KS_HD_CFG = "$RMT_SDS_PATH/$DMS_SDS_PATH/ks_hd.cfg";

         # Change all 'sdaX' references to discovered '/var/log' partition number
         if ( update_ks_cfg($KS_HD_CFG, 'sdaX', $part) !=  $OK )
         {
            baselog($LOG_ERROR, "Failed to update $KS_HD_CFG", $TRUE);
            return $ERROR;
         }

         # Update the $COTS_KERN_PARAMS with the above partition
         if ( $COTS_KERN_PARAMS =~ s/hd:(.+):/hd:$part:/ )
         {
            baselog($LOG_INFO, "Update \"$COTS_KERN_PARAMS\" with $part partition device", $TRUE);
         }
         else
         {
            baselog($LOG_ERROR, "Unable to update \"$COTS_KERN_PARAMS\"  with $part partition device", $TRUE);
            return $ERROR;
         }

         #
         # Update the GRUB configuration file with a INSTALL boot entry
         # - an automatic reboot of the target will happen
         #
         if (update_grub_conf($COTS_KERN_PARAMS) != $OK)
         {
            baselog($LOG_ERROR, "Unable to modify GRUB Configuration file", $TRUE);
            $retVal = $ERROR;
         }
      } ## end else [ if (is_cppm())
      if ($retVal != $OK)
      {
         return $ERROR;
      }
   } ## end elsif (defined $update_grub_flag...
   #######################################################################################
   elsif (defined $archive_full_path)
   {  #
      # Perform the un-tarring of the archives specified by the UCM/DM Server
      # - The UCM/DM server will dump the archive into a local folder specified
      #   by $archive_full_path passed in when called
      #
      if (un_archive($archive_full_path) != $OK)
      {
         baselog($LOG_ERROR, "Unable to Un-Archive $archive_full_path", $TRUE);
         return $ERROR;
      }
      return $OK;
   } ## end elsif (defined $archive_full_path...
   #######################################################################################
   elsif (defined $clear_temp_repository_flag)
   {  #
      my $retval = $OK;

      baselog($LOG_INFO, "Clearing Temporary Archive Folders", $TRUE);

      # Clear the Temporary DM Repository (.../var/sds)
      # - we will need to be running as "root"
      #
      if ( !do_system([$RM, "-rf", "$RMT_SDS_PATH/$DMS_SDS_PATH"]) )
      {
         baselog($LOG_ERROR, "Unable to remove $RMT_SDS_PATH", $TRUE);
         $retval = $ERROR;
      }
      if ( !do_system([$MKDIR, "-p", "$RMT_SDS_PATH/$DMS_SDS_PATH"]) )
      {
         baselog($LOG_ERROR, "Unable to create $RMT_SDS_PATH", $TRUE);
         $retval = $ERROR;
      }
      if ( !do_system([$CHOWN, "-R", "nortel.nortel", $RMT_SDS_PATH]) )
      {
         baselog($LOG_ERROR, "Unable to chown $RMT_SDS_PATH", $TRUE);
         $retval = $ERROR;
      }
      baselog($LOG_INFO, "Purging of $RMT_SDS_PATH done", $TRUE);

      # Clear the Temporary Archive Folder
      if ( !do_system([$RM, "-rf", $RMT_ARCH_PATH]) )
      {
         baselog($LOG_ERROR, "Unable to remove $RMT_ARCH_PATH", $TRUE);
         $retval = $ERROR;
      }
      if ( !do_system([$MKDIR, "-p", $RMT_ARCH_PATH]) )
      {
         baselog($LOG_ERROR, "Unable to create $RMT_ARCH_PATH", $TRUE);
         $retval = $ERROR;
      }
      if ( !do_system([$CHOWN, "nortel.nortel", $RMT_ARCH_PATH]) )
      {
         baselog($LOG_ERROR, "Unable to chown $RMT_ARCH_PATH", $TRUE);
         $retval = $ERROR;
      }
      baselog($LOG_INFO, "Purging of $RMT_ARCH_PATH done", $TRUE);
      return $retval;
   }
   #######################################################################################
   else ## end elsif (defined $clear_temp_repository_flag
   {
      baselog($LOG_ERROR, "An action needs to be specified\n", $TRUE);
      return $ERROR;
   }
   return $OK;
}  # rmtSdcUpgrade()
