blob: ae5d0e6973965198b721b1758eef3e1a0bec1f23 [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, "",
38 "Debug flag to pass to qemu. e.g. --qemu_gdb=tcp::1234");
39
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, "",
60 "The codename of the device's hardware, one of {vsoc, virtio}");
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.");
97DEFINE_bool(start_stream_audio, true,
98 "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 "
131 "server. Only relevant with --adb_mode=tunnel or vsock_tunnel");
132DEFINE_string(adb_connector_binary,
133 vsoc::DefaultHostArtifactsPath("bin/adb_connector"),
134 "Location of the adb_connector binary. Only relevant if "
135 "--run_adb_connector is true");
136DEFINE_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.");
143DEFINE_string(wifi_interface, "", // default handled on ParseCommandLine
144 "Network interface to use for wifi");
145DEFINE_string(wifi_tap_name, "", // default handled on ParseCommandLine
146 "The name of the tap interface to use for wifi");
147DEFINE_int32(vsock_guest_cid,
148 vsoc::GetDefaultPerInstanceVsockCid(),
149 "Guest identifier for vsock. Disabled if under 3.");
150
151// TODO(b/72969289) This should be generated
152DEFINE_string(dtb, "", "Path to the cuttlefish.dtb file");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800153DEFINE_string(gsi_fstab,
154 vsoc::DefaultHostArtifactsPath("config/gsi.fstab"),
155 "Path to the GSI fstab file");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800156
157DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
158 "UUID to use for the device. Random if not specified");
159DEFINE_bool(daemon, false,
160 "Run cuttlefish in background, the launcher exits on boot "
161 "completed/failed");
162
163DEFINE_string(device_title, "", "Human readable name for the instance, "
164 "used by the vnc_server for its server title");
165DEFINE_string(setupwizard_mode, "DISABLED",
166 "One of DISABLED,OPTIONAL,REQUIRED");
167
168DEFINE_string(qemu_binary,
169 "/usr/bin/qemu-system-x86_64",
170 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800171DEFINE_string(crosvm_binary,
172 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
173 "The Crosvm binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800174DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
175DEFINE_bool(run_e2e_test, true, "Run e2e test after device launches");
176DEFINE_string(e2e_test_binary,
177 vsoc::DefaultHostArtifactsPath("bin/host_region_e2e_test"),
178 "Location of the region end to end test binary");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800179DEFINE_string(logcat_receiver_binary,
180 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"),
181 "Binary for the logcat server");
182DEFINE_string(logcat_mode, "", "How to send android's log messages from "
183 "guest to host. One of [serial, vsock]");
184DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
185 "The port for logcat over vsock");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800186namespace {
187
188template<typename S, typename T>
189static std::string concat(const S& s, const T& t) {
190 std::ostringstream os;
191 os << s << t;
192 return os.str();
193}
194
195bool ResolveInstanceFiles() {
196 if (FLAGS_system_image_dir.empty()) {
197 LOG(ERROR) << "--system_image_dir must be specified.";
198 return false;
199 }
200
201 // If user did not specify location of either of these files, expect them to
202 // be placed in --system_image_dir location.
203 std::string default_system_image = FLAGS_system_image_dir + "/system.img";
204 SetCommandLineOptionWithMode("system_image", default_system_image.c_str(),
205 google::FlagSettingMode::SET_FLAGS_DEFAULT);
206 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
207 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
208 google::FlagSettingMode::SET_FLAGS_DEFAULT);
209 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
210 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
211 google::FlagSettingMode::SET_FLAGS_DEFAULT);
212 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
213 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
214 google::FlagSettingMode::SET_FLAGS_DEFAULT);
215 std::string default_vendor_image = FLAGS_system_image_dir + "/vendor.img";
216 SetCommandLineOptionWithMode("vendor_image", default_vendor_image.c_str(),
217 google::FlagSettingMode::SET_FLAGS_DEFAULT);
218
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800219 return true;
220}
221
222std::string GetCuttlefishEnvPath() {
223 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
224}
225
226// Initializes the config object and saves it to file. It doesn't return it, all
227// further uses of the config should happen through the singleton
228bool InitializeCuttlefishConfiguration(
229 const cvd::BootImageUnpacker& boot_image_unpacker) {
230 vsoc::CuttlefishConfig tmp_config_obj;
231 auto& memory_layout = *vsoc::VSoCMemoryLayout::Get();
232 // Set this first so that calls to PerInstancePath below are correct
233 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
234 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
235 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
236 return false;
237 }
238 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
239
240 tmp_config_obj.set_serial_number(FLAGS_serial_number);
241
242 tmp_config_obj.set_cpus(FLAGS_cpus);
243 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
244
245 tmp_config_obj.set_dpi(FLAGS_dpi);
246 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
247 tmp_config_obj.set_x_res(FLAGS_x_res);
248 tmp_config_obj.set_y_res(FLAGS_y_res);
249 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
250 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
251 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
252 tmp_config_obj.set_adb_mode(FLAGS_adb_mode);
253 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
254
255 tmp_config_obj.set_device_title(FLAGS_device_title);
256 if (FLAGS_kernel_path.size()) {
257 tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
258 tmp_config_obj.set_use_unpacked_kernel(false);
259 } else {
260 tmp_config_obj.set_kernel_image_path(
261 tmp_config_obj.PerInstancePath("kernel"));
262 tmp_config_obj.set_use_unpacked_kernel(true);
263 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800264 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
265 if (FLAGS_decompress_kernel) {
266 tmp_config_obj.set_decompressed_kernel_image_path(
267 tmp_config_obj.PerInstancePath("vmlinux"));
268 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800269
270 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
271 bool use_ramdisk = boot_image_unpacker.HasRamdiskImage();
272 if (!use_ramdisk) {
273 LOG(INFO) << "No ramdisk present; assuming system-as-root build";
274 ramdisk_path = "";
275 }
276
277 // This needs to be done here because the dtb path depends on the presence of
278 // the ramdisk
279 if (FLAGS_dtb.empty()) {
280 if (use_ramdisk) {
281 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/initrd-root.dtb");
282 } else {
283 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/system-root.dtb");
284 }
285 }
286
287 tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
288 if (!use_ramdisk) {
289 tmp_config_obj.add_kernel_cmdline("root=/dev/vda");
290 }
291 tmp_config_obj.add_kernel_cmdline("init=/init");
292 tmp_config_obj.add_kernel_cmdline(
293 concat("androidboot.serialno=", FLAGS_serial_number));
294 tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0");
295 tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi));
296 tmp_config_obj.add_kernel_cmdline(
297 concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode));
298 tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part));
299 if (!FLAGS_console.empty()) {
300 tmp_config_obj.add_kernel_cmdline(concat("console=", FLAGS_console));
301 }
302 if (!FLAGS_androidboot_console.empty()) {
303 tmp_config_obj.add_kernel_cmdline(
304 concat("androidboot.console=", FLAGS_androidboot_console));
305 }
306 if (!FLAGS_hardware_name.empty()) {
307 tmp_config_obj.add_kernel_cmdline(
308 concat("androidboot.hardware=", FLAGS_hardware_name));
309 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800310 if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) {
311 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=",
312 FLAGS_logcat_vsock_port));
313 }
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800314 tmp_config_obj.set_hardware_name(FLAGS_hardware_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800315 if (!FLAGS_guest_security.empty()) {
316 tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security));
317 if (FLAGS_guest_enforce_security) {
318 tmp_config_obj.add_kernel_cmdline("enforcing=1");
319 } else {
320 tmp_config_obj.add_kernel_cmdline("enforcing=0");
321 tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive");
322 }
323 if (FLAGS_guest_audit_security) {
324 tmp_config_obj.add_kernel_cmdline("audit=1");
325 } else {
326 tmp_config_obj.add_kernel_cmdline("audit=0");
327 }
328 }
329 if (FLAGS_run_e2e_test) {
330 tmp_config_obj.add_kernel_cmdline("androidboot.vsoc_e2e_test=1");
331 }
332 if (FLAGS_extra_kernel_cmdline.size()) {
333 tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline);
334 }
335
336 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
337 tmp_config_obj.set_system_image_path(FLAGS_system_image);
338 tmp_config_obj.set_cache_image_path(FLAGS_cache_image);
339 tmp_config_obj.set_data_image_path(FLAGS_data_image);
340 tmp_config_obj.set_vendor_image_path(FLAGS_vendor_image);
341 tmp_config_obj.set_dtb_path(FLAGS_dtb);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800342 tmp_config_obj.set_gsi_fstab_path(FLAGS_gsi_fstab);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800343
344 tmp_config_obj.set_mempath(FLAGS_mempath);
345 tmp_config_obj.set_ivshmem_qemu_socket_path(
346 tmp_config_obj.PerInstancePath("ivshmem_socket_qemu"));
347 tmp_config_obj.set_ivshmem_client_socket_path(
348 tmp_config_obj.PerInstancePath("ivshmem_socket_client"));
349 tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size());
350
351 if (AdbUsbEnabled(tmp_config_obj)) {
352 tmp_config_obj.set_usb_v1_socket_name(tmp_config_obj.PerInstancePath("usb-v1"));
353 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
354 tmp_config_obj.set_usb_ip_socket_name(tmp_config_obj.PerInstancePath("usb-ip"));
355 }
356
357 tmp_config_obj.set_kernel_log_socket_name(tmp_config_obj.PerInstancePath("kernel-log"));
358 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
359 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
360 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800361 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800362 tmp_config_obj.set_launcher_log_path(tmp_config_obj.PerInstancePath("launcher.log"));
363 tmp_config_obj.set_launcher_monitor_socket_path(
364 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
365
366 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
367 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
Jorge E. Moreiraba673b72019-02-07 14:03:45 -0800368 ConfigureRil(&tmp_config_obj);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800369
370 tmp_config_obj.set_wifi_bridge_name(FLAGS_wifi_interface);
371 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
372
373 tmp_config_obj.set_wifi_guest_mac_addr(FLAGS_guest_mac_address);
374 tmp_config_obj.set_wifi_host_mac_addr(FLAGS_host_mac_address);
375
376 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
377
378 tmp_config_obj.set_entropy_source("/dev/urandom");
379 tmp_config_obj.set_uuid(FLAGS_uuid);
380
381 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800382 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800383 tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
384 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
385
386 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
387 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
388 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
389
390 tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio);
391 tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary);
392 tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port);
393
394 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
395 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
396 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
397 tmp_config_obj.set_virtual_usb_manager_binary(
398 FLAGS_virtual_usb_manager_binary);
399 tmp_config_obj.set_socket_forward_proxy_binary(
400 FLAGS_socket_forward_proxy_binary);
401 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
402 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
403 tmp_config_obj.set_run_e2e_test(FLAGS_run_e2e_test);
404 tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary);
405
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800406 tmp_config_obj.set_data_policy(FLAGS_data_policy);
407 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
408 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
409
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800410 if(!AdbUsbEnabled(tmp_config_obj)) {
411 tmp_config_obj.disable_usb_adb();
412 }
413
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800414 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
415 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
416
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800417 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
418
419 auto config_file = GetConfigFilePath(tmp_config_obj);
420 auto config_link = vsoc::GetGlobalConfigFileLink();
421 // Save the config object before starting any host process
422 if (!tmp_config_obj.SaveToFile(config_file)) {
423 LOG(ERROR) << "Unable to save config object";
424 return false;
425 }
426 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
427 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
428 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
429 << ": " << strerror(errno);
430 return false;
431 }
432
433 return true;
434}
435
436void SetDefaultFlagsForQemu() {
437 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
438 SetCommandLineOptionWithMode("mobile_interface",
439 default_mobile_interface.c_str(),
440 google::FlagSettingMode::SET_FLAGS_DEFAULT);
441 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
442 SetCommandLineOptionWithMode("mobile_tap_name",
443 default_mobile_tap_name.c_str(),
444 google::FlagSettingMode::SET_FLAGS_DEFAULT);
445 auto default_wifi_interface = GetPerInstanceDefault("cvd-wbr-");
446 SetCommandLineOptionWithMode("wifi_interface",
447 default_wifi_interface.c_str(),
448 google::FlagSettingMode::SET_FLAGS_DEFAULT);
449 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
450 SetCommandLineOptionWithMode("wifi_tap_name",
451 default_wifi_tap_name.c_str(),
452 google::FlagSettingMode::SET_FLAGS_DEFAULT);
453 auto default_instance_dir =
454 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
455 SetCommandLineOptionWithMode("instance_dir",
456 default_instance_dir.c_str(),
457 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800458 SetCommandLineOptionWithMode("hardware_name",
459 "vsoc",
460 google::FlagSettingMode::SET_FLAGS_DEFAULT);
461 SetCommandLineOptionWithMode("adb_mode", "tunnel",
462 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800463 SetCommandLineOptionWithMode("decompress_kernel", "false",
464 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800465 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatSerialMode,
466 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800467}
468
469void SetDefaultFlagsForCrosvm() {
470 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
471 SetCommandLineOptionWithMode("mobile_interface",
472 default_mobile_interface.c_str(),
473 google::FlagSettingMode::SET_FLAGS_DEFAULT);
474 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
475 SetCommandLineOptionWithMode("mobile_tap_name",
476 default_mobile_tap_name.c_str(),
477 google::FlagSettingMode::SET_FLAGS_DEFAULT);
478 auto default_wifi_interface = GetPerInstanceDefault("cvd-wbr-");
479 SetCommandLineOptionWithMode("wifi_interface",
480 default_wifi_interface.c_str(),
481 google::FlagSettingMode::SET_FLAGS_DEFAULT);
482 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
483 SetCommandLineOptionWithMode("wifi_tap_name",
484 default_wifi_tap_name.c_str(),
485 google::FlagSettingMode::SET_FLAGS_DEFAULT);
486 auto default_instance_dir =
487 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
488 SetCommandLineOptionWithMode("instance_dir",
489 default_instance_dir.c_str(),
490 google::FlagSettingMode::SET_FLAGS_DEFAULT);
491 SetCommandLineOptionWithMode("hardware_name",
492 "virtio",
493 google::FlagSettingMode::SET_FLAGS_DEFAULT);
494 SetCommandLineOptionWithMode("adb_mode", "vsock_tunnel",
495 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800496 SetCommandLineOptionWithMode("decompress_kernel", "true",
497 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira84d93c12019-02-05 12:02:29 -0800498 SetCommandLineOptionWithMode("run_e2e_test", "false",
499 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiracf4fc2a2019-02-05 17:33:20 -0800500 SetCommandLineOptionWithMode("start_vnc_server", "false",
501 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafb95b0d2019-02-05 17:40:20 -0800502 SetCommandLineOptionWithMode("start_stream_audio", "false",
503 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800504 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
505 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800506}
507
508bool ParseCommandLineFlags(int* argc, char*** argv) {
509 // The config_file is created by the launcher, so the launcher is the only
510 // host process that doesn't use the flag.
511 // Set the default to empty.
512 google::SetCommandLineOptionWithMode("config_file", "",
513 gflags::SET_FLAGS_DEFAULT);
514 google::ParseCommandLineNonHelpFlags(argc, argv, true);
515 bool invalid_manager = false;
516 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
517 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800518 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
519 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800520 } else {
521 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
522 << std::endl;
523 invalid_manager = true;
524 }
525 google::HandleCommandLineHelpFlags();
526 if (invalid_manager) {
527 return false;
528 }
529 // Set the env variable to empty (in case the caller passed a value for it).
530 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
531
532 return ResolveInstanceFiles();
533}
534
535bool CleanPriorFiles() {
536 // Everything on the instance directory
537 std::string prior_files = FLAGS_instance_dir + "/*";
538 // The shared memory file
539 prior_files += " " + FLAGS_mempath;
540 // The environment file
541 prior_files += " " + GetCuttlefishEnvPath();
542 // The global link to the config file
543 prior_files += " " + vsoc::GetGlobalConfigFileLink();
544 LOG(INFO) << "Assuming prior files of " << prior_files;
545 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
546 int rval = std::system(fuser_cmd.c_str());
547 // fuser returns 0 if any of the files are open
548 if (WEXITSTATUS(rval) == 0) {
549 LOG(ERROR) << "Clean aborted: files are in use";
550 return false;
551 }
552 std::string clean_command = "rm -rf " + prior_files;
553 rval = std::system(clean_command.c_str());
554 if (WEXITSTATUS(rval) != 0) {
555 LOG(ERROR) << "Remove of files failed";
556 return false;
557 }
558 return true;
559}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800560
561bool DecompressKernel(const std::string& src, const std::string& dst) {
562 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
563 decomp_cmd.AddParameter(src);
564 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
565 if (!output_file->IsOpen()) {
566 LOG(ERROR) << "Unable to create decompressed image file: "
567 << output_file->StrError();
568 return false;
569 }
570 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
571 auto decomp_proc = decomp_cmd.Start(false);
572 return decomp_proc.Started() && decomp_proc.Wait() == 0;
573}
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800574} // namespace
575
576vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(int* argc, char*** argv) {
577 if (!ParseCommandLineFlags(argc, argv)) {
578 LOG(ERROR) << "Failed to parse command arguments";
579 exit(LauncherExitCodes::kArgumentParsingError);
580 }
581
582 // Clean up prior files before saving the config file (doing it after would
583 // delete it)
584 if (!CleanPriorFiles()) {
585 LOG(ERROR) << "Failed to clean prior files";
586 exit(LauncherExitCodes::kPrioFilesCleanupError);
587 }
588 // Create instance directory if it doesn't exist.
589 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
590 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
591 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
592 LOG(ERROR) << "Failed to create instance directory: "
593 << FLAGS_instance_dir << ". Error: " << errno;
594 exit(LauncherExitCodes::kInstanceDirCreationError);
595 }
596 }
597
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800598 if (!cvd::FileHasContent(FLAGS_boot_image)) {
599 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
600 exit(cvd::kCuttlefishConfigurationInitError);
601 }
602
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800603 auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image);
604
605 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) {
606 LOG(ERROR) << "Failed to initialize configuration";
607 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
608 }
609 // Do this early so that the config object is ready for anything that needs it
610 auto config = vsoc::CuttlefishConfig::Get();
611 if (!config) {
612 LOG(ERROR) << "Failed to obtain config singleton";
613 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
614 }
615
616 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
617 config->use_unpacked_kernel()
618 ? config->kernel_image_path()
619 : "")) {
620 LOG(ERROR) << "Failed to unpack boot image";
621 exit(LauncherExitCodes::kBootImageUnpackError);
622 }
623
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800624 if (config->decompress_kernel()) {
625 if (!DecompressKernel(config->kernel_image_path(),
626 config->decompressed_kernel_image_path())) {
627 LOG(ERROR) << "Failed to decompress kernel";
628 exit(LauncherExitCodes::kKernelDecompressError);
629 }
630 }
631
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800632 ValidateAdbModeFlag(*config);
633
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800634 // Create data if necessary
635 if (!ApplyDataImagePolicy(*config)) {
636 exit(cvd::kCuttlefishConfigurationInitError);
637 }
638
639 // Check that the files exist
640 for (const auto& file :
641 {config->system_image_path(), config->vendor_image_path(),
642 config->cache_image_path(), config->data_image_path()}) {
643 if (!cvd::FileHasContent(file.c_str())) {
644 LOG(ERROR) << "File not found: " << file;
645 exit(cvd::kCuttlefishConfigurationInitError);
646 }
647 }
648
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800649 return config;
650}
651
652std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
653 return config.PerInstancePath("cuttlefish_config.json");
654}