#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/mksysb/cmdbsys_lib.sh 1.4.2.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2012 
# 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 

export PATH="/usr/bin:/usr/sbin:/dev:/etc:$PATH"
######################### create_data_file ##########################
##
## Name:  create_data_file
##
## Function:  The create_data_file function is responsible for creating
##            a mkcd.data file.  This file will contain a timestamp,
##            media volume.  The timestamp will be used to verify if
##            the multi-volume media belong to one another.  The
##            volume ID will indicate the volume number in a set.  If
##            the volume ID is left blank, then the media is not
##            multi-volume.  This file is also used with media other 
##            than CD.
##
## Parameters:  VOLUME_ID - volume number
##              MORE_VOL  - more volumes
##              VGTYPE    - rootvg or other vg
##              BOOTABLE  - bootable
##              LOCATION  - where to put mkcd.data
##              TIMESTAMP - date
##
## Returns:  None
##
## Calling command:  mksysb 
##
#####################################################################
create_data_file()
{
typeset mkcd_data_file
typeset VOLUME_ID=$1
typeset MORE_VOL=$2
typeset VGTYPE=$3
typeset BOOTABLE=$4
typeset LOCATION=$5
typeset TIMESTAMP=$6

##########################################################
## Create the mkcd.data, by adding the time stamp to the
## file.  This information will be used to verify that
## all the media volumes are from the same creation.
##########################################################

        mkcd_data_file="/tmp/mkcd.data_${PID}"
        echo "DATE=$TIMESTAMP" >$mkcd_data_file

##########################################################
## Put the volume ID in the mkcd.data file.  If the media is
## the first of a one volume set or is the last media in a
## multi-volume set, then put an annotation on the 
## mkcd.data file.
##########################################################

        echo "VOLUME=$VOLUME_ID" >>$mkcd_data_file
        if [ $MORE_VOL -eq 0 ];then
                echo "LASTVOLUME" >>$mkcd_data_file
        fi

##########################################################
## Populate the mkcd.data file with the media type.
##########################################################

        if [ "$VGTYPE" != "rootvg" ]; then
                echo "TYPE=savevg" >>$mkcd_data_file
        elif [ $BOOTABLE -eq 1 ];then
                echo "TYPE=rootvg(bootable)" >>$mkcd_data_file
        else
                echo "TYPE=rootvg(nonbootable)" >>$mkcd_data_file
        fi
        cp $mkcd_data_file ${LOCATION}/mkcd.data
        rm -f $mkcd_data_file

} ## end of create_data_file

######################### is_pid_alive ##############################
##
#####################################################################
is_pid_alive()
{
   typeset PID="$1"
   typeset ALIVE=0

   ps -p $PID > /dev/null && ALIVE=1

   echo "$ALIVE"

   return 0

}  # end of is_pid_alive()

######################### mount_fs_wpar #############################
##
## Name:  mount_fs_wpar
##
## Function:  The mount_fs_wpar function is responsible for mount any
##            filesystem that belongs to any WPAR in the Defined state
##            for backup options.
## Parameters:  None
##
## Returns:  NONE
##
##
## Calling Routine:  mkszfile, mkcd, mkdvd, mksysb
##
## This function is also in the altlib.sh file.  If you modify this 
## function, please modify the one in altlib.sh also.
########################################################################
mount_fs_wpar()
{
D_WPARS=`/usr/sbin/lswpar -q -s D -a name 2>/dev/null`
if [ -n "$D_WPARS" ]
then

        /usr/bin/dspmsg -s 6 mksysb.cat 1 \
        "Mounting all workload partition file systems.\n"
        set -A MOUNTED_FS
        typeset k=0
        for i in $D_WPARS
        do
                M_WPARS_FS=`/usr/sbin/lswpar -qcM -a vfs,mountpoint "$i" | grep ^jfs | cut -d: -f2 2>/dev/null`
                for j in $M_WPARS_FS
                do
                        MESG=`/usr/sbin/mount $j`
                        RC=$?
                        if [ $RC -eq 0 ]; then
                                MOUNTED_FS[$k]=$j;
                                k=$k+1
                        else
                                echo "$MESG"
                        fi
                done
        done
else
	NNFLAG="no"
fi
} # end of mount_fs_wpar

######################### populate_media_fs ###############################
##
## Name:  populate_media_fs
##
## Function:  The populate_media_fs function is responsible for populating
##            the media file structure with the data specified in the
##            required and optional prototype files.
##
## Parameters:  destination directory, proto file, is proto file optional
##              name of calling command, existing backup image
##
## Returns:  0 for success or 1 for failure
##
## Calling Routine:  Currently only called from mksysb
##
########################################################################

function populate_media_fs
{

typeset cd_file
typeset err_file_list
typeset fset
typeset files_not_found
typeset separator
typeset sys_file
typeset title1
typeset title2

##########################################################
## Assign the passed parameters and assign necessary
## local variables.
##########################################################

typeset dest_dir=$1      ## destination directory for population
typeset cdfs_list=$2     ## the CD file system proto file
typeset optional_file=$3 ## optional proto list
typeset name=$4          ## calling command
typeset mksysb_image=$5  ## specified vg backup image
typeset olddir
typeset tmpsys_file="/tmp/.sys_file$$"
typeset tmpcd_file="/tmp/.cd_file$$"

        err_file_list=
        files_not_found=${dest_dir}/.files_not_found
        rm -f $files_not_found 2>/dev/null

##########################################################
## Extract CD prototype file list and use it to extract
## files from the specified backup image.
##########################################################

        if [ -n "$mksysb_image" ]; then
           olddir=`pwd`
           cd $dest_dir
           >$tmpsys_file
           >$tmpcd_file
           /usr/sbin/restore -xqf $mksysb_image .$cdfs_list >/dev/null
           /usr/bin/cat .$cdfs_list | grep -v "^#" | \
              while read sys_file cd_file fset; do
                 echo ".$sys_file" >> $tmpsys_file
                 if [ $sys_file != $cd_file ]; then
                    echo ".$sys_file .$cd_file" >> $tmpcd_file
                 fi
              done
           xargs /usr/sbin/restore -xqf $mksysb_image < $tmpsys_file >/dev/null
           if [ $? -ne 0 ]; then
              /usr/bin/dspmsg -s 5  mksysb.cat 94 \
                 "0512-300 $name: Error in routine: $0.\n" $name $0
              return 1
           fi
           /usr/bin/cat $tmpcd_file | grep -v "^#" | \
              while read sys_file cd_file; do
                 if [ ! -d `/usr/bin/dirname $cd_file` ]; then
                    /usr/bin/mkdir -p `/usr/bin/dirname $cd_file`
                    if [ $? -ne 0 ]; then
                       /usr/bin/dspmsg -s 5  mksysb.cat 94 \
                          "0512-300 $name: Error in routine: $0.\n" $name $0
                       return 1
                    fi
                 fi
                 /usr/bin/cp -p -R $sys_file $cd_file 2>$MSGBUF
                 if [ $? -ne 0 ]; then     ## cp command return code
                    if [ $optional_file -eq 1 ]; then
                       /usr/bin/dspmsg -s 5  mksysb.cat 108 \
                          "Warning: $name was unable to \
copy the optional file: $name $sys_file\n\
and did not include this file in the backup.\n" $name $sys_file

##########################################################
## If the file is not optional and the copy attempt failed
## then log the error and return unsuccessfully.
##########################################################

                    else
                       /usr/bin/cat $MSGBUF
                       /usr/bin/dspmsg -s 5  mksysb.cat 94 \
                          "0512-300 $name: Error in routine: $0.\n" $name $0
                       cd ${olddir}
                       return 1
                    fi
                 fi
              done
           rm -f .$cdfs_list $tmpsys_file $tmpcd_file 2>/dev/null
           cd ${olddir}
           return
        fi

##########################################################
## No previously existing vg backup specified.  Read each
## line in the CD prototype file list and copy the file
## from the system to the target directory
##########################################################

        /usr/bin/cat $cdfs_list | grep -v "^#" | \
           while read sys_file cd_file fset; do

##########################################################
## The next loop is used to process non existent files on
## the source system.  This loop will also be used to
## compile a list of missing required files.
##########################################################

              if [ ! -r "$sys_file" ] || \
                 [ -n "$err_file_list" ]; then

##########################################################
## Create a file that will exist on the media that
## contains a list of optional files that are missing
##########################################################
                 if [ $optional_file -eq 1 ]; then
                    if [ -s "$files_not_found" ]; then
                       printf '%-40s %s \n' $sys_file $fset >> $files_not_found
                    else
                       title1=`/usr/bin/dspmsg -s 5 mksysb.cat 29 \
                              "Files not available on the system"`
                       title2=`/usr/bin/dspmsg -s 5 mksysb.cat 30 "Fileset\n"`
                       separator="-------------------"
                       printf '%-40s %s\n' "$title1" "$title2" > $files_not_found
                       printf '%-40s %s\n' "$separator" "$separator" >> $files_not_found
                       printf '%-40s %s \n' $sys_file $fset >> $files_not_found
                    fi
                 elif [ ! -r "$sys_file" ]; then
                    mesg="$sys_file\t$fset\n"
                    err_file_list="$err_file_list$mesg"
                 fi
                 continue
              fi

#########################################################
## Ensure the directory path exists before copying each
## file.  If the directory does not exist, create it.
##########################################################

              if [ ! -d `/usr/bin/dirname ${dest_dir}${cd_file}` ]; then
                 /usr/bin/mkdir -p `/usr/bin/dirname ${dest_dir}${cd_file}` 2>$MSGBUF
                 if [ $? -ne 0 ]; then     ## mkdir command return code
                    /usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
                    /usr/bin/dspmsg -s 5  mksysb.cat 94 \
                       "0512-300 $name: Error in routine: $0.\n" $name $0
                    return 1
                 fi
              fi

##########################################################
## Copy the system file to the media path.  If an error
## occurs while copying the file and the OPTIONAL flag
## is set, then log the file as not included.
##########################################################

              /usr/bin/cp -p -R $sys_file ${dest_dir}${cd_file} 2>$MSGBUF
              if [ $? -ne 0 ]; then     ## cp command return code
                 if [ $optional_file -eq 1 ]; then
                    /usr/bin/dspmsg -s 5  mksysb.cat 108 \
                       "Warning: $name was unable to copy the optional \
file: $name $sys_file\n\
and did not include this file in the backup.\n" $sys_file

##########################################################
## If the file is not optional and the copy attempt failed
## then log the error and return unsuccessfully.
##########################################################

                 else
                    /usr/bin/cat $MSGBUF
                    /usr/bin/dspmsg -s 5  mksysb.cat 94 \
                       "0512-300 $name: Error in routine: $0.\n" $name $0
                    return 1
                 fi
              fi
              /usr/bin/chmod +r ${dest_dir}${cd_file} > /dev/null 2>&1
           done

##########################################################
## If a list of missing required files exists, then
## display an error message that contains all the missing
## files and the associated filesets.  Then return from
## the routine unsuccessfully.
##########################################################
           if [ -n "$err_file_list" ]; then
              /usr/bin/dspmsg -s 6  mksysb.cat 3 \
                 "0512-323 $name: The following files are required for the\n\
creation of the backup image and are not available on the source system:\n\
${err_file_list}\n\
The files can be installed from the listed filesets.\n" $name $err_file_list
              return 1
           fi

} ## end of populate_media_fs

######################### unmount_wpar_fs #############################
##
## Name:  unmount_wpar_fs
##
## Function:  The unmount_wpar_fs function is responsible for unmount any
##            filesystem mounted for backup purposes.
##
## Parameters:  None
##
## Returns: NONE
##
##
## Calling Routine: mkszfile, mksysb, mkcd, mkdvd
##
## This function is also in the altlib.sh file.  If you modify this 
## function, please modify the one in altlib.sh also.
########################################################################
unmount_wpar_fs()
{
        # If any filesystems were mounted by this process, unmount them in
        # reverse order.
        /usr/bin/dspmsg -s 6 mksysb.cat 2 \
        "Unmounting all workload partition file systems.\n"
        typeset i=${#MOUNTED_FS[*]}
        while [[ $i -gt 0 ]]; do
                i=$((i-1))
                MSG=`/usr/sbin/unmount -f  ${MOUNTED_FS[$i]}`
                RC=$?
                if [ $RC -ne 0 ]; then
                        echo "$MESG"
                fi
        done
} # end of unmount_wpar_fs
