#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/chroot.nim.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# 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 
#
# WARNING:
#          Script is shipped "as-is" and supported using "best-effort" attempts.
#          Because of this fact, it is recommended that a backup spot remain
#          available if (for any reason) the chroot.nim script changes cannot
#          be recovered.
# ACTIONS:
#          this script creates an interactive environment within a SPOT by
#          first creating a chroot environment, then chrooting inside the SPOT
#          and starting a new shell session under the relative root path.
#
# RECOMMENDED USE:
#          use the chroot script to modify odm-specific tunables within a
#          NIM spot (such as options set when using bosdebug and vmo commands)
# EXAMPLE:
#
#          Run the following commands to set memory and kdb options:
#                  `/tmp/chroot.nim -a spot=spotName \
#                               -a lpp_source=lppName`
#                  `bosdebug -D -M`
#                  type `ctrl-d` to exit.  (or just type exit)
#
#          note:  When finished with chroot, rebuild the spot's boot images
#                  `nim -Fo check spotName`
#

# include common NIM shell defines/functions
NIM="/usr/sbin/nim"
NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib
M_CHATTR="${NIM_METHODS}/m_chattr"

#---------------------------- local defines     --------------------------------
server=""
spotObj=""
lppObj=""
err_out="/tmp/chroot.err_out"

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="spot lpp_source"
OPTIONAL_ATTRS=""
location=""

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
typeset c=""
${RM} $err_out 2>/dev/null

# set parameters from command line
while getopts :a:qv c
do
	case ${c} in

		a)			# validate the attr ass
					parse_attr_ass "${OPTARG}" 
					
					# include the assignment for use in this environment
					eval ${variable}=\"${value}\"
					;;

		q)			# show attr info
					cmd_what
					exit 0
					;;

		v)			# verbose mode (for debugging)
					set -x
					for i in $(typeset +f)
					do
						typeset -ft $i
					done
					;;

		\?)			# unknown option
					cmd_what
					exit 1
					;;
	esac
done

# check for missing attrs
ck_attrs

# confirm & set spot path for chroot global vars
if ${LSNIM} $spot >/dev/null 2>${err_out} ; then

	server=`${LSNIM} -a server $spot | ${AWK} '/server/ {print $3}'`

	if [[ $server = "master" ]] ; then

		spotObj="$spot"
		# chroot uses location var
		location=`${LSNIM} -a location $spotObj | ${AWK} '/location/ {print $3}'` 

	else
		echo "Sorry, cannot use chroot with remote resources. -- exiting." >${err_out}
	fi
fi

# print any errors
[[ -s ${err_out} ]] && ${CAT} ${err_out}

# confirm & set lpp_source path for chroot global vars
if ${LSNIM} $lpp_source >/dev/null 2>${err_out} ; then

	server=`${LSNIM} -a server $lpp_source | ${AWK} '/server/ {print $3}'`

	if [[ $server = "master" ]] ; then

		lppObj="$lpp_source"
		# chroot uses lpp_source var
		lpp_source=`${LSNIM} -a location $lppObj | ${AWK} '/location/ {print $3}'`
		[[ -d ${lpp_source}/installp/ppc ]] && lpp_source="${lpp_source}/installp/ppc"

	else
		echo "Sorry, cannot use chroot with remote resources. -- exiting." >${err_out}
	fi
fi

# print any errors
[[ -s ${err_out} ]] && ${CAT} ${err_out}


# last sanity check before chroot session is attempted

if [[ ! -d $location ]] || [[ ! -d $lpp_source ]] ; then

	echo "Unable to proceed. Check resource name(s) also verify path location(s)"
	exit 1
else

	# setup chroot environment
	setup_chroot_env
	${SET_CHROOT_LIBPATH}
	
	# mount the lpp_source
	nim_mount ${lpp_source} ${new_root}/$$
	
	# add session id to spot comment field
	# on a graceful exit, we'll update this to show completion timestamp
	if tty -s
	then
		$NIM -o change -a comments="chroot session $$ is enabled on `tty`" $spotObj
	fi
	
	# sensitive process : place "unavailable" state on spot & lpp_source
	${M_CHATTR} -a Rstate=unavailable $spotObj
	${M_CHATTR} -a Rstate=unavailable $lppObj

	# chroot inside the SPOT
	echo " Starting chroot session $$. Type ^d to exit."
	${chroot} /bin/ksh

	# sensitive process has completed
	${M_CHATTR} -a Rstate=available $spotObj
	${M_CHATTR} -a Rstate=available $lppObj

	# update spot comment field
	$NIM -o change -a comments="chroot session $$ has ended" $spotObj
fi

# all done
exit 0
