Support dynamic partitions feature by default

Build with dynamic partitions enabled by default, which produces a
"super.img" file instead of the {system,product,vendor}.img files
generated previously. The system image is no longer a "root image"; it
still contains a useful root filesystem, but the true root is now
contained in the ramdisk, which chain loads it after the dynamic
partition has been constructed.

Use TARGET_USE_DYNAMIC_PARTITIONS=false to disable this feature.

Bug: 113175337
Change-Id: I15e951e6bf7d9bfced33c5266b3c8058fd544950
Signed-off-by: Alistair Strachan <astrachan@google.com>
diff --git a/shared/BoardConfig.mk b/shared/BoardConfig.mk
index 2ed6ee1..8df1ba9 100644
--- a/shared/BoardConfig.mk
+++ b/shared/BoardConfig.mk
@@ -29,7 +29,6 @@
 # Build a separate vendor.img partition
 BOARD_USES_VENDORIMAGE := true
 BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_VENDORIMAGE_PARTITION_SIZE := 536870912 # 512MB
 TARGET_COPY_OUT_VENDOR := vendor
 
 BOARD_USES_METADATA_PARTITION := true
@@ -37,7 +36,6 @@
 # Build a separate product.img partition
 BOARD_USES_PRODUCTIMAGE := true
 BOARD_PRODUCTIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_PRODUCTIMAGE_PARTITION_SIZE := 1610612736 # 1.5GB
 TARGET_COPY_OUT_PRODUCT := product
 
 ifeq ($(TARGET_BUILD_SYSTEM_ROOT_IMAGE),true)
@@ -55,7 +53,6 @@
 # The compiler will occasionally generate movaps, etc.
 BOARD_MALLOC_ALIGNMENT := 16
 
-BOARD_SYSTEMIMAGE_PARTITION_SIZE := 4294967296 # 4 GB
 # Make the userdata partition 4G to accomodate ASAN and CTS
 BOARD_USERDATAIMAGE_PARTITION_SIZE := 4294967296
 
@@ -146,16 +143,33 @@
 # TODO(b/73078796): remove
 BOARD_PROPERTY_OVERRIDES_SPLIT_ENABLED := true
 
-TARGET_NO_RECOVERY ?= true
 TARGET_RECOVERY_PIXEL_FORMAT := ABGR_8888
 ifeq ($(TARGET_BUILD_SYSTEM_ROOT_IMAGE),true)
 # Use the initrd version for the dtb build, because we need to have /system
 # defined somewhere, and the dtb fstab doesn't define it (deliberately)
 TARGET_RECOVERY_FSTAB ?= device/google/cuttlefish/shared/config/fstab.initrd
+else ifeq ($(TARGET_USE_DYNAMIC_PARTITIONS),true)
+TARGET_RECOVERY_FSTAB ?= device/google/cuttlefish/shared/config/fstab.initrd-dynamic-partitions
 else
 TARGET_RECOVERY_FSTAB ?= device/google/cuttlefish/shared/config/fstab.initrd
 endif
 
+ifeq ($(TARGET_USE_DYNAMIC_PARTITIONS),true)
+  BOARD_SUPER_PARTITION_SIZE := 6442450944
+  BOARD_SUPER_PARTITION_GROUPS := google_dynamic_partitions
+  BOARD_GOOGLE_DYNAMIC_PARTITIONS_PARTITION_LIST := system vendor product
+  BOARD_GOOGLE_DYNAMIC_PARTITIONS_SIZE := 6442450944
+  BOARD_SUPER_PARTITION_METADATA_DEVICE := vda
+  BOARD_BUILD_SUPER_IMAGE_BY_DEFAULT := true
+  BOARD_SUPER_IMAGE_IN_UPDATE_PACKAGE := true
+else
+  # No dynamic partitions support; we must specify maximum sizes
+  BOARD_SYSTEMIMAGE_PARTITION_SIZE := 4294967296 # 4 GB
+  BOARD_VENDORIMAGE_PARTITION_SIZE := 536870912 # 512MB
+  BOARD_PRODUCTIMAGE_PARTITION_SIZE := 1610612736 # 1.5GB
+  TARGET_NO_RECOVERY ?= true
+endif
+
 # To see full logs from init, disable ratelimiting.
 # The default is 5 messages per second amortized, with a burst of up to 10.
 BOARD_KERNEL_CMDLINE += printk.devkmsg=on
diff --git a/shared/config/fstab.initrd-dynamic-partitions b/shared/config/fstab.initrd-dynamic-partitions
new file mode 100644
index 0000000..551e168
--- /dev/null
+++ b/shared/config/fstab.initrd-dynamic-partitions
@@ -0,0 +1,11 @@
+boot /boot emmc defaults recoveryonly
+system /system ext4 noatime,ro,errors=panic wait,logical,first_stage_mount
+# Add all non-dynamic partitions except system, after this comment
+/dev/block/vdb /data ext4 nodev,noatime,nosuid,errors=panic wait,fileencryption=aes-256-xts:aes-256-cts
+/dev/block/vdc /cache ext4 nodev,noatime,nosuid,errors=panic wait
+/dev/block/vdd /metadata ext4 nodev,noatime,nosuid,errors=panic wait,formattable,first_stage_mount
+# Add all dynamic partitions except system, after this comment
+vendor /vendor ext4 noatime,ro,errors=panic wait,logical,first_stage_mount
+product /product ext4 noatime,ro,errors=panic wait,logical,first_stage_mount
+/dev/block/zram0 none swap defaults zramsize=75%
+/tmp /sdcard none defaults,bind recoveryonly
diff --git a/shared/device.mk b/shared/device.mk
index 08c753c..eb5ecea 100644
--- a/shared/device.mk
+++ b/shared/device.mk
@@ -165,6 +165,13 @@
     device/google/cuttlefish/shared/config/fstab.dtb:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.cutf_ivsh \
     device/google/cuttlefish/shared/config/fstab.dtb:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.cutf_cvm \
 
+else ifeq ($(TARGET_USE_DYNAMIC_PARTITIONS),true)
+PRODUCT_COPY_FILES += \
+    device/google/cuttlefish/shared/config/fstab.initrd-dynamic-partitions:$(TARGET_COPY_OUT_RAMDISK)/fstab.cutf_ivsh \
+    device/google/cuttlefish/shared/config/fstab.initrd-dynamic-partitions:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.cutf_ivsh \
+    device/google/cuttlefish/shared/config/fstab.initrd-dynamic-partitions:$(TARGET_COPY_OUT_RAMDISK)/fstab.cutf_cvm \
+    device/google/cuttlefish/shared/config/fstab.initrd-dynamic-partitions:$(TARGET_COPY_OUT_VENDOR)/etc/fstab.cutf_cvm \
+
 else
 PRODUCT_COPY_FILES += \
     device/google/cuttlefish/shared/config/fstab.initrd:$(TARGET_COPY_OUT_RAMDISK)/fstab.cutf_ivsh \
diff --git a/shared/phone/device.mk b/shared/phone/device.mk
index bffe8f2..115f80f 100644
--- a/shared/phone/device.mk
+++ b/shared/phone/device.mk
@@ -20,8 +20,6 @@
 $(call inherit-product, frameworks/native/build/phone-xhdpi-2048-dalvik-heap.mk)
 $(call inherit-product, device/google/cuttlefish/shared/device.mk)
 
-TARGET_BUILD_SYSTEM_ROOT_IMAGE ?= true
-
 PRODUCT_CHARACTERISTICS := nosdcard
 
 PRODUCT_PROPERTY_OVERRIDES += \
@@ -45,3 +43,11 @@
 # These flags are important for the GSI, but break auto
 PRODUCT_ENFORCE_RRO_TARGETS := framework-res
 PRODUCT_ENFORCE_RRO_EXCLUDED_OVERLAYS := device/google/cuttlefish/shared/overlay
+
+TARGET_USE_DYNAMIC_PARTITIONS ?= true
+ifeq ($(TARGET_USE_DYNAMIC_PARTITIONS),true)
+  PRODUCT_USE_DYNAMIC_PARTITIONS := true
+  TARGET_BUILD_SYSTEM_ROOT_IMAGE := false
+else
+  TARGET_BUILD_SYSTEM_ROOT_IMAGE ?= true
+endif