blob: 94149f85495cf86a6dfa10b82240d6b01e12d6c1 [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
8#include "common/libs/utils/environment.h"
9#include "common/libs/utils/files.h"
10#include "common/vsoc/lib/vsoc_memory.h"
11#include "host/commands/launch/boot_image_unpacker.h"
12#include "host/commands/launch/data_image.h"
13#include "host/commands/launch/launch.h"
14#include "host/commands/launch/launcher_defs.h"
Jorge E. Moreiraba673b72019-02-07 14:03:45 -080015#include "host/commands/launch/ril_config.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.");
28DEFINE_int32(cpus, 2, "Virtual CPU count.");
29DEFINE_string(data_image, "", "Location of the data partition image.");
30DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
31 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
32 "'always_create'.");
33DEFINE_int32(blank_data_image_mb, 0,
34 "The size of the blank data image to generate, MB.");
35DEFINE_string(blank_data_image_fmt, "ext4",
36 "The fs format for the blank data image. Used with mkfs.");
37DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000038 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080039
40DEFINE_int32(x_res, 720, "Width of the screen in pixels");
41DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
42DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
43DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
44DEFINE_int32(num_screen_buffers, 3, "The number of screen buffers");
45DEFINE_string(kernel_path, "",
46 "Path to the kernel. Overrides the one from the boot image");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080047DEFINE_bool(decompress_kernel, false,
48 "Whether to decompress the kernel image. Required for crosvm.");
49DEFINE_string(kernel_decompresser_executable,
50 vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"),
51 "Path to the extract-vmlinux executable.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080052DEFINE_string(extra_kernel_cmdline, "",
53 "Additional flags to put on the kernel command line");
54DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
55DEFINE_string(console, "ttyS0", "Console device for the guest kernel.");
56DEFINE_string(androidboot_console, "ttyS1",
57 "Console device for the Android framework");
Jorge E. Moreiraba626622019-01-28 17:47:50 -080058DEFINE_string(
59 hardware_name, "",
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -080060 "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080061DEFINE_string(guest_security, "selinux",
62 "The security module to use in the guest");
63DEFINE_bool(guest_enforce_security, false,
64 "Whether to run in enforcing mode (non permissive). Ignored if "
65 "-guest_security is empty.");
66DEFINE_bool(guest_audit_security, true,
67 "Whether to log security audits.");
68DEFINE_string(boot_image, "", "Location of cuttlefish boot image.");
69DEFINE_int32(memory_mb, 2048,
70 "Total amount of memory available for guest, MB.");
71std::string g_default_mempath{vsoc::GetDefaultMempath()};
72DEFINE_string(mempath, g_default_mempath.c_str(),
73 "Target location for the shmem file.");
74DEFINE_string(mobile_interface, "", // default handled on ParseCommandLine
75 "Network interface to use for mobile networking");
76DEFINE_string(mobile_tap_name, "", // default handled on ParseCommandLine
77 "The name of the tap interface to use for mobile");
78std::string g_default_serial_number{GetPerInstanceDefault("CUTTLEFISHCVD")};
79DEFINE_string(serial_number, g_default_serial_number.c_str(),
80 "Serial number to use for the device");
81DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
82 "A directory to put all instance specific files");
83DEFINE_string(
84 vm_manager, vm_manager::QemuManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080085 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080086DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
87 "Location of the system partition images.");
88DEFINE_string(vendor_image, "", "Location of the vendor partition image.");
89
90DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
91 " host kernel. This is only used during transition of our clients."
92 " Will be deprecated soon.");
93DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process.");
94DEFINE_string(vnc_server_binary,
95 vsoc::DefaultHostArtifactsPath("bin/vnc_server"),
96 "Location of the vnc server binary.");
Alistair Strachan7eeddf42019-03-04 18:13:34 -080097DEFINE_bool(start_stream_audio, false,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080098 "Whether to start the stream audio process.");
99DEFINE_string(stream_audio_binary,
100 vsoc::DefaultHostArtifactsPath("bin/stream_audio"),
101 "Location of the stream_audio binary.");
102DEFINE_string(virtual_usb_manager_binary,
103 vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager"),
104 "Location of the virtual usb manager binary.");
105DEFINE_string(kernel_log_monitor_binary,
106 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"),
107 "Location of the log monitor binary.");
108DEFINE_string(ivserver_binary,
109 vsoc::DefaultHostArtifactsPath("bin/ivserver"),
110 "Location of the ivshmem server binary.");
111DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444),
112 "The port on which the vnc server should listen");
113DEFINE_int32(stream_audio_port, GetPerInstanceDefault(7444),
114 "The port on which stream_audio should listen.");
115DEFINE_string(socket_forward_proxy_binary,
116 vsoc::DefaultHostArtifactsPath("bin/socket_forward_proxy"),
117 "Location of the socket_forward_proxy binary.");
118DEFINE_string(socket_vsock_proxy_binary,
119 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"),
120 "Location of the socket_vsock_proxy binary.");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800121DEFINE_string(adb_mode, "",
Cody Schuffelen63d10052019-02-26 12:21:53 -0800122 "Mode for ADB connection. Can be 'usb' for USB forwarding, "
123 "'tunnel' for a TCP connection tunneled through VSoC, "
124 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
125 "'native_vsock' for a direct connection to the guest ADB over "
126 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
127 "the guest ADB server, or a comma separated list of types as in "
128 "'usb,tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800129DEFINE_bool(run_adb_connector, true,
130 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000131 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800132DEFINE_string(adb_connector_binary,
133 vsoc::DefaultHostArtifactsPath("bin/adb_connector"),
134 "Location of the adb_connector binary. Only relevant if "
Matthias Maennich454d7872019-02-06 16:35:17 +0000135 "-run_adb_connector is true");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800136DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb");
137DEFINE_string(guest_mac_address,
138 GetPerInstanceDefault("00:43:56:44:80:"), // 00:43:56:44:80:0x
139 "MAC address of the wifi interface to be created on the guest.");
140DEFINE_string(host_mac_address,
141 "42:00:00:00:00:00",
142 "MAC address of the wifi interface running on the host.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800143DEFINE_string(wifi_tap_name, "", // default handled on ParseCommandLine
144 "The name of the tap interface to use for wifi");
145DEFINE_int32(vsock_guest_cid,
146 vsoc::GetDefaultPerInstanceVsockCid(),
147 "Guest identifier for vsock. Disabled if under 3.");
148
149// TODO(b/72969289) This should be generated
150DEFINE_string(dtb, "", "Path to the cuttlefish.dtb file");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800151DEFINE_string(gsi_fstab,
152 vsoc::DefaultHostArtifactsPath("config/gsi.fstab"),
153 "Path to the GSI fstab file");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800154
155DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
156 "UUID to use for the device. Random if not specified");
157DEFINE_bool(daemon, false,
158 "Run cuttlefish in background, the launcher exits on boot "
159 "completed/failed");
160
161DEFINE_string(device_title, "", "Human readable name for the instance, "
162 "used by the vnc_server for its server title");
163DEFINE_string(setupwizard_mode, "DISABLED",
164 "One of DISABLED,OPTIONAL,REQUIRED");
165
166DEFINE_string(qemu_binary,
167 "/usr/bin/qemu-system-x86_64",
168 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800169DEFINE_string(crosvm_binary,
170 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
171 "The Crosvm binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800172DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
173DEFINE_bool(run_e2e_test, true, "Run e2e test after device launches");
174DEFINE_string(e2e_test_binary,
175 vsoc::DefaultHostArtifactsPath("bin/host_region_e2e_test"),
176 "Location of the region end to end test binary");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800177DEFINE_string(logcat_receiver_binary,
178 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"),
179 "Binary for the logcat server");
180DEFINE_string(logcat_mode, "", "How to send android's log messages from "
181 "guest to host. One of [serial, vsock]");
182DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
183 "The port for logcat over vsock");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800184namespace {
185
186template<typename S, typename T>
187static std::string concat(const S& s, const T& t) {
188 std::ostringstream os;
189 os << s << t;
190 return os.str();
191}
192
193bool ResolveInstanceFiles() {
194 if (FLAGS_system_image_dir.empty()) {
195 LOG(ERROR) << "--system_image_dir must be specified.";
196 return false;
197 }
198
199 // If user did not specify location of either of these files, expect them to
200 // be placed in --system_image_dir location.
201 std::string default_system_image = FLAGS_system_image_dir + "/system.img";
202 SetCommandLineOptionWithMode("system_image", default_system_image.c_str(),
203 google::FlagSettingMode::SET_FLAGS_DEFAULT);
204 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
205 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
206 google::FlagSettingMode::SET_FLAGS_DEFAULT);
207 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
208 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
209 google::FlagSettingMode::SET_FLAGS_DEFAULT);
210 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
211 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
212 google::FlagSettingMode::SET_FLAGS_DEFAULT);
213 std::string default_vendor_image = FLAGS_system_image_dir + "/vendor.img";
214 SetCommandLineOptionWithMode("vendor_image", default_vendor_image.c_str(),
215 google::FlagSettingMode::SET_FLAGS_DEFAULT);
216
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800217 return true;
218}
219
220std::string GetCuttlefishEnvPath() {
221 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
222}
223
224// Initializes the config object and saves it to file. It doesn't return it, all
225// further uses of the config should happen through the singleton
226bool InitializeCuttlefishConfiguration(
227 const cvd::BootImageUnpacker& boot_image_unpacker) {
228 vsoc::CuttlefishConfig tmp_config_obj;
229 auto& memory_layout = *vsoc::VSoCMemoryLayout::Get();
230 // Set this first so that calls to PerInstancePath below are correct
231 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
232 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
233 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
234 return false;
235 }
236 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
237
238 tmp_config_obj.set_serial_number(FLAGS_serial_number);
239
240 tmp_config_obj.set_cpus(FLAGS_cpus);
241 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
242
243 tmp_config_obj.set_dpi(FLAGS_dpi);
244 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
245 tmp_config_obj.set_x_res(FLAGS_x_res);
246 tmp_config_obj.set_y_res(FLAGS_y_res);
247 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
248 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
249 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
250 tmp_config_obj.set_adb_mode(FLAGS_adb_mode);
251 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
252
253 tmp_config_obj.set_device_title(FLAGS_device_title);
254 if (FLAGS_kernel_path.size()) {
255 tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
256 tmp_config_obj.set_use_unpacked_kernel(false);
257 } else {
258 tmp_config_obj.set_kernel_image_path(
259 tmp_config_obj.PerInstancePath("kernel"));
260 tmp_config_obj.set_use_unpacked_kernel(true);
261 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800262 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
263 if (FLAGS_decompress_kernel) {
264 tmp_config_obj.set_decompressed_kernel_image_path(
265 tmp_config_obj.PerInstancePath("vmlinux"));
266 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800267
268 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
269 bool use_ramdisk = boot_image_unpacker.HasRamdiskImage();
270 if (!use_ramdisk) {
271 LOG(INFO) << "No ramdisk present; assuming system-as-root build";
272 ramdisk_path = "";
273 }
274
275 // This needs to be done here because the dtb path depends on the presence of
276 // the ramdisk
277 if (FLAGS_dtb.empty()) {
278 if (use_ramdisk) {
279 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/initrd-root.dtb");
280 } else {
281 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/system-root.dtb");
282 }
283 }
284
285 tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
286 if (!use_ramdisk) {
287 tmp_config_obj.add_kernel_cmdline("root=/dev/vda");
288 }
289 tmp_config_obj.add_kernel_cmdline("init=/init");
290 tmp_config_obj.add_kernel_cmdline(
291 concat("androidboot.serialno=", FLAGS_serial_number));
292 tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0");
293 tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi));
294 tmp_config_obj.add_kernel_cmdline(
295 concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode));
296 tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part));
297 if (!FLAGS_console.empty()) {
298 tmp_config_obj.add_kernel_cmdline(concat("console=", FLAGS_console));
299 }
300 if (!FLAGS_androidboot_console.empty()) {
301 tmp_config_obj.add_kernel_cmdline(
302 concat("androidboot.console=", FLAGS_androidboot_console));
303 }
304 if (!FLAGS_hardware_name.empty()) {
305 tmp_config_obj.add_kernel_cmdline(
306 concat("androidboot.hardware=", FLAGS_hardware_name));
307 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800308 if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) {
309 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=",
310 FLAGS_logcat_vsock_port));
311 }
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800312 tmp_config_obj.set_hardware_name(FLAGS_hardware_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800313 if (!FLAGS_guest_security.empty()) {
314 tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security));
315 if (FLAGS_guest_enforce_security) {
316 tmp_config_obj.add_kernel_cmdline("enforcing=1");
317 } else {
318 tmp_config_obj.add_kernel_cmdline("enforcing=0");
319 tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive");
320 }
321 if (FLAGS_guest_audit_security) {
322 tmp_config_obj.add_kernel_cmdline("audit=1");
323 } else {
324 tmp_config_obj.add_kernel_cmdline("audit=0");
325 }
326 }
327 if (FLAGS_run_e2e_test) {
328 tmp_config_obj.add_kernel_cmdline("androidboot.vsoc_e2e_test=1");
329 }
330 if (FLAGS_extra_kernel_cmdline.size()) {
331 tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline);
332 }
333
334 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
335 tmp_config_obj.set_system_image_path(FLAGS_system_image);
336 tmp_config_obj.set_cache_image_path(FLAGS_cache_image);
337 tmp_config_obj.set_data_image_path(FLAGS_data_image);
338 tmp_config_obj.set_vendor_image_path(FLAGS_vendor_image);
339 tmp_config_obj.set_dtb_path(FLAGS_dtb);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800340 tmp_config_obj.set_gsi_fstab_path(FLAGS_gsi_fstab);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800341
342 tmp_config_obj.set_mempath(FLAGS_mempath);
343 tmp_config_obj.set_ivshmem_qemu_socket_path(
344 tmp_config_obj.PerInstancePath("ivshmem_socket_qemu"));
345 tmp_config_obj.set_ivshmem_client_socket_path(
346 tmp_config_obj.PerInstancePath("ivshmem_socket_client"));
347 tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size());
348
349 if (AdbUsbEnabled(tmp_config_obj)) {
350 tmp_config_obj.set_usb_v1_socket_name(tmp_config_obj.PerInstancePath("usb-v1"));
351 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
352 tmp_config_obj.set_usb_ip_socket_name(tmp_config_obj.PerInstancePath("usb-ip"));
353 }
354
355 tmp_config_obj.set_kernel_log_socket_name(tmp_config_obj.PerInstancePath("kernel-log"));
356 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
357 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
358 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800359 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800360 tmp_config_obj.set_launcher_log_path(tmp_config_obj.PerInstancePath("launcher.log"));
361 tmp_config_obj.set_launcher_monitor_socket_path(
362 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
363
364 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
365 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
Jorge E. Moreiraba673b72019-02-07 14:03:45 -0800366 ConfigureRil(&tmp_config_obj);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800367
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800368 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
369
370 tmp_config_obj.set_wifi_guest_mac_addr(FLAGS_guest_mac_address);
371 tmp_config_obj.set_wifi_host_mac_addr(FLAGS_host_mac_address);
372
373 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
374
375 tmp_config_obj.set_entropy_source("/dev/urandom");
376 tmp_config_obj.set_uuid(FLAGS_uuid);
377
378 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800379 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800380 tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
381 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
382
383 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
384 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
385 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
386
387 tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio);
388 tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary);
389 tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port);
390
391 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
392 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
393 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
394 tmp_config_obj.set_virtual_usb_manager_binary(
395 FLAGS_virtual_usb_manager_binary);
396 tmp_config_obj.set_socket_forward_proxy_binary(
397 FLAGS_socket_forward_proxy_binary);
398 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
399 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
400 tmp_config_obj.set_run_e2e_test(FLAGS_run_e2e_test);
401 tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary);
402
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800403 tmp_config_obj.set_data_policy(FLAGS_data_policy);
404 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
405 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
406
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800407 if(!AdbUsbEnabled(tmp_config_obj)) {
408 tmp_config_obj.disable_usb_adb();
409 }
410
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800411 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
412 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
413
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800414 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
415
416 auto config_file = GetConfigFilePath(tmp_config_obj);
417 auto config_link = vsoc::GetGlobalConfigFileLink();
418 // Save the config object before starting any host process
419 if (!tmp_config_obj.SaveToFile(config_file)) {
420 LOG(ERROR) << "Unable to save config object";
421 return false;
422 }
423 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
424 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
425 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
426 << ": " << strerror(errno);
427 return false;
428 }
429
430 return true;
431}
432
433void SetDefaultFlagsForQemu() {
434 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
435 SetCommandLineOptionWithMode("mobile_interface",
436 default_mobile_interface.c_str(),
437 google::FlagSettingMode::SET_FLAGS_DEFAULT);
438 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
439 SetCommandLineOptionWithMode("mobile_tap_name",
440 default_mobile_tap_name.c_str(),
441 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800442 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
443 SetCommandLineOptionWithMode("wifi_tap_name",
444 default_wifi_tap_name.c_str(),
445 google::FlagSettingMode::SET_FLAGS_DEFAULT);
446 auto default_instance_dir =
447 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
448 SetCommandLineOptionWithMode("instance_dir",
449 default_instance_dir.c_str(),
450 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800451 SetCommandLineOptionWithMode("hardware_name", "cutf_ivsh",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800452 google::FlagSettingMode::SET_FLAGS_DEFAULT);
453 SetCommandLineOptionWithMode("adb_mode", "tunnel",
454 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800455 SetCommandLineOptionWithMode("decompress_kernel", "false",
456 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800457 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatSerialMode,
458 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800459}
460
461void SetDefaultFlagsForCrosvm() {
462 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
463 SetCommandLineOptionWithMode("mobile_interface",
464 default_mobile_interface.c_str(),
465 google::FlagSettingMode::SET_FLAGS_DEFAULT);
466 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
467 SetCommandLineOptionWithMode("mobile_tap_name",
468 default_mobile_tap_name.c_str(),
469 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800470 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
471 SetCommandLineOptionWithMode("wifi_tap_name",
472 default_wifi_tap_name.c_str(),
473 google::FlagSettingMode::SET_FLAGS_DEFAULT);
474 auto default_instance_dir =
475 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
476 SetCommandLineOptionWithMode("instance_dir",
477 default_instance_dir.c_str(),
478 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800479 SetCommandLineOptionWithMode("hardware_name", "cutf_cvm",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800480 google::FlagSettingMode::SET_FLAGS_DEFAULT);
481 SetCommandLineOptionWithMode("adb_mode", "vsock_tunnel",
482 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800483 SetCommandLineOptionWithMode("decompress_kernel", "true",
484 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira84d93c12019-02-05 12:02:29 -0800485 SetCommandLineOptionWithMode("run_e2e_test", "false",
486 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiracf4fc2a2019-02-05 17:33:20 -0800487 SetCommandLineOptionWithMode("start_vnc_server", "false",
488 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800489 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
490 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800491}
492
493bool ParseCommandLineFlags(int* argc, char*** argv) {
494 // The config_file is created by the launcher, so the launcher is the only
495 // host process that doesn't use the flag.
496 // Set the default to empty.
497 google::SetCommandLineOptionWithMode("config_file", "",
498 gflags::SET_FLAGS_DEFAULT);
499 google::ParseCommandLineNonHelpFlags(argc, argv, true);
500 bool invalid_manager = false;
501 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
502 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800503 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
504 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800505 } else {
506 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
507 << std::endl;
508 invalid_manager = true;
509 }
510 google::HandleCommandLineHelpFlags();
511 if (invalid_manager) {
512 return false;
513 }
514 // Set the env variable to empty (in case the caller passed a value for it).
515 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
516
517 return ResolveInstanceFiles();
518}
519
520bool CleanPriorFiles() {
521 // Everything on the instance directory
522 std::string prior_files = FLAGS_instance_dir + "/*";
523 // The shared memory file
524 prior_files += " " + FLAGS_mempath;
525 // The environment file
526 prior_files += " " + GetCuttlefishEnvPath();
527 // The global link to the config file
528 prior_files += " " + vsoc::GetGlobalConfigFileLink();
529 LOG(INFO) << "Assuming prior files of " << prior_files;
530 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
531 int rval = std::system(fuser_cmd.c_str());
532 // fuser returns 0 if any of the files are open
533 if (WEXITSTATUS(rval) == 0) {
534 LOG(ERROR) << "Clean aborted: files are in use";
535 return false;
536 }
537 std::string clean_command = "rm -rf " + prior_files;
538 rval = std::system(clean_command.c_str());
539 if (WEXITSTATUS(rval) != 0) {
540 LOG(ERROR) << "Remove of files failed";
541 return false;
542 }
543 return true;
544}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800545
546bool DecompressKernel(const std::string& src, const std::string& dst) {
547 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
548 decomp_cmd.AddParameter(src);
549 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
550 if (!output_file->IsOpen()) {
551 LOG(ERROR) << "Unable to create decompressed image file: "
552 << output_file->StrError();
553 return false;
554 }
555 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
556 auto decomp_proc = decomp_cmd.Start(false);
557 return decomp_proc.Started() && decomp_proc.Wait() == 0;
558}
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800559} // namespace
560
561vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(int* argc, char*** argv) {
562 if (!ParseCommandLineFlags(argc, argv)) {
563 LOG(ERROR) << "Failed to parse command arguments";
564 exit(LauncherExitCodes::kArgumentParsingError);
565 }
566
567 // Clean up prior files before saving the config file (doing it after would
568 // delete it)
569 if (!CleanPriorFiles()) {
570 LOG(ERROR) << "Failed to clean prior files";
571 exit(LauncherExitCodes::kPrioFilesCleanupError);
572 }
573 // Create instance directory if it doesn't exist.
574 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
575 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
576 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
577 LOG(ERROR) << "Failed to create instance directory: "
578 << FLAGS_instance_dir << ". Error: " << errno;
579 exit(LauncherExitCodes::kInstanceDirCreationError);
580 }
581 }
582
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800583 if (!cvd::FileHasContent(FLAGS_boot_image)) {
584 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
585 exit(cvd::kCuttlefishConfigurationInitError);
586 }
587
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800588 auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image);
589
590 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) {
591 LOG(ERROR) << "Failed to initialize configuration";
592 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
593 }
594 // Do this early so that the config object is ready for anything that needs it
595 auto config = vsoc::CuttlefishConfig::Get();
596 if (!config) {
597 LOG(ERROR) << "Failed to obtain config singleton";
598 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
599 }
600
601 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
602 config->use_unpacked_kernel()
603 ? config->kernel_image_path()
604 : "")) {
605 LOG(ERROR) << "Failed to unpack boot image";
606 exit(LauncherExitCodes::kBootImageUnpackError);
607 }
608
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800609 if (config->decompress_kernel()) {
610 if (!DecompressKernel(config->kernel_image_path(),
611 config->decompressed_kernel_image_path())) {
612 LOG(ERROR) << "Failed to decompress kernel";
613 exit(LauncherExitCodes::kKernelDecompressError);
614 }
615 }
616
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800617 ValidateAdbModeFlag(*config);
618
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800619 // Create data if necessary
620 if (!ApplyDataImagePolicy(*config)) {
621 exit(cvd::kCuttlefishConfigurationInitError);
622 }
623
624 // Check that the files exist
625 for (const auto& file :
626 {config->system_image_path(), config->vendor_image_path(),
627 config->cache_image_path(), config->data_image_path()}) {
628 if (!cvd::FileHasContent(file.c_str())) {
629 LOG(ERROR) << "File not found: " << file;
630 exit(cvd::kCuttlefishConfigurationInitError);
631 }
632 }
633
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800634 return config;
635}
636
637std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
638 return config.PerInstancePath("cuttlefish_config.json");
639}