phatboislym

update.sh

Oct 11th, 2022 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.99 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # This script is copied into the guest with the 32 and 64 bit upgrader binaries.
  5. # It selects one of the binaries based on the userland bitness of the guest and
  6. # runs it.
  7. #
  8.  
  9. SCRIPT=$0
  10. BINDIR=`dirname $0`
  11. DBPATH="/etc/vmware-tools/locations"
  12. LOGFILE='/var/log/vmware-tools-upgrader.log'
  13. UPGRADER64='vmware-tools-upgrader-64'
  14. UPGRADER32='vmware-tools-upgrader-32'
  15. ARGS="$@"
  16.  
  17. # Setup the logfile in a spot that avoids SElinux errors and allows for
  18. # better debugging of upgrader errors.  Add the date as the first line
  19. # so we can get a better handle on the situation.  This also effectively
  20. # deletes any previous log file that may have been there.
  21. date >$LOGFILE
  22. chmod 0600 $LOGFILE
  23.  
  24. # If the temp directory on the system has a 'noexec' property, the upgrade
  25. # binary as well as the vmware-install.pl will need to be executed from
  26. # another location.
  27.  
  28. # Function to return the last positional parameter passed in.
  29. function last_field {
  30.    eval last_value='$'$#
  31.    echo $last_value
  32. }
  33.  
  34. # Check whether the single argument is a directory located in a 'noexec'
  35. # filesystem.  If the directory is on a 'noexec' filesystem, return TRUE (0).
  36. # Otherwise return FALSE (1).
  37. function check_tmp_noexec {
  38.    if [ $# -eq 1 -a -n "$1" -a -d "$1" ];
  39.    then
  40.       df_out="`df $1 | tail -1`"
  41.       mount_point=`last_field $df_out`
  42.       if [ -n "$mount_point" -a -d "$mount_point" ];
  43.       then
  44.          if grep -Eq "^[^ ]+ $mount_point [^ ]+ ([^ ]*,)?noexec[, ]" \
  45.                  /proc/mounts;
  46.          then
  47.             # Looking at a 'noexec' filesystem; return TRUE
  48.             return `true`
  49.          fi
  50.       fi
  51.    fi
  52.    # Return FALSE.
  53.    return `false`
  54. }
  55.  
  56. # Create a temporary directory in the same directory as the VMware Tools
  57. # LIBDIR which must already exist on a non-noexec filesystem.
  58. function create_executable_temp_dir {
  59.    libdir=`grep "^answer LIBDIR" $DBPATH | tail -1 | cut -d ' ' -f3`
  60.    if [ -n "$libdir" ];
  61.    then
  62.       libdir=`dirname $libdir`
  63.    fi
  64.    if [ -z "$libdir" -o ! -d "$libdir" ];
  65.    then
  66.       # Default to /usr/lib on Linux.
  67.       libdir=/usr/lib
  68.    fi
  69.    echo `mktemp -d -p $libdir vmware-tools-temp-XXXXXX`
  70. }
  71.  
  72. # Close all the inherited FDs. See bug 138500 for details.
  73. MAXFD=$(expr `ulimit -n` - 1)
  74. for fd in `seq 3 $MAXFD`; do
  75.    eval "exec $fd>&-"
  76. done
  77.  
  78. # Check userland bitness.  Remove the unneeded upgrader binary.
  79. which file >& /dev/null
  80. if [ $? -ne 0 ];then
  81.     echo "Dependency file package not installed" >>$LOGFILE 2>&1
  82.     exit 1
  83. fi
  84. if LANG=C file -L -- "$SHELL" | grep 64-bit >& /dev/null;
  85. then
  86.    UPGRADER=$UPGRADER64;
  87.    UPGRADER_UNUSED=$BINDIR/$UPGRADER32
  88. else
  89.    UPGRADER=$UPGRADER32;
  90.    UPGRADER_UNUSED=$BINDIR/$UPGRADER64
  91. fi
  92. echo "Deleting the unneeded upgrader binary: ${UPGRADER_UNUSED}" \
  93.      >>$LOGFILE 2>&1
  94. rm -f ${UPGRADER_UNUSED}
  95.  
  96. # Pass a '-d' option to the upgrader to direct the binary to delete itself
  97. # after starting execution.
  98. UPGRADERARGS="-d"
  99.  
  100. # Check if the executable is currently in a 'noexec' $BINDIR.
  101. if `check_tmp_noexec $BINDIR`;
  102. then
  103.    # Get an executable temp directory and move the UPGRADER binary into
  104.    # that directory.  Pass the directory to the upgrader; the '-d' option
  105.    # directs the binary to delete itself after starting execution.
  106.    newdir=`create_executable_temp_dir`
  107.    mv $BINDIR/$UPGRADER $newdir/$UPGRADER
  108.    UPGRADERARGS="$UPGRADERARGS -t $newdir"
  109.    # Set BINDIR to the new temporary directory.
  110.    BINDIR=$newdir
  111. fi
  112.  
  113. # Set UPGRADER to a full path.
  114. UPGRADER=$BINDIR/$UPGRADER
  115.  
  116. # Complete argument list based on whether Tools is
  117. # already installed or not. If user passes args, though,
  118. # use them blindly.
  119. # (-s checks to see if a file exists and has nonzero length.)
  120.  
  121. if [ ! -s "$DBPATH" ] && [ "x$ARGS" = "x" ];
  122. then
  123.    UPGRADERARGS="$UPGRADERARGS -p --default --force-install"
  124. else
  125.    # Has no effect if $@ was empty on entry.
  126.    UPGRADERARGS="$UPGRADERARGS $ARGS"
  127. fi
  128.  
  129. # cope with "-p" which needs all subsequent args as one.
  130. PARGS=""
  131. set -- $UPGRADERARGS
  132. UPGRADERARGS=""
  133. while [ $# -gt 0 ]; do
  134.    UPGRADERARGS="$UPGRADERARGS $1"
  135.    if [ "x-p" = "x$1" ];
  136.    then
  137.       shift
  138.       PARGS="$@"
  139.       break
  140.    fi
  141.    shift
  142. done
  143.  
  144. chmod +x $UPGRADER
  145. # Run the upgrader.
  146. # Delay starting the upgrader till rhgb-client (if present and running) has
  147. # quit.
  148. RHGBCLNT=/usr/bin/rhgb-client
  149. if [ -x $RHGBCLNT ]; then
  150.    while true ; do
  151.       if ! $RHGBCLNT --ping ; then
  152.          break
  153.       fi
  154.       echo "Waiting for rhgb to exit, will sleep for 30 seconds..." >> $LOGFILE
  155.       sleep 30
  156.    done
  157. fi
  158.  
  159. # Before executing the UPGRADER binary, remove this script from the guest
  160. echo "Removing this script from the guest: $SCRIPT" >>$LOGFILE 2>&1
  161. rm -f $SCRIPT
  162.  
  163. echo "Executing \"$UPGRADER $UPGRADERARGS $PARGS\"" >>$LOGFILE 2>&1
  164. if [ "x$PARGS" = "x" ]
  165. then
  166.    exec $UPGRADER $UPGRADERARGS >>$LOGFILE 2>&1
  167. else
  168.    exec $UPGRADER $UPGRADERARGS "$PARGS" >>$LOGFILE 2>&1
  169. fi
  170.  
  171. # If here then the exec failed.
  172. echo "Exec call failed!" >>$LOGFILE 2>&1
  173. exit 1
Add Comment
Please, Sign In to add comment