blob: be4b33f0c763e46977556ac9ffbbfd208edb6614 [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. Moreiraba673b72019-02-07 14:03:45 -080016#include "host/commands/launch/ril_config.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080017#include "host/libs/vm_manager/crosvm_manager.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080018#include "host/libs/vm_manager/qemu_manager.h"
19#include "host/libs/vm_manager/vm_manager.h"
20
21using vsoc::GetPerInstanceDefault;
22using cvd::LauncherExitCodes;
23
24DEFINE_string(
25 system_image, "",
26 "Path to the system image, if empty it is assumed to be a file named "
27 "system.img in the directory specified by -system_image_dir");
28DEFINE_string(cache_image, "", "Location of the cache partition image.");
Paul Trautrimba8f8e92019-03-12 17:55:48 +090029DEFINE_string(metadata_image, "", "Location of the metadata partition image "
30 "to be generated.");
31DEFINE_int32(blank_metadata_image_mb, 16,
32 "The size of the blank metadata image to generate, MB.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080033DEFINE_int32(cpus, 2, "Virtual CPU count.");
34DEFINE_string(data_image, "", "Location of the data partition image.");
35DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
36 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
37 "'always_create'.");
38DEFINE_int32(blank_data_image_mb, 0,
39 "The size of the blank data image to generate, MB.");
40DEFINE_string(blank_data_image_fmt, "ext4",
41 "The fs format for the blank data image. Used with mkfs.");
42DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000043 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080044
45DEFINE_int32(x_res, 720, "Width of the screen in pixels");
46DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
47DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
48DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
49DEFINE_int32(num_screen_buffers, 3, "The number of screen buffers");
50DEFINE_string(kernel_path, "",
51 "Path to the kernel. Overrides the one from the boot image");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080052DEFINE_bool(decompress_kernel, false,
53 "Whether to decompress the kernel image. Required for crosvm.");
54DEFINE_string(kernel_decompresser_executable,
55 vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"),
56 "Path to the extract-vmlinux executable.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080057DEFINE_string(extra_kernel_cmdline, "",
58 "Additional flags to put on the kernel command line");
59DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
60DEFINE_string(console, "ttyS0", "Console device for the guest kernel.");
61DEFINE_string(androidboot_console, "ttyS1",
62 "Console device for the Android framework");
Jorge E. Moreiraba626622019-01-28 17:47:50 -080063DEFINE_string(
64 hardware_name, "",
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -080065 "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080066DEFINE_string(guest_security, "selinux",
67 "The security module to use in the guest");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070068DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080069 "Whether to run in enforcing mode (non permissive). Ignored if "
70 "-guest_security is empty.");
71DEFINE_bool(guest_audit_security, true,
72 "Whether to log security audits.");
73DEFINE_string(boot_image, "", "Location of cuttlefish boot image.");
74DEFINE_int32(memory_mb, 2048,
75 "Total amount of memory available for guest, MB.");
76std::string g_default_mempath{vsoc::GetDefaultMempath()};
77DEFINE_string(mempath, g_default_mempath.c_str(),
78 "Target location for the shmem file.");
79DEFINE_string(mobile_interface, "", // default handled on ParseCommandLine
80 "Network interface to use for mobile networking");
81DEFINE_string(mobile_tap_name, "", // default handled on ParseCommandLine
82 "The name of the tap interface to use for mobile");
83std::string g_default_serial_number{GetPerInstanceDefault("CUTTLEFISHCVD")};
84DEFINE_string(serial_number, g_default_serial_number.c_str(),
85 "Serial number to use for the device");
86DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
87 "A directory to put all instance specific files");
88DEFINE_string(
89 vm_manager, vm_manager::QemuManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080090 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070091DEFINE_string(
92 gpu_mode, vsoc::kGpuModeGuestAshmem,
93 "What gpu configuration to use, one of {guest_ashmem, guest_drm}");
94DEFINE_string(wayland_socket, "",
95 "Location of the wayland socket to use for *_drm gpu_modes.");
96
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080097DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
98 "Location of the system partition images.");
99DEFINE_string(vendor_image, "", "Location of the vendor partition image.");
Alistair Strachan5782ce42019-03-28 22:05:13 -0700100DEFINE_string(product_image, "", "Location of the product 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.");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800133DEFINE_string(adb_mode, "",
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. Moreirac87c2c72019-03-06 16:12:23 -0800196DEFINE_int32(frames_vsock_port, vsoc::GetPerInstanceDefault(5580),
197 "The vsock port to receive frames from the guest on");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800198namespace {
199
200template<typename S, typename T>
201static std::string concat(const S& s, const T& t) {
202 std::ostringstream os;
203 os << s << t;
204 return os.str();
205}
206
207bool ResolveInstanceFiles() {
208 if (FLAGS_system_image_dir.empty()) {
209 LOG(ERROR) << "--system_image_dir must be specified.";
210 return false;
211 }
212
213 // If user did not specify location of either of these files, expect them to
214 // be placed in --system_image_dir location.
215 std::string default_system_image = FLAGS_system_image_dir + "/system.img";
216 SetCommandLineOptionWithMode("system_image", default_system_image.c_str(),
217 google::FlagSettingMode::SET_FLAGS_DEFAULT);
218 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
219 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
220 google::FlagSettingMode::SET_FLAGS_DEFAULT);
221 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
222 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
223 google::FlagSettingMode::SET_FLAGS_DEFAULT);
224 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
225 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
226 google::FlagSettingMode::SET_FLAGS_DEFAULT);
227 std::string default_vendor_image = FLAGS_system_image_dir + "/vendor.img";
228 SetCommandLineOptionWithMode("vendor_image", default_vendor_image.c_str(),
229 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900230 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
231 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
232 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan5782ce42019-03-28 22:05:13 -0700233 std::string default_product_image = FLAGS_system_image_dir + "/product.img";
234 SetCommandLineOptionWithMode("product_image", default_product_image.c_str(),
235 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800236
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800237 return true;
238}
239
240std::string GetCuttlefishEnvPath() {
241 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
242}
243
244// Initializes the config object and saves it to file. It doesn't return it, all
245// further uses of the config should happen through the singleton
246bool InitializeCuttlefishConfiguration(
247 const cvd::BootImageUnpacker& boot_image_unpacker) {
248 vsoc::CuttlefishConfig tmp_config_obj;
249 auto& memory_layout = *vsoc::VSoCMemoryLayout::Get();
250 // Set this first so that calls to PerInstancePath below are correct
251 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
252 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
253 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
254 return false;
255 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700256 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
257 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
258 return false;
259 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800260 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700261 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
262 if (!vm_manager::VmManager::ConfigureGpuMode(&tmp_config_obj)) {
263 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
264 " does not work with vm_manager=" << FLAGS_vm_manager;
265 return false;
266 }
267 tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800268
269 tmp_config_obj.set_serial_number(FLAGS_serial_number);
270
271 tmp_config_obj.set_cpus(FLAGS_cpus);
272 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
273
274 tmp_config_obj.set_dpi(FLAGS_dpi);
275 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
276 tmp_config_obj.set_x_res(FLAGS_x_res);
277 tmp_config_obj.set_y_res(FLAGS_y_res);
278 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
279 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
280 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800281 std::vector<std::string> adb = cvd::StrSplit(FLAGS_adb_mode, ',');
282 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800283 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
284
285 tmp_config_obj.set_device_title(FLAGS_device_title);
286 if (FLAGS_kernel_path.size()) {
287 tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
288 tmp_config_obj.set_use_unpacked_kernel(false);
289 } else {
290 tmp_config_obj.set_kernel_image_path(
291 tmp_config_obj.PerInstancePath("kernel"));
292 tmp_config_obj.set_use_unpacked_kernel(true);
293 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800294 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
295 if (FLAGS_decompress_kernel) {
296 tmp_config_obj.set_decompressed_kernel_image_path(
297 tmp_config_obj.PerInstancePath("vmlinux"));
298 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800299
300 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
301 bool use_ramdisk = boot_image_unpacker.HasRamdiskImage();
302 if (!use_ramdisk) {
303 LOG(INFO) << "No ramdisk present; assuming system-as-root build";
304 ramdisk_path = "";
305 }
306
307 // This needs to be done here because the dtb path depends on the presence of
308 // the ramdisk
309 if (FLAGS_dtb.empty()) {
310 if (use_ramdisk) {
311 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/initrd-root.dtb");
312 } else {
313 FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/system-root.dtb");
314 }
315 }
316
317 tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
318 if (!use_ramdisk) {
319 tmp_config_obj.add_kernel_cmdline("root=/dev/vda");
320 }
321 tmp_config_obj.add_kernel_cmdline("init=/init");
322 tmp_config_obj.add_kernel_cmdline(
323 concat("androidboot.serialno=", FLAGS_serial_number));
324 tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0");
325 tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi));
326 tmp_config_obj.add_kernel_cmdline(
327 concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode));
328 tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part));
329 if (!FLAGS_console.empty()) {
330 tmp_config_obj.add_kernel_cmdline(concat("console=", FLAGS_console));
331 }
332 if (!FLAGS_androidboot_console.empty()) {
333 tmp_config_obj.add_kernel_cmdline(
334 concat("androidboot.console=", FLAGS_androidboot_console));
335 }
336 if (!FLAGS_hardware_name.empty()) {
337 tmp_config_obj.add_kernel_cmdline(
338 concat("androidboot.hardware=", FLAGS_hardware_name));
339 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800340 if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) {
341 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=",
342 FLAGS_logcat_vsock_port));
343 }
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800344 tmp_config_obj.set_hardware_name(FLAGS_hardware_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800345 if (!FLAGS_guest_security.empty()) {
346 tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security));
347 if (FLAGS_guest_enforce_security) {
348 tmp_config_obj.add_kernel_cmdline("enforcing=1");
349 } else {
350 tmp_config_obj.add_kernel_cmdline("enforcing=0");
351 tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive");
352 }
353 if (FLAGS_guest_audit_security) {
354 tmp_config_obj.add_kernel_cmdline("audit=1");
355 } else {
356 tmp_config_obj.add_kernel_cmdline("audit=0");
357 }
358 }
359 if (FLAGS_run_e2e_test) {
360 tmp_config_obj.add_kernel_cmdline("androidboot.vsoc_e2e_test=1");
361 }
362 if (FLAGS_extra_kernel_cmdline.size()) {
363 tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline);
364 }
365
366 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
367 tmp_config_obj.set_system_image_path(FLAGS_system_image);
368 tmp_config_obj.set_cache_image_path(FLAGS_cache_image);
369 tmp_config_obj.set_data_image_path(FLAGS_data_image);
370 tmp_config_obj.set_vendor_image_path(FLAGS_vendor_image);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900371 tmp_config_obj.set_metadata_image_path(FLAGS_metadata_image);
Alistair Strachan5782ce42019-03-28 22:05:13 -0700372 tmp_config_obj.set_product_image_path(FLAGS_product_image);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800373 tmp_config_obj.set_dtb_path(FLAGS_dtb);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800374 tmp_config_obj.set_gsi_fstab_path(FLAGS_gsi_fstab);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800375
376 tmp_config_obj.set_mempath(FLAGS_mempath);
377 tmp_config_obj.set_ivshmem_qemu_socket_path(
378 tmp_config_obj.PerInstancePath("ivshmem_socket_qemu"));
379 tmp_config_obj.set_ivshmem_client_socket_path(
380 tmp_config_obj.PerInstancePath("ivshmem_socket_client"));
381 tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size());
382
383 if (AdbUsbEnabled(tmp_config_obj)) {
384 tmp_config_obj.set_usb_v1_socket_name(tmp_config_obj.PerInstancePath("usb-v1"));
385 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
386 tmp_config_obj.set_usb_ip_socket_name(tmp_config_obj.PerInstancePath("usb-ip"));
387 }
388
389 tmp_config_obj.set_kernel_log_socket_name(tmp_config_obj.PerInstancePath("kernel-log"));
390 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
391 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
392 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800393 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800394 tmp_config_obj.set_launcher_log_path(tmp_config_obj.PerInstancePath("launcher.log"));
395 tmp_config_obj.set_launcher_monitor_socket_path(
396 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
397
398 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
399 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
Jorge E. Moreiraba673b72019-02-07 14:03:45 -0800400 ConfigureRil(&tmp_config_obj);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800401
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800402 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
403
404 tmp_config_obj.set_wifi_guest_mac_addr(FLAGS_guest_mac_address);
405 tmp_config_obj.set_wifi_host_mac_addr(FLAGS_host_mac_address);
406
407 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
408
409 tmp_config_obj.set_entropy_source("/dev/urandom");
410 tmp_config_obj.set_uuid(FLAGS_uuid);
411
412 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800413 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800414 tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
415 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
416
417 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
418 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
419 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
420
421 tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio);
422 tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary);
423 tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port);
424
425 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
426 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
427 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
428 tmp_config_obj.set_virtual_usb_manager_binary(
429 FLAGS_virtual_usb_manager_binary);
430 tmp_config_obj.set_socket_forward_proxy_binary(
431 FLAGS_socket_forward_proxy_binary);
432 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
433 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
434 tmp_config_obj.set_run_e2e_test(FLAGS_run_e2e_test);
435 tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary);
436
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800437 tmp_config_obj.set_data_policy(FLAGS_data_policy);
438 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
439 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
440
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800441 if(!AdbUsbEnabled(tmp_config_obj)) {
442 tmp_config_obj.disable_usb_adb();
443 }
444
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800445 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
446 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800447 tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port);
448 if (!tmp_config_obj.enable_ivserver()) {
449 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_frames_port=",
450 FLAGS_frames_vsock_port));
451 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800452
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800453 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
454
455 auto config_file = GetConfigFilePath(tmp_config_obj);
456 auto config_link = vsoc::GetGlobalConfigFileLink();
457 // Save the config object before starting any host process
458 if (!tmp_config_obj.SaveToFile(config_file)) {
459 LOG(ERROR) << "Unable to save config object";
460 return false;
461 }
462 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
463 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
464 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
465 << ": " << strerror(errno);
466 return false;
467 }
468
469 return true;
470}
471
472void SetDefaultFlagsForQemu() {
473 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
474 SetCommandLineOptionWithMode("mobile_interface",
475 default_mobile_interface.c_str(),
476 google::FlagSettingMode::SET_FLAGS_DEFAULT);
477 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
478 SetCommandLineOptionWithMode("mobile_tap_name",
479 default_mobile_tap_name.c_str(),
480 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800481 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
482 SetCommandLineOptionWithMode("wifi_tap_name",
483 default_wifi_tap_name.c_str(),
484 google::FlagSettingMode::SET_FLAGS_DEFAULT);
485 auto default_instance_dir =
486 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
487 SetCommandLineOptionWithMode("instance_dir",
488 default_instance_dir.c_str(),
489 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800490 SetCommandLineOptionWithMode("hardware_name", "cutf_ivsh",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800491 google::FlagSettingMode::SET_FLAGS_DEFAULT);
492 SetCommandLineOptionWithMode("adb_mode", "tunnel",
493 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800494 SetCommandLineOptionWithMode("decompress_kernel", "false",
495 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800496 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatSerialMode,
497 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800498}
499
500void SetDefaultFlagsForCrosvm() {
501 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
502 SetCommandLineOptionWithMode("mobile_interface",
503 default_mobile_interface.c_str(),
504 google::FlagSettingMode::SET_FLAGS_DEFAULT);
505 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
506 SetCommandLineOptionWithMode("mobile_tap_name",
507 default_mobile_tap_name.c_str(),
508 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800509 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
510 SetCommandLineOptionWithMode("wifi_tap_name",
511 default_wifi_tap_name.c_str(),
512 google::FlagSettingMode::SET_FLAGS_DEFAULT);
513 auto default_instance_dir =
514 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
515 SetCommandLineOptionWithMode("instance_dir",
516 default_instance_dir.c_str(),
517 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700518 auto default_wayland_socket = vsoc::DefaultEnvironmentPath(
519 "XDG_RUNTIME_DIR", default_instance_dir.c_str(), "wayland-0");
520 SetCommandLineOptionWithMode("wayland_socket",
521 default_wayland_socket.c_str(),
522 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800523 SetCommandLineOptionWithMode("hardware_name", "cutf_cvm",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800524 google::FlagSettingMode::SET_FLAGS_DEFAULT);
525 SetCommandLineOptionWithMode("adb_mode", "vsock_tunnel",
526 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800527 SetCommandLineOptionWithMode("decompress_kernel", "true",
528 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira84d93c12019-02-05 12:02:29 -0800529 SetCommandLineOptionWithMode("run_e2e_test", "false",
530 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800531 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
532 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800533}
534
535bool ParseCommandLineFlags(int* argc, char*** argv) {
536 // The config_file is created by the launcher, so the launcher is the only
537 // host process that doesn't use the flag.
538 // Set the default to empty.
539 google::SetCommandLineOptionWithMode("config_file", "",
540 gflags::SET_FLAGS_DEFAULT);
541 google::ParseCommandLineNonHelpFlags(argc, argv, true);
542 bool invalid_manager = false;
543 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
544 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800545 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
546 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800547 } else {
548 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
549 << std::endl;
550 invalid_manager = true;
551 }
552 google::HandleCommandLineHelpFlags();
553 if (invalid_manager) {
554 return false;
555 }
556 // Set the env variable to empty (in case the caller passed a value for it).
557 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
558
559 return ResolveInstanceFiles();
560}
561
562bool CleanPriorFiles() {
563 // Everything on the instance directory
564 std::string prior_files = FLAGS_instance_dir + "/*";
565 // The shared memory file
566 prior_files += " " + FLAGS_mempath;
567 // The environment file
568 prior_files += " " + GetCuttlefishEnvPath();
569 // The global link to the config file
570 prior_files += " " + vsoc::GetGlobalConfigFileLink();
571 LOG(INFO) << "Assuming prior files of " << prior_files;
572 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
573 int rval = std::system(fuser_cmd.c_str());
574 // fuser returns 0 if any of the files are open
575 if (WEXITSTATUS(rval) == 0) {
576 LOG(ERROR) << "Clean aborted: files are in use";
577 return false;
578 }
579 std::string clean_command = "rm -rf " + prior_files;
580 rval = std::system(clean_command.c_str());
581 if (WEXITSTATUS(rval) != 0) {
582 LOG(ERROR) << "Remove of files failed";
583 return false;
584 }
585 return true;
586}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800587
588bool DecompressKernel(const std::string& src, const std::string& dst) {
589 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
590 decomp_cmd.AddParameter(src);
591 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
592 if (!output_file->IsOpen()) {
593 LOG(ERROR) << "Unable to create decompressed image file: "
594 << output_file->StrError();
595 return false;
596 }
597 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
598 auto decomp_proc = decomp_cmd.Start(false);
599 return decomp_proc.Started() && decomp_proc.Wait() == 0;
600}
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800601} // namespace
602
603vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(int* argc, char*** argv) {
604 if (!ParseCommandLineFlags(argc, argv)) {
605 LOG(ERROR) << "Failed to parse command arguments";
606 exit(LauncherExitCodes::kArgumentParsingError);
607 }
608
609 // Clean up prior files before saving the config file (doing it after would
610 // delete it)
611 if (!CleanPriorFiles()) {
612 LOG(ERROR) << "Failed to clean prior files";
613 exit(LauncherExitCodes::kPrioFilesCleanupError);
614 }
615 // Create instance directory if it doesn't exist.
616 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
617 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
618 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
619 LOG(ERROR) << "Failed to create instance directory: "
620 << FLAGS_instance_dir << ". Error: " << errno;
621 exit(LauncherExitCodes::kInstanceDirCreationError);
622 }
623 }
624
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800625 if (!cvd::FileHasContent(FLAGS_boot_image)) {
626 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
627 exit(cvd::kCuttlefishConfigurationInitError);
628 }
629
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800630 auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image);
631
632 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) {
633 LOG(ERROR) << "Failed to initialize configuration";
634 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
635 }
636 // Do this early so that the config object is ready for anything that needs it
637 auto config = vsoc::CuttlefishConfig::Get();
638 if (!config) {
639 LOG(ERROR) << "Failed to obtain config singleton";
640 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
641 }
642
643 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
644 config->use_unpacked_kernel()
645 ? config->kernel_image_path()
646 : "")) {
647 LOG(ERROR) << "Failed to unpack boot image";
648 exit(LauncherExitCodes::kBootImageUnpackError);
649 }
650
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800651 if (config->decompress_kernel()) {
652 if (!DecompressKernel(config->kernel_image_path(),
653 config->decompressed_kernel_image_path())) {
654 LOG(ERROR) << "Failed to decompress kernel";
655 exit(LauncherExitCodes::kKernelDecompressError);
656 }
657 }
658
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800659 ValidateAdbModeFlag(*config);
660
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800661 // Create data if necessary
662 if (!ApplyDataImagePolicy(*config)) {
663 exit(cvd::kCuttlefishConfigurationInitError);
664 }
665
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900666 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
667
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800668 // Check that the files exist
669 for (const auto& file :
670 {config->system_image_path(), config->vendor_image_path(),
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900671 config->cache_image_path(), config->data_image_path(),
Alistair Strachan5782ce42019-03-28 22:05:13 -0700672 config->metadata_image_path(), config->product_image_path()}) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800673 if (!cvd::FileHasContent(file.c_str())) {
674 LOG(ERROR) << "File not found: " << file;
675 exit(cvd::kCuttlefishConfigurationInitError);
676 }
677 }
678
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800679 return config;
680}
681
682std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
683 return config.PerInstancePath("cuttlefish_config.json");
684}