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