Re-work the devinstall wipe to specificlally wipe only the devinstall state.

Devinstall installs packages into /usr/local/bin which is bind mounted from
/mnt/stateful_partition/dev_image. Instead of attempting to wipe all of
/mnt/stateful_partition/* we instead just wipe
/mnt/stateful_partition/dev_image.

BUG=chromium:245393
TEST=Ran it multiple times.

Change-Id: Ide4fba063f067aefef2ae2c41cbbbed84578f330
Reviewed-on: https://gerrit.chromium.org/gerrit/60888
Tested-by: Chris Sosa <sosa@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Commit-Queue: Chris Sosa <sosa@chromium.org>
diff --git a/devmode-test/devinstall_test.py b/devmode-test/devinstall_test.py
index 0e498fb..ca19e5f 100755
--- a/devmode-test/devinstall_test.py
+++ b/devmode-test/devinstall_test.py
@@ -12,6 +12,7 @@
 stateful partition and then runs gmerge.
 """
 
+import getpass
 import logging
 import optparse
 import os
@@ -90,17 +91,20 @@
         _LOCALHOST, self.tmpdir, self.port,
         debug_level=logging.DEBUG, interactive=False)
 
-  def _WipeStatefulPartition(self):
-    """Deletes everything from the working image path's stateful partition."""
+  def _WipeDevInstall(self):
+    """Wipes the devinstall state."""
     r_mount_point = os.path.join(self.tmpdir, 'm')
     s_mount_point = os.path.join(self.tmpdir, 's')
+    dev_image_path = os.path.join(s_mount_point, 'dev_image')
     mount_helper.MountImage(self.working_image_path,
                             r_mount_point, s_mount_point, read_only=False,
                             safe=True)
-    # Run in shell mode to interpret '*' as a glob.
-    cros_build_lib.SudoRunCommand('rm -rf %s/*' % s_mount_point, shell=True,
-                                  debug_level=logging.DEBUG)
-    mount_helper.UnmountImage(r_mount_point, s_mount_point)
+    try:
+      cros_build_lib.SudoRunCommand(['chown', '--recursive', getpass.getuser(),
+                                     s_mount_point], debug_level=logging.DEBUG)
+      shutil.rmtree(dev_image_path)
+    finally:
+      mount_helper.UnmountImage(r_mount_point, s_mount_point)
 
   def _FindUnusedPort(self):
     """Returns a currently unused port."""
@@ -156,8 +160,8 @@
     shutil.copyfile(vm_path, self.working_image_path)
     logging.debug('Copy of vm image stored at %s.', self.working_image_path)
 
-    logging.info('Wiping the stateful partition to prepare test.')
-    self._WipeStatefulPartition()
+    logging.info('Wiping /usr/local/bin from the image.')
+    self._WipeDevInstall()
 
     logging.info('Starting the vm on port %d.', self.port)
     self._RobustlyStartVMWithSSH()