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