Merge "emulator-qemu: Create empty cache partition image on demand."
diff --git a/Makefile.qemu-launcher b/Makefile.qemu-launcher
index 9c26567..16c1dbf 100644
--- a/Makefile.qemu-launcher
+++ b/Makefile.qemu-launcher
@@ -26,6 +26,9 @@
 LOCAL_CFLAGS := $(qemu_launcher_CFLAGS)
 LOCAL_STATIC_LIBRARIES := \
     emulator-common \
+    emulator-libext4_utils \
+    emulator-libsparse \
+    emulator-libselinux \
     emulator-zlib
 LOCAL_LDLIBS := $(qemu_launcher_LDLIBS)
 $(call gen-hw-config-defs)
@@ -36,6 +39,9 @@
 LOCAL_CFLAGS := $(qemu_launcher_CFLAGS)
 LOCAL_STATIC_LIBRARIES := \
     emulator64-common \
+    emulator64-libext4_utils \
+    emulator64-libsparse \
+    emulator64-libselinux \
     emulator64-zlib
 LOCAL_LDLIBS := $(qemu_launcher_LDLIBS)
 $(call gen-hw-config-defs)
diff --git a/android/qemu-launcher/emulator-qemu.cpp b/android/qemu-launcher/emulator-qemu.cpp
index e6b5c35..fdd38a7 100644
--- a/android/qemu-launcher/emulator-qemu.cpp
+++ b/android/qemu-launcher/emulator-qemu.cpp
@@ -23,6 +23,7 @@
 #include "android/cmdline-option.h"
 #include "android/globals.h"
 #include "android/help.h"
+#include "android/filesystems/ext4_utils.h"
 #include "android/kernel/kernel_utils.h"
 #include "android/main-common.h"
 #include "android/utils/bufprint.h"
@@ -729,6 +730,21 @@
         }
     }
 
+    // Create cache partition image if it doesn't exist already.
+    if (!path_exists(hw->disk_cachePartition_path)) {
+        D("Creating empty ext4 cache partition: %s",
+          hw->disk_cachePartition_path);
+        int ret = android_createEmptyExt4Image(
+                hw->disk_cachePartition_path,
+                hw->disk_cachePartition_size,
+                "cache");
+        if (ret < 0) {
+            derror("Could not create %s: %s", hw->disk_cachePartition_path,
+                   strerror(-ret));
+            exit(1);
+        }
+    }
+
     // Now build the QEMU parameters.
     const char* args[128];
     int n = 0;