Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.78 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # VMware Installer Launcher
  4. #
  5. # This is the executable stub to check if the VMware Installer Service
  6. # is installed and if so, launch it.  If it is not installed, the
  7. # attached payload is extracted, the VMIS is installed, and the VMIS
  8. # is launched to install the bundle as normal.
  9.  
  10. # Architecture this bundle was built for (x86 or x64)
  11. ARCH=x64
  12.  
  13. if [ -z "$BASH" ]; then
  14.    # $- expands to the current options so things like -x get passed through
  15.    if [ ! -z "$-" ]; then
  16.       opts="-$-"
  17.    fi
  18.  
  19.    # dash flips out of $opts is quoted, so don't.
  20.    exec /usr/bin/env bash $opts "$0" "$@"
  21.    echo "Unable to restart with bash shell"
  22.    exit 1
  23. fi
  24.  
  25. set -e
  26.  
  27. ETCDIR=/etc/vmware
  28.  
  29. ### Offsets ###
  30. # These are offsets that are later used relative to EOF.
  31. FOOTER_SIZE=52
  32.  
  33. # This won't work with non-GNU stat.
  34. FILE_SIZE=`stat --format "%s" "$0"`
  35. offset=$(($FILE_SIZE - 4))
  36.  
  37. MAGIC_OFFSET=$offset
  38. offset=$(($offset - 4))
  39.  
  40. CHECKSUM_OFFSET=$offset
  41. offset=$(($offset - 4))
  42.  
  43. VERSION_OFFSET=$offset
  44. offset=$(($offset - 4))
  45.  
  46. PREPAYLOAD_OFFSET=$offset
  47. offset=$(($offset - 4))
  48.  
  49. PREPAYLOAD_SIZE_OFFSET=$offset
  50. offset=$(($offset - 4))
  51.  
  52. LAUNCHER_SIZE_OFFSET=$offset
  53. offset=$(($offset - 4))
  54.  
  55. PAYLOAD_OFFSET=$offset
  56. offset=$(($offset - 4))
  57.  
  58. PAYLOAD_SIZE_OFFSET=$offset
  59. offset=$(($offset - 4))
  60.  
  61. # Rest of the offsets ommitted
  62.  
  63. ### End offsets ###
  64.  
  65. # Short name (ie, vmware-workstation).  This isn't technically correct
  66. # since there could be multiple product components in a bundle.
  67. PRODUCT_NAME=vmware-player
  68.  
  69. # Called when the script exits
  70. #
  71. # Arguments:
  72. #    None
  73. #
  74. # Side effects:
  75. #    - VMIS_TEMP and PREPAYLOAD is removed unless VMIS_KEEP_TEMP is set
  76. on_exit() {
  77.    if [ -e "$VMIS_TEMP" -a -z "$VMIS_KEEP_TEMP" ]; then
  78.       rm -rf "$VMIS_TEMP"
  79.    fi
  80.  
  81.    if [ -e "$PREPAYLOAD" -a -z "$VMIS_KEEP_TEMP" ]; then
  82.       rm -rf "$PREPAYLOAD"
  83.    fi
  84. }
  85.  
  86. trap on_exit EXIT
  87. trap "" USR1
  88.  
  89. # Retrives and sets the various lengths that are extracted from the
  90. # footer of the file.
  91. #
  92. # Arguments:
  93. #    $1 => bundle to get the lengths from
  94. #
  95. # Side effects:
  96. #    - MAGIC_NUMBER, LAUNCHER_SIZE, and PAYLOAD_SIZE are set.
  97. #
  98. # Returns:
  99. #    0 if successful, else 1
  100. set_lengths() {
  101.    local file="$1"
  102.    if [ ! -s "$file" ]; then
  103.       echo "$file does not exist"
  104.       exit 1
  105.    fi
  106.  
  107.    # XXX: put extraction in its own function
  108.    MAGIC_NUMBER=`od -An -t u4 -N 4 -j $MAGIC_OFFSET "$file" | tr -d ' '`
  109.  
  110.    if [ "$MAGIC_NUMBER" != "907380241" ]; then
  111.       echo "magic number does not match"
  112.       exit 1
  113.    fi
  114.  
  115.    LAUNCHER_SIZE=`od -An -t u4 -N 4 -j $LAUNCHER_SIZE_OFFSET "$file" | tr -d ' '`
  116.    PAYLOAD_SIZE=`od -An -t u4 -N 4 -j $PAYLOAD_SIZE_OFFSET "$file" | tr -d ' '`
  117.    PREPAYLOAD_SIZE=`od -An -t u4 -N 4 -j $PREPAYLOAD_SIZE_OFFSET "$file" | tr -d ' '`
  118.  
  119.    SKIP_BYTES=$(($PREPAYLOAD_SIZE + $LAUNCHER_SIZE))
  120.  
  121.    return 0
  122. }
  123.  
  124. # Determines whether the user land is 32 or 64-bit.
  125. #
  126. # Side effects:
  127. #    None.
  128. #
  129. # Returns:
  130. #    "x86" or "x64" on success with error code 0.  Exits with non-zero
  131. #    status and undefined text on failure.
  132. get_arch() {
  133.    # First byte is the ELF magic number.  The 5th byte is whether it's
  134.    # a 32 or 64-bit machine (1 or 2, respectively).  See `man elf` for
  135.    # details.
  136.    local ELF_MAGIC=7f
  137.  
  138.    if [ "`od -N1 -An -t x1 < /bin/sh | tr -d ' '`" != "$ELF_MAGIC" ]; then
  139.       exit 1
  140.    fi
  141.  
  142.    local arch=`od -j4 -N1 -An -t u1 < /bin/sh | tr -d ' '`
  143.  
  144.    case $arch in
  145.       1)
  146.          echo "x86"
  147.      exit 0
  148.      ;;
  149.       2)
  150.          echo "x64"
  151.          exit 0
  152.      ;;
  153.       *)
  154.          exit 1
  155.          ;;
  156.    esac
  157. }
  158.  
  159. # Determines if path is relative.
  160. #
  161. # Side effects:
  162. #    None.
  163. #
  164. # Returns:
  165. #    0 if relative, otherwise 1.
  166. is_relative() {
  167.     local path="$1"
  168.     shift
  169.  
  170.     [ "${path:0:1}" != "/" ]
  171.     return
  172. }
  173.  
  174. # Extracts the payload data into a temporary directory.
  175. #
  176. # Side effects:
  177. #    - temporary directory is created
  178. #    - VMIS_TEMP is set to temporary directory
  179. #
  180. # Returns:
  181. #    None
  182. extract_self() {
  183.    VMIS_TEMP=`mktemp -d /tmp/vmis.XXXXXX`
  184.    local file="$0"
  185.    local filter=""
  186.    local bootstrapper="PREPAYLOAD"/bootstrapper-gtk
  187.  
  188.    if [ ! -d "$VMIS_TEMP" ]; then
  189.       echo "Unable to create temporary directory."
  190.       exit 1
  191.    fi
  192.  
  193.    if is_relative "$file"; then
  194.       file="$PWD/$file"
  195.    fi
  196.  
  197.    if [ -e "$bootstrapper" ] && "$bootstrapper" --validate 2> /dev/null; then
  198.       filter=' | "$PREPAYLOAD"/bootstrapper-gtk --title "VMware Installer" \
  199.                --message "Please wait while extracting the VMware Installer..." \
  200.                --total $PAYLOAD_SIZE"'
  201.    else
  202.       echo -n "Extracting VMware Installer..."
  203.    fi
  204.  
  205.    (cd $VMIS_TEMP && dd if="$file" ibs=$SKIP_BYTES obs=1024 skip=1 2> /dev/null \
  206.       $filter | gunzip -c 2> /dev/null | tar -xf - 2> /dev/null)
  207.  
  208.    if [ ! -e "$bootstrapper" ]; then
  209.       echo "done."
  210.    fi
  211. }
  212.  
  213. extract_prepayload() {
  214.    PREPAYLOAD=`mktemp -d /tmp/vmis.XXXXXX`
  215.    local file="$0"
  216.  
  217.    if [ ! -d "$PREPAYLOAD" ]; then
  218.       echo "Unable to create temporary directory."
  219.       exit 1
  220.    fi
  221.  
  222.    if is_relative "$file"; then
  223.       file="$PWD/$file"
  224.    fi
  225.  
  226.    (cd $PREPAYLOAD && dd if="$file" ibs=$LAUNCHER_SIZE obs=1024 skip=1 2> /dev/null | \
  227.       gunzip -c 2> /dev/null | tar -xf - 2> /dev/null)
  228. }
  229.  
  230. # Determines if a program is in the user's PATH.  This is used instead
  231. # of the external which because Solaris' version does not work as
  232. # expected.
  233. #
  234. # Side effects:
  235. #    None
  236. #
  237. # Arguments:
  238. #    $1 => program to check
  239. #
  240. # Returns:
  241. #    0 if found, else 1
  242. internal_which() {
  243.    local binary="$1"
  244.  
  245.    for dir in `echo $PATH | tr ":" "\n"`; do
  246.       if [ -s "$dir/$binary" -a -x "$dir/$binary" ]; then
  247.          return 0
  248.       fi
  249.    done
  250.  
  251.    return 1
  252. }
  253.  
  254.  
  255. # Installs the installer and the current bundle.
  256. #
  257. # Arguments:
  258. #    $1 => file source
  259. #    $2 => true if show help
  260. #    $3 => path to bundle
  261. #
  262. # Returns:
  263. #    None
  264. install() {
  265.    local source="$1"/install
  266.    shift
  267.    local help="$1"
  268.    shift
  269.    local bundle="$1"
  270.    shift
  271.  
  272.    if [ ! -d "$source" ]; then
  273.       echo "$source does not exist" >&2
  274.       exit 1
  275.    fi
  276.  
  277.    export VMWARE_BOOTSTRAP="$VMIS_TEMP"/bootstrap
  278.  
  279.    cp -f "$source"/vmware-installer/bootstrap "$VMWARE_BOOTSTRAP"
  280.    sed -i -e "s,@@LIBDIR@@,$source,g" "$VMWARE_BOOTSTRAP"
  281.    sed -i -e "s,@@VMWARE_INSTALLER@@,$source/vmware-installer,g" "$VMWARE_BOOTSTRAP"
  282.  
  283.    . "$VMWARE_BOOTSTRAP"
  284.  
  285.    local installer="$VMWARE_INSTALLER"/vmware-installer
  286.  
  287.    if [ -n "$help" ]; then
  288.       "$installer" --help
  289.       exit 0
  290.    fi
  291.  
  292.    # We must fixup the paths in Pango or the fonts will be all messed up
  293.    local libconf="$source"/vmware-installer/lib/libconf
  294.    for file in etc/pango/pangorc etc/pango/pango.modules etc/pango/pangox.aliases \
  295.                etc/gtk-2.0/gdk-pixbuf.loaders etc/gtk-2.0/gtk.immodules; do
  296.        sed -i -e "s,@@LIBCONF_DIR@@,$libconf,g" "$libconf/$file"
  297.    done
  298.  
  299.    # Pass all options the user passed in so that the correct UI type
  300.    # gets set.
  301.    if ! "$installer" --set-setting vmware-installer.libconf "$libconf"  \
  302.                      --install-component "$source"/vmware-installer     \
  303.              --install-bundle "$bundle" "$@"; then
  304.       exit 1
  305.    fi
  306.  
  307.    return 0
  308. }
  309.  
  310.  
  311. # Uninstall existing bundle installation.
  312. #
  313. # Arguments:
  314. #    $1 => etcdir
  315. #    $2 => suffix to add to vmware-uninstall (ie -vix)
  316. #
  317. # Returns:
  318. #    0 on success
  319. uninstall_bundle() {
  320.    etcdir="$1"
  321.    shift
  322.    suffix="$1"
  323.    shift
  324.  
  325.    local bootstrap="$etcdir"/bootstrap
  326.  
  327.    if [ -e "$bootstrap" ]; then
  328.       # Preserve network settings
  329.       if [ -e "$etcdir"/networking ]; then
  330.      local networking=`mktemp /tmp/vmwareNetworking.XXXXXX`
  331.      cp -f "$etcdir"/networking $networking
  332.      export VMWARE_RESTORE_NETWORKING=$networking
  333.       fi
  334.  
  335.       local bindir="`. $etcdir/bootstrap && echo $BINDIR`"
  336.       local installer="$bindir"/vmware-uninstall$suffix
  337.       [ -e "$installer" ] && "$installer" "$@"
  338.    fi
  339.  
  340.    return 0
  341. }
  342.  
  343.  
  344. # Uninstall a tar installation.
  345. #
  346. # Arguments:
  347. #    $1 => etcdir
  348. #    $2 => suffix to add to vmware-uninstall (ie -vix)
  349. #
  350. # Returns:
  351. #    0 on success
  352. uninstall_tar() {
  353.    etcdir="$1"
  354.    shift
  355.    suffix="$1"
  356.    shift
  357.  
  358.    locations="$etcdir"/locations
  359.  
  360.    if [ -e $locations ]; then
  361.       local bindir=`grep "^answer BINDIR " $locations | tail -n 1 | sed 's,answer BINDIR ,,g'`
  362.       local installer="$bindir"/vmware-uninstall$suffix.pl
  363.  
  364.       if [ -e "$installer" ]; then
  365.      echo "Uninstalling legacy installation..."
  366.      "$installer" -d
  367.       else          # No uninstaller present, get rid of locations db.
  368.      rm -f $locations
  369.       fi
  370.    fi
  371. }
  372.  
  373.  
  374. # Uninstalls legacy Player/Workstation.
  375. #
  376. # Arguments:
  377. #    None
  378. #
  379. # Returns:
  380. #    0 on success.
  381. uninstall_legacy() {
  382.    local etcdir="$1"
  383.    shift
  384.  
  385.    local hosted=`echo "$PRODUCT_NAME" | grep "\(vmware-workstation\|vmware-player\|vmware-server\)"`
  386.  
  387.    if [ -n "$hosted" ]; then # Check to see if rpm is installed
  388.       for pkg in VMwareWorkstation VMwarePlayer VMware-server; do
  389.      if rpm -q $pkg > /dev/null 2>&1; then
  390.         rpm -e $pkg
  391.         return 0
  392.      fi
  393.       done
  394.    fi
  395.  
  396.    uninstall_tar "$etcdir" ""
  397.  
  398.    # config was a mess under the tar/rpm.  It always got renamed and
  399.    # crazy things.  Clean them up.
  400.    rm -f "$etcdir"/config.[0-9]*
  401.  
  402.    # Networking is sometimes still running after stopping services so
  403.    # forceably kill it.  If it's still running then vmnet can't be
  404.    # removed and network settings aren't migrated properly either.
  405.    killall --wait -9 vmnet-netifup vmnet-dhcpd vmnet-natd vmnet-bridge \
  406.                      vmnet-detect vmnet-sniffer 2> /dev/null || true
  407.    /sbin/rmmod vmnet 2> /dev/null || true
  408.  
  409.    return 0
  410. }
  411.  
  412.  
  413. # Uninstalls bundle rpm for Player/Workstation.
  414. #
  415. # Arguments:
  416. #    None
  417. #
  418. # Returns:
  419. #    None.
  420. uninstall_rpm() {
  421.    local hosted=`echo "$PRODUCT_NAME" | grep "\(vmware-workstation\|vmware-player\|vmware-server\)"`
  422.  
  423.    if [ -n "$hosted" ]; then # Check to see if rpm is installed
  424.       for pkg in VMware-Workstation VMware-Player; do
  425.          if rpm -q $pkg > /dev/null 2>&1; then
  426.             rpm -e $pkg
  427.          fi
  428.       done
  429.    fi
  430. }
  431.  
  432.  
  433. # Migrates pre-Workstation 6.5/Player 2.5 networking settings.
  434. #
  435. # Arguments:
  436. #    None
  437. #
  438. # Returns:
  439. #    None.
  440. migrate_networks() {
  441.    local locations="$ETCDIR"/locations
  442.  
  443.    if [[ -e $locations && ! -e $ETCDIR/networking ]]; then
  444.       local tempLocations=`mktemp /tmp/vmwareLocations.XXXXXX`
  445.       cp -f $locations $tempLocations
  446.       export VMWARE_MIGRATE_NETWORKING=$tempLocations
  447.    fi
  448.  
  449.    return 0
  450. }
  451.  
  452.  
  453. # Main entry point.  Checks whether the VMIS is installed and if so launches it.
  454. # Otherwise extracts itself then installs the VMIS.
  455. main() {
  456.    local fullpath="$0"
  457.    local help
  458.  
  459.    if [ "`get_arch`" != "$ARCH" ]; then
  460.       echo "This is a $ARCH bundle and does not match that of the current "
  461.       echo "architecture.  Please download the `get_arch` bundle."
  462.       exit 1
  463.    fi
  464.  
  465.    if [ "$1" = "-h" -o "$1" = "--help" ]; then
  466.       help=1
  467.       shift
  468.    fi
  469.  
  470.    if is_relative "$fullpath"; then
  471.       fullpath="$PWD/$fullpath"
  472.    fi
  473.  
  474.    if [ ! -w / ]; then
  475.       echo "root access is required to install the VMware Installer Service."
  476.       exit 1
  477.    fi
  478.  
  479.    if [ -z "$help" ]; then
  480.       case "$PRODUCT_NAME" in
  481.           vmware-workstation | vmware-player | vwmare-server)
  482.               migrate_networks
  483.  
  484.               # This will uninstall legacy tar/rpm installations. Note that
  485.               # we do not need to be concerned about checking for
  486.               # VMWARE_SKIP_RPM_UNINSTALL since the bundle rpms are marked
  487.               # to conflict with legacy rpms.
  488.               uninstall_legacy /etc/vmware
  489.  
  490.               # VMWARE_SKIP_RPM_UNINSTALL will be set if we're installing
  491.               # in an rpm context. In that case, we don't want to run any
  492.               # rpm commands to prevent rpm deadlock.
  493.               if [ -z "$VMWARE_SKIP_RPM_UNINSTALL" ]; then
  494.                  uninstall_rpm
  495.               fi
  496.  
  497.               uninstall_bundle /etc/vmware "" "$@"
  498.               ;;
  499.           vmware-vix)
  500.               uninstall_tar /etc/vmware-vix -vix
  501.               uninstall_bundle /etc/vmware-vix -vix "$@"
  502.               ;;
  503.           test-component)
  504.               uninstall_bundle /etc/vmware-test -test "$@"
  505.       esac
  506.    fi
  507.  
  508.    if ! set_lengths "$0"; then
  509.       echo "Unable to extract lengths from bundle."
  510.       exit 1
  511.    fi
  512.  
  513.    extract_prepayload
  514.    extract_self
  515.  
  516.    install "$VMIS_TEMP" "$help" "$fullpath" "$@"
  517. }
  518.  
  519. main "$@"
  520.  
  521. # Exit here to ensure we don't fall through into data.
  522. exit
  523. ‹�.'†J�íÎ1‚@À}ʽ@YîÞÃHÿ/e™˜`„Qw2U;ìí—sï﬙_ù™cË6ÖÖ†ã>×>Eé׿ñÜöåQJ¬ûvºûÕ��������À½�
  524. ßñ¤�(��‹�+'†J�ì[
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement