Generate extra userdata partition if needed

* To support variants of devices which may come in 16/32/64GB variants.

Change-Id: I74c32d8316f0450a1445fe95a95e5cabb7a9dd1b
(cherry picked from commit aae837f9cbcff739cb660016a6d1abd7ef4663db)
diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index eab8113..ce474f7 100755
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -191,6 +191,42 @@
   os.rmdir(temp_dir)
 
 
+def AddUserdataExtra(output_zip):
+  """Create extra userdata image and store it in output_zip."""
+
+  image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
+                                                  "data_extra")
+  # If no userdataextra_size is provided for extfs, skip userdata_extra.img.
+  if (image_props.get("fs_type", "").startswith("ext") and
+      not image_props.get("partition_size")):
+    return
+
+  extra_name = image_props.get("partition_name", "extra")
+
+  print "creating userdata_%s.img..." % extra_name
+
+  # The name of the directory it is making an image out of matters to
+  # mkyaffs2image.  So we create a temp dir, and within it we create an
+  # empty dir named "data", and build the image from that.
+  temp_dir = tempfile.mkdtemp()
+  user_dir = os.path.join(temp_dir, "data")
+  os.mkdir(user_dir)
+  img = tempfile.NamedTemporaryFile()
+
+  fstab = OPTIONS.info_dict["fstab"]
+  if fstab:
+    image_props["fs_type" ] = fstab["/data"].fs_type
+  succ = build_image.BuildImage(user_dir, image_props, img.name)
+  assert succ, "build userdata_%s.img image failed" % extra_name
+
+  # Disable size check since this fetches original data partition size
+  #common.CheckSize(img.name, "userdata_extra.img", OPTIONS.info_dict)
+  output_zip.write(img.name, "userdata_%s.img" % extra_name)
+  img.close()
+  os.rmdir(user_dir)
+  os.rmdir(temp_dir)
+
+
 def AddCache(output_zip, prefix="IMAGES/"):
   """Create an empty cache image and store it in output_zip."""
 
@@ -290,6 +326,8 @@
     AddVendor(output_zip)
   banner("userdata")
   AddUserdata(output_zip)
+  banner("extrauserdata")
+  AddUserdataExtra(output_zip)
   banner("cache")
   AddCache(output_zip)
 
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index 79ef8da..91fe54b 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -264,8 +264,12 @@
     if "extfs_sparse_flag" in prop_dict:
       build_command.append(prop_dict["extfs_sparse_flag"])
       #run_fsck = True
-    build_command.extend([in_dir, out_file, fs_type,
-                          prop_dict["mount_point"]])
+    if "is_userdataextra" in prop_dict:
+      build_command.extend([in_dir, out_file, fs_type,
+                           "data"])
+    else:
+      build_command.extend([in_dir, out_file, fs_type,
+                            prop_dict["mount_point"]])
     build_command.append(prop_dict["partition_size"])
     if "journal_size" in prop_dict:
       build_command.extend(["-j", prop_dict["journal_size"]])
@@ -439,6 +443,11 @@
     copy_prop("fs_type", "fs_type")
     copy_prop("userdata_fs_type", "fs_type")
     copy_prop("userdata_size", "partition_size")
+  elif mount_point == "data_extra":
+    copy_prop("fs_type", "fs_type")
+    copy_prop("userdataextra_size", "partition_size")
+    copy_prop("userdataextra_name", "partition_name")
+    d["is_userdataextra"] = True
   elif mount_point == "cache":
     copy_prop("cache_fs_type", "fs_type")
     copy_prop("cache_size", "partition_size")
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index 3b523a5..36f3305 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -612,8 +612,8 @@
   fs_type = None
   limit = None
   if info_dict["fstab"]:
-    if mount_point == "/userdata":
-      mount_point = "/data"
+    if mount_point == "/userdata_extra": mount_point = "/data"
+    if mount_point == "/userdata": mount_point = "/data"
     p = info_dict["fstab"][mount_point]
     fs_type = p.fs_type
     device = p.device