cros flash: don't depend on 'cgpt'

While cgpt is a base utility in the CrOS chroot, it is not widely
available on common Linux distributions. 'cros flash' needs to work
outside the chroot for Chromium (browser) development, so let's remove
that dependency.

sfdisk (part of util-linux) has had GPT support since at least version
2.30, but it didn't gain support for fixing resized disks until 2.32
[1]. Anyway, we can do a similar repair by running a basic 'write'
command (echo write | sudo sfdisk <device>). If it fails, we're no worse
off than before -- so just ignore errors, at least until v2.32 is
widespread.

Bonus: recent sfdisk corrects the PMBR size parameters as well, whereas
'cgpt' does not.

[1] See:
    https://github.com/karelzak/util-linux/issues/532

BUG=chromium:898946
TEST=`cros flash usb:// ...` in and out of chroot; test boot, and verify
     GPT (primary and alternate) look OK (older util-linux won't fix)

Change-Id: I6ef2f3b8510cc521fedf860327f0e12dd3608e8e
Signed-off-by: Brian Norris <briannorris@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1306377
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/cli/flash.py b/cli/flash.py
index 5720943..4f2c488 100644
--- a/cli/flash.py
+++ b/cli/flash.py
@@ -232,10 +232,14 @@
           cmd, debug_level=logging.NOTICE,
           print_cmd=logging.getLogger().getEffectiveLevel() < logging.NOTICE)
 
-    # dd likely didn't put the backup GPT in the last block. cgpt can fix this
-    # up for us, so we have a standards-conforming GPT.
-    cros_build_lib.SudoRunCommand(['cgpt', 'repair', device],
+    # dd likely didn't put the backup GPT in the last block. sfdisk fixes this
+    # up for us with a 'write' command, so we have a standards-conforming GPT.
+    # Ignore errors because sfdisk (util-linux < v2.32) isn't always happy to
+    # fix GPT sanity issues.
+    cros_build_lib.SudoRunCommand(['sfdisk', device], input='write\n',
+                                  error_code_ok=True,
                                   debug_level=self.debug_level)
+
     cros_build_lib.SudoRunCommand(['sync'], debug_level=self.debug_level)
 
   def _GetImagePath(self):