1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <android-base/logging.h>
17 #include <android-base/strings.h>
18
19 #include "host/commands/assemble_cvd/flags_defaults.h"
20 #include "host/commands/cvd/parser/cf_configs_common.h"
21 #include "host/commands/cvd/parser/instance/cf_vm_configs.h"
22 #include "host/libs/config/cuttlefish_config.h"
23
24 #define UI_DEFAULTS_MEMORY_MB 2048
25
26 namespace cuttlefish {
27
InitVmManagerConfig(Json::Value & instances)28 void InitVmManagerConfig(Json::Value& instances) {
29 // Allocate and initialize with default values
30 int size = instances.size();
31 for (int i = 0; i < size; i++) {
32 if (instances[i].isMember("vm")) {
33 if (instances[i]["vm"].isMember("crosvm")) {
34 instances[i]["vm"]["vm_manager"] = "crosvm";
35 } else if (instances[i]["vm"].isMember("qemu")) {
36 instances[i]["vm"]["vm_manager"] = "qemu_cli";
37 } else if (instances[i]["vm"].isMember("gem5")) {
38 instances[i]["vm"]["vm_manager"] = "gem5";
39 } else {
40 // Set vm manager to default value (crosvm)
41 instances[i]["vm"]["vm_manager"] = "crosvm";
42 }
43 } else {
44 // vm object doesn't exist , set the default vm manager to crosvm
45 instances[i]["vm"]["vm_manager"] = "crosvm";
46 }
47 }
48 }
49
InitVmConfigs(Json::Value & instances)50 void InitVmConfigs(Json::Value& instances) {
51 InitIntConfig(instances, "vm", "cpus", CF_DEFAULTS_CPUS);
52 InitIntConfig(instances, "vm", "memory_mb", UI_DEFAULTS_MEMORY_MB);
53 InitBoolConfig(instances, "vm", "use_sdcard", CF_DEFAULTS_USE_SDCARD);
54 InitStringConfig(instances, "vm", "setupwizard_mode",
55 CF_DEFAULTS_SETUPWIZARD_MODE);
56 InitStringConfig(instances, "vm", "uuid", CF_DEFAULTS_UUID);
57 InitVmManagerConfig(instances);
58 InitBoolConfigSubGroup(instances, "vm", "crosvm", "enable_sandbox",
59 CF_DEFAULTS_ENABLE_SANDBOX);
60 }
61
GenerateCustomConfigsFlags(const Json::Value & instances)62 std::vector<std::string> GenerateCustomConfigsFlags(
63 const Json::Value& instances) {
64 std::vector<std::string> result;
65 int size = instances.size();
66 for (int i = 0; i < size; i++) {
67 if (instances[i].isMember("vm") &&
68 instances[i]["vm"].isMember("custom_actions")) {
69 Json::StreamWriterBuilder factory;
70 std::string mapped_text =
71 Json::writeString(factory, instances[i]["vm"]["custom_actions"]);
72 // format json string string to match aosp/2374890 input format
73 mapped_text = android::base::StringReplace(mapped_text, "\n", "", true);
74 mapped_text = android::base::StringReplace(mapped_text, "\r", "", true);
75 mapped_text =
76 android::base::StringReplace(mapped_text, "\"", "\\\"", true);
77 std::stringstream buff;
78 buff << "--custom_actions=" << mapped_text;
79 result.emplace_back(buff.str());
80 } else {
81 // custom_actions parameter doesn't exist in the configuration file
82 result.emplace_back("--custom_actions=unset");
83 }
84 }
85 return result;
86 }
87
GenerateVmFlags(const Json::Value & instances)88 std::vector<std::string> GenerateVmFlags(const Json::Value& instances) {
89 std::vector<std::string> result;
90 result.emplace_back(GenerateGflag(instances, "cpus", "vm", "cpus"));
91 result.emplace_back(GenerateGflag(instances, "memory_mb", "vm", "memory_mb"));
92 result.emplace_back(
93 GenerateGflag(instances, "use_sdcard", "vm", "use_sdcard"));
94 result.emplace_back(
95 GenerateGflag(instances, "vm_manager", "vm", "vm_manager"));
96 result.emplace_back(
97 GenerateGflag(instances, "setupwizard_mode", "vm", "setupwizard_mode"));
98 if (!GENERATE_MVP_FLAGS_ONLY) {
99 result.emplace_back(GenerateGflag(instances, "uuid", "vm", "uuid"));
100 }
101 result.emplace_back(GenerateGflagSubGroup(instances, "enable_sandbox", "vm",
102 "crosvm", "enable_sandbox"));
103
104 result = MergeResults(result, GenerateCustomConfigsFlags(instances));
105
106 return result;
107 }
108
109 } // namespace cuttlefish
110