deploy_chrome: Umount existing mount instead of mounting over it

Check if mount_dir is a mountpoint and umount it first, instead of
mounting over it. This prevents us from creating a large number of
mounts over each other, each time we deploy.

BUG=None
TEST=run and verify existing mount is umounted before mounting

Change-Id: Icbe6f10adc3da7b1ff949e0546c2650681228ce4
Reviewed-on: https://chromium-review.googlesource.com/236827
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Pawel Osciak <posciak@chromium.org>
Tested-by: Pawel Osciak <posciak@chromium.org>
diff --git a/scripts/deploy_chrome.py b/scripts/deploy_chrome.py
index 0ba0ac3..c17aef2 100644
--- a/scripts/deploy_chrome.py
+++ b/scripts/deploy_chrome.py
@@ -65,6 +65,7 @@
 _CHROME_DIR = '/opt/google/chrome'
 _CHROME_DIR_MOUNT = '/mnt/stateful_partition/deploy_rootfs/opt/google/chrome'
 
+_UMOUNT_DIR_IF_MOUNTPOINT_CMD = 'mountpoint -q %(dir)s && umount %(dir)s'
 _BIND_TO_FINAL_DIR_CMD = 'mount --rbind %s %s'
 _SET_MOUNT_FLAGS_CMD = 'mount -o remount,exec,suid %s'
 
@@ -298,6 +299,9 @@
     # Create directory if does not exist
     self.device.RunCommand('mkdir -p --mode 0775 %s' % (
                                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})
     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
@@ -390,10 +394,14 @@
                     help='Show more debug output.')
   parser.add_option('--mount-dir', type='path', default=None,
                     help='Deploy Chrome in target directory and bind it '
-                         'to the directory specified by this flag.')
+                         'to the directory specified by this flag.'
+                         'Any existing mount on this directory will be '
+                         'umounted first.')
   parser.add_option('--mount', action='store_true', default=False,
                     help='Deploy Chrome to default target directory and bind '
-                         'it to the default mount directory.')
+                         'it to the default mount directory.'
+                         'Any existing mount on this directory will be '
+                         'umounted first.')
 
   group = optparse.OptionGroup(parser, 'Advanced Options')
   group.add_option('-l', '--local-pkg-path', type='path',