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