blob: ed50269bf5ee8f96dd43bfae48c33424e20ff2d5 [file] [log] [blame]
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08001#include "host/commands/launch/flags.h"
2
3#include <iostream>
4
5#include <gflags/gflags.h>
6#include <glog/logging.h>
7
Cody Schuffelen90b2fb22019-02-28 18:55:21 -08008#include "common/libs/strings/str_split.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08009#include "common/libs/utils/environment.h"
10#include "common/libs/utils/files.h"
11#include "common/vsoc/lib/vsoc_memory.h"
12#include "host/commands/launch/boot_image_unpacker.h"
13#include "host/commands/launch/data_image.h"
14#include "host/commands/launch/launch.h"
15#include "host/commands/launch/launcher_defs.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080016#include "host/libs/vm_manager/crosvm_manager.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080017#include "host/libs/vm_manager/qemu_manager.h"
18#include "host/libs/vm_manager/vm_manager.h"
19
20using vsoc::GetPerInstanceDefault;
21using cvd::LauncherExitCodes;
22
23DEFINE_string(
24 system_image, "",
25 "Path to the system image, if empty it is assumed to be a file named "
26 "system.img in the directory specified by -system_image_dir");
27DEFINE_string(cache_image, "", "Location of the cache partition image.");
Paul Trautrimba8f8e92019-03-12 17:55:48 +090028DEFINE_string(metadata_image, "", "Location of the metadata partition image "
29 "to be generated.");
30DEFINE_int32(blank_metadata_image_mb, 16,
31 "The size of the blank metadata image to generate, MB.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080032DEFINE_int32(cpus, 2, "Virtual CPU count.");
33DEFINE_string(data_image, "", "Location of the data partition image.");
34DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
35 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
36 "'always_create'.");
37DEFINE_int32(blank_data_image_mb, 0,
38 "The size of the blank data image to generate, MB.");
39DEFINE_string(blank_data_image_fmt, "ext4",
40 "The fs format for the blank data image. Used with mkfs.");
41DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000042 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080043
44DEFINE_int32(x_res, 720, "Width of the screen in pixels");
45DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
46DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
47DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
48DEFINE_int32(num_screen_buffers, 3, "The number of screen buffers");
49DEFINE_string(kernel_path, "",
50 "Path to the kernel. Overrides the one from the boot image");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080051DEFINE_bool(decompress_kernel, false,
52 "Whether to decompress the kernel image. Required for crosvm.");
53DEFINE_string(kernel_decompresser_executable,
54 vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"),
55 "Path to the extract-vmlinux executable.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080056DEFINE_string(extra_kernel_cmdline, "",
57 "Additional flags to put on the kernel command line");
58DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
59DEFINE_string(console, "ttyS0", "Console device for the guest kernel.");
60DEFINE_string(androidboot_console, "ttyS1",
61 "Console device for the Android framework");
Jorge E. Moreiraba626622019-01-28 17:47:50 -080062DEFINE_string(
63 hardware_name, "",
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -080064 "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080065DEFINE_string(guest_security, "selinux",
66 "The security module to use in the guest");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070067DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080068 "Whether to run in enforcing mode (non permissive). Ignored if "
69 "-guest_security is empty.");
70DEFINE_bool(guest_audit_security, true,
71 "Whether to log security audits.");
72DEFINE_string(boot_image, "", "Location of cuttlefish boot image.");
73DEFINE_int32(memory_mb, 2048,
74 "Total amount of memory available for guest, MB.");
75std::string g_default_mempath{vsoc::GetDefaultMempath()};
76DEFINE_string(mempath, g_default_mempath.c_str(),
77 "Target location for the shmem file.");
78DEFINE_string(mobile_interface, "", // default handled on ParseCommandLine
79 "Network interface to use for mobile networking");
80DEFINE_string(mobile_tap_name, "", // default handled on ParseCommandLine
81 "The name of the tap interface to use for mobile");
82std::string g_default_serial_number{GetPerInstanceDefault("CUTTLEFISHCVD")};
83DEFINE_string(serial_number, g_default_serial_number.c_str(),
84 "Serial number to use for the device");
85DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
86 "A directory to put all instance specific files");
87DEFINE_string(
Jorge E. Moreira0d2275d2019-04-29 13:43:11 -070088 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080089 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070090DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070091 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
92 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070093DEFINE_string(wayland_socket, "",
Greg Hartmanbc1bed42019-04-05 20:02:00 -070094 "Location of the wayland socket to use for drm_virgl gpu_mode.");
Greg Hartmana3c552d2019-03-28 18:20:48 -070095
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080096DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
97 "Location of the system partition images.");
98DEFINE_string(vendor_image, "", "Location of the vendor partition image.");
Alistair Strachan5782ce42019-03-28 22:05:13 -070099DEFINE_string(product_image, "", "Location of the product partition image.");
Alistair Strachan050ca932018-11-27 12:41:19 -0800100DEFINE_string(super_image, "", "Location of the super partition image.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800101
102DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
103 " host kernel. This is only used during transition of our clients."
104 " Will be deprecated soon.");
105DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process.");
106DEFINE_string(vnc_server_binary,
107 vsoc::DefaultHostArtifactsPath("bin/vnc_server"),
108 "Location of the vnc server binary.");
Alistair Strachan7eeddf42019-03-04 18:13:34 -0800109DEFINE_bool(start_stream_audio, false,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800110 "Whether to start the stream audio process.");
111DEFINE_string(stream_audio_binary,
112 vsoc::DefaultHostArtifactsPath("bin/stream_audio"),
113 "Location of the stream_audio binary.");
114DEFINE_string(virtual_usb_manager_binary,
115 vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager"),
116 "Location of the virtual usb manager binary.");
117DEFINE_string(kernel_log_monitor_binary,
118 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"),
119 "Location of the log monitor binary.");
120DEFINE_string(ivserver_binary,
121 vsoc::DefaultHostArtifactsPath("bin/ivserver"),
122 "Location of the ivshmem server binary.");
123DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444),
124 "The port on which the vnc server should listen");
125DEFINE_int32(stream_audio_port, GetPerInstanceDefault(7444),
126 "The port on which stream_audio should listen.");
127DEFINE_string(socket_forward_proxy_binary,
128 vsoc::DefaultHostArtifactsPath("bin/socket_forward_proxy"),
129 "Location of the socket_forward_proxy binary.");
130DEFINE_string(socket_vsock_proxy_binary,
131 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"),
132 "Location of the socket_vsock_proxy binary.");
Cody Schuffelen10743332019-04-15 16:50:49 -0700133DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen63d10052019-02-26 12:21:53 -0800134 "Mode for ADB connection. Can be 'usb' for USB forwarding, "
135 "'tunnel' for a TCP connection tunneled through VSoC, "
136 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
137 "'native_vsock' for a direct connection to the guest ADB over "
138 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
139 "the guest ADB server, or a comma separated list of types as in "
140 "'usb,tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800141DEFINE_bool(run_adb_connector, true,
142 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000143 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800144DEFINE_string(adb_connector_binary,
145 vsoc::DefaultHostArtifactsPath("bin/adb_connector"),
146 "Location of the adb_connector binary. Only relevant if "
Matthias Maennich454d7872019-02-06 16:35:17 +0000147 "-run_adb_connector is true");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800148DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb");
149DEFINE_string(guest_mac_address,
150 GetPerInstanceDefault("00:43:56:44:80:"), // 00:43:56:44:80:0x
151 "MAC address of the wifi interface to be created on the guest.");
152DEFINE_string(host_mac_address,
153 "42:00:00:00:00:00",
154 "MAC address of the wifi interface running on the host.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800155DEFINE_string(wifi_tap_name, "", // default handled on ParseCommandLine
156 "The name of the tap interface to use for wifi");
157DEFINE_int32(vsock_guest_cid,
158 vsoc::GetDefaultPerInstanceVsockCid(),
159 "Guest identifier for vsock. Disabled if under 3.");
160
161// TODO(b/72969289) This should be generated
162DEFINE_string(dtb, "", "Path to the cuttlefish.dtb file");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800163DEFINE_string(gsi_fstab,
164 vsoc::DefaultHostArtifactsPath("config/gsi.fstab"),
165 "Path to the GSI fstab file");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800166
167DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
168 "UUID to use for the device. Random if not specified");
169DEFINE_bool(daemon, false,
170 "Run cuttlefish in background, the launcher exits on boot "
171 "completed/failed");
172
173DEFINE_string(device_title, "", "Human readable name for the instance, "
174 "used by the vnc_server for its server title");
175DEFINE_string(setupwizard_mode, "DISABLED",
176 "One of DISABLED,OPTIONAL,REQUIRED");
177
178DEFINE_string(qemu_binary,
179 "/usr/bin/qemu-system-x86_64",
180 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800181DEFINE_string(crosvm_binary,
182 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
183 "The Crosvm binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800184DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
185DEFINE_bool(run_e2e_test, true, "Run e2e test after device launches");
186DEFINE_string(e2e_test_binary,
187 vsoc::DefaultHostArtifactsPath("bin/host_region_e2e_test"),
188 "Location of the region end to end test binary");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800189DEFINE_string(logcat_receiver_binary,
190 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"),
191 "Binary for the logcat server");
192DEFINE_string(logcat_mode, "", "How to send android's log messages from "
193 "guest to host. One of [serial, vsock]");
194DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
195 "The port for logcat over vsock");
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700196DEFINE_string(config_server_binary,
197 vsoc::DefaultHostArtifactsPath("bin/config_server"),
198 "Binary for the configuration server");
199DEFINE_int32(config_server_port, vsoc::GetPerInstanceDefault(4680),
200 "The (vsock) port for the configuration server");
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800201DEFINE_int32(frames_vsock_port, vsoc::GetPerInstanceDefault(5580),
202 "The vsock port to receive frames from the guest on");
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700203DEFINE_bool(enable_tombstone_receiver, false, "Enables the tombstone logger on "
204 "both the guest and the host");
205DEFINE_string(tombstone_receiver_binary,
206 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"),
207 "Binary for the tombstone server");
208DEFINE_int32(tombstone_receiver_port, vsoc::GetPerInstanceDefault(5630),
209 "The vsock port for tombstones");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800210namespace {
211
212template<typename S, typename T>
213static std::string concat(const S& s, const T& t) {
214 std::ostringstream os;
215 os << s << t;
216 return os.str();
217}
218
219bool ResolveInstanceFiles() {
220 if (FLAGS_system_image_dir.empty()) {
221 LOG(ERROR) << "--system_image_dir must be specified.";
222 return false;
223 }
224
225 // If user did not specify location of either of these files, expect them to
226 // be placed in --system_image_dir location.
227 std::string default_system_image = FLAGS_system_image_dir + "/system.img";
228 SetCommandLineOptionWithMode("system_image", default_system_image.c_str(),
229 google::FlagSettingMode::SET_FLAGS_DEFAULT);
230 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
231 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
232 google::FlagSettingMode::SET_FLAGS_DEFAULT);
233 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
234 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
235 google::FlagSettingMode::SET_FLAGS_DEFAULT);
236 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
237 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
238 google::FlagSettingMode::SET_FLAGS_DEFAULT);
239 std::string default_vendor_image = FLAGS_system_image_dir + "/vendor.img";
240 SetCommandLineOptionWithMode("vendor_image", default_vendor_image.c_str(),
241 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900242 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
243 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
244 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan5782ce42019-03-28 22:05:13 -0700245 std::string default_product_image = FLAGS_system_image_dir + "/product.img";
246 SetCommandLineOptionWithMode("product_image", default_product_image.c_str(),
247 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800248 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
249 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
250 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800251
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800252 return true;
253}
254
255std::string GetCuttlefishEnvPath() {
256 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
257}
258
259// Initializes the config object and saves it to file. It doesn't return it, all
260// further uses of the config should happen through the singleton
261bool InitializeCuttlefishConfiguration(
262 const cvd::BootImageUnpacker& boot_image_unpacker) {
263 vsoc::CuttlefishConfig tmp_config_obj;
264 auto& memory_layout = *vsoc::VSoCMemoryLayout::Get();
265 // Set this first so that calls to PerInstancePath below are correct
266 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
267 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
268 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
269 return false;
270 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700271 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
272 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
273 return false;
274 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800275 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700276 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
277 if (!vm_manager::VmManager::ConfigureGpuMode(&tmp_config_obj)) {
278 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
279 " does not work with vm_manager=" << FLAGS_vm_manager;
280 return false;
281 }
282 tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800283
Alistair Strachan050ca932018-11-27 12:41:19 -0800284 vm_manager::VmManager::ConfigureBootDevices(&tmp_config_obj);
285
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800286 tmp_config_obj.set_serial_number(FLAGS_serial_number);
287
288 tmp_config_obj.set_cpus(FLAGS_cpus);
289 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
290
291 tmp_config_obj.set_dpi(FLAGS_dpi);
292 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
293 tmp_config_obj.set_x_res(FLAGS_x_res);
294 tmp_config_obj.set_y_res(FLAGS_y_res);
295 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
296 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
297 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800298 std::vector<std::string> adb = cvd::StrSplit(FLAGS_adb_mode, ',');
299 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800300 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
301
302 tmp_config_obj.set_device_title(FLAGS_device_title);
303 if (FLAGS_kernel_path.size()) {
304 tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
305 tmp_config_obj.set_use_unpacked_kernel(false);
306 } else {
307 tmp_config_obj.set_kernel_image_path(
308 tmp_config_obj.PerInstancePath("kernel"));
309 tmp_config_obj.set_use_unpacked_kernel(true);
310 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800311 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
312 if (FLAGS_decompress_kernel) {
313 tmp_config_obj.set_decompressed_kernel_image_path(
314 tmp_config_obj.PerInstancePath("vmlinux"));
315 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800316
317 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
318 bool use_ramdisk = boot_image_unpacker.HasRamdiskImage();
319 if (!use_ramdisk) {
320 LOG(INFO) << "No ramdisk present; assuming system-as-root build";
321 ramdisk_path = "";
322 }
323
Alistair Strachan050ca932018-11-27 12:41:19 -0800324 // Fallback for older builds, or builds from branches without DAP
325 if (!FLAGS_super_image.empty() && !cvd::FileHasContent(FLAGS_super_image.c_str())) {
326 LOG(INFO) << "No super image detected; assuming non-DAP build";
327 FLAGS_super_image.clear();
328 }
329
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800330 // This needs to be done here because the dtb path depends on the presence of
Alistair Strachan050ca932018-11-27 12:41:19 -0800331 // the ramdisk. If we are booting a super image, the fstab is passed through
332 // from the ramdisk, it should never be defined by dt.
333 if (FLAGS_super_image.empty() && FLAGS_dtb.empty()) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800334 if (use_ramdisk) {
335 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/initrd-root.dtb");
336 } else {
337 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/system-root.dtb");
338 }
339 }
340
341 tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
342 if (!use_ramdisk) {
343 tmp_config_obj.add_kernel_cmdline("root=/dev/vda");
344 }
Alistair Strachan050ca932018-11-27 12:41:19 -0800345 if (!FLAGS_super_image.empty()) {
346 tmp_config_obj.add_kernel_cmdline("androidboot.super_partition=vda");
347 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800348 tmp_config_obj.add_kernel_cmdline("init=/init");
349 tmp_config_obj.add_kernel_cmdline(
350 concat("androidboot.serialno=", FLAGS_serial_number));
351 tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0");
352 tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi));
353 tmp_config_obj.add_kernel_cmdline(
354 concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode));
355 tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part));
356 if (!FLAGS_console.empty()) {
357 tmp_config_obj.add_kernel_cmdline(concat("console=", FLAGS_console));
358 }
359 if (!FLAGS_androidboot_console.empty()) {
360 tmp_config_obj.add_kernel_cmdline(
361 concat("androidboot.console=", FLAGS_androidboot_console));
362 }
363 if (!FLAGS_hardware_name.empty()) {
364 tmp_config_obj.add_kernel_cmdline(
365 concat("androidboot.hardware=", FLAGS_hardware_name));
366 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800367 if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) {
368 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=",
369 FLAGS_logcat_vsock_port));
370 }
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700371 tmp_config_obj.add_kernel_cmdline(concat("androidboot.cuttlefish_config_server_port=",
372 FLAGS_config_server_port));
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800373 tmp_config_obj.set_hardware_name(FLAGS_hardware_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800374 if (!FLAGS_guest_security.empty()) {
375 tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security));
376 if (FLAGS_guest_enforce_security) {
377 tmp_config_obj.add_kernel_cmdline("enforcing=1");
378 } else {
379 tmp_config_obj.add_kernel_cmdline("enforcing=0");
380 tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive");
381 }
382 if (FLAGS_guest_audit_security) {
383 tmp_config_obj.add_kernel_cmdline("audit=1");
384 } else {
385 tmp_config_obj.add_kernel_cmdline("audit=0");
386 }
387 }
388 if (FLAGS_run_e2e_test) {
389 tmp_config_obj.add_kernel_cmdline("androidboot.vsoc_e2e_test=1");
390 }
391 if (FLAGS_extra_kernel_cmdline.size()) {
392 tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline);
393 }
394
Alistair Strachan050ca932018-11-27 12:41:19 -0800395 if (FLAGS_super_image.empty()) {
396 tmp_config_obj.set_system_image_path(FLAGS_system_image);
397 tmp_config_obj.set_vendor_image_path(FLAGS_vendor_image);
398 tmp_config_obj.set_product_image_path(FLAGS_product_image);
399 tmp_config_obj.set_super_image_path("");
400 tmp_config_obj.set_dtb_path(FLAGS_dtb);
401 tmp_config_obj.set_gsi_fstab_path(FLAGS_gsi_fstab);
402 } else {
403 tmp_config_obj.set_system_image_path("");
404 tmp_config_obj.set_vendor_image_path("");
405 tmp_config_obj.set_product_image_path("");
406 tmp_config_obj.set_super_image_path(FLAGS_super_image);
407 tmp_config_obj.set_dtb_path("");
408 tmp_config_obj.set_gsi_fstab_path("");
409 }
410
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800411 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800412 tmp_config_obj.set_cache_image_path(FLAGS_cache_image);
413 tmp_config_obj.set_data_image_path(FLAGS_data_image);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900414 tmp_config_obj.set_metadata_image_path(FLAGS_metadata_image);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800415
416 tmp_config_obj.set_mempath(FLAGS_mempath);
417 tmp_config_obj.set_ivshmem_qemu_socket_path(
418 tmp_config_obj.PerInstancePath("ivshmem_socket_qemu"));
419 tmp_config_obj.set_ivshmem_client_socket_path(
420 tmp_config_obj.PerInstancePath("ivshmem_socket_client"));
421 tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size());
422
423 if (AdbUsbEnabled(tmp_config_obj)) {
424 tmp_config_obj.set_usb_v1_socket_name(tmp_config_obj.PerInstancePath("usb-v1"));
425 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
426 tmp_config_obj.set_usb_ip_socket_name(tmp_config_obj.PerInstancePath("usb-ip"));
427 }
428
429 tmp_config_obj.set_kernel_log_socket_name(tmp_config_obj.PerInstancePath("kernel-log"));
430 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
431 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
432 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800433 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700434 tmp_config_obj.set_config_server_binary(FLAGS_config_server_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800435 tmp_config_obj.set_launcher_log_path(tmp_config_obj.PerInstancePath("launcher.log"));
436 tmp_config_obj.set_launcher_monitor_socket_path(
437 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
438
439 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
440 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
441
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800442 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
443
444 tmp_config_obj.set_wifi_guest_mac_addr(FLAGS_guest_mac_address);
445 tmp_config_obj.set_wifi_host_mac_addr(FLAGS_host_mac_address);
446
447 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
448
449 tmp_config_obj.set_entropy_source("/dev/urandom");
450 tmp_config_obj.set_uuid(FLAGS_uuid);
451
452 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800453 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800454 tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
455 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
456
457 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
458 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
459 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
460
461 tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio);
462 tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary);
463 tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port);
464
465 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
466 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
467 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
468 tmp_config_obj.set_virtual_usb_manager_binary(
469 FLAGS_virtual_usb_manager_binary);
470 tmp_config_obj.set_socket_forward_proxy_binary(
471 FLAGS_socket_forward_proxy_binary);
472 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
473 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
474 tmp_config_obj.set_run_e2e_test(FLAGS_run_e2e_test);
475 tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary);
476
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800477 tmp_config_obj.set_data_policy(FLAGS_data_policy);
478 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
479 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
480
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800481 if(!AdbUsbEnabled(tmp_config_obj)) {
482 tmp_config_obj.disable_usb_adb();
483 }
484
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800485 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
486 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700487 tmp_config_obj.set_config_server_port(FLAGS_config_server_port);
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800488 tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port);
489 if (!tmp_config_obj.enable_ivserver()) {
490 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_frames_port=",
491 FLAGS_frames_vsock_port));
492 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800493
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700494 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
495 tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port);
496 tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary);
497 if (FLAGS_enable_tombstone_receiver) {
498 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=1");
499 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_tombstone_port="
500 ,FLAGS_tombstone_receiver_port));
Alistair Strachan050ca932018-11-27 12:41:19 -0800501 // TODO (b/128842613) populate a cid flag to read the host CID during
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700502 // runtime
503 } else {
504 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=0");
505 }
506
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800507 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
508
509 auto config_file = GetConfigFilePath(tmp_config_obj);
510 auto config_link = vsoc::GetGlobalConfigFileLink();
511 // Save the config object before starting any host process
512 if (!tmp_config_obj.SaveToFile(config_file)) {
513 LOG(ERROR) << "Unable to save config object";
514 return false;
515 }
516 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
517 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
518 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
519 << ": " << strerror(errno);
520 return false;
521 }
522
523 return true;
524}
525
526void SetDefaultFlagsForQemu() {
527 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
528 SetCommandLineOptionWithMode("mobile_interface",
529 default_mobile_interface.c_str(),
530 google::FlagSettingMode::SET_FLAGS_DEFAULT);
531 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
532 SetCommandLineOptionWithMode("mobile_tap_name",
533 default_mobile_tap_name.c_str(),
534 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800535 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
536 SetCommandLineOptionWithMode("wifi_tap_name",
537 default_wifi_tap_name.c_str(),
538 google::FlagSettingMode::SET_FLAGS_DEFAULT);
539 auto default_instance_dir =
540 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
541 SetCommandLineOptionWithMode("instance_dir",
542 default_instance_dir.c_str(),
543 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800544 SetCommandLineOptionWithMode("hardware_name", "cutf_ivsh",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800545 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800546 SetCommandLineOptionWithMode("decompress_kernel", "false",
547 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800548 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatSerialMode,
549 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800550}
551
552void SetDefaultFlagsForCrosvm() {
553 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
554 SetCommandLineOptionWithMode("mobile_interface",
555 default_mobile_interface.c_str(),
556 google::FlagSettingMode::SET_FLAGS_DEFAULT);
557 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
558 SetCommandLineOptionWithMode("mobile_tap_name",
559 default_mobile_tap_name.c_str(),
560 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800561 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
562 SetCommandLineOptionWithMode("wifi_tap_name",
563 default_wifi_tap_name.c_str(),
564 google::FlagSettingMode::SET_FLAGS_DEFAULT);
565 auto default_instance_dir =
566 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
567 SetCommandLineOptionWithMode("instance_dir",
568 default_instance_dir.c_str(),
569 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700570 auto default_wayland_socket = vsoc::DefaultEnvironmentPath(
571 "XDG_RUNTIME_DIR", default_instance_dir.c_str(), "wayland-0");
572 SetCommandLineOptionWithMode("wayland_socket",
573 default_wayland_socket.c_str(),
574 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800575 SetCommandLineOptionWithMode("hardware_name", "cutf_cvm",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800576 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800577 SetCommandLineOptionWithMode("decompress_kernel", "true",
578 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira84d93c12019-02-05 12:02:29 -0800579 SetCommandLineOptionWithMode("run_e2e_test", "false",
580 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800581 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
582 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800583}
584
585bool ParseCommandLineFlags(int* argc, char*** argv) {
586 // The config_file is created by the launcher, so the launcher is the only
587 // host process that doesn't use the flag.
588 // Set the default to empty.
589 google::SetCommandLineOptionWithMode("config_file", "",
590 gflags::SET_FLAGS_DEFAULT);
591 google::ParseCommandLineNonHelpFlags(argc, argv, true);
592 bool invalid_manager = false;
593 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
594 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800595 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
596 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800597 } else {
598 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
599 << std::endl;
600 invalid_manager = true;
601 }
602 google::HandleCommandLineHelpFlags();
603 if (invalid_manager) {
604 return false;
605 }
606 // Set the env variable to empty (in case the caller passed a value for it).
607 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
608
609 return ResolveInstanceFiles();
610}
611
612bool CleanPriorFiles() {
613 // Everything on the instance directory
614 std::string prior_files = FLAGS_instance_dir + "/*";
615 // The shared memory file
616 prior_files += " " + FLAGS_mempath;
617 // The environment file
618 prior_files += " " + GetCuttlefishEnvPath();
619 // The global link to the config file
620 prior_files += " " + vsoc::GetGlobalConfigFileLink();
621 LOG(INFO) << "Assuming prior files of " << prior_files;
622 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
623 int rval = std::system(fuser_cmd.c_str());
624 // fuser returns 0 if any of the files are open
625 if (WEXITSTATUS(rval) == 0) {
626 LOG(ERROR) << "Clean aborted: files are in use";
627 return false;
628 }
629 std::string clean_command = "rm -rf " + prior_files;
630 rval = std::system(clean_command.c_str());
631 if (WEXITSTATUS(rval) != 0) {
632 LOG(ERROR) << "Remove of files failed";
633 return false;
634 }
635 return true;
636}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800637
638bool DecompressKernel(const std::string& src, const std::string& dst) {
639 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
640 decomp_cmd.AddParameter(src);
641 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
642 if (!output_file->IsOpen()) {
643 LOG(ERROR) << "Unable to create decompressed image file: "
644 << output_file->StrError();
645 return false;
646 }
647 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
648 auto decomp_proc = decomp_cmd.Start(false);
649 return decomp_proc.Started() && decomp_proc.Wait() == 0;
650}
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800651} // namespace
652
653vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(int* argc, char*** argv) {
654 if (!ParseCommandLineFlags(argc, argv)) {
655 LOG(ERROR) << "Failed to parse command arguments";
656 exit(LauncherExitCodes::kArgumentParsingError);
657 }
658
659 // Clean up prior files before saving the config file (doing it after would
660 // delete it)
661 if (!CleanPriorFiles()) {
662 LOG(ERROR) << "Failed to clean prior files";
663 exit(LauncherExitCodes::kPrioFilesCleanupError);
664 }
665 // Create instance directory if it doesn't exist.
666 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
667 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
668 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
669 LOG(ERROR) << "Failed to create instance directory: "
670 << FLAGS_instance_dir << ". Error: " << errno;
671 exit(LauncherExitCodes::kInstanceDirCreationError);
672 }
673 }
674
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800675 if (!cvd::FileHasContent(FLAGS_boot_image)) {
676 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
677 exit(cvd::kCuttlefishConfigurationInitError);
678 }
679
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800680 auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image);
681
682 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) {
683 LOG(ERROR) << "Failed to initialize configuration";
684 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
685 }
686 // Do this early so that the config object is ready for anything that needs it
687 auto config = vsoc::CuttlefishConfig::Get();
688 if (!config) {
689 LOG(ERROR) << "Failed to obtain config singleton";
690 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
691 }
692
693 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
694 config->use_unpacked_kernel()
695 ? config->kernel_image_path()
696 : "")) {
697 LOG(ERROR) << "Failed to unpack boot image";
698 exit(LauncherExitCodes::kBootImageUnpackError);
699 }
700
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800701 if (config->decompress_kernel()) {
702 if (!DecompressKernel(config->kernel_image_path(),
703 config->decompressed_kernel_image_path())) {
704 LOG(ERROR) << "Failed to decompress kernel";
705 exit(LauncherExitCodes::kKernelDecompressError);
706 }
707 }
708
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800709 ValidateAdbModeFlag(*config);
710
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800711 // Create data if necessary
712 if (!ApplyDataImagePolicy(*config)) {
713 exit(cvd::kCuttlefishConfigurationInitError);
714 }
715
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900716 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
717
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800718 // Check that the files exist
719 for (const auto& file :
Alistair Strachan050ca932018-11-27 12:41:19 -0800720 {config->system_image_path(), config->cache_image_path(),
721 config->data_image_path(), config->vendor_image_path(),
722 config->metadata_image_path(), config->product_image_path(),
723 config->super_image_path()}) {
724 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800725 LOG(ERROR) << "File not found: " << file;
726 exit(cvd::kCuttlefishConfigurationInitError);
727 }
728 }
729
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800730 return config;
731}
732
733std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
734 return config.PerInstancePath("cuttlefish_config.json");
735}