image_tool: Extract ExpandPartition and ShrinkPartition function.

BUG=chromium:912115
TEST=make test

Change-Id: I728c33cd8a6c85e42160edadc086e9f0127df653
Reviewed-on: https://chromium-review.googlesource.com/1400641
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Cheng-Han Yang <chenghan@chromium.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/py/utils/pygpt.py b/py/utils/pygpt.py
index 5161e44..87176d6 100755
--- a/py/utils/pygpt.py
+++ b/py/utils/pygpt.py
@@ -669,13 +669,18 @@
     part.block_size = ref.block_size
     self.partitions[number - 1] = part
 
-  def Resize(self, new_size):
+  def GetSize(self):
+    return self.block_size * (self.header.BackupLBA + 1)
+
+  def Resize(self, new_size, check_overlap=True):
     """Adjust GPT for a disk image in given size.
 
     Args:
       new_size: Integer for new size of disk image file.
+      check_overlap: Checks if the backup partition table overlaps used
+                     partitions.
     """
-    old_size = self.block_size * (self.header.BackupLBA + 1)
+    old_size = self.GetSize()
     if new_size % self.block_size:
       raise GPTError(
           'New file size %d is not valid for image files.' % new_size)
@@ -692,7 +697,7 @@
     backup_lba = new_blocks - 1
     last_usable_lba = backup_lba - self.header.FirstUsableLBA
 
-    if last_usable_lba < self.header.LastUsableLBA:
+    if check_overlap and last_usable_lba < self.header.LastUsableLBA:
       max_used_lba = self.GetMaxUsedLBA()
       if last_usable_lba < max_used_lba:
         raise GPTError('Backup partition tables will overlap used partitions')
@@ -708,6 +713,9 @@
   def ExpandPartition(self, number):
     """Expands a given partition to last usable LBA.
 
+    The size of the partition can actually be reduced if the last usable LBA
+    decreases.
+
     Args:
       number: an integer to specify partition in 1-based number.
 
@@ -729,7 +737,7 @@
     p.Update(LastLBA=self.header.LastUsableLBA)
     new_blocks = p.blocks
     logging.warn(
-        '%s expanded, size in LBA: %d -> %d.', p, old_blocks, new_blocks)
+        '%s size changed in LBA: %d -> %d.', p, old_blocks, new_blocks)
     return (old_blocks, new_blocks)
 
   def CheckIntegrity(self):