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