diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2019-12-06 01:22:14 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-12-06 01:22:14 +0000 |
commit | 17907ab7686492b8dfc6ea1bc8b27621a5c49533 (patch) | |
tree | 15dfe51477fdd61995900be5c79bd918577a05e5 | |
parent | af9c88256a19e3f63064c3cde2f98ccb70a78c73 (diff) | |
parent | a872cca089b266a27273da252f7645bede5a3457 (diff) | |
download | cuttlefish_common-17907ab7686492b8dfc6ea1bc8b27621a5c49533.tar.gz |
Merge changes from topic "schuffelen-2019-12-04-cf-kernel-args"
* changes:
Delay constructing the kernel command line.
Move some kernel cmdline args to BoardConfig.mk
Specify kernel arguments directly to VmManager.
-rw-r--r-- | host/commands/assemble_cvd/flags.cc | 106 | ||||
-rw-r--r-- | host/commands/run_cvd/main.cc | 79 | ||||
-rw-r--r-- | host/libs/config/cuttlefish_config.cpp | 110 | ||||
-rw-r--r-- | host/libs/config/cuttlefish_config.h | 24 | ||||
-rw-r--r-- | host/libs/vm_manager/crosvm_manager.cpp | 41 | ||||
-rw-r--r-- | host/libs/vm_manager/crosvm_manager.h | 9 | ||||
-rw-r--r-- | host/libs/vm_manager/qemu_manager.cpp | 28 | ||||
-rw-r--r-- | host/libs/vm_manager/qemu_manager.h | 9 | ||||
-rw-r--r-- | host/libs/vm_manager/vm_manager.cpp | 43 | ||||
-rw-r--r-- | host/libs/vm_manager/vm_manager.h | 19 |
10 files changed, 247 insertions, 221 deletions
diff --git a/host/commands/assemble_cvd/flags.cc b/host/commands/assemble_cvd/flags.cc index 5c606747..2d6007ef 100644 --- a/host/commands/assemble_cvd/flags.cc +++ b/host/commands/assemble_cvd/flags.cc @@ -55,16 +55,8 @@ DEFINE_string(kernel_decompresser_executable, DEFINE_string(extra_kernel_cmdline, "", "Additional flags to put on the kernel command line"); DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions"); -DEFINE_string(androidboot_console, "ttyS1", - "Console device for the Android framework"); -DEFINE_string( - hardware_name, "", - "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}"); -DEFINE_string(guest_security, "selinux", - "The security module to use in the guest"); DEFINE_bool(guest_enforce_security, true, - "Whether to run in enforcing mode (non permissive). Ignored if " - "-guest_security is empty."); + "Whether to run in enforcing mode (non permissive)."); DEFINE_bool(guest_audit_security, true, "Whether to log security audits."); DEFINE_string(boot_image, "", @@ -205,13 +197,6 @@ const std::string kKernelDefaultPath = "kernel"; const std::string kInitramfsImg = "initramfs.img"; const std::string kRamdiskConcatExt = ".concat"; -template<typename S, typename T> -static std::string concat(const S& s, const T& t) { - std::ostringstream os; - os << s << t; - return os.str(); -} - bool ResolveInstanceFiles() { if (FLAGS_system_image_dir.empty()) { LOG(ERROR) << "--system_image_dir must be specified."; @@ -277,7 +262,8 @@ bool InitializeCuttlefishConfiguration( } tmp_config_obj.set_vm_manager(FLAGS_vm_manager); tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode); - if (!vm_manager::VmManager::ConfigureGpuMode(&tmp_config_obj)) { + if (vm_manager::VmManager::ConfigureGpuMode(tmp_config_obj.vm_manager(), + tmp_config_obj.gpu_mode()).empty()) { LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode << " does not work with vm_manager=" << FLAGS_vm_manager; return false; @@ -285,8 +271,6 @@ bool InitializeCuttlefishConfiguration( tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket); tmp_config_obj.set_x_display(FLAGS_x_display); - vm_manager::VmManager::ConfigureBootDevices(&tmp_config_obj); - tmp_config_obj.set_serial_number(FLAGS_serial_number); tmp_config_obj.set_cpus(FLAGS_cpus); @@ -328,56 +312,17 @@ bool InitializeCuttlefishConfiguration( return false; } - tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline()); - tmp_config_obj.add_kernel_cmdline("init=/init"); - tmp_config_obj.add_kernel_cmdline( - concat("androidboot.serialno=", FLAGS_serial_number)); - tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0"); - tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi)); - tmp_config_obj.add_kernel_cmdline( - concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode)); - tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part)); - if (!FLAGS_androidboot_console.empty()) { - tmp_config_obj.add_kernel_cmdline( - concat("androidboot.console=", FLAGS_androidboot_console)); - } - if (!FLAGS_hardware_name.empty()) { - tmp_config_obj.add_kernel_cmdline( - concat("androidboot.hardware=", FLAGS_hardware_name)); - } - if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) { - tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=", - FLAGS_logcat_vsock_port)); - } - tmp_config_obj.add_kernel_cmdline(concat("androidboot.cuttlefish_config_server_port=", - FLAGS_config_server_port)); - tmp_config_obj.set_hardware_name(FLAGS_hardware_name); - if (!FLAGS_guest_security.empty()) { - tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security)); - if (FLAGS_guest_enforce_security) { - tmp_config_obj.add_kernel_cmdline("enforcing=1"); - } else { - tmp_config_obj.add_kernel_cmdline("enforcing=0"); - tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive"); - } - if (FLAGS_guest_audit_security) { - tmp_config_obj.add_kernel_cmdline("audit=1"); - } else { - tmp_config_obj.add_kernel_cmdline("audit=0"); - } - } - if (FLAGS_extra_kernel_cmdline.size()) { - tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline); - } + tmp_config_obj.set_boot_image_kernel_cmdline(boot_image_unpacker.kernel_cmdline()); + tmp_config_obj.set_loop_max_part(FLAGS_loop_max_part); + tmp_config_obj.set_guest_enforce_security(FLAGS_guest_enforce_security); + tmp_config_obj.set_guest_audit_security(FLAGS_guest_audit_security); + tmp_config_obj.set_extra_kernel_cmdline(FLAGS_extra_kernel_cmdline); tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk}); tmp_config_obj.set_ramdisk_image_path(ramdisk_path); tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path); - // Boot as recovery is set so normal boot needs to be forced every boot - tmp_config_obj.add_kernel_cmdline("androidboot.force_normal_boot=1"); - std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg); std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk; if (foreign_kernel.size() && !foreign_ramdisk.size()) { @@ -456,32 +401,13 @@ bool InitializeCuttlefishConfiguration( tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port); tmp_config_obj.set_config_server_port(FLAGS_config_server_port); tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port); - if (tmp_config_obj.enable_vnc_server()) { - tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_frames_port=", - FLAGS_frames_vsock_port)); - } tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver); tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port); tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary); - if (FLAGS_enable_tombstone_receiver) { - tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=1"); - tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_tombstone_port=" - ,FLAGS_tombstone_receiver_port)); - // TODO (b/128842613) populate a cid flag to read the host CID during - // runtime - } else { - tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=0"); - } tmp_config_obj.set_touch_socket_port(FLAGS_touch_server_port); tmp_config_obj.set_keyboard_socket_port(FLAGS_keyboard_server_port); - if (FLAGS_vm_manager == vm_manager::QemuManager::name()) { - tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_touch_port=", - FLAGS_touch_server_port)); - tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_keyboard_port=", - FLAGS_keyboard_server_port)); - } tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader); tmp_config_obj.set_bootloader(FLAGS_bootloader); @@ -490,16 +416,6 @@ bool InitializeCuttlefishConfiguration( tmp_config_obj.set_boot_slot(FLAGS_boot_slot); } - if (!FLAGS_use_bootloader) { - std::string slot_suffix; - if (FLAGS_boot_slot.empty()) { - slot_suffix = "_a"; - } else { - slot_suffix = "_" + FLAGS_boot_slot; - } - tmp_config_obj.add_kernel_cmdline("androidboot.slot_suffix=" + slot_suffix); - } - tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath()); auto config_file = GetConfigFilePath(tmp_config_obj); @@ -525,9 +441,6 @@ void SetDefaultFlagsForQemu() { SetCommandLineOptionWithMode("instance_dir", default_instance_dir.c_str(), google::FlagSettingMode::SET_FLAGS_DEFAULT); - // TODO(b/144111429): Consolidate to one hardware name - SetCommandLineOptionWithMode("hardware_name", "cutf_cvm", - google::FlagSettingMode::SET_FLAGS_DEFAULT); // TODO(b/144119457) Use the serial port. SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode, google::FlagSettingMode::SET_FLAGS_DEFAULT); @@ -545,9 +458,6 @@ void SetDefaultFlagsForCrosvm() { SetCommandLineOptionWithMode("x_display", getenv("DISPLAY"), google::FlagSettingMode::SET_FLAGS_DEFAULT); - // TODO(b/144111429): Consolidate to one hardware name - SetCommandLineOptionWithMode("hardware_name", "cutf_cvm", - google::FlagSettingMode::SET_FLAGS_DEFAULT); SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode, google::FlagSettingMode::SET_FLAGS_DEFAULT); } diff --git a/host/commands/run_cvd/main.cc b/host/commands/run_cvd/main.cc index fdd909d9..486a0db4 100644 --- a/host/commands/run_cvd/main.cc +++ b/host/commands/run_cvd/main.cc @@ -277,6 +277,80 @@ std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) { return config.PerInstancePath("cuttlefish_config.json"); } +template<typename T> +void AppendVector(std::vector<T>* destination, const std::vector<T>& source) { + destination->insert(destination->end(), source.begin(), source.end()); +} + +template<typename S, typename T> +static std::string concat(const S& s, const T& t) { + std::ostringstream os; + os << s << t; + return os.str(); +} + +std::vector<std::string> KernelCommandLineFromConfig(const vsoc::CuttlefishConfig& config) { + std::vector<std::string> kernel_cmdline; + + AppendVector(&kernel_cmdline, config.boot_image_kernel_cmdline()); + AppendVector(&kernel_cmdline, + vm_manager::VmManager::ConfigureGpuMode(config.vm_manager(), config.gpu_mode())); + AppendVector(&kernel_cmdline, vm_manager::VmManager::ConfigureBootDevices(config.vm_manager())); + + kernel_cmdline.push_back(concat("androidboot.serialno=", config.serial_number())); + kernel_cmdline.push_back(concat("androidboot.lcd_density=", config.dpi())); + if (config.logcat_mode() == cvd::kLogcatVsockMode) { + kernel_cmdline.push_back(concat("androidboot.vsock_logcat_port=", config.logcat_vsock_port())); + } + if (config.enable_vnc_server()) { + kernel_cmdline.push_back(concat("androidboot.vsock_frames_port=", config.frames_vsock_port())); + } + if (config.enable_tombstone_receiver()) { + kernel_cmdline.push_back("androidboot.tombstone_transmit=1"); + kernel_cmdline.push_back(concat( + "androidboot.vsock_tombstone_port=", + config.tombstone_receiver_port())); + // TODO (b/128842613) populate a cid flag to read the host CID during + // runtime + } else { + kernel_cmdline.push_back("androidboot.tombstone_transmit=0"); + } + kernel_cmdline.push_back(concat( + "androidboot.cuttlefish_config_server_port=", config.config_server_port())); + kernel_cmdline.push_back(concat( + "androidboot.setupwizard_mode=", config.setupwizard_mode())); + if (!config.use_bootloader()) { + std::string slot_suffix; + if (config.boot_slot().empty()) { + slot_suffix = "_a"; + } else { + slot_suffix = "_" + config.boot_slot(); + } + kernel_cmdline.push_back(concat("androidboot.slot_suffix=", slot_suffix)); + } + if (config.vm_manager() == vm_manager::QemuManager::name()) { + kernel_cmdline.push_back(concat("androidboot.vsock_touch_port=", config.touch_socket_port())); + kernel_cmdline.push_back(concat( + "androidboot.vsock_keyboard_port=", config.keyboard_socket_port())); + } + kernel_cmdline.push_back(concat("loop.max_part=", config.loop_max_part())); + if (config.guest_enforce_security()) { + kernel_cmdline.push_back("enforcing=1"); + } else { + kernel_cmdline.push_back("enforcing=0"); + kernel_cmdline.push_back("androidboot.selinux=permissive"); + } + if (config.guest_audit_security()) { + kernel_cmdline.push_back("audit=1"); + } else { + kernel_cmdline.push_back("audit=0"); + } + + AppendVector(&kernel_cmdline, config.extra_kernel_cmdline()); + + return kernel_cmdline; +} + } // namespace int main(int argc, char** argv) { @@ -416,7 +490,10 @@ int main(int argc, char** argv) { *config, &process_monitor, GetOnSubprocessExitCallback(*config)); // Start the guest VM - auto vmm_commands = vm_manager->StartCommands(frontend_enabled); + vm_manager->WithFrontend(frontend_enabled); + auto kernel_args = KernelCommandLineFromConfig(*config); + vm_manager->WithKernelCommandLine(android::base::Join(kernel_args, " ")); + 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/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp index 7a655253..3b223d9a 100644 --- a/host/libs/config/cuttlefish_config.cpp +++ b/host/libs/config/cuttlefish_config.cpp @@ -26,6 +26,7 @@ #include <sstream> #include <string> +#include <android-base/strings.h> #include <glog/logging.h> #include <json/json.h> @@ -72,7 +73,6 @@ const char* kVmManager = "vm_manager"; const char* const kGpuMode = "gpu_mode"; const char* const kWaylandSocket = "wayland_socket"; const char* const kXDisplay = "x_display"; -const char* kHardwareName = "hardware_name"; const char* kDeviceTitle = "device_title"; const char* kCpus = "cpus"; @@ -88,7 +88,6 @@ const char* kUseUnpackedKernel = "use_unpacked_kernel"; const char* kDecompressedKernelImagePath = "decompressed_kernel_image_path"; const char* kDecompressKernel = "decompress_kernel"; const char* kGdbFlag = "gdb_flag"; -const char* kKernelCmdline = "kernel_cmdline"; const char* kRamdiskImagePath = "ramdisk_image_path"; const char* kInitramfsPath = "initramfs_path"; const char* kFinalRamdiskPath = "final_ramdisk_path"; @@ -160,6 +159,12 @@ const char* kBootSlot = "boot_slot"; const char* kTouchSocketPort = "touch_socket_port"; const char* kKeyboardSocketPort = "keyboard_socket_port"; +const char* kLoopMaxPart = "loop_max_part"; +const char* kGuestEnforceSecurity = "guest_enforce_security"; +const char* kGuestAuditSecurity = "guest_audit_security"; +const char* kBootImageKernelCmdline = "boot_image_kernel_cmdline"; +const char* kExtraKernelCmdline = "extra_kernel_cmdline"; + } // namespace namespace vsoc { @@ -213,13 +218,6 @@ void CuttlefishConfig::set_x_display(const std::string& address) { (*dictionary_)[kXDisplay] = address; } -std::string CuttlefishConfig::hardware_name() const { - return (*dictionary_)[kHardwareName].asString(); -} -void CuttlefishConfig::set_hardware_name(const std::string& name) { - (*dictionary_)[kHardwareName] = name; -} - std::string CuttlefishConfig::serial_number() const { return (*dictionary_)[kSerialNumber].asString(); } @@ -307,49 +305,6 @@ void CuttlefishConfig::set_gdb_flag(const std::string& device) { (*dictionary_)[kGdbFlag] = device; } -std::set<std::string> CuttlefishConfig::kernel_cmdline() const { - std::set<std::string> args_set; - auto args_json_obj = (*dictionary_)[kKernelCmdline]; - std::transform(args_json_obj.begin(), args_json_obj.end(), - std::inserter(args_set, args_set.begin()), - [](const Json::Value& it) { return it.asString(); }); - return args_set; -} -void CuttlefishConfig::set_kernel_cmdline( - const std::set<std::string>& kernel_cmdline) { - Json::Value args_json_obj(Json::arrayValue); - for (const auto& arg : kernel_cmdline) { - args_json_obj.append(arg); - } - (*dictionary_)[kKernelCmdline] = args_json_obj; -} -void CuttlefishConfig::add_kernel_cmdline( - const std::set<std::string>& extra_args) { - std::set<std::string> cmdline = kernel_cmdline(); - for (const auto& arg : extra_args) { - if (cmdline.count(arg)) { - LOG(ERROR) << "Kernel argument " << arg << " is duplicated"; - } - cmdline.insert(arg); - } - set_kernel_cmdline(cmdline); -} -void CuttlefishConfig::add_kernel_cmdline(const std::string& kernel_cmdline) { - std::stringstream args_stream(kernel_cmdline); - std::set<std::string> kernel_cmdline_set; - using is_iter = std::istream_iterator<std::string>; - std::copy(is_iter(args_stream), is_iter(), - std::inserter(kernel_cmdline_set, kernel_cmdline_set.begin())); - add_kernel_cmdline(kernel_cmdline_set); -} -std::string CuttlefishConfig::kernel_cmdline_as_string() const { - auto args_set = kernel_cmdline(); - std::stringstream output; - std::copy(args_set.begin(), args_set.end(), - std::ostream_iterator<std::string>(output, " ")); - return output.str(); -} - std::string CuttlefishConfig::ramdisk_image_path() const { return (*dictionary_)[kRamdiskImagePath].asString(); } @@ -859,6 +814,57 @@ int CuttlefishConfig::keyboard_socket_port() const { return (*dictionary_)[kKeyboardSocketPort].asInt(); } +void CuttlefishConfig::set_loop_max_part(int loop_max_part) { + (*dictionary_)[kLoopMaxPart] = loop_max_part; +} +int CuttlefishConfig::loop_max_part() const { + return (*dictionary_)[kLoopMaxPart].asInt(); +} + +void CuttlefishConfig::set_guest_enforce_security(bool guest_enforce_security) { + (*dictionary_)[kGuestEnforceSecurity] = guest_enforce_security; +} +bool CuttlefishConfig::guest_enforce_security() const { + return (*dictionary_)[kGuestEnforceSecurity].asBool(); +} + +void CuttlefishConfig::set_guest_audit_security(bool guest_audit_security) { + (*dictionary_)[kGuestAuditSecurity] = guest_audit_security; +} +bool CuttlefishConfig::guest_audit_security() const { + return (*dictionary_)[kGuestAuditSecurity].asBool(); +} + +void CuttlefishConfig::set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline) { + Json::Value args_json_obj(Json::arrayValue); + for (const auto& arg : android::base::Split(boot_image_kernel_cmdline, " ")) { + args_json_obj.append(arg); + } + (*dictionary_)[kBootImageKernelCmdline] = args_json_obj; +} +std::vector<std::string> CuttlefishConfig::boot_image_kernel_cmdline() const { + std::vector<std::string> cmdline; + for (const Json::Value& arg : (*dictionary_)[kBootImageKernelCmdline]) { + cmdline.push_back(arg.asString()); + } + return cmdline; +} + +void CuttlefishConfig::set_extra_kernel_cmdline(std::string extra_cmdline) { + Json::Value args_json_obj(Json::arrayValue); + for (const auto& arg : android::base::Split(extra_cmdline, " ")) { + args_json_obj.append(arg); + } + (*dictionary_)[kExtraKernelCmdline] = extra_cmdline; +} +std::vector<std::string> CuttlefishConfig::extra_kernel_cmdline() const { + std::vector<std::string> cmdline; + for (const Json::Value& arg : (*dictionary_)[kExtraKernelCmdline]) { + cmdline.push_back(arg.asString()); + } + return cmdline; +} + // Creates the (initially empty) config object and populates it with values from // the config file if the CUTTLEFISH_CONFIG_FILE env variable is present. // Returns nullptr if there was an error loading from file diff --git a/host/libs/config/cuttlefish_config.h b/host/libs/config/cuttlefish_config.h index a920e760..bf3d2317 100644 --- a/host/libs/config/cuttlefish_config.h +++ b/host/libs/config/cuttlefish_config.h @@ -86,9 +86,6 @@ class CuttlefishConfig { std::string x_display() const; void set_x_display(const std::string& address); - std::string hardware_name() const; - void set_hardware_name(const std::string& name); - std::string serial_number() const; void set_serial_number(const std::string& serial_number); @@ -137,12 +134,6 @@ class CuttlefishConfig { bool use_unpacked_kernel() const; void set_use_unpacked_kernel(bool use_unpacked_kernel); - std::set<std::string> kernel_cmdline() const; - void set_kernel_cmdline(const std::set<std::string>& kernel_cmdline); - void add_kernel_cmdline(const std::string& arg); - void add_kernel_cmdline(const std::set<std::string>& kernel_cmdline); - std::string kernel_cmdline_as_string() const; - std::string gdb_flag() const; void set_gdb_flag(const std::string& gdb); @@ -327,6 +318,21 @@ class CuttlefishConfig { void set_keyboard_socket_port(int keyboard_socket_port); int keyboard_socket_port() const; + void set_loop_max_part(int loop_max_part); + int loop_max_part() const; + + void set_guest_enforce_security(bool guest_enforce_security); + bool guest_enforce_security() const; + + void set_guest_audit_security(bool guest_audit_security); + bool guest_audit_security() const; + + void set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline); + std::vector<std::string> boot_image_kernel_cmdline() const; + + void set_extra_kernel_cmdline(std::string extra_cmdline); + std::vector<std::string> extra_kernel_cmdline() const; + private: std::unique_ptr<Json::Value> dictionary_; diff --git a/host/libs/vm_manager/crosvm_manager.cpp b/host/libs/vm_manager/crosvm_manager.cpp index f132bacd..6ed43c21 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" @@ -62,39 +63,39 @@ bool Stop() { const std::string CrosvmManager::name() { return "crosvm"; } -bool CrosvmManager::ConfigureGpu(vsoc::CuttlefishConfig* config) { +std::vector<std::string> CrosvmManager::ConfigureGpu(const std::string& gpu_mode) { // Override the default HAL search paths in all cases. We do this because // the HAL search path allows for fallbacks, and fallbacks in conjunction // with properities lead to non-deterministic behavior while loading the // HALs. - if (config->gpu_mode() == vsoc::kGpuModeDrmVirgl) { - config->add_kernel_cmdline("androidboot.hardware.gralloc=minigbm"); - config->add_kernel_cmdline("androidboot.hardware.hwcomposer=drm_minigbm"); - config->add_kernel_cmdline("androidboot.hardware.egl=mesa"); - return true; + if (gpu_mode == vsoc::kGpuModeDrmVirgl) { + return { + "androidboot.hardware.gralloc=minigbm", + "androidboot.hardware.hwcomposer=drm_minigbm", + "androidboot.hardware.egl=mesa", + }; } - if (config->gpu_mode() == vsoc::kGpuModeGuestSwiftshader) { - config->add_kernel_cmdline("androidboot.hardware.gralloc=cutf_ashmem"); - config->add_kernel_cmdline( - "androidboot.hardware.hwcomposer=cutf_cvm_ashmem"); - config->add_kernel_cmdline("androidboot.hardware.egl=swiftshader"); - config->add_kernel_cmdline("androidboot.hardware.vulkan=pastel"); - return true; + if (gpu_mode == vsoc::kGpuModeGuestSwiftshader) { + return { + "androidboot.hardware.gralloc=cutf_ashmem", + "androidboot.hardware.hwcomposer=cutf_cvm_ashmem", + "androidboot.hardware.egl=swiftshader", + "androidboot.hardware.vulkan=pastel", + }; } - return false; + return {}; } -void CrosvmManager::ConfigureBootDevices(vsoc::CuttlefishConfig* config) { +std::vector<std::string> CrosvmManager::ConfigureBootDevices() { // PCI domain 0, bus 0, device 1, function 0 // TODO There is no way to control this assignment with crosvm (yet) - config->add_kernel_cmdline( - "androidboot.boot_devices=pci0000:00/0000:00:01.0"); + return { "androidboot.boot_devices=pci0000:00/0000:00:01.0" }; } 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 @@ std::vector<cvd::Command> CrosvmManager::StartCommands(bool with_frontend) { 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 74ed8c53..848f2ae4 100644 --- a/host/libs/vm_manager/crosvm_manager.h +++ b/host/libs/vm_manager/crosvm_manager.h @@ -15,6 +15,9 @@ */ #pragma once +#include <string> +#include <vector> + #include "host/libs/vm_manager/vm_manager.h" #include "common/libs/fs/shared_fd.h" @@ -28,13 +31,13 @@ class CrosvmManager : public VmManager { public: static const std::string name(); static bool EnsureInstanceDirExists(const std::string& instance_dir); - static bool ConfigureGpu(vsoc::CuttlefishConfig* config); - static void ConfigureBootDevices(vsoc::CuttlefishConfig* config); + static std::vector<std::string> ConfigureGpu(const std::string& gpu_mode); + static std::vector<std::string> ConfigureBootDevices(); 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 b40be877..45e3003a 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" @@ -100,33 +101,32 @@ bool Stop() { const std::string QemuManager::name() { return "qemu_cli"; } -bool QemuManager::ConfigureGpu(vsoc::CuttlefishConfig *config) { - if (config->gpu_mode() != vsoc::kGpuModeGuestSwiftshader) { - return false; +std::vector<std::string> QemuManager::ConfigureGpu(const std::string& gpu_mode) { + if (gpu_mode != vsoc::kGpuModeGuestSwiftshader) { + return {}; } // Override the default HAL search paths in all cases. We do this because // the HAL search path allows for fallbacks, and fallbacks in conjunction // with properities lead to non-deterministic behavior while loading the // HALs. - config->add_kernel_cmdline("androidboot.hardware.gralloc=cutf_ashmem"); - config->add_kernel_cmdline( - "androidboot.hardware.hwcomposer=cutf_cvm_ashmem"); - config->add_kernel_cmdline("androidboot.hardware.egl=swiftshader"); - config->add_kernel_cmdline("androidboot.hardware.vulkan=pastel"); - return true; + return { + "androidboot.hardware.gralloc=cutf_ashmem", + "androidboot.hardware.hwcomposer=cutf_cvm_ashmem", + "androidboot.hardware.egl=swiftshader", + "androidboot.hardware.vulkan=pastel", + }; } -void QemuManager::ConfigureBootDevices(vsoc::CuttlefishConfig* config) { +std::vector<std::string> QemuManager::ConfigureBootDevices() { // PCI domain 0, bus 0, device 3, function 0 // This is controlled with 'addr=0x3' in cf_qemu.sh - config->add_kernel_cmdline( - "androidboot.boot_devices=pci0000:00/0000:00:03.0"); + return { "androidboot.boot_devices=pci0000:00/0000:00:03.0" }; } 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 +137,7 @@ std::vector<cvd::Command> QemuManager::StartCommands(bool /*with_frontend*/) { 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 9b0df497..4b048762 100644 --- a/host/libs/vm_manager/qemu_manager.h +++ b/host/libs/vm_manager/qemu_manager.h @@ -15,6 +15,9 @@ */ #pragma once +#include <string> +#include <vector> + #include "host/libs/vm_manager/vm_manager.h" #include "common/libs/fs/shared_fd.h" @@ -26,13 +29,13 @@ namespace vm_manager { class QemuManager : public VmManager { public: static const std::string name(); - static bool ConfigureGpu(vsoc::CuttlefishConfig* config); - static void ConfigureBootDevices(vsoc::CuttlefishConfig* config); + static std::vector<std::string> ConfigureGpu(const std::string& gpu_mode); + static std::vector<std::string> ConfigureBootDevices(); 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 ac5531a4..2ab41578 100644 --- a/host/libs/vm_manager/vm_manager.cpp +++ b/host/libs/vm_manager/vm_manager.cpp @@ -48,11 +48,11 @@ std::map<std::string, VmManager::VmManagerHelper> return GetManagerSingleton<QemuManager>(config); }, []() { return vsoc::HostSupportsQemuCli(); }, - [](vsoc::CuttlefishConfig* c) { - return QemuManager::ConfigureGpu(c); + [](const std::string& gpu_mode) { + return QemuManager::ConfigureGpu(gpu_mode); }, - [](vsoc::CuttlefishConfig* c) { - return QemuManager::ConfigureBootDevices(c); + []() { + return QemuManager::ConfigureBootDevices(); } }, }, @@ -64,11 +64,11 @@ std::map<std::string, VmManager::VmManagerHelper> }, // Same as Qemu for the time being []() { return vsoc::HostSupportsQemuCli(); }, - [](vsoc::CuttlefishConfig* c) { - return CrosvmManager::ConfigureGpu(c); + [](const std::string& gpu_mode) { + return CrosvmManager::ConfigureGpu(gpu_mode); }, - [](vsoc::CuttlefishConfig* c) { - return CrosvmManager::ConfigureBootDevices(c); + []() { + return CrosvmManager::ConfigureBootDevices(); } } } @@ -92,20 +92,22 @@ bool VmManager::IsVmManagerSupported(const std::string& name) { vm_manager_helpers_[name].support_checker(); } -bool VmManager::ConfigureGpuMode(vsoc::CuttlefishConfig* config) { - auto it = vm_manager_helpers_.find(config->vm_manager()); +std::vector<std::string> VmManager::ConfigureGpuMode( + const std::string& vmm_name, const std::string& gpu_mode) { + auto it = vm_manager_helpers_.find(vmm_name); if (it == vm_manager_helpers_.end()) { - return false; + return {}; } - return it->second.configure_gpu_mode(config); + return it->second.configure_gpu_mode(gpu_mode); } -void VmManager::ConfigureBootDevices(vsoc::CuttlefishConfig* config) { - auto it = vm_manager_helpers_.find(config->vm_manager()); +std::vector<std::string> VmManager::ConfigureBootDevices( + const std::string& vmm_name) { + auto it = vm_manager_helpers_.find(vmm_name); if (it == vm_manager_helpers_.end()) { - return; + return {}; } - it->second.configure_boot_devices(config); + return it->second.configure_boot_devices(); } std::vector<std::string> VmManager::GetValidNames() { @@ -154,4 +156,13 @@ bool VmManager::ValidateHostConfiguration( 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 62adbf8c..a14ad05d 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> @@ -35,18 +36,23 @@ class VmManager { static VmManager* Get(const std::string& vm_manager_name, const vsoc::CuttlefishConfig* config); static bool IsValidName(const std::string& name); - static bool ConfigureGpuMode(vsoc::CuttlefishConfig* config); - static void ConfigureBootDevices(vsoc::CuttlefishConfig* config); + static std::vector<std::string> ConfigureGpuMode( + const std::string& vmm_name, const std::string& gpu_mode); + static std::vector<std::string> ConfigureBootDevices( + const std::string& vmm_name); static bool IsVmManagerSupported(const std::string& name); static std::vector<std::string> GetValidNames(); 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,14 +65,17 @@ class VmManager { const vsoc::CuttlefishConfig* config_; VmManager(const vsoc::CuttlefishConfig* config); + bool frontend_enabled_; + std::string kernel_cmdline_; + private: struct VmManagerHelper { // The singleton implementation std::function<VmManager*(const vsoc::CuttlefishConfig*)> builder; // Whether the host packages support this vm manager std::function<bool()> support_checker; - std::function<bool(vsoc::CuttlefishConfig*)> configure_gpu_mode; - std::function<void(vsoc::CuttlefishConfig*)> configure_boot_devices; + std::function<std::vector<std::string>(const std::string&)> configure_gpu_mode; + std::function<std::vector<std::string>()> configure_boot_devices; }; // Asociates a vm manager helper to every valid vm manager name static std::map<std::string, VmManagerHelper> vm_manager_helpers_; |