Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 1 | #include "host/commands/launch/flags.h" |
| 2 | |
| 3 | #include <iostream> |
| 4 | |
| 5 | #include <gflags/gflags.h> |
| 6 | #include <glog/logging.h> |
| 7 | |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 8 | #include "common/libs/strings/str_split.h" |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 9 | #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. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 16 | #include "host/libs/vm_manager/crosvm_manager.h" |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 17 | #include "host/libs/vm_manager/qemu_manager.h" |
| 18 | #include "host/libs/vm_manager/vm_manager.h" |
| 19 | |
| 20 | using vsoc::GetPerInstanceDefault; |
| 21 | using cvd::LauncherExitCodes; |
| 22 | |
| 23 | DEFINE_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"); |
| 27 | DEFINE_string(cache_image, "", "Location of the cache partition image."); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 28 | DEFINE_string(metadata_image, "", "Location of the metadata partition image " |
| 29 | "to be generated."); |
| 30 | DEFINE_int32(blank_metadata_image_mb, 16, |
| 31 | "The size of the blank metadata image to generate, MB."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 32 | DEFINE_int32(cpus, 2, "Virtual CPU count."); |
| 33 | DEFINE_string(data_image, "", "Location of the data partition image."); |
| 34 | DEFINE_string(data_policy, "use_existing", "How to handle userdata partition." |
| 35 | " Either 'use_existing', 'create_if_missing', 'resize_up_to', or " |
| 36 | "'always_create'."); |
| 37 | DEFINE_int32(blank_data_image_mb, 0, |
| 38 | "The size of the blank data image to generate, MB."); |
| 39 | DEFINE_string(blank_data_image_fmt, "ext4", |
| 40 | "The fs format for the blank data image. Used with mkfs."); |
| 41 | DEFINE_string(qemu_gdb, "", |
Matthias Maennich | 454d787 | 2019-02-06 16:35:17 +0000 | [diff] [blame] | 42 | "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 43 | |
| 44 | DEFINE_int32(x_res, 720, "Width of the screen in pixels"); |
| 45 | DEFINE_int32(y_res, 1280, "Height of the screen in pixels"); |
| 46 | DEFINE_int32(dpi, 160, "Pixels per inch for the screen"); |
| 47 | DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz"); |
| 48 | DEFINE_int32(num_screen_buffers, 3, "The number of screen buffers"); |
| 49 | DEFINE_string(kernel_path, "", |
| 50 | "Path to the kernel. Overrides the one from the boot image"); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 51 | DEFINE_bool(decompress_kernel, false, |
| 52 | "Whether to decompress the kernel image. Required for crosvm."); |
| 53 | DEFINE_string(kernel_decompresser_executable, |
| 54 | vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"), |
| 55 | "Path to the extract-vmlinux executable."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 56 | DEFINE_string(extra_kernel_cmdline, "", |
| 57 | "Additional flags to put on the kernel command line"); |
| 58 | DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions"); |
| 59 | DEFINE_string(console, "ttyS0", "Console device for the guest kernel."); |
| 60 | DEFINE_string(androidboot_console, "ttyS1", |
| 61 | "Console device for the Android framework"); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 62 | DEFINE_string( |
| 63 | hardware_name, "", |
Jorge E. Moreira | fd1a6b0 | 2019-02-27 16:25:00 -0800 | [diff] [blame] | 64 | "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 65 | DEFINE_string(guest_security, "selinux", |
| 66 | "The security module to use in the guest"); |
Jorge E. Moreira | 300f97f | 2019-04-09 13:48:01 -0700 | [diff] [blame] | 67 | DEFINE_bool(guest_enforce_security, true, |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 68 | "Whether to run in enforcing mode (non permissive). Ignored if " |
| 69 | "-guest_security is empty."); |
| 70 | DEFINE_bool(guest_audit_security, true, |
| 71 | "Whether to log security audits."); |
| 72 | DEFINE_string(boot_image, "", "Location of cuttlefish boot image."); |
| 73 | DEFINE_int32(memory_mb, 2048, |
| 74 | "Total amount of memory available for guest, MB."); |
| 75 | std::string g_default_mempath{vsoc::GetDefaultMempath()}; |
| 76 | DEFINE_string(mempath, g_default_mempath.c_str(), |
| 77 | "Target location for the shmem file."); |
| 78 | DEFINE_string(mobile_interface, "", // default handled on ParseCommandLine |
| 79 | "Network interface to use for mobile networking"); |
| 80 | DEFINE_string(mobile_tap_name, "", // default handled on ParseCommandLine |
| 81 | "The name of the tap interface to use for mobile"); |
| 82 | std::string g_default_serial_number{GetPerInstanceDefault("CUTTLEFISHCVD")}; |
| 83 | DEFINE_string(serial_number, g_default_serial_number.c_str(), |
| 84 | "Serial number to use for the device"); |
| 85 | DEFINE_string(instance_dir, "", // default handled on ParseCommandLine |
| 86 | "A directory to put all instance specific files"); |
| 87 | DEFINE_string( |
| 88 | vm_manager, vm_manager::QemuManager::name(), |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 89 | "What virtual machine manager to use, one of {qemu_cli, crosvm}"); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 90 | DEFINE_string( |
| 91 | gpu_mode, vsoc::kGpuModeGuestAshmem, |
| 92 | "What gpu configuration to use, one of {guest_ashmem, guest_drm}"); |
| 93 | DEFINE_string(wayland_socket, "", |
| 94 | "Location of the wayland socket to use for *_drm gpu_modes."); |
| 95 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 96 | DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""), |
| 97 | "Location of the system partition images."); |
| 98 | DEFINE_string(vendor_image, "", "Location of the vendor partition image."); |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 99 | DEFINE_string(product_image, "", "Location of the product partition image."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 100 | |
| 101 | DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to" |
| 102 | " host kernel. This is only used during transition of our clients." |
| 103 | " Will be deprecated soon."); |
| 104 | DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process."); |
| 105 | DEFINE_string(vnc_server_binary, |
| 106 | vsoc::DefaultHostArtifactsPath("bin/vnc_server"), |
| 107 | "Location of the vnc server binary."); |
Alistair Strachan | 7eeddf4 | 2019-03-04 18:13:34 -0800 | [diff] [blame] | 108 | DEFINE_bool(start_stream_audio, false, |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 109 | "Whether to start the stream audio process."); |
| 110 | DEFINE_string(stream_audio_binary, |
| 111 | vsoc::DefaultHostArtifactsPath("bin/stream_audio"), |
| 112 | "Location of the stream_audio binary."); |
| 113 | DEFINE_string(virtual_usb_manager_binary, |
| 114 | vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager"), |
| 115 | "Location of the virtual usb manager binary."); |
| 116 | DEFINE_string(kernel_log_monitor_binary, |
| 117 | vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"), |
| 118 | "Location of the log monitor binary."); |
| 119 | DEFINE_string(ivserver_binary, |
| 120 | vsoc::DefaultHostArtifactsPath("bin/ivserver"), |
| 121 | "Location of the ivshmem server binary."); |
| 122 | DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444), |
| 123 | "The port on which the vnc server should listen"); |
| 124 | DEFINE_int32(stream_audio_port, GetPerInstanceDefault(7444), |
| 125 | "The port on which stream_audio should listen."); |
| 126 | DEFINE_string(socket_forward_proxy_binary, |
| 127 | vsoc::DefaultHostArtifactsPath("bin/socket_forward_proxy"), |
| 128 | "Location of the socket_forward_proxy binary."); |
| 129 | DEFINE_string(socket_vsock_proxy_binary, |
| 130 | vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"), |
| 131 | "Location of the socket_vsock_proxy binary."); |
Cody Schuffelen | 1074333 | 2019-04-15 16:50:49 -0700 | [diff] [blame] | 132 | DEFINE_string(adb_mode, "vsock_half_tunnel", |
Cody Schuffelen | 63d1005 | 2019-02-26 12:21:53 -0800 | [diff] [blame] | 133 | "Mode for ADB connection. Can be 'usb' for USB forwarding, " |
| 134 | "'tunnel' for a TCP connection tunneled through VSoC, " |
| 135 | "'vsock_tunnel' for a TCP connection tunneled through vsock, " |
| 136 | "'native_vsock' for a direct connection to the guest ADB over " |
| 137 | "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to " |
| 138 | "the guest ADB server, or a comma separated list of types as in " |
| 139 | "'usb,tunnel'"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 140 | DEFINE_bool(run_adb_connector, true, |
| 141 | "Maintain adb connection by sending 'adb connect' commands to the " |
Matthias Maennich | 454d787 | 2019-02-06 16:35:17 +0000 | [diff] [blame] | 142 | "server. Only relevant with -adb_mode=tunnel or vsock_tunnel"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 143 | DEFINE_string(adb_connector_binary, |
| 144 | vsoc::DefaultHostArtifactsPath("bin/adb_connector"), |
| 145 | "Location of the adb_connector binary. Only relevant if " |
Matthias Maennich | 454d787 | 2019-02-06 16:35:17 +0000 | [diff] [blame] | 146 | "-run_adb_connector is true"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 147 | DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb"); |
| 148 | DEFINE_string(guest_mac_address, |
| 149 | GetPerInstanceDefault("00:43:56:44:80:"), // 00:43:56:44:80:0x |
| 150 | "MAC address of the wifi interface to be created on the guest."); |
| 151 | DEFINE_string(host_mac_address, |
| 152 | "42:00:00:00:00:00", |
| 153 | "MAC address of the wifi interface running on the host."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 154 | DEFINE_string(wifi_tap_name, "", // default handled on ParseCommandLine |
| 155 | "The name of the tap interface to use for wifi"); |
| 156 | DEFINE_int32(vsock_guest_cid, |
| 157 | vsoc::GetDefaultPerInstanceVsockCid(), |
| 158 | "Guest identifier for vsock. Disabled if under 3."); |
| 159 | |
| 160 | // TODO(b/72969289) This should be generated |
| 161 | DEFINE_string(dtb, "", "Path to the cuttlefish.dtb file"); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 162 | DEFINE_string(gsi_fstab, |
| 163 | vsoc::DefaultHostArtifactsPath("config/gsi.fstab"), |
| 164 | "Path to the GSI fstab file"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 165 | |
| 166 | DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix), |
| 167 | "UUID to use for the device. Random if not specified"); |
| 168 | DEFINE_bool(daemon, false, |
| 169 | "Run cuttlefish in background, the launcher exits on boot " |
| 170 | "completed/failed"); |
| 171 | |
| 172 | DEFINE_string(device_title, "", "Human readable name for the instance, " |
| 173 | "used by the vnc_server for its server title"); |
| 174 | DEFINE_string(setupwizard_mode, "DISABLED", |
| 175 | "One of DISABLED,OPTIONAL,REQUIRED"); |
| 176 | |
| 177 | DEFINE_string(qemu_binary, |
| 178 | "/usr/bin/qemu-system-x86_64", |
| 179 | "The qemu binary to use"); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 180 | DEFINE_string(crosvm_binary, |
| 181 | vsoc::DefaultHostArtifactsPath("bin/crosvm"), |
| 182 | "The Crosvm binary to use"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 183 | DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process"); |
| 184 | DEFINE_bool(run_e2e_test, true, "Run e2e test after device launches"); |
| 185 | DEFINE_string(e2e_test_binary, |
| 186 | vsoc::DefaultHostArtifactsPath("bin/host_region_e2e_test"), |
| 187 | "Location of the region end to end test binary"); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 188 | DEFINE_string(logcat_receiver_binary, |
| 189 | vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"), |
| 190 | "Binary for the logcat server"); |
| 191 | DEFINE_string(logcat_mode, "", "How to send android's log messages from " |
| 192 | "guest to host. One of [serial, vsock]"); |
| 193 | DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620), |
| 194 | "The port for logcat over vsock"); |
Jorge E. Moreira | f1f7cb3 | 2019-04-15 18:44:41 -0700 | [diff] [blame] | 195 | DEFINE_string(config_server_binary, |
| 196 | vsoc::DefaultHostArtifactsPath("bin/config_server"), |
| 197 | "Binary for the configuration server"); |
| 198 | DEFINE_int32(config_server_port, vsoc::GetPerInstanceDefault(4680), |
| 199 | "The (vsock) port for the configuration server"); |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 200 | DEFINE_int32(frames_vsock_port, vsoc::GetPerInstanceDefault(5580), |
| 201 | "The vsock port to receive frames from the guest on"); |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame^] | 202 | DEFINE_bool(enable_tombstone_receiver, false, "Enables the tombstone logger on " |
| 203 | "both the guest and the host"); |
| 204 | DEFINE_string(tombstone_receiver_binary, |
| 205 | vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"), |
| 206 | "Binary for the tombstone server"); |
| 207 | DEFINE_int32(tombstone_receiver_port, vsoc::GetPerInstanceDefault(5630), |
| 208 | "The vsock port for tombstones"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 209 | namespace { |
| 210 | |
| 211 | template<typename S, typename T> |
| 212 | static std::string concat(const S& s, const T& t) { |
| 213 | std::ostringstream os; |
| 214 | os << s << t; |
| 215 | return os.str(); |
| 216 | } |
| 217 | |
| 218 | bool ResolveInstanceFiles() { |
| 219 | if (FLAGS_system_image_dir.empty()) { |
| 220 | LOG(ERROR) << "--system_image_dir must be specified."; |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | // If user did not specify location of either of these files, expect them to |
| 225 | // be placed in --system_image_dir location. |
| 226 | std::string default_system_image = FLAGS_system_image_dir + "/system.img"; |
| 227 | SetCommandLineOptionWithMode("system_image", default_system_image.c_str(), |
| 228 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 229 | std::string default_boot_image = FLAGS_system_image_dir + "/boot.img"; |
| 230 | SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(), |
| 231 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 232 | std::string default_cache_image = FLAGS_system_image_dir + "/cache.img"; |
| 233 | SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(), |
| 234 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 235 | std::string default_data_image = FLAGS_system_image_dir + "/userdata.img"; |
| 236 | SetCommandLineOptionWithMode("data_image", default_data_image.c_str(), |
| 237 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 238 | std::string default_vendor_image = FLAGS_system_image_dir + "/vendor.img"; |
| 239 | SetCommandLineOptionWithMode("vendor_image", default_vendor_image.c_str(), |
| 240 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 241 | std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img"; |
| 242 | SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(), |
| 243 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 244 | std::string default_product_image = FLAGS_system_image_dir + "/product.img"; |
| 245 | SetCommandLineOptionWithMode("product_image", default_product_image.c_str(), |
| 246 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 247 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | |
| 251 | std::string GetCuttlefishEnvPath() { |
| 252 | return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh"; |
| 253 | } |
| 254 | |
| 255 | // Initializes the config object and saves it to file. It doesn't return it, all |
| 256 | // further uses of the config should happen through the singleton |
| 257 | bool InitializeCuttlefishConfiguration( |
| 258 | const cvd::BootImageUnpacker& boot_image_unpacker) { |
| 259 | vsoc::CuttlefishConfig tmp_config_obj; |
| 260 | auto& memory_layout = *vsoc::VSoCMemoryLayout::Get(); |
| 261 | // Set this first so that calls to PerInstancePath below are correct |
| 262 | tmp_config_obj.set_instance_dir(FLAGS_instance_dir); |
| 263 | if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) { |
| 264 | LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager; |
| 265 | return false; |
| 266 | } |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 267 | if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) { |
| 268 | LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager; |
| 269 | return false; |
| 270 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 271 | tmp_config_obj.set_vm_manager(FLAGS_vm_manager); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 272 | tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode); |
| 273 | if (!vm_manager::VmManager::ConfigureGpuMode(&tmp_config_obj)) { |
| 274 | LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode << |
| 275 | " does not work with vm_manager=" << FLAGS_vm_manager; |
| 276 | return false; |
| 277 | } |
| 278 | tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 279 | |
| 280 | tmp_config_obj.set_serial_number(FLAGS_serial_number); |
| 281 | |
| 282 | tmp_config_obj.set_cpus(FLAGS_cpus); |
| 283 | tmp_config_obj.set_memory_mb(FLAGS_memory_mb); |
| 284 | |
| 285 | tmp_config_obj.set_dpi(FLAGS_dpi); |
| 286 | tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode); |
| 287 | tmp_config_obj.set_x_res(FLAGS_x_res); |
| 288 | tmp_config_obj.set_y_res(FLAGS_y_res); |
| 289 | tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers); |
| 290 | tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz); |
| 291 | tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb); |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 292 | std::vector<std::string> adb = cvd::StrSplit(FLAGS_adb_mode, ','); |
| 293 | tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end())); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 294 | tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort())); |
| 295 | |
| 296 | tmp_config_obj.set_device_title(FLAGS_device_title); |
| 297 | if (FLAGS_kernel_path.size()) { |
| 298 | tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path); |
| 299 | tmp_config_obj.set_use_unpacked_kernel(false); |
| 300 | } else { |
| 301 | tmp_config_obj.set_kernel_image_path( |
| 302 | tmp_config_obj.PerInstancePath("kernel")); |
| 303 | tmp_config_obj.set_use_unpacked_kernel(true); |
| 304 | } |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 305 | tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel); |
| 306 | if (FLAGS_decompress_kernel) { |
| 307 | tmp_config_obj.set_decompressed_kernel_image_path( |
| 308 | tmp_config_obj.PerInstancePath("vmlinux")); |
| 309 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 310 | |
| 311 | auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img"); |
| 312 | bool use_ramdisk = boot_image_unpacker.HasRamdiskImage(); |
| 313 | if (!use_ramdisk) { |
| 314 | LOG(INFO) << "No ramdisk present; assuming system-as-root build"; |
| 315 | ramdisk_path = ""; |
| 316 | } |
| 317 | |
| 318 | // This needs to be done here because the dtb path depends on the presence of |
| 319 | // the ramdisk |
| 320 | if (FLAGS_dtb.empty()) { |
| 321 | if (use_ramdisk) { |
| 322 | FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/initrd-root.dtb"); |
| 323 | } else { |
| 324 | FLAGS_dtb = vsoc::DefaultHostArtifactsPath("config/system-root.dtb"); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline()); |
| 329 | if (!use_ramdisk) { |
| 330 | tmp_config_obj.add_kernel_cmdline("root=/dev/vda"); |
| 331 | } |
| 332 | tmp_config_obj.add_kernel_cmdline("init=/init"); |
| 333 | tmp_config_obj.add_kernel_cmdline( |
| 334 | concat("androidboot.serialno=", FLAGS_serial_number)); |
| 335 | tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0"); |
| 336 | tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi)); |
| 337 | tmp_config_obj.add_kernel_cmdline( |
| 338 | concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode)); |
| 339 | tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part)); |
| 340 | if (!FLAGS_console.empty()) { |
| 341 | tmp_config_obj.add_kernel_cmdline(concat("console=", FLAGS_console)); |
| 342 | } |
| 343 | if (!FLAGS_androidboot_console.empty()) { |
| 344 | tmp_config_obj.add_kernel_cmdline( |
| 345 | concat("androidboot.console=", FLAGS_androidboot_console)); |
| 346 | } |
| 347 | if (!FLAGS_hardware_name.empty()) { |
| 348 | tmp_config_obj.add_kernel_cmdline( |
| 349 | concat("androidboot.hardware=", FLAGS_hardware_name)); |
| 350 | } |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 351 | if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) { |
| 352 | tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=", |
| 353 | FLAGS_logcat_vsock_port)); |
| 354 | } |
Jorge E. Moreira | f1f7cb3 | 2019-04-15 18:44:41 -0700 | [diff] [blame] | 355 | tmp_config_obj.add_kernel_cmdline(concat("androidboot.cuttlefish_config_server_port=", |
| 356 | FLAGS_config_server_port)); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 357 | tmp_config_obj.set_hardware_name(FLAGS_hardware_name); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 358 | if (!FLAGS_guest_security.empty()) { |
| 359 | tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security)); |
| 360 | if (FLAGS_guest_enforce_security) { |
| 361 | tmp_config_obj.add_kernel_cmdline("enforcing=1"); |
| 362 | } else { |
| 363 | tmp_config_obj.add_kernel_cmdline("enforcing=0"); |
| 364 | tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive"); |
| 365 | } |
| 366 | if (FLAGS_guest_audit_security) { |
| 367 | tmp_config_obj.add_kernel_cmdline("audit=1"); |
| 368 | } else { |
| 369 | tmp_config_obj.add_kernel_cmdline("audit=0"); |
| 370 | } |
| 371 | } |
| 372 | if (FLAGS_run_e2e_test) { |
| 373 | tmp_config_obj.add_kernel_cmdline("androidboot.vsoc_e2e_test=1"); |
| 374 | } |
| 375 | if (FLAGS_extra_kernel_cmdline.size()) { |
| 376 | tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline); |
| 377 | } |
| 378 | |
| 379 | tmp_config_obj.set_ramdisk_image_path(ramdisk_path); |
| 380 | tmp_config_obj.set_system_image_path(FLAGS_system_image); |
| 381 | tmp_config_obj.set_cache_image_path(FLAGS_cache_image); |
| 382 | tmp_config_obj.set_data_image_path(FLAGS_data_image); |
| 383 | tmp_config_obj.set_vendor_image_path(FLAGS_vendor_image); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 384 | tmp_config_obj.set_metadata_image_path(FLAGS_metadata_image); |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 385 | tmp_config_obj.set_product_image_path(FLAGS_product_image); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 386 | tmp_config_obj.set_dtb_path(FLAGS_dtb); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 387 | tmp_config_obj.set_gsi_fstab_path(FLAGS_gsi_fstab); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 388 | |
| 389 | tmp_config_obj.set_mempath(FLAGS_mempath); |
| 390 | tmp_config_obj.set_ivshmem_qemu_socket_path( |
| 391 | tmp_config_obj.PerInstancePath("ivshmem_socket_qemu")); |
| 392 | tmp_config_obj.set_ivshmem_client_socket_path( |
| 393 | tmp_config_obj.PerInstancePath("ivshmem_socket_client")); |
| 394 | tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size()); |
| 395 | |
| 396 | if (AdbUsbEnabled(tmp_config_obj)) { |
| 397 | tmp_config_obj.set_usb_v1_socket_name(tmp_config_obj.PerInstancePath("usb-v1")); |
| 398 | tmp_config_obj.set_vhci_port(FLAGS_vhci_port); |
| 399 | tmp_config_obj.set_usb_ip_socket_name(tmp_config_obj.PerInstancePath("usb-ip")); |
| 400 | } |
| 401 | |
| 402 | tmp_config_obj.set_kernel_log_socket_name(tmp_config_obj.PerInstancePath("kernel-log")); |
| 403 | tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed); |
| 404 | tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console")); |
| 405 | tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat")); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 406 | tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary); |
Jorge E. Moreira | f1f7cb3 | 2019-04-15 18:44:41 -0700 | [diff] [blame] | 407 | tmp_config_obj.set_config_server_binary(FLAGS_config_server_binary); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 408 | tmp_config_obj.set_launcher_log_path(tmp_config_obj.PerInstancePath("launcher.log")); |
| 409 | tmp_config_obj.set_launcher_monitor_socket_path( |
| 410 | tmp_config_obj.PerInstancePath("launcher_monitor.sock")); |
| 411 | |
| 412 | tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface); |
| 413 | tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name); |
| 414 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 415 | tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name); |
| 416 | |
| 417 | tmp_config_obj.set_wifi_guest_mac_addr(FLAGS_guest_mac_address); |
| 418 | tmp_config_obj.set_wifi_host_mac_addr(FLAGS_host_mac_address); |
| 419 | |
| 420 | tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid); |
| 421 | |
| 422 | tmp_config_obj.set_entropy_source("/dev/urandom"); |
| 423 | tmp_config_obj.set_uuid(FLAGS_uuid); |
| 424 | |
| 425 | tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 426 | tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 427 | tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary); |
| 428 | tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary); |
| 429 | |
| 430 | tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server); |
| 431 | tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary); |
| 432 | tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port); |
| 433 | |
| 434 | tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio); |
| 435 | tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary); |
| 436 | tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port); |
| 437 | |
| 438 | tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses); |
| 439 | tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector); |
| 440 | tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary); |
| 441 | tmp_config_obj.set_virtual_usb_manager_binary( |
| 442 | FLAGS_virtual_usb_manager_binary); |
| 443 | tmp_config_obj.set_socket_forward_proxy_binary( |
| 444 | FLAGS_socket_forward_proxy_binary); |
| 445 | tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary); |
| 446 | tmp_config_obj.set_run_as_daemon(FLAGS_daemon); |
| 447 | tmp_config_obj.set_run_e2e_test(FLAGS_run_e2e_test); |
| 448 | tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary); |
| 449 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 450 | tmp_config_obj.set_data_policy(FLAGS_data_policy); |
| 451 | tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb); |
| 452 | tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt); |
| 453 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 454 | if(!AdbUsbEnabled(tmp_config_obj)) { |
| 455 | tmp_config_obj.disable_usb_adb(); |
| 456 | } |
| 457 | |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 458 | tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode); |
| 459 | tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port); |
Jorge E. Moreira | f1f7cb3 | 2019-04-15 18:44:41 -0700 | [diff] [blame] | 460 | tmp_config_obj.set_config_server_port(FLAGS_config_server_port); |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 461 | tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port); |
| 462 | if (!tmp_config_obj.enable_ivserver()) { |
| 463 | tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_frames_port=", |
| 464 | FLAGS_frames_vsock_port)); |
| 465 | } |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 466 | |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame^] | 467 | tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver); |
| 468 | tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port); |
| 469 | tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary); |
| 470 | if (FLAGS_enable_tombstone_receiver) { |
| 471 | tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=1"); |
| 472 | tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_tombstone_port=" |
| 473 | ,FLAGS_tombstone_receiver_port)); |
| 474 | // TODO (b/128842613) populate a cid flag to read the host CID during |
| 475 | // runtime |
| 476 | } else { |
| 477 | tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=0"); |
| 478 | } |
| 479 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 480 | tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath()); |
| 481 | |
| 482 | auto config_file = GetConfigFilePath(tmp_config_obj); |
| 483 | auto config_link = vsoc::GetGlobalConfigFileLink(); |
| 484 | // Save the config object before starting any host process |
| 485 | if (!tmp_config_obj.SaveToFile(config_file)) { |
| 486 | LOG(ERROR) << "Unable to save config object"; |
| 487 | return false; |
| 488 | } |
| 489 | setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true); |
| 490 | if (symlink(config_file.c_str(), config_link.c_str()) != 0) { |
| 491 | LOG(ERROR) << "Failed to create symlink to config file at " << config_link |
| 492 | << ": " << strerror(errno); |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | void SetDefaultFlagsForQemu() { |
| 500 | auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-"); |
| 501 | SetCommandLineOptionWithMode("mobile_interface", |
| 502 | default_mobile_interface.c_str(), |
| 503 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 504 | auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-"); |
| 505 | SetCommandLineOptionWithMode("mobile_tap_name", |
| 506 | default_mobile_tap_name.c_str(), |
| 507 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 508 | auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-"); |
| 509 | SetCommandLineOptionWithMode("wifi_tap_name", |
| 510 | default_wifi_tap_name.c_str(), |
| 511 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 512 | auto default_instance_dir = |
| 513 | cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime"; |
| 514 | SetCommandLineOptionWithMode("instance_dir", |
| 515 | default_instance_dir.c_str(), |
| 516 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | fd1a6b0 | 2019-02-27 16:25:00 -0800 | [diff] [blame] | 517 | SetCommandLineOptionWithMode("hardware_name", "cutf_ivsh", |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 518 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 519 | SetCommandLineOptionWithMode("decompress_kernel", "false", |
| 520 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 521 | SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatSerialMode, |
| 522 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void SetDefaultFlagsForCrosvm() { |
| 526 | auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-"); |
| 527 | SetCommandLineOptionWithMode("mobile_interface", |
| 528 | default_mobile_interface.c_str(), |
| 529 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 530 | auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-"); |
| 531 | SetCommandLineOptionWithMode("mobile_tap_name", |
| 532 | default_mobile_tap_name.c_str(), |
| 533 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 534 | auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-"); |
| 535 | SetCommandLineOptionWithMode("wifi_tap_name", |
| 536 | default_wifi_tap_name.c_str(), |
| 537 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 538 | auto default_instance_dir = |
| 539 | cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime"; |
| 540 | SetCommandLineOptionWithMode("instance_dir", |
| 541 | default_instance_dir.c_str(), |
| 542 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 543 | auto default_wayland_socket = vsoc::DefaultEnvironmentPath( |
| 544 | "XDG_RUNTIME_DIR", default_instance_dir.c_str(), "wayland-0"); |
| 545 | SetCommandLineOptionWithMode("wayland_socket", |
| 546 | default_wayland_socket.c_str(), |
| 547 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | fd1a6b0 | 2019-02-27 16:25:00 -0800 | [diff] [blame] | 548 | SetCommandLineOptionWithMode("hardware_name", "cutf_cvm", |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 549 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 550 | SetCommandLineOptionWithMode("decompress_kernel", "true", |
| 551 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | 84d93c1 | 2019-02-05 12:02:29 -0800 | [diff] [blame] | 552 | SetCommandLineOptionWithMode("run_e2e_test", "false", |
| 553 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 554 | SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode, |
| 555 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | bool ParseCommandLineFlags(int* argc, char*** argv) { |
| 559 | // The config_file is created by the launcher, so the launcher is the only |
| 560 | // host process that doesn't use the flag. |
| 561 | // Set the default to empty. |
| 562 | google::SetCommandLineOptionWithMode("config_file", "", |
| 563 | gflags::SET_FLAGS_DEFAULT); |
| 564 | google::ParseCommandLineNonHelpFlags(argc, argv, true); |
| 565 | bool invalid_manager = false; |
| 566 | if (FLAGS_vm_manager == vm_manager::QemuManager::name()) { |
| 567 | SetDefaultFlagsForQemu(); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 568 | } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) { |
| 569 | SetDefaultFlagsForCrosvm(); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 570 | } else { |
| 571 | std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager |
| 572 | << std::endl; |
| 573 | invalid_manager = true; |
| 574 | } |
| 575 | google::HandleCommandLineHelpFlags(); |
| 576 | if (invalid_manager) { |
| 577 | return false; |
| 578 | } |
| 579 | // Set the env variable to empty (in case the caller passed a value for it). |
| 580 | unsetenv(vsoc::kCuttlefishConfigEnvVarName); |
| 581 | |
| 582 | return ResolveInstanceFiles(); |
| 583 | } |
| 584 | |
| 585 | bool CleanPriorFiles() { |
| 586 | // Everything on the instance directory |
| 587 | std::string prior_files = FLAGS_instance_dir + "/*"; |
| 588 | // The shared memory file |
| 589 | prior_files += " " + FLAGS_mempath; |
| 590 | // The environment file |
| 591 | prior_files += " " + GetCuttlefishEnvPath(); |
| 592 | // The global link to the config file |
| 593 | prior_files += " " + vsoc::GetGlobalConfigFileLink(); |
| 594 | LOG(INFO) << "Assuming prior files of " << prior_files; |
| 595 | std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null"; |
| 596 | int rval = std::system(fuser_cmd.c_str()); |
| 597 | // fuser returns 0 if any of the files are open |
| 598 | if (WEXITSTATUS(rval) == 0) { |
| 599 | LOG(ERROR) << "Clean aborted: files are in use"; |
| 600 | return false; |
| 601 | } |
| 602 | std::string clean_command = "rm -rf " + prior_files; |
| 603 | rval = std::system(clean_command.c_str()); |
| 604 | if (WEXITSTATUS(rval) != 0) { |
| 605 | LOG(ERROR) << "Remove of files failed"; |
| 606 | return false; |
| 607 | } |
| 608 | return true; |
| 609 | } |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 610 | |
| 611 | bool DecompressKernel(const std::string& src, const std::string& dst) { |
| 612 | cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable); |
| 613 | decomp_cmd.AddParameter(src); |
| 614 | auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666); |
| 615 | if (!output_file->IsOpen()) { |
| 616 | LOG(ERROR) << "Unable to create decompressed image file: " |
| 617 | << output_file->StrError(); |
| 618 | return false; |
| 619 | } |
| 620 | decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file); |
| 621 | auto decomp_proc = decomp_cmd.Start(false); |
| 622 | return decomp_proc.Started() && decomp_proc.Wait() == 0; |
| 623 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 624 | } // namespace |
| 625 | |
| 626 | vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(int* argc, char*** argv) { |
| 627 | if (!ParseCommandLineFlags(argc, argv)) { |
| 628 | LOG(ERROR) << "Failed to parse command arguments"; |
| 629 | exit(LauncherExitCodes::kArgumentParsingError); |
| 630 | } |
| 631 | |
| 632 | // Clean up prior files before saving the config file (doing it after would |
| 633 | // delete it) |
| 634 | if (!CleanPriorFiles()) { |
| 635 | LOG(ERROR) << "Failed to clean prior files"; |
| 636 | exit(LauncherExitCodes::kPrioFilesCleanupError); |
| 637 | } |
| 638 | // Create instance directory if it doesn't exist. |
| 639 | if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) { |
| 640 | LOG(INFO) << "Setting up " << FLAGS_instance_dir; |
| 641 | if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) { |
| 642 | LOG(ERROR) << "Failed to create instance directory: " |
| 643 | << FLAGS_instance_dir << ". Error: " << errno; |
| 644 | exit(LauncherExitCodes::kInstanceDirCreationError); |
| 645 | } |
| 646 | } |
| 647 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 648 | if (!cvd::FileHasContent(FLAGS_boot_image)) { |
| 649 | LOG(ERROR) << "File not found: " << FLAGS_boot_image; |
| 650 | exit(cvd::kCuttlefishConfigurationInitError); |
| 651 | } |
| 652 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 653 | auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image); |
| 654 | |
| 655 | if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) { |
| 656 | LOG(ERROR) << "Failed to initialize configuration"; |
| 657 | exit(LauncherExitCodes::kCuttlefishConfigurationInitError); |
| 658 | } |
| 659 | // Do this early so that the config object is ready for anything that needs it |
| 660 | auto config = vsoc::CuttlefishConfig::Get(); |
| 661 | if (!config) { |
| 662 | LOG(ERROR) << "Failed to obtain config singleton"; |
| 663 | exit(LauncherExitCodes::kCuttlefishConfigurationInitError); |
| 664 | } |
| 665 | |
| 666 | if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(), |
| 667 | config->use_unpacked_kernel() |
| 668 | ? config->kernel_image_path() |
| 669 | : "")) { |
| 670 | LOG(ERROR) << "Failed to unpack boot image"; |
| 671 | exit(LauncherExitCodes::kBootImageUnpackError); |
| 672 | } |
| 673 | |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 674 | if (config->decompress_kernel()) { |
| 675 | if (!DecompressKernel(config->kernel_image_path(), |
| 676 | config->decompressed_kernel_image_path())) { |
| 677 | LOG(ERROR) << "Failed to decompress kernel"; |
| 678 | exit(LauncherExitCodes::kKernelDecompressError); |
| 679 | } |
| 680 | } |
| 681 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 682 | ValidateAdbModeFlag(*config); |
| 683 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 684 | // Create data if necessary |
| 685 | if (!ApplyDataImagePolicy(*config)) { |
| 686 | exit(cvd::kCuttlefishConfigurationInitError); |
| 687 | } |
| 688 | |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 689 | CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none"); |
| 690 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 691 | // Check that the files exist |
| 692 | for (const auto& file : |
| 693 | {config->system_image_path(), config->vendor_image_path(), |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 694 | config->cache_image_path(), config->data_image_path(), |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 695 | config->metadata_image_path(), config->product_image_path()}) { |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 696 | if (!cvd::FileHasContent(file.c_str())) { |
| 697 | LOG(ERROR) << "File not found: " << file; |
| 698 | exit(cvd::kCuttlefishConfigurationInitError); |
| 699 | } |
| 700 | } |
| 701 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 702 | return config; |
| 703 | } |
| 704 | |
| 705 | std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) { |
| 706 | return config.PerInstancePath("cuttlefish_config.json"); |
| 707 | } |