Specify kernel arguments directly to VmManager.

This lets run_cvd add kernel arguments at the last minute, e.g. to react
to a dynamically assigned port.

Test: Build and run
Bug: 145247175
Change-Id: Icfd45363aa7a38b73160e58383af249fd3f973ff
diff --git a/host/commands/run_cvd/main.cc b/host/commands/run_cvd/main.cc
index fdd909d..0c4261b 100644
--- a/host/commands/run_cvd/main.cc
+++ b/host/commands/run_cvd/main.cc
@@ -416,7 +416,9 @@
       *config, &process_monitor, GetOnSubprocessExitCallback(*config));
 
   // Start the guest VM
-  auto vmm_commands = vm_manager->StartCommands(frontend_enabled);
+  vm_manager->WithFrontend(frontend_enabled);
+  vm_manager->WithKernelCommandLine(config->kernel_cmdline_as_string());
+  auto vmm_commands = vm_manager->StartCommands();
   for (auto& vmm_cmd: vmm_commands) {
       process_monitor.StartSubprocess(std::move(vmm_cmd),
                                       GetOnSubprocessExitCallback(*config));
diff --git a/host/libs/vm_manager/crosvm_manager.cpp b/host/libs/vm_manager/crosvm_manager.cpp
index f132bac..1131db1 100644
--- a/host/libs/vm_manager/crosvm_manager.cpp
+++ b/host/libs/vm_manager/crosvm_manager.cpp
@@ -22,6 +22,7 @@
 #include <string>
 #include <vector>
 
+#include <android-base/strings.h>
 #include <glog/logging.h>
 
 #include "common/libs/utils/network.h"
@@ -94,7 +95,7 @@
 CrosvmManager::CrosvmManager(const vsoc::CuttlefishConfig* config)
     : VmManager(config) {}
 
-std::vector<cvd::Command> CrosvmManager::StartCommands(bool with_frontend) {
+std::vector<cvd::Command> CrosvmManager::StartCommands() {
   cvd::Command crosvm_cmd(config_->crosvm_binary(), [](cvd::Subprocess* proc) {
     auto stopped = Stop();
     if (stopped) {
@@ -120,13 +121,13 @@
   crosvm_cmd.AddParameter("--null-audio");
   crosvm_cmd.AddParameter("--mem=", config_->memory_mb());
   crosvm_cmd.AddParameter("--cpus=", config_->cpus());
-  crosvm_cmd.AddParameter("--params=", config_->kernel_cmdline_as_string());
+  crosvm_cmd.AddParameter("--params=", kernel_cmdline_);
   for (const auto& disk : config_->virtual_disk_paths()) {
     crosvm_cmd.AddParameter("--rwdisk=", disk);
   }
   crosvm_cmd.AddParameter("--socket=", GetControlSocketPath(config_));
 
-  if (with_frontend) {
+  if (frontend_enabled_) {
     crosvm_cmd.AddParameter("--single-touch=", config_->touch_socket_path(),
                             ":", config_->x_res(), ":", config_->y_res());
     crosvm_cmd.AddParameter("--keyboard=", config_->keyboard_socket_path());
diff --git a/host/libs/vm_manager/crosvm_manager.h b/host/libs/vm_manager/crosvm_manager.h
index 74ed8c5..ec0b844 100644
--- a/host/libs/vm_manager/crosvm_manager.h
+++ b/host/libs/vm_manager/crosvm_manager.h
@@ -34,7 +34,7 @@
   CrosvmManager(const vsoc::CuttlefishConfig* config);
   virtual ~CrosvmManager() = default;
 
-  std::vector<cvd::Command> StartCommands(bool with_frontend) override;
+  std::vector<cvd::Command> StartCommands() override;
 };
 
 }  // namespace vm_manager
diff --git a/host/libs/vm_manager/qemu_manager.cpp b/host/libs/vm_manager/qemu_manager.cpp
index b40be87..c62d0cd 100644
--- a/host/libs/vm_manager/qemu_manager.cpp
+++ b/host/libs/vm_manager/qemu_manager.cpp
@@ -30,6 +30,7 @@
 #include <thread>
 #include <vector>
 
+#include <android-base/strings.h>
 #include <glog/logging.h>
 
 #include "common/libs/fs/shared_select.h"
@@ -126,7 +127,7 @@
 QemuManager::QemuManager(const vsoc::CuttlefishConfig* config)
   : VmManager(config) {}
 
-std::vector<cvd::Command> QemuManager::StartCommands(bool /*with_frontend*/) {
+std::vector<cvd::Command> QemuManager::StartCommands() {
   // Set the config values in the environment
   LogAndSetEnv("qemu_binary", config_->qemu_binary());
   LogAndSetEnv("instance_name", config_->instance_name());
@@ -137,7 +138,7 @@
   LogAndSetEnv("kernel_image_path", config_->GetKernelImageToUse());
   LogAndSetEnv("gdb_flag", config_->gdb_flag());
   LogAndSetEnv("ramdisk_image_path", config_->final_ramdisk_path());
-  LogAndSetEnv("kernel_cmdline", config_->kernel_cmdline_as_string());
+  LogAndSetEnv("kernel_cmdline", kernel_cmdline_);
   LogAndSetEnv("virtual_disk_paths", JoinString(config_->virtual_disk_paths(),
                                                 ";"));
   LogAndSetEnv("wifi_tap_name", config_->wifi_tap_name());
diff --git a/host/libs/vm_manager/qemu_manager.h b/host/libs/vm_manager/qemu_manager.h
index 9b0df49..ee81d5c 100644
--- a/host/libs/vm_manager/qemu_manager.h
+++ b/host/libs/vm_manager/qemu_manager.h
@@ -32,7 +32,7 @@
   QemuManager(const vsoc::CuttlefishConfig* config);
   virtual ~QemuManager() = default;
 
-  std::vector<cvd::Command> StartCommands(bool with_frontend) override;
+  std::vector<cvd::Command> StartCommands() override;
 };
 
 }  // namespace vm_manager
diff --git a/host/libs/vm_manager/vm_manager.cpp b/host/libs/vm_manager/vm_manager.cpp
index ac5531a..47818e8 100644
--- a/host/libs/vm_manager/vm_manager.cpp
+++ b/host/libs/vm_manager/vm_manager.cpp
@@ -154,4 +154,13 @@
   auto linux_ver_4_8 = VmManager::LinuxVersionAtLeast4_8(config_commands);
   return in_kvm && in_cvdnetwork && linux_ver_4_8;
 }
+
+void VmManager::WithFrontend(bool enabled) {
+  frontend_enabled_ = enabled;
+}
+
+void VmManager::WithKernelCommandLine(const std::string& kernel_cmdline) {
+  kernel_cmdline_ = kernel_cmdline;
+}
+
 }  // namespace vm_manager
diff --git a/host/libs/vm_manager/vm_manager.h b/host/libs/vm_manager/vm_manager.h
index 62adbf8..a87bda5 100644
--- a/host/libs/vm_manager/vm_manager.h
+++ b/host/libs/vm_manager/vm_manager.h
@@ -16,6 +16,7 @@
 #pragma once
 
 #include <map>
+#include <set>
 #include <string>
 #include <utility>
 #include <vector>
@@ -42,11 +43,14 @@
 
   virtual ~VmManager() = default;
 
+  virtual void WithFrontend(bool);
+  virtual void WithKernelCommandLine(const std::string&);
+
   // Starts the VMM. It will usually build a command and pass it to the
   // command_starter function, although it may start more than one. The
   // command_starter function allows to customize the way vmm commands are
   // started/tracked/etc.
-  virtual std::vector<cvd::Command> StartCommands(bool with_frontend) = 0;
+  virtual std::vector<cvd::Command> StartCommands() = 0;
 
   virtual bool ValidateHostConfiguration(
       std::vector<std::string>* config_commands) const;
@@ -59,6 +63,9 @@
   const vsoc::CuttlefishConfig* config_;
   VmManager(const vsoc::CuttlefishConfig* config);
 
+  bool frontend_enabled_;
+  std::string kernel_cmdline_;
+
  private:
   struct VmManagerHelper {
     // The singleton implementation