cgpt.py: Include fs_options in the partition script.

For squashfs filesystem, the options used to create the filesystem
are required if you want to modify it, in order to unsquash and
squash it again.

This patch includes those options when generating the partition
script.

BUG=chromium:444311
TEST=`build_library/cgpt.py write base .../disk_layout.json output.sh`
includes the FS_OPTIONS_*.

Change-Id: I73f81745612fe4028feff3effe12ea4e07bf7c72
Reviewed-on: https://chromium-review.googlesource.com/245653
Trybot-Ready: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
Reviewed-by: Daniel Ehrenberg <dehrenberg@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
diff --git a/build_library/cgpt.py b/build_library/cgpt.py
index 773640a..c4ab58a 100755
--- a/build_library/cgpt.py
+++ b/build_library/cgpt.py
@@ -502,6 +502,21 @@
   partitions = copy.deepcopy(config['layouts'][image_type])
   metadata = config['metadata']
 
+  # Convert fs_options to a string.
+  for partition in partitions:
+    fs_options = partition.get('fs_options', '')
+    if isinstance(fs_options, dict):
+      fs_format = partition.get('fs_format')
+      fs_options = fs_options.get(fs_format, '')
+    elif not isinstance(fs_options, basestring):
+      raise InvalidLayout('Partition number %s: fs_format must be a string or '
+                          'dict, not %s' % (partition.get('num'),
+                                            type(fs_options)))
+    if '"' in fs_options or "'" in fs_options:
+      raise InvalidLayout('Partition number %s: fs_format cannot have quotes' %
+                          partition.get('num'))
+    partition['fs_options'] = fs_options
+
   for adjustment_str in options.adjust_part.split():
     adjustment = adjustment_str.split(':')
     if len(adjustment) < 2:
@@ -747,11 +762,13 @@
         fs_bytes = partition.get('fs_bytes', part_bytes)
         part_format = partition.get('format', '')
         fs_format = partition.get('fs_format', '')
+        fs_options = partition.get('fs_options', '')
         lines += [
             'PARTITION_SIZE_%s=%s' % (shell_label, part_bytes),
             '     DATA_SIZE_%s=%s' % (shell_label, fs_bytes),
             '        FORMAT_%s=%s' % (shell_label, part_format),
             '     FS_FORMAT_%s=%s' % (shell_label, fs_format),
+            '    FS_OPTIONS_%s="%s"' % (shell_label, fs_options),
         ]
 
   sfile.write('%s\n}\n' % '\n  '.join(lines))
@@ -768,7 +785,7 @@
     An object for the selected partition
   """
   for partition in partitions:
-    if partition.get('num', None) == int(num):
+    if partition.get('num') == int(num):
       return partition
 
   raise PartitionNotFound('Partition %s not found' % num)
@@ -784,7 +801,7 @@
     An object for the metadata partition
   """
   for partition in partitions:
-    if partition.get('num', None) == "metadata":
+    if partition.get('num') == "metadata":
       return partition
 
   return {}
@@ -818,7 +835,6 @@
     layout_filename: Path to partition configuration file
     sfilename: Filename to write the finished script to
   """
-
   config = LoadPartitionConfig(layout_filename)
 
   with open(sfilename, 'w') as f:
@@ -1006,20 +1022,7 @@
   partitions = GetPartitionTableFromConfig(options, layout_filename, image_type)
   partition = GetPartitionByNumber(partitions, num)
 
-  fs_options = partition.get('fs_options', {})
-  if isinstance(fs_options, dict):
-    fs_format = partition.get('fs_format')
-    result = fs_options.get(fs_format, '')
-  elif isinstance(fs_options, basestring):
-    result = fs_options
-  else:
-    raise InvalidLayout('Partition number %s: fs_format must be a string or '
-                        'dict, not %s' % (num, type(fs_options)))
-  if '"' in result or "'" in result:
-    raise InvalidLayout('Partition number %s: fs_format cannot have quotes' %
-                        num)
-
-  return result
+  return partition.get('fs_options')
 
 
 def GetFilesystemSize(options, image_type, layout_filename, num):