Index: /trunk/src/VBox/Additions/linux/installer/vboxadd.sh
===================================================================
--- /trunk/src/VBox/Additions/linux/installer/vboxadd.sh	(revision 35199)
+++ /trunk/src/VBox/Additions/linux/installer/vboxadd.sh	(revision 35200)
@@ -326,27 +326,32 @@
 }
 
-# setup_script
-setup()
-{
-    # don't stop the old modules here -- they might be in use
-    begin "Uninstalling old VirtualBox DKMS kernel modules"
+# Remove any existing VirtualBox guest kernel modules from the disk, but not
+# from the kernel as they may still be in use
+cleanup_modules()
+{
+    begin "Removing existing VirtualBox DKMS kernel modules"
     $DODKMS uninstall > $LOG
     succ_msg
-    if find /lib/modules/`uname -r` -name "vboxvideo\.*" 2>/dev/null|grep -q vboxvideo; then
-        begin "Removing old VirtualBox vboxvideo kernel module"
-        find /lib/modules/`uname -r` -name "vboxvideo\.*" 2>/dev/null|xargs rm -f 2>/dev/null
-        succ_msg
-    fi
-    if find /lib/modules/`uname -r` -name "vboxsf\.*" 2>/dev/null|grep -q vboxsf; then
-        begin "Removing old VirtualBox vboxsf kernel module"
-        find /lib/modules/`uname -r` -name "vboxsf\.*" 2>/dev/null|xargs rm -f 2>/dev/null
-        succ_msg
-    fi
-    if find /lib/modules/`uname -r` -name "vboxguest\.*" 2>/dev/null|grep -q vboxguest; then
-        begin "Removing old VirtualBox vboxguest kernel module"
-        find /lib/modules/`uname -r` -name "vboxguest\.*" 2>/dev/null|xargs rm -f 2>/dev/null
-        succ_msg
-    fi
+    begin "Removing existing VirtualBox non-DKMS kernel modules"
+    find /lib/modules -name vboxadd\* | xargs rm 2>/dev/null
+    find /lib/modules -name vboxguest\* | xargs rm 2>/dev/null
+    find /lib/modules -name vboxvfs\* | xargs rm 2>/dev/null
+    find /lib/modules -name vboxsf\* | xargs rm 2>/dev/null
+    find /lib/modules -name vboxvideo\* | xargs rm 2>/dev/null
+    succ_msg
+}
+
+# Build and install the VirtualBox guest kernel modules
+setup_modules()
+{
+    # don't stop the old modules here -- they might be in use
+    cleanup_modules
     begin "Building the VirtualBox Guest Additions kernel modules"
+
+    # Short cut out if a dkms build succeeds
+    if $DODKMS install >> $LOG 2>&1; then
+        return 0
+    fi
+
     test_sane_kernel_dir
 
@@ -354,8 +359,6 @@
         --no-print-directory >> $LOG 2>&1; then
         fail_msg
-        printf "Your system does not seem to be set up to build kernel modules.\nLook at $LOG to find out what went wrong.  Once you have corrected it, you can\nrun\n\n  /etc/init.d/vboxadd setup\n\nto build them."
-        BUILDVBOXGUEST=""
-        BUILDVBOXSF=""
-        BUILDVBOXVIDEO=""
+        printf "Your system does not seem to be set up to build kernel modules.\nLook at $LOG to find out what went wrong.\nOnce you have corrected it, you can run\n\n  /etc/init.d/vboxadd setup\n\nto build them.\n\n"
+        return 1
     else
         if ! sh /usr/share/$PACKAGE/test_drm/build_in_tmp \
@@ -366,35 +369,39 @@
     fi
     echo
-    if ! $DODKMS install >> $LOG 2>&1; then
-        if [ -n "$BUILDVBOXGUEST" ]; then
-            begin "Building the main Guest Additions module"
-            if ! $BUILDVBOXGUEST \
-                --save-module-symvers /tmp/vboxguest-Module.symvers \
-                --no-print-directory install >> $LOG 2>&1; then
-                fail "Look at $LOG to find out what went wrong"
-            fi
-            succ_msg
-        fi
-        if [ -n "$BUILDVBOXSF" ]; then
-            begin "Building the shared folder support module"
-            if ! $BUILDVBOXSF \
-                --use-module-symvers /tmp/vboxguest-Module.symvers \
-                --no-print-directory install >> $LOG 2>&1; then
-                fail "Look at $LOG to find out what went wrong"
-            fi
-            succ_msg
-        fi
-        if [ -n "$BUILDVBOXVIDEO" ]; then
-            begin "Building the OpenGL support module"
-            if ! $BUILDVBOXVIDEO \
-                --use-module-symvers /tmp/vboxguest-Module.symvers \
-                --no-print-directory install >> $LOG 2>&1; then
-                fail "Look at $LOG to find out what went wrong"
-            fi
-            succ_msg
-        fi
-        depmod
-    fi
-
+    if [ -n "$BUILDVBOXGUEST" ]; then
+        begin "Building the main Guest Additions module"
+        if ! $BUILDVBOXGUEST \
+            --save-module-symvers /tmp/vboxguest-Module.symvers \
+            --no-print-directory install >> $LOG 2>&1; then
+            fail "Look at $LOG to find out what went wrong"
+        fi
+        succ_msg
+    fi
+    if [ -n "$BUILDVBOXSF" ]; then
+        begin "Building the shared folder support module"
+        if ! $BUILDVBOXSF \
+            --use-module-symvers /tmp/vboxguest-Module.symvers \
+            --no-print-directory install >> $LOG 2>&1; then
+            fail "Look at $LOG to find out what went wrong"
+        fi
+        succ_msg
+    fi
+    if [ -n "$BUILDVBOXVIDEO" ]; then
+        begin "Building the OpenGL support module"
+        if ! $BUILDVBOXVIDEO \
+            --use-module-symvers /tmp/vboxguest-Module.symvers \
+            --no-print-directory install >> $LOG 2>&1; then
+            fail "Look at $LOG to find out what went wrong"
+        fi
+        succ_msg
+    fi
+    depmod
+    return 0
+}
+
+# Do non-kernel bits needed for the kernel modules to work properly (user
+# creation, udev, mount helper...)
+extra_setup()
+{
     begin "Doing non-kernel setup of the Guest Additions"
     echo "Creating user for the Guest Additions." >> $LOG
@@ -443,5 +450,11 @@
 
     succ_msg
-    if [ -n "$BUILDVBOXGUEST" ]; then
+}
+
+# setup_script
+setup()
+{
+    setup_modules
+    if [ "$?" -eq "0" ]; then
         if running_vboxguest || running_vboxadd; then
             printf "You should restart your guest to make sure the new modules are actually used\n\n"
@@ -450,4 +463,5 @@
         fi
     fi
+    extra_setup
 }
 
@@ -456,27 +470,5 @@
 {
     # Delete old versions of VBox modules.
-    DKMS=`which dkms 2>/dev/null`
-    if [ -n "$DKMS" ]; then
-      echo "Attempt to remove old DKMS modules..."
-      for mod in vboxadd vboxguest vboxvfs vboxsf vboxvideo; do
-        $DKMS status -m $mod | while read line; do
-          if echo "$line" | grep -q added > /dev/null ||
-             echo "$line" | grep -q built > /dev/null ||
-             echo "$line" | grep -q installed > /dev/null; then
-            version=`echo "$line" | sed "s/$mod,\([^,]*\)[,:].*/\1/;t;d"`
-            echo "  removing module $mod version $version"
-            $DKMS remove -m $mod -v $version --all 1>&2
-          fi
-        done
-      done
-      echo "Done."
-    fi
-
-    # Remove old installed modules
-    find /lib/modules -name vboxadd\* | xargs rm 2>/dev/null
-    find /lib/modules -name vboxguest\* | xargs rm 2>/dev/null
-    find /lib/modules -name vboxvfs\* | xargs rm 2>/dev/null
-    find /lib/modules -name vboxsf\* | xargs rm 2>/dev/null
-    find /lib/modules -name vboxvideo\* | xargs rm 2>/dev/null
+    cleanup_modules
     depmod
 
