From Will Pittenger, 7 Years ago, written in Plain Text.
Embed
  1. #!/bin/sh
  2. #
  3. # Oracle VM VirtualBox
  4. # VirtualBox Makeself installation starter script
  5. # for Linux Guest Additions
  6.  
  7. #
  8. # Copyright (C) 2006-2015 Oracle Corporation
  9. #
  10. # This file is part of VirtualBox Open Source Edition (OSE), as
  11. # available from http://www.virtualbox.org. This file is free software;
  12. # you can redistribute it and/or modify it under the terms of the GNU
  13. # General Public License (GPL) as published by the Free Software
  14. # Foundation, in version 2 as it comes in the "COPYING" file of the
  15. # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
  16. # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
  17. #
  18.  
  19. # This is a stub installation script to be included in VirtualBox Makeself
  20. # installers which removes any previous installations of the package, unpacks
  21. # the package into the filesystem (by default under /opt) and starts the real
  22. # installation script.
  23. #
  24. PATH=$PATH:/bin:/sbin:/usr/sbin
  25.  
  26. # Note: These variable names must *not* clash with variables in $CONFIG_DIR/$CONFIG!
  27. PACKAGE="VBoxGuestAdditions"
  28. PACKAGE_NAME="VirtualBox Guest Additions"
  29. UNINSTALL="uninstall.sh"
  30. ROUTINES="routines.sh"
  31. ARCH="x86"
  32. INSTALLATION_VER="5.0.20"
  33. INSTALLATION_REV="106931"
  34. BUILD_TYPE="release"
  35. USERNAME="vbox"
  36. UNINSTALL_SCRIPTS="vboxadd-x11 vboxvfs vboxadd-timesync vboxadd-service vboxadd"
  37.  
  38. INSTALLATION_DIR="/opt/$PACKAGE-$INSTALLATION_VER"
  39. CONFIG_DIR="/var/lib/$PACKAGE"
  40. CONFIG="config"
  41. CONFIG_FILES="filelist"
  42. SELF=$1
  43. LOGFILE="/var/log/$PACKAGE.log"
  44.  
  45. . "./$ROUTINES"
  46.  
  47. check_root
  48.  
  49. create_log "$LOGFILE"
  50.  
  51. ## @todo r=andy: Explain options like "force" and "no_setup" -- not self-explanatory
  52. #        to the user.
  53. usage()
  54. {
  55.     info ""
  56.     info "Usage: $SELF install [<installation directory>]"
  57.     info "       [--with-<module>] |"
  58.     info "       uninstall"
  59.     info "       [--force] [--no-setup]"
  60.     info ""
  61.     info "Example:"
  62.     info "$SELF install"
  63.     exit 1
  64. }
  65.  
  66. # Create a symlink in the filesystem and add it to the list of package files
  67. add_symlink()
  68. {
  69.     self=add_symlink
  70.     ## Parameters:
  71.     # The file the link should point to
  72.     target="$1"
  73.     # The name of the actual symlink file.  Must be an absolute path to a
  74.     # non-existing file in an existing directory.
  75.     link="$2"
  76.     link_dir="`dirname "$link"`"
  77.     test -n "$target" ||
  78.         { echo 1>&2 "$self: no target specified"; return 1; }
  79.     test -d "$link_dir" ||
  80.         { echo 1>&2 "$self: link directory $link_dir does not exist"; return 1; }
  81.     expr "$link" : "/.*" > /dev/null ||
  82.         { echo 1>&2 "$self: link file name is not absolute"; return 1; }
  83.     rm -f "$link"
  84.     ln -s "$target" "$link"
  85.     echo "$link" >> "$CONFIG_DIR/$CONFIG_FILES"
  86. }
  87.  
  88. # Create symbolic links targeting all files in a directory in another
  89. # directory in the filesystem
  90. link_into_fs()
  91. {
  92.     ## Parameters:
  93.     # Directory containing the link target files
  94.     target_branch="$1"
  95.     # Directory to create the link files in
  96.     directory="$2"
  97.     for i in "$INSTALLATION_DIR/$target_branch"/*; do
  98.         test -e "$i" &&
  99.             add_symlink "$i" "$directory/`basename $i`"
  100.     done
  101. }
  102.  
  103. # Look for broken installations or installations without a known uninstaller
  104. # and try to clean them up, asking the user first.
  105. def_uninstall()
  106. {
  107.     ## Parameters:
  108.     # Whether to force cleanup without asking the user
  109.     force="$1"
  110.  
  111.     . ./deffiles
  112.     found=0
  113.     for i in "/opt/$PACKAGE-"*; do
  114.         test -e "$i" && found=1
  115.     done
  116.     for i in $DEFAULT_FILE_NAMES; do
  117.         test "$found" = 0 && test -e "$i" && found=1
  118.     done
  119.     test "$found" = 0 &&
  120.         for i in $DEFAULT_VERSIONED_FILE_NAMES; do
  121.             for j in $i-*; do
  122.                 test "$found" = 0 && test -e "$j" && found=1
  123.             done
  124.         done
  125.     test "$found" = 0 && return 0
  126.     if ! test "$1" = "force" ; then
  127.         cat 1>&2 << EOF
  128. You appear to have a version of the VBoxGuestAdditions software
  129. on your system which was installed from a different source or using a
  130. different type of installer.  If you installed it from a package from your
  131. Linux distribution or if it is a default part of the system then we strongly
  132. recommend that you cancel this installation and remove it properly before
  133. installing this version.  If this is simply an older or a damaged
  134. installation you may safely proceed.
  135.  
  136. Do you wish to continue anyway? [yes or no]
  137. EOF
  138.         read reply dummy
  139.         if ! expr "$reply" : [yY] > /dev/null &&
  140.             ! expr "$reply" : [yY][eE][sS] > /dev/null
  141.         then
  142.             info
  143.             info "Cancelling installation."
  144.             return 1
  145.         fi
  146.     fi
  147.     # Stop what we can in the way of services and remove them from the
  148.     # system
  149.     for i in $UNINSTALL_SCRIPTS; do
  150.         stop_init_script "$i" 2>> "${LOGFILE}"
  151.         test -z "$NO_CLEANUP" && test -x "./$i" && "./$i" cleanup 1>&2 2>> "$LOGFILE"
  152.         delrunlevel "$i" 2>> "${LOGFILE}"
  153.         remove_init_script "$i" 2>> "${LOGFILE}"
  154.     done
  155.     for i in "/opt/$PACKAGE-"*/init/*; do
  156.       test -z "$NO_CLEANUP" && grep -q '^# *cleanup_script *$' "${i}" && "${i}" cleanup 1>&2 2>> "$LOGFILE"
  157.     done
  158.  
  159.     # Get rid of any remaining files
  160.     for i in $DEFAULT_FILE_NAMES; do
  161.         rm -f "$i" 2> /dev/null
  162.     done
  163.     for i in $DEFAULT_VERSIONED_FILE_NAMES; do
  164.         rm -f "$i-"* 2> /dev/null
  165.     done
  166.     rm -f "/usr/lib/$PACKAGE" "/usr/lib64/$PACKAGE" "/usr/share/$PACKAGE" \
  167.         "/usr/lib/i386-linux-gnu/$PACKAGE" "/usr/lib/x86_64-linux-gnu/$PACKAGE"
  168.  
  169.     # And any packages left under /opt
  170.     for i in "/opt/$PACKAGE-"*; do
  171.         test -d "$i" && rm -rf "$i"
  172.     done
  173.     return 0
  174. }
  175.  
  176. info "$PACKAGE_NAME installer"
  177.  
  178. # Check architecture
  179. cpu=`uname -m`;
  180. case "$cpu" in
  181.   i[3456789]86|x86)
  182.     cpu="x86"
  183.     lib_candidates="/usr/lib/i386-linux-gnu /usr/lib /lib"
  184.     ;;
  185.   x86_64|amd64)
  186.     cpu="amd64"
  187.     lib_candidates="/usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib /lib64 /lib"
  188.     ;;
  189.   *)
  190.     cpu="unknown"
  191. esac
  192. ARCH_PACKAGE="$PACKAGE-$cpu.tar.bz2"
  193. if [ ! -r "$ARCH_PACKAGE" ]; then
  194.   info "Detected unsupported $cpu machine type."
  195.   exit 1
  196. fi
  197. # Find the most appropriate libary folder by seeing which of the candidate paths
  198. # are actually in the shared linker path list and choosing the first.  We look
  199. # for Debian-specific paths first, then LSB ones, then the new RedHat ones.
  200. libs=`ldconfig -v 2>/dev/null | grep -v ^$'\t'`
  201. for i in $lib_candidates; do
  202.   if echo $libs | grep -q $i; then
  203.     lib_path=$i
  204.     break
  205.   fi
  206. done
  207. if [ ! -x "$lib_path" ]; then
  208.   info "Unable to determine correct library path."
  209.   exit 1
  210. fi
  211.  
  212. # Sensible default actions
  213. ACTION="install"
  214. DO_SETUP="true"
  215. NO_CLEANUP=""
  216. FORCE_UPGRADE=""
  217.  
  218. while [ $# -ge 2 ];
  219. do
  220.     ARG=$2
  221.     shift
  222.  
  223.     if [ -z "$MY_END_OF_OPTIONS" ]; then
  224.         case "$ARG" in
  225.  
  226.             install)
  227.                 ACTION="install"
  228.                 ;;
  229.  
  230.             uninstall)
  231.                 ACTION="uninstall"
  232.                 ;;
  233.  
  234.             ## @todo Add per-module options handling, e.g. --lightdm-greeter-dir
  235.              #       or --lightdm-config
  236.  
  237.             ## @todo Add listing all available modules (+ their options, e.g.
  238.              #       with callback mod_mymod_show_options?)
  239.  
  240.             --with-*)
  241.                 MODULE_CUR=`expr "$ARG" : '--with-\(.*\)'`
  242.                 # Check if corresponding module in installer/module-$1 exists.
  243.                 # Note: Module names may not contain spaces or other funny things.
  244.                 if [ ! -f "./installer/module-${MODULE_CUR}" ]; then
  245.                     info "Error: Module \"${MODULE_CUR}\" does not exist."
  246.                     usage
  247.                 fi
  248.                 # Give the module the chance of doing initialization work / checks.
  249.                 . "./installer/module-${MODULE_CUR}"
  250.                 mod_${MODULE_CUR}_init
  251.                 if test $? -ne 0; then
  252.                     echo 1>&2 "Module '${MODULE_CUR}' failed to initialize"
  253.                     if ! test "$FORCE_UPGRADE" = "force"; then
  254.                         return 1
  255.                     fi
  256.                     # Continue initialization.
  257.                 fi
  258.                 # Add module to the list of modules to handle later.
  259.                 if test -z "${INSTALLATION_MODULES_LIST}"; then
  260.                     INSTALLATION_MODULES_LIST="${MODULE_CUR}"
  261.                 else
  262.                     INSTALLATION_MODULES_LIST="${INSTALLATION_MODULES_LIST} ${MODULE_CUR}"
  263.                 fi
  264.                 shift
  265.                 ;;
  266.  
  267.             --force|force) # Keep "force" for backwards compatibility.
  268.                 FORCE_UPGRADE="force"
  269.                 ;;
  270.  
  271.             --no-setup|no_setup) # Keep "no_setup" for backwards compatibility.
  272.                 DO_SETUP=""
  273.                 ;;
  274.  
  275.             --no-cleanup|no_cleanup) # Keep "no_cleanup" for backwards compatibility.
  276.                 # Do not do cleanup of old modules when removing them.  For
  277.                 # testing purposes only.
  278.                 DO_SETUP=""
  279.                 NO_CLEANUP="no_cleanup"
  280.                 ;;
  281.  
  282.             --)
  283.                 MY_END_OF_OPTIONS="1"
  284.                 ;;
  285.  
  286.             *)
  287.                 if [ "`echo $1|cut -c1`" != "/" ]; then
  288.                     info "Please specify an absolute path"
  289.                     usage
  290.                 fi
  291.                 INSTALLATION_DIR="$1"
  292.                 shift
  293.                 ;;
  294.         esac
  295.     fi
  296. done
  297.  
  298. # uninstall any previous installation
  299. INSTALL_DIR=""
  300. uninstalled=0
  301. test -r "$CONFIG_DIR/$CONFIG" &&
  302.     eval `grep ^INSTALL_DIR= "$CONFIG_DIR/$CONFIG"` 2>/dev/null &&
  303.     eval `grep ^UNINSTALLER= "$CONFIG_DIR/$CONFIG"` 2>/dev/null
  304. if test -n "$INSTALL_DIR" -a -x "$INSTALL_DIR/$UNINSTALLER"; then
  305.   "$INSTALL_DIR/$UNINSTALLER" $NO_CLEANUP 1>&2 ||
  306.     abort "Failed to remove existing installation.  Aborting..."
  307.   uninstalled=1
  308. fi
  309. test "$uninstalled" = 0 && def_uninstall "$FORCE_UPGRADE" && uninstalled=1
  310. test "$uninstalled" = 0 && exit 1
  311. rm -f "$CONFIG_DIR/$CONFIG"
  312. rm -f "$CONFIG_DIR/$CONFIG_FILES"
  313. rmdir "$CONFIG_DIR" 2>/dev/null
  314. test "$ACTION" = "install" || exit 0
  315.  
  316. # Choose a proper umask
  317. umask 022
  318.  
  319. # Set installer modules directory
  320. INSTALLATION_MODULES_DIR="$INSTALLATION_DIR/installer/"
  321.  
  322. # install the new version
  323. mkdir -p -m 755 "$CONFIG_DIR"
  324. test ! -d "$INSTALLATION_DIR" && REMOVE_INSTALLATION_DIR=1
  325. mkdir -p -m 755 "$INSTALLATION_DIR"
  326.  
  327. # install and load installer modules
  328. if [ -d installer ]; then
  329.   info "Copying additional installer modules ..."
  330.   mkdir -p -m 755 "$INSTALLATION_MODULES_DIR"
  331.   for CUR_FILE in `ls installer/*`; do
  332.       install -p -m 755 "$CUR_FILE" "$INSTALLATION_MODULES_DIR"
  333.       if [ $? -ne 0 ]; then
  334.           info "Error: Failed to copy installer module \"$CUR_FILE\""
  335.           if ! test "$FORCE_UPGRADE" = "force"; then
  336.               exit 1
  337.           fi
  338.       fi
  339.   done
  340. fi
  341.  
  342. # Create a list of the files in the archive, skipping any directories which
  343. # already exist in the filesystem.
  344. bzip2 -d -c "$ARCH_PACKAGE" | tar -tf - |
  345.   while read name; do
  346.     fullname="$INSTALLATION_DIR/$name"
  347.     case "$fullname" in
  348.       */)
  349.         test ! -d "$fullname" &&
  350.           echo "$fullname" >> "$CONFIG_DIR/$CONFIG_FILES"
  351.         ;;
  352.       *)
  353.         echo "$fullname" >> "$CONFIG_DIR/$CONFIG_FILES"
  354.         ;;
  355.     esac
  356.   done
  357. bzip2 -d -c "$ARCH_PACKAGE" | tar -xf - -C "$INSTALLATION_DIR" || exit 1
  358.  
  359. # Set symlinks into /usr and /sbin
  360. link_into_fs "bin" "/usr/bin"
  361. link_into_fs "sbin" "/usr/sbin"
  362. link_into_fs "lib" "$lib_path"
  363. add_symlink "$INSTALLATION_DIR/lib/$PACKAGE" /usr/lib/"$PACKAGE"
  364. link_into_fs "share" "/usr/share"
  365. link_into_fs "src" "/usr/src"
  366.  
  367. if [ -d "$INSTALLATION_MODULES_DIR" ]; then
  368.   info "Installing additional modules ..."
  369.   for CUR_MODULE in `find "$INSTALLATION_MODULES_DIR" 2>/dev/null`
  370.       do
  371.           echo "$CUR_MODULE" >> "$CONFIG_DIR/$CONFIG_FILES"
  372.       done
  373. fi
  374.  
  375. for CUR_MODULE in ${INSTALLATION_MODULES_LIST}
  376. do
  377.     mod_${CUR_MODULE}_install
  378.     if [ $? -ne 0 ]; then
  379.         info "Error: Failed to install module \"$CUR_MODULE\""
  380.         if ! test "$FORCE_UPGRADE" = "force"; then
  381.             exit 1
  382.         fi
  383.     fi
  384. done
  385.  
  386. # Remember our installation configuration before we call any init scripts
  387. cat > "$CONFIG_DIR/$CONFIG" << EOF
  388. # $PACKAGE installation record.
  389. # Package installation directory
  390. INSTALL_DIR='$INSTALLATION_DIR'
  391. # Additional installation modules
  392. INSTALL_MODULES_DIR='$INSTALLATION_MODULES_DIR'
  393. INSTALL_MODULES_LIST='$INSTALLATION_MODULES_LIST'
  394. # Package uninstaller.  If you repackage this software, please make sure
  395. # that this prints a message and returns an error so that the default
  396. # uninstaller does not attempt to delete the files installed by your
  397. # package.
  398. UNINSTALLER='$UNINSTALL'
  399. # Package version
  400. INSTALL_VER='$INSTALLATION_VER'
  401. INSTALL_REV='$INSTALLATION_REV'
  402. # Build type and user name for logging purposes
  403. BUILD_TYPE='$BUILD_TYPE'
  404. USERNAME='$USERNAME'
  405. EOF
  406.  
  407. # Give the modules the chance to write their stuff
  408. # to the installation config as well.
  409. if [ -n "${INSTALLATION_MODULES_LIST}" ]; then
  410.   info "Saving modules configuration ..."
  411.   for CUR_MODULE in ${INSTALLATION_MODULES_LIST}
  412.   do
  413.     echo "`mod_${CUR_MODULE}_config_save`" >> "$CONFIG_DIR/$CONFIG"
  414.   done
  415. fi
  416.  
  417. # Install, set up and start init scripts
  418. for i in "$INSTALLATION_DIR/init/"*; do
  419.   if test -r "$i"; then
  420.     install_init_script "$i" "`basename "$i"`" 2>> "${LOGFILE}"
  421.     addrunlevel "`basename "$i"`" 2>> "${LOGFILE}"
  422.     test -n "$DO_SETUP" && grep -q '^# *setup_script *$' "${i}" && "${i}" setup 1>&2 2>> "${LOGFILE}"
  423.     start_init_script "`basename "$i"`" 2>> "${LOGFILE}"
  424.   fi
  425. done
  426.  
  427. cp $ROUTINES $INSTALLATION_DIR
  428. echo $INSTALLATION_DIR/$ROUTINES >> "$CONFIG_DIR/$CONFIG_FILES"
  429. cat > $INSTALLATION_DIR/$UNINSTALL << EOF
  430. #!/bin/sh
  431. # Auto-generated uninstallation file
  432.  
  433. PATH=\$PATH:/bin:/sbin:/usr/sbin
  434. LOGFILE="/var/log/$PACKAGE-uninstall.log"
  435.  
  436. # Read routines.sh
  437. if ! test -r "$INSTALLATION_DIR/$ROUTINES"; then
  438.     echo 1>&2 "Required file $ROUTINES not found.  Aborting..."
  439.     return 1
  440. fi
  441. . "$INSTALLATION_DIR/$ROUTINES"
  442.  
  443. # We need to be run as root
  444. check_root
  445.  
  446. create_log "\$LOGFILE"
  447.  
  448. echo 1>&2 "Removing installed version $INSTALLATION_VER of $PACKAGE_NAME..."
  449.  
  450. NO_CLEANUP=""
  451. if test "\$1" = "no_cleanup"; then
  452.     shift
  453.     NO_CLEANUP="no_cleanup"
  454. fi
  455.  
  456. test -r "$CONFIG_DIR/$CONFIG_FILES" || abort "Required file $CONFIG_FILES not found.  Aborting..."
  457.  
  458. # Stop and clean up all services
  459. for i in "$INSTALLATION_DIR/init/"*; do
  460.     if test -r "\$i"; then
  461.         stop_init_script "\`basename "\$i"\`" 2>> "${LOGFILE}"
  462.         test -z "\${NO_CLEANUP}" && grep -q '^# *cleanup_script *$' "\${i}" && "\${i}" cleanup 2>> "\$LOGFILE"
  463.         delrunlevel "\`basename "\$i"\`" 2>> "${LOGFILE}"
  464.         remove_init_script "\`basename "\$i"\`" 2>> "${LOGFILE}"
  465.     fi
  466. done
  467.  
  468. # Load all modules
  469. # Important: This needs to be done before loading the configuration
  470. #            value below to not override values which are set to a default
  471. #            value in the modules itself.
  472. for CUR_MODULE in `find "$INSTALLATION_MODULES_DIR" -name "module-*" 2>/dev/null`
  473.     do
  474.         . "\$CUR_MODULE"
  475.     done
  476.  
  477. # Load configuration values
  478. test -r "$CONFIG_DIR/$CONFIG" && . "$CONFIG_DIR/$CONFIG"
  479.  
  480. # Call uninstallation initialization of all modules
  481. for CUR_MODULE in "$INSTALLATION_MODULES_LIST"
  482.     do
  483.         if test -z "\$CUR_MODULE"; then
  484.             continue
  485.         fi
  486.         mod_\${CUR_MODULE}_pre_uninstall
  487.         if [ $? -ne 0 ]; then
  488.             echo 1>&2 "Module \"\$CUR_MODULE\" failed to initialize uninstallation"
  489.             # Continue initialization.
  490.         fi
  491.     done
  492.  
  493. # Call uninstallation of all modules
  494. for CUR_MODULE in "$INSTALLATION_MODULES_LIST"
  495.     do
  496.         if test -z "\$CUR_MODULE"; then
  497.             continue
  498.         fi
  499.         mod_\${CUR_MODULE}_uninstall
  500.         if [ $? -ne 0 ]; then
  501.             echo 1>&2 "Module \"\$CUR_MODULE\" failed to uninstall"
  502.             # Continue uninstallation.
  503.         fi
  504.     done
  505.  
  506. # And remove all files and empty installation directories
  507. # Remove any non-directory entries
  508. cat "$CONFIG_DIR/$CONFIG_FILES" | xargs rm 2>/dev/null
  509. # Remove any empty (of files) directories in the file list
  510. cat "$CONFIG_DIR/$CONFIG_FILES" |
  511.     while read file; do
  512.         case "\$file" in
  513.             */)
  514.             test -d "\$file" &&
  515.                 find "\$file" -depth -type d -exec rmdir '{}' ';' 2>/dev/null
  516.             ;;
  517.         esac
  518.     done
  519.  
  520. # Remove configuration files
  521. rm "$CONFIG_DIR/$CONFIG_FILES" 2>/dev/null
  522. rm "$CONFIG_DIR/$CONFIG" 2>/dev/null
  523. rmdir "$CONFIG_DIR" 2>/dev/null
  524. exit 0
  525. EOF
  526. chmod 0755 $INSTALLATION_DIR/$UNINSTALL
  527. echo $INSTALLATION_DIR/$UNINSTALL >> "$CONFIG_DIR/$CONFIG_FILES"
  528. test -n "$REMOVE_INSTALLATION_DIR" &&
  529.   echo "$INSTALLATION_DIR/" >> "$CONFIG_DIR/$CONFIG_FILES"