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