deploy_chrome: Add logs to check the process blocking umount if it fails

Sometimes umount fails with EBUSY message because some previous run
failed to kill a process accessing a directory. This will allow us to
determine what process is blocking umount.

Tested on a CrOS VM by opening a file in mount-directory using 'less'
and running 'deploy_chrome

Eg: https://chrome-swarming.appspot.com/task?id=47efda8992794810

BUG=chromium:1015157
TEST=lint pass
TEST=unittests pass

Change-Id: Ic7393e059a67d7771e726c097774c264ce074cc8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1867047
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
Tested-by: Anushruth  <anushruth@chromium.org>
Commit-Queue: Anushruth  <anushruth@chromium.org>
diff --git a/scripts/deploy_chrome.py b/scripts/deploy_chrome.py
index b67a331..9498a8e 100644
--- a/scripts/deploy_chrome.py
+++ b/scripts/deploy_chrome.py
@@ -51,7 +51,8 @@
 POST_KILL_WAIT = 2
 
 MOUNT_RW_COMMAND = 'mount -o remount,rw /'
-LSOF_COMMAND = 'lsof %s/chrome'
+LSOF_COMMAND_CHROME = 'lsof %s/chrome'
+LSOF_COMMAND = 'lsof %s'
 DBUS_RELOAD_COMMAND = 'killall -HUP dbus-daemon'
 
 _ANDROID_DIR = '/system/chrome'
@@ -64,6 +65,7 @@
     'if mountpoint -q %(dir)s; then umount %(dir)s; fi')
 _BIND_TO_FINAL_DIR_CMD = 'mount --rbind %s %s'
 _SET_MOUNT_FLAGS_CMD = 'mount -o remount,exec,suid %s'
+_MKDIR_P_CMD = 'mkdir -p --mode 0775 %s'
 
 DF_COMMAND = 'df -k %s'
 
@@ -127,7 +129,8 @@
     return int(result.output.split()[0])
 
   def _ChromeFileInUse(self):
-    result = self.device.RunCommand(LSOF_COMMAND % (self.options.target_dir,),
+    result = self.device.RunCommand(LSOF_COMMAND_CHROME %
+                                    (self.options.target_dir,),
                                     error_code_ok=True, capture_output=True)
     return result.returncode == 0
 
@@ -328,14 +331,25 @@
   def _MountTarget(self):
     logging.info('Mounting Chrome...')
 
-    # Create directory if does not exist
-    self.device.RunCommand(['mkdir', '-p', '--mode', '0775',
-                            self.options.mount_dir])
-    # Umount the existing mount on mount_dir if present first
-    self.device.RunCommand(_UMOUNT_DIR_IF_MOUNTPOINT_CMD %
-                           {'dir': self.options.mount_dir})
+    # Create directory if does not exist.
+    self.device.RunCommand(_MKDIR_P_CMD % self.options.mount_dir)
+    try:
+      # Umount the existing mount on mount_dir if present first.
+      self.device.RunCommand(_UMOUNT_DIR_IF_MOUNTPOINT_CMD %
+                             {'dir': self.options.mount_dir})
+    except cros_build_lib.RunCommandError as e:
+      logging.error('Failed to umount %s', self.options.mount_dir)
+      # If there is a failure, check if some processs is using the mount_dir.
+      result = self.device.RunCommand(LSOF_COMMAND % (self.options.mount_dir,),
+                                      check=False, capture_output=True,
+                                      encoding='utf-8')
+      logging.error('lsof %s -->', self.options.mount_dir)
+      logging.error(result.stdout)
+      raise e
+
     self.device.RunCommand(_BIND_TO_FINAL_DIR_CMD % (self.options.target_dir,
                                                      self.options.mount_dir))
+
     # Chrome needs partition to have exec and suid flags set
     self.device.RunCommand(_SET_MOUNT_FLAGS_CMD % (self.options.mount_dir,))