Augment the auto-loadvm behaviour with corresponding auto-savevm-on-exit behaviour.

Autosave fires when loadvm would have been tried - whether it succeeded or not - in
order to bootstrap from an empty snapshot file.

 - New autosave behaviour inhibited with -no_snapshot_save flag.
 - Underlying behaviour implemented with a new qemu option -savevm_on_exit

Change-Id: If64d89f004565ecbb431bc7e96ecc37e27876d67
diff --git a/android/cmdline-options.h b/android/cmdline-options.h
index 1ae3121..3aaa478 100644
--- a/android/cmdline-options.h
+++ b/android/cmdline-options.h
@@ -81,6 +81,7 @@
 OPT_FLAG ( no_snapstorage, "do not mount a snapshot storage file (this disables all snapshot functionality)" )
 OPT_PARAM( snapshot,       "<name>", "immediately load state snapshot rather than doing a full boot (default 'default-boot')" )
 OPT_FLAG ( no_snapshot,    "do not start from snapshot, but perform a full boot sequence" )
+OPT_FLAG ( no_snapshot_save, "do not preserve snapshot, if restore was attempted" )
 OPT_FLAG ( snapshot_list,  "show a list of available snapshots" )
 #endif
 OPT_FLAG ( wipe_data, "reset the use data image (copy it from initdata)" )
diff --git a/android/help.c b/android/help.c
index 2103b00..348e2e6 100644
--- a/android/help.c
+++ b/android/help.c
@@ -701,6 +701,15 @@
 }
 
 static void
+help_no_snapshot_save(stralloc_t*  out)
+{
+    PRINTF(
+    "  Prevents the emulator from saving the AVD's state to the snapshot\n"
+    "  storage on exit, meaning that all changes will be lost.\n\n"
+    );
+}
+
+static void
 help_snapshot_list(stralloc_t*  out)
 {
     PRINTF(
diff --git a/android/main-ui.c b/android/main-ui.c
index e1ad576..9366efd 100644
--- a/android/main-ui.c
+++ b/android/main-ui.c
@@ -1518,12 +1518,13 @@
         }
 
         if (!opts->no_snapshot) {
+            char* snapshot_name =
+                opts->snapshot ? opts->snapshot : "default-boot";
             args[n++] = "-loadvm";
-            if (opts->snapshot) {
-                args[n++] = opts->snapshot;
-            } else {
-                // name of state snapshot to load if not specified by user
-                args[n++] = "default-boot";
+            args[n++] = snapshot_name;
+            if (!opts->no_snapshot_save) {
+              args[n++] = "-savevm-on-exit";
+              args[n++] = snapshot_name;
             }
         } else if (opts->snapshot) {
             dwarning("option '-no-snapshot' overrides '-snapshot', continuing with boot sequence");
diff --git a/android/main.c b/android/main.c
index c1a4a9f..677980b 100644
--- a/android/main.c
+++ b/android/main.c
@@ -1410,12 +1410,13 @@
         }
 
         if (!opts->no_snapshot) {
+            char* snapshot_name =
+                opts->snapshot ? opts->snapshot : "default-boot";
             args[n++] = "-loadvm";
-            if (opts->snapshot) {
-                args[n++] = opts->snapshot;
-            } else {
-                // name of state snapshot to load if not specified by user
-                args[n++] = "default-boot";
+            args[n++] = snapshot_name;
+            if (!opts->no_snapshot_save) {
+              args[n++] = "-savevm-on-exit";
+              args[n++] = snapshot_name;
             }
         } else if (opts->snapshot) {
             dwarning("option '-no-snapshot' overrides '-snapshot', continuing with boot sequence");