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