#!/bin/bash # Script: patchme.bash # Task: automatically search and apply patches in a TSP medium # Author: Benjamin 'blindcoder' Schieder # Version: 0.1-20090322 # global variables SCRIPTNAME=$(basename ${0} .bash) EXIT_SUCCESS=0 EXIT_FAILURE=1 EXIT_ERROR=2 EXIT_BUG=10 # variables for option switches with default values VERBOSE="n" OPTFILE="" PATCHFILE="/tmp/patches.txt" MEDIUM="$( ls -d /export/home/*/. | head -n 1 )" UNZIPPARAMETER="-q" TARPARAMETER="" # functions function usage { exec 1>&2 echo "Usage: ${SCRIPTNAME} [-h] [-v] [-p patchfile] [-m path/to/medium]" echo "Parameters:" echo " -p Path to file containing Solaris Patch-IDs (1 per line)" echo " Defaults to '${PATCHFILE}'" echo " -m Path to medium" echo " Defaults to '${MEDIUM}'" echo " -v verbose mode" echo " -h Help" [[ ${#} -eq 1 ]] && exit ${1} || exit ${EXIT_FAILURE} } while getopts 'p:m:vh' OPTION ; do case ${OPTION} in v) VERBOSE=y unset UNZIPPARAMETER TARPARAMETER="v" ;; m) MEDIUM="${OPTARG}" ;; p) PATCHFILE="${OPTARG}" ;; h) usage ${EXIT_SUCCESS} ;; \?) echo "unknown option \"-${OPTARG}\"." >&2 usage ${EXIT_ERROR} ;; :) echo "option \"-${OPTARG}\" requires an argument." >&2 usage ${EXIT_ERROR} ;; *) echo "Impossible error. parameter: ${OPTION}" >&2 usage ${EXIT_BUG} ;; esac done # skip parsed options shift $(( OPTIND - 1 )) if (( $# > 0 )) ; then echo "Unknown arguments $*" >&2 usage ${EXIT_ERROR} fi if ! who -r | grep "run-level 1" >/dev/null ; then echo "YOU ARE NOT IN SINGLE USER MODE!" echo "Please press ^C to stop patch installation" echo "or wait 30 seconds to start anyway." for x in 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 ; do echo -e "${x} \r" done echo fi mkdir -p /tmp/patches cd /tmp/patches for patch in $( cat ${PATCHFILE} ); do patchlevel="${patch##*-}" patchfile="`find ${MEDIUM} -name "${patch}*" -type f`" if [ -z "${patchfile}" ] ; then patchfile="`find ${MEDIUM} -name "${patch%-*}*" -type f`" fi if [ -z "${patchfile}" ] ; then echo "Could not find patch ${patch}" >&2 continue else cp "${patchfile}" /tmp/patches fi foundpatchlevel="${patchfile##*-}" foundpatchlevel="${foundpatchlevel%%.*}" if [ ${foundpatchlevel} -lt ${patchlevel} ] ; then echo "${patchfile##*/} patchlevel is too old (${patchlevel} expected!)" >&2 continue fi patchfile="/tmp/patches/${patchfile##*/}" [ "${patchfile: -4}" == ".zip" ] && unzip ${UNZIPPARAMETER} "${patchfile}" [ "${patchfile: -6}" == ".tar.Z" ] && uncompress "${patchfile}" && tar x${TARPARAMETER}f "${patchfile%.Z}" [ "${patchfile: -7}" == ".tar.gz" ] && gzcat "${patchfile}" | tar x${TARPARAMETER}f - patchadd ${patchfile%%.*} retval=$? [ ${retval} -ne 0 ] && echo "${patch} exited with exit-status: ${retval}" >&2 done