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 | |
Cody Schuffelen | f4a1cdb | 2019-11-13 16:51:16 -0800 | [diff] [blame] | 6 | #include <android-base/strings.h> |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 7 | #include <gflags/gflags.h> |
| 8 | #include <glog/logging.h> |
| 9 | |
| 10 | #include "common/libs/utils/environment.h" |
| 11 | #include "common/libs/utils/files.h" |
Cody Schuffelen | 147b88e | 2019-09-09 16:00:11 -0700 | [diff] [blame] | 12 | #include "host/commands/assemble_cvd/boot_image_unpacker.h" |
| 13 | #include "host/commands/assemble_cvd/data_image.h" |
| 14 | #include "host/commands/assemble_cvd/image_aggregator.h" |
| 15 | #include "host/commands/assemble_cvd/assembler_defs.h" |
Cody Schuffelen | b737ef5 | 2019-09-25 14:54:38 -0700 | [diff] [blame] | 16 | #include "host/commands/assemble_cvd/super_image_mixer.h" |
Cody Schuffelen | 605e685 | 2019-10-16 16:22:24 -0700 | [diff] [blame] | 17 | #include "host/libs/config/fetcher_config.h" |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 18 | #include "host/libs/vm_manager/crosvm_manager.h" |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 19 | #include "host/libs/vm_manager/qemu_manager.h" |
| 20 | #include "host/libs/vm_manager/vm_manager.h" |
| 21 | |
| 22 | using vsoc::GetPerInstanceDefault; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 23 | using cvd::AssemblerExitCodes; |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 24 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 25 | DEFINE_string(cache_image, "", "Location of the cache partition image."); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 26 | DEFINE_string(metadata_image, "", "Location of the metadata partition image " |
| 27 | "to be generated."); |
| 28 | DEFINE_int32(blank_metadata_image_mb, 16, |
| 29 | "The size of the blank metadata image to generate, MB."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 30 | DEFINE_int32(cpus, 2, "Virtual CPU count."); |
| 31 | DEFINE_string(data_image, "", "Location of the data partition image."); |
| 32 | DEFINE_string(data_policy, "use_existing", "How to handle userdata partition." |
| 33 | " Either 'use_existing', 'create_if_missing', 'resize_up_to', or " |
| 34 | "'always_create'."); |
| 35 | DEFINE_int32(blank_data_image_mb, 0, |
| 36 | "The size of the blank data image to generate, MB."); |
Cody Schuffelen | bdca7b8 | 2019-12-04 20:09:22 +0000 | [diff] [blame] | 37 | DEFINE_string(blank_data_image_fmt, "ext4", |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 38 | "The fs format for the blank data image. Used with mkfs."); |
| 39 | DEFINE_string(qemu_gdb, "", |
Matthias Maennich | 454d787 | 2019-02-06 16:35:17 +0000 | [diff] [blame] | 40 | "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 41 | |
| 42 | DEFINE_int32(x_res, 720, "Width of the screen in pixels"); |
| 43 | DEFINE_int32(y_res, 1280, "Height of the screen in pixels"); |
| 44 | DEFINE_int32(dpi, 160, "Pixels per inch for the screen"); |
| 45 | DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 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."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 51 | DEFINE_string(extra_kernel_cmdline, "", |
| 52 | "Additional flags to put on the kernel command line"); |
| 53 | DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions"); |
Jorge E. Moreira | 300f97f | 2019-04-09 13:48:01 -0700 | [diff] [blame] | 54 | DEFINE_bool(guest_enforce_security, true, |
Cody Schuffelen | 79d4c19 | 2019-12-04 15:13:26 -0800 | [diff] [blame] | 55 | "Whether to run in enforcing mode (non permissive)."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 56 | DEFINE_bool(guest_audit_security, true, |
| 57 | "Whether to log security audits."); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 58 | DEFINE_string(boot_image, "", |
| 59 | "Location of cuttlefish boot image. If empty it is assumed to be " |
| 60 | "boot.img in the directory specified by -system_image_dir."); |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 61 | DEFINE_string(vendor_boot_image, "", |
| 62 | "Location of cuttlefish vendor boot image. If empty it is assumed to " |
| 63 | "be vendor_boot.img in the directory specified by -system_image_dir."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 64 | DEFINE_int32(memory_mb, 2048, |
| 65 | "Total amount of memory available for guest, MB."); |
Cody Schuffelen | 7a18451 | 2019-10-30 16:52:55 -0700 | [diff] [blame] | 66 | DEFINE_string(mobile_interface, GetPerInstanceDefault("cvd-mbr-"), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 67 | "Network interface to use for mobile networking"); |
Cody Schuffelen | 7a18451 | 2019-10-30 16:52:55 -0700 | [diff] [blame] | 68 | DEFINE_string(mobile_tap_name, GetPerInstanceDefault("cvd-mtap-"), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 69 | "The name of the tap interface to use for mobile"); |
Cody Schuffelen | 7a18451 | 2019-10-30 16:52:55 -0700 | [diff] [blame] | 70 | DEFINE_string(serial_number, GetPerInstanceDefault("CUTTLEFISHCVD"), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 71 | "Serial number to use for the device"); |
| 72 | DEFINE_string(instance_dir, "", // default handled on ParseCommandLine |
| 73 | "A directory to put all instance specific files"); |
| 74 | DEFINE_string( |
Alistair Strachan | 5ab095c | 2019-05-23 20:41:20 +0000 | [diff] [blame] | 75 | vm_manager, vm_manager::CrosvmManager::name(), |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 76 | "What virtual machine manager to use, one of {qemu_cli, crosvm}"); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 77 | DEFINE_string( |
Greg Hartman | bc1bed4 | 2019-04-05 20:02:00 -0700 | [diff] [blame] | 78 | gpu_mode, vsoc::kGpuModeGuestSwiftshader, |
| 79 | "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}"); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 80 | DEFINE_string(wayland_socket, "", |
Greg Hartman | bc1bed4 | 2019-04-05 20:02:00 -0700 | [diff] [blame] | 81 | "Location of the wayland socket to use for drm_virgl gpu_mode."); |
Greg Hartman | eb04ac5 | 2019-07-16 16:44:18 -0700 | [diff] [blame] | 82 | DEFINE_string(x_display, "", |
| 83 | "X display to use for drm_virgl gpu_mode."); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 84 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 85 | DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""), |
| 86 | "Location of the system partition images."); |
Alistair Strachan | 050ca93 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 87 | DEFINE_string(super_image, "", "Location of the super partition image."); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 88 | DEFINE_string(misc_image, "", |
| 89 | "Location of the misc partition image. If the image does not " |
| 90 | "exist, a blank new misc partition image is created."); |
David Anderson | 64581ce | 2019-10-03 17:52:22 -0700 | [diff] [blame] | 91 | DEFINE_string(composite_disk, "", "Location of the composite disk image. " |
| 92 | "If empty, a composite disk is not used."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 93 | |
| 94 | DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to" |
| 95 | " host kernel. This is only used during transition of our clients." |
| 96 | " Will be deprecated soon."); |
| 97 | DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 98 | DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444), |
| 99 | "The port on which the vnc server should listen"); |
Cody Schuffelen | 1074333 | 2019-04-15 16:50:49 -0700 | [diff] [blame] | 100 | DEFINE_string(adb_mode, "vsock_half_tunnel", |
Cody Schuffelen | 63d1005 | 2019-02-26 12:21:53 -0800 | [diff] [blame] | 101 | "Mode for ADB connection. Can be 'usb' for USB forwarding, " |
| 102 | "'tunnel' for a TCP connection tunneled through VSoC, " |
| 103 | "'vsock_tunnel' for a TCP connection tunneled through vsock, " |
| 104 | "'native_vsock' for a direct connection to the guest ADB over " |
| 105 | "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to " |
| 106 | "the guest ADB server, or a comma separated list of types as in " |
| 107 | "'usb,tunnel'"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 108 | DEFINE_bool(run_adb_connector, true, |
| 109 | "Maintain adb connection by sending 'adb connect' commands to the " |
Matthias Maennich | 454d787 | 2019-02-06 16:35:17 +0000 | [diff] [blame] | 110 | "server. Only relevant with -adb_mode=tunnel or vsock_tunnel"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 111 | DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb"); |
Cody Schuffelen | 7a18451 | 2019-10-30 16:52:55 -0700 | [diff] [blame] | 112 | DEFINE_string(wifi_tap_name, GetPerInstanceDefault("cvd-wtap-"), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 113 | "The name of the tap interface to use for wifi"); |
| 114 | DEFINE_int32(vsock_guest_cid, |
| 115 | vsoc::GetDefaultPerInstanceVsockCid(), |
| 116 | "Guest identifier for vsock. Disabled if under 3."); |
| 117 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 118 | DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix), |
| 119 | "UUID to use for the device. Random if not specified"); |
| 120 | DEFINE_bool(daemon, false, |
| 121 | "Run cuttlefish in background, the launcher exits on boot " |
| 122 | "completed/failed"); |
| 123 | |
| 124 | DEFINE_string(device_title, "", "Human readable name for the instance, " |
| 125 | "used by the vnc_server for its server title"); |
| 126 | DEFINE_string(setupwizard_mode, "DISABLED", |
| 127 | "One of DISABLED,OPTIONAL,REQUIRED"); |
| 128 | |
| 129 | DEFINE_string(qemu_binary, |
| 130 | "/usr/bin/qemu-system-x86_64", |
| 131 | "The qemu binary to use"); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 132 | DEFINE_string(crosvm_binary, |
| 133 | vsoc::DefaultHostArtifactsPath("bin/crosvm"), |
| 134 | "The Crosvm binary to use"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 135 | DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process"); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 136 | DEFINE_string(logcat_mode, "", "How to send android's log messages from " |
| 137 | "guest to host. One of [serial, vsock]"); |
Ram Muthiah | 6e9c98c | 2019-05-28 15:18:27 -0700 | [diff] [blame] | 138 | DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on " |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 139 | "both the guest and the host"); |
Cody Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 140 | DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader"); |
| 141 | DEFINE_string(bootloader, "", "Bootloader binary path"); |
David Anderson | 637bfd6 | 2019-09-26 13:23:21 -0700 | [diff] [blame] | 142 | DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, " |
| 143 | "the slot will be chosen based on the misc partition if using a " |
| 144 | "bootloader. It will default to 'a' if empty and not using a " |
| 145 | "bootloader."); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 146 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 147 | namespace { |
| 148 | |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 149 | const std::string kKernelDefaultPath = "kernel"; |
| 150 | const std::string kInitramfsImg = "initramfs.img"; |
| 151 | const std::string kRamdiskConcatExt = ".concat"; |
Cody Schuffelen | 9fd9547 | 2019-10-04 13:49:57 -0700 | [diff] [blame] | 152 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 153 | bool ResolveInstanceFiles() { |
| 154 | if (FLAGS_system_image_dir.empty()) { |
| 155 | LOG(ERROR) << "--system_image_dir must be specified."; |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // If user did not specify location of either of these files, expect them to |
| 160 | // be placed in --system_image_dir location. |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 161 | std::string default_boot_image = FLAGS_system_image_dir + "/boot.img"; |
| 162 | SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(), |
| 163 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 164 | std::string default_cache_image = FLAGS_system_image_dir + "/cache.img"; |
| 165 | SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(), |
| 166 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 167 | std::string default_data_image = FLAGS_system_image_dir + "/userdata.img"; |
| 168 | SetCommandLineOptionWithMode("data_image", default_data_image.c_str(), |
| 169 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 170 | std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img"; |
| 171 | SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(), |
| 172 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Alistair Strachan | 050ca93 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 173 | std::string default_super_image = FLAGS_system_image_dir + "/super.img"; |
| 174 | SetCommandLineOptionWithMode("super_image", default_super_image.c_str(), |
| 175 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 176 | std::string default_misc_image = FLAGS_system_image_dir + "/misc.img"; |
| 177 | SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(), |
| 178 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
David Anderson | 64581ce | 2019-10-03 17:52:22 -0700 | [diff] [blame] | 179 | std::string default_composite_disk = FLAGS_system_image_dir + "/composite.img"; |
| 180 | SetCommandLineOptionWithMode("composite_disk", default_composite_disk.c_str(), |
| 181 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 182 | std::string default_vendor_boot_image = FLAGS_system_image_dir |
| 183 | + "/vendor_boot.img"; |
| 184 | SetCommandLineOptionWithMode("vendor_boot_image", |
| 185 | default_vendor_boot_image.c_str(), |
| 186 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 187 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
| 191 | std::string GetCuttlefishEnvPath() { |
| 192 | return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh"; |
| 193 | } |
| 194 | |
Cody Schuffelen | e0d0c46 | 2019-09-09 14:13:01 -0700 | [diff] [blame] | 195 | int GetHostPort() { |
| 196 | constexpr int kFirstHostPort = 6520; |
| 197 | return vsoc::GetPerInstanceDefault(kFirstHostPort); |
| 198 | } |
| 199 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 200 | // Initializes the config object and saves it to file. It doesn't return it, all |
| 201 | // further uses of the config should happen through the singleton |
| 202 | bool InitializeCuttlefishConfiguration( |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 203 | const cvd::BootImageUnpacker& boot_image_unpacker, |
| 204 | const cvd::FetcherConfig& fetcher_config) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 205 | vsoc::CuttlefishConfig tmp_config_obj; |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 206 | // Set this first so that calls to PerInstancePath below are correct |
| 207 | tmp_config_obj.set_instance_dir(FLAGS_instance_dir); |
| 208 | if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) { |
| 209 | LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager; |
| 210 | return false; |
| 211 | } |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 212 | if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) { |
| 213 | LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager; |
| 214 | return false; |
| 215 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 216 | tmp_config_obj.set_vm_manager(FLAGS_vm_manager); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 217 | tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode); |
Cody Schuffelen | 2de6ead | 2019-12-04 16:16:47 -0800 | [diff] [blame] | 218 | if (vm_manager::VmManager::ConfigureGpuMode(tmp_config_obj.vm_manager(), |
| 219 | tmp_config_obj.gpu_mode()).empty()) { |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 220 | LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode << |
| 221 | " does not work with vm_manager=" << FLAGS_vm_manager; |
| 222 | return false; |
| 223 | } |
| 224 | tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket); |
Greg Hartman | eb04ac5 | 2019-07-16 16:44:18 -0700 | [diff] [blame] | 225 | tmp_config_obj.set_x_display(FLAGS_x_display); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 226 | |
| 227 | tmp_config_obj.set_serial_number(FLAGS_serial_number); |
| 228 | |
| 229 | tmp_config_obj.set_cpus(FLAGS_cpus); |
| 230 | tmp_config_obj.set_memory_mb(FLAGS_memory_mb); |
| 231 | |
| 232 | tmp_config_obj.set_dpi(FLAGS_dpi); |
| 233 | tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode); |
| 234 | tmp_config_obj.set_x_res(FLAGS_x_res); |
| 235 | tmp_config_obj.set_y_res(FLAGS_y_res); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 236 | tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz); |
| 237 | tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb); |
Cody Schuffelen | f4a1cdb | 2019-11-13 16:51:16 -0800 | [diff] [blame] | 238 | std::vector<std::string> adb = android::base::Split(FLAGS_adb_mode, ","); |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 239 | 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] | 240 | tmp_config_obj.set_host_port(GetHostPort()); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 241 | tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort())); |
| 242 | |
| 243 | tmp_config_obj.set_device_title(FLAGS_device_title); |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 244 | std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath); |
| 245 | std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel; |
| 246 | if (foreign_kernel.size()) { |
| 247 | tmp_config_obj.set_kernel_image_path(foreign_kernel); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 248 | tmp_config_obj.set_use_unpacked_kernel(false); |
| 249 | } else { |
| 250 | tmp_config_obj.set_kernel_image_path( |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 251 | tmp_config_obj.PerInstancePath(kKernelDefaultPath.c_str())); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 252 | tmp_config_obj.set_use_unpacked_kernel(true); |
| 253 | } |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 254 | tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel); |
| 255 | if (FLAGS_decompress_kernel) { |
| 256 | tmp_config_obj.set_decompressed_kernel_image_path( |
| 257 | tmp_config_obj.PerInstancePath("vmlinux")); |
| 258 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 259 | |
| 260 | auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img"); |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 261 | auto vendor_ramdisk_path = tmp_config_obj.PerInstancePath("vendor_ramdisk.img"); |
Cody Schuffelen | 1e64554 | 2019-11-19 15:44:22 -0800 | [diff] [blame] | 262 | if (!boot_image_unpacker.HasRamdiskImage()) { |
| 263 | LOG(INFO) << "A ramdisk is required, but the boot image did not have one."; |
| 264 | return false; |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Cody Schuffelen | 2de6ead | 2019-12-04 16:16:47 -0800 | [diff] [blame] | 267 | tmp_config_obj.set_boot_image_kernel_cmdline(boot_image_unpacker.kernel_cmdline()); |
| 268 | tmp_config_obj.set_loop_max_part(FLAGS_loop_max_part); |
| 269 | tmp_config_obj.set_guest_enforce_security(FLAGS_guest_enforce_security); |
| 270 | tmp_config_obj.set_guest_audit_security(FLAGS_guest_audit_security); |
| 271 | tmp_config_obj.set_extra_kernel_cmdline(FLAGS_extra_kernel_cmdline); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 272 | |
Cody Schuffelen | 2ced6f1 | 2019-11-19 15:54:10 -0800 | [diff] [blame] | 273 | tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk}); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 274 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 275 | tmp_config_obj.set_ramdisk_image_path(ramdisk_path); |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 276 | tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path); |
| 277 | |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 278 | std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg); |
| 279 | std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk; |
| 280 | if (foreign_kernel.size() && !foreign_ramdisk.size()) { |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 281 | // If there's a kernel that's passed in without an initramfs, that implies |
| 282 | // user error or a kernel built with no modules. In either case, let's |
| 283 | // choose to avoid loading the modules from the vendor ramdisk which are |
| 284 | // built for the default cf kernel. Once boot occurs, user error will |
| 285 | // become obvious. |
| 286 | tmp_config_obj.set_final_ramdisk_path(ramdisk_path); |
| 287 | } else { |
| 288 | tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt); |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 289 | if(foreign_ramdisk.size()) { |
| 290 | tmp_config_obj.set_initramfs_path(foreign_ramdisk); |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 291 | } |
Cody Schuffelen | 9fd9547 | 2019-10-04 13:49:57 -0700 | [diff] [blame] | 292 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 293 | |
Cody Schuffelen | 75ef887 | 2019-09-06 17:35:54 -0700 | [diff] [blame] | 294 | if (tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) > 0) { |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 295 | tmp_config_obj.set_usb_v1_socket_name( |
| 296 | tmp_config_obj.PerInstanceInternalPath("usb-v1")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 297 | tmp_config_obj.set_vhci_port(FLAGS_vhci_port); |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 298 | tmp_config_obj.set_usb_ip_socket_name( |
| 299 | tmp_config_obj.PerInstanceInternalPath("usb-ip")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 302 | tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 303 | tmp_config_obj.set_logcat_receiver_binary( |
| 304 | vsoc::DefaultHostArtifactsPath("bin/logcat_receiver")); |
| 305 | tmp_config_obj.set_config_server_binary( |
| 306 | vsoc::DefaultHostArtifactsPath("bin/config_server")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 307 | |
| 308 | tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface); |
| 309 | tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name); |
| 310 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 311 | tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name); |
| 312 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 313 | tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid); |
| 314 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 315 | tmp_config_obj.set_uuid(FLAGS_uuid); |
| 316 | |
| 317 | tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 318 | tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 319 | tmp_config_obj.set_console_forwarder_binary( |
| 320 | vsoc::DefaultHostArtifactsPath("bin/console_forwarder")); |
| 321 | tmp_config_obj.set_kernel_log_monitor_binary( |
| 322 | vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 323 | |
| 324 | tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 325 | tmp_config_obj.set_vnc_server_binary( |
| 326 | vsoc::DefaultHostArtifactsPath("bin/vnc_server")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 327 | tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port); |
| 328 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 329 | tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses); |
| 330 | tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 331 | tmp_config_obj.set_adb_connector_binary( |
| 332 | vsoc::DefaultHostArtifactsPath("bin/adb_connector")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 333 | tmp_config_obj.set_virtual_usb_manager_binary( |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 334 | vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager")); |
| 335 | tmp_config_obj.set_socket_vsock_proxy_binary( |
| 336 | vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 337 | tmp_config_obj.set_run_as_daemon(FLAGS_daemon); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 338 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 339 | tmp_config_obj.set_data_policy(FLAGS_data_policy); |
| 340 | tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb); |
| 341 | tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt); |
| 342 | |
Cody Schuffelen | 75ef887 | 2019-09-06 17:35:54 -0700 | [diff] [blame] | 343 | if(tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) == 0) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 344 | tmp_config_obj.disable_usb_adb(); |
| 345 | } |
| 346 | |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 347 | tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 348 | |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 349 | tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 350 | tmp_config_obj.set_tombstone_receiver_binary( |
| 351 | vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver")); |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 352 | |
Cody Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 353 | tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader); |
| 354 | tmp_config_obj.set_bootloader(FLAGS_bootloader); |
| 355 | |
David Anderson | 637bfd6 | 2019-09-26 13:23:21 -0700 | [diff] [blame] | 356 | if (!FLAGS_boot_slot.empty()) { |
| 357 | tmp_config_obj.set_boot_slot(FLAGS_boot_slot); |
| 358 | } |
| 359 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 360 | tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath()); |
| 361 | |
| 362 | auto config_file = GetConfigFilePath(tmp_config_obj); |
| 363 | auto config_link = vsoc::GetGlobalConfigFileLink(); |
| 364 | // Save the config object before starting any host process |
| 365 | if (!tmp_config_obj.SaveToFile(config_file)) { |
| 366 | LOG(ERROR) << "Unable to save config object"; |
| 367 | return false; |
| 368 | } |
| 369 | setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true); |
| 370 | if (symlink(config_file.c_str(), config_link.c_str()) != 0) { |
| 371 | LOG(ERROR) << "Failed to create symlink to config file at " << config_link |
| 372 | << ": " << strerror(errno); |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | void SetDefaultFlagsForQemu() { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 380 | auto default_instance_dir = |
| 381 | cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime"; |
| 382 | SetCommandLineOptionWithMode("instance_dir", |
| 383 | default_instance_dir.c_str(), |
| 384 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 385 | // TODO(b/144119457) Use the serial port. |
| 386 | SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode, |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 387 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void SetDefaultFlagsForCrosvm() { |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 391 | auto default_instance_dir = |
| 392 | cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime"; |
| 393 | SetCommandLineOptionWithMode("instance_dir", |
| 394 | default_instance_dir.c_str(), |
| 395 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 396 | SetCommandLineOptionWithMode("wayland_socket", |
Greg Hartman | eb04ac5 | 2019-07-16 16:44:18 -0700 | [diff] [blame] | 397 | "", |
| 398 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 399 | SetCommandLineOptionWithMode("x_display", |
| 400 | getenv("DISPLAY"), |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 401 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 402 | SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode, |
| 403 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | bool ParseCommandLineFlags(int* argc, char*** argv) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 407 | google::ParseCommandLineNonHelpFlags(argc, argv, true); |
| 408 | bool invalid_manager = false; |
| 409 | if (FLAGS_vm_manager == vm_manager::QemuManager::name()) { |
| 410 | SetDefaultFlagsForQemu(); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 411 | } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) { |
| 412 | SetDefaultFlagsForCrosvm(); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 413 | } else { |
| 414 | std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager |
| 415 | << std::endl; |
| 416 | invalid_manager = true; |
| 417 | } |
| 418 | google::HandleCommandLineHelpFlags(); |
| 419 | if (invalid_manager) { |
| 420 | return false; |
| 421 | } |
| 422 | // Set the env variable to empty (in case the caller passed a value for it). |
| 423 | unsetenv(vsoc::kCuttlefishConfigEnvVarName); |
| 424 | |
| 425 | return ResolveInstanceFiles(); |
| 426 | } |
| 427 | |
| 428 | bool CleanPriorFiles() { |
| 429 | // Everything on the instance directory |
| 430 | std::string prior_files = FLAGS_instance_dir + "/*"; |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 431 | // The environment file |
| 432 | prior_files += " " + GetCuttlefishEnvPath(); |
| 433 | // The global link to the config file |
| 434 | prior_files += " " + vsoc::GetGlobalConfigFileLink(); |
| 435 | LOG(INFO) << "Assuming prior files of " << prior_files; |
| 436 | std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null"; |
| 437 | int rval = std::system(fuser_cmd.c_str()); |
| 438 | // fuser returns 0 if any of the files are open |
| 439 | if (WEXITSTATUS(rval) == 0) { |
| 440 | LOG(ERROR) << "Clean aborted: files are in use"; |
| 441 | return false; |
| 442 | } |
| 443 | std::string clean_command = "rm -rf " + prior_files; |
| 444 | rval = std::system(clean_command.c_str()); |
| 445 | if (WEXITSTATUS(rval) != 0) { |
| 446 | LOG(ERROR) << "Remove of files failed"; |
| 447 | return false; |
| 448 | } |
| 449 | return true; |
| 450 | } |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 451 | |
| 452 | bool DecompressKernel(const std::string& src, const std::string& dst) { |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 453 | cvd::Command decomp_cmd(vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux")); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 454 | decomp_cmd.AddParameter(src); |
| 455 | auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666); |
| 456 | if (!output_file->IsOpen()) { |
| 457 | LOG(ERROR) << "Unable to create decompressed image file: " |
| 458 | << output_file->StrError(); |
| 459 | return false; |
| 460 | } |
| 461 | decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file); |
Cody Schuffelen | e567087 | 2019-12-10 15:04:59 -0800 | [diff] [blame] | 462 | auto decomp_proc = decomp_cmd.Start(); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 463 | return decomp_proc.Started() && decomp_proc.Wait() == 0; |
| 464 | } |
Cody Schuffelen | 78824ed | 2019-09-06 17:43:15 -0700 | [diff] [blame] | 465 | |
| 466 | void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) { |
| 467 | auto adb_modes = config.adb_mode(); |
| 468 | adb_modes.erase(vsoc::AdbMode::Unknown); |
| 469 | if (adb_modes.size() < 1) { |
| 470 | LOG(INFO) << "ADB not enabled"; |
| 471 | } |
| 472 | } |
| 473 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 474 | } // namespace |
| 475 | |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 476 | namespace { |
| 477 | |
| 478 | std::vector<ImagePartition> disk_config() { |
| 479 | std::vector<ImagePartition> partitions; |
Cody Schuffelen | 4f7e441 | 2019-08-30 16:10:59 -0700 | [diff] [blame] | 480 | partitions.push_back(ImagePartition { |
| 481 | .label = "super", |
| 482 | .image_file_path = FLAGS_super_image, |
| 483 | }); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 484 | partitions.push_back(ImagePartition { |
| 485 | .label = "userdata", |
| 486 | .image_file_path = FLAGS_data_image, |
| 487 | }); |
| 488 | partitions.push_back(ImagePartition { |
| 489 | .label = "cache", |
| 490 | .image_file_path = FLAGS_cache_image, |
| 491 | }); |
| 492 | partitions.push_back(ImagePartition { |
| 493 | .label = "metadata", |
| 494 | .image_file_path = FLAGS_metadata_image, |
| 495 | }); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 496 | partitions.push_back(ImagePartition { |
| 497 | .label = "boot", |
| 498 | .image_file_path = FLAGS_boot_image, |
| 499 | }); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 500 | partitions.push_back(ImagePartition { |
| 501 | .label = "misc", |
| 502 | .image_file_path = FLAGS_misc_image |
| 503 | }); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 504 | return partitions; |
| 505 | } |
| 506 | |
| 507 | bool ShouldCreateCompositeDisk() { |
Cody Schuffelen | c3fd6e5 | 2019-08-30 15:14:07 -0700 | [diff] [blame] | 508 | if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) { |
| 509 | // The crosvm implementation is very fast to rebuild but also more brittle due to being split |
| 510 | // into multiple files. The QEMU implementation is slow to build, but completely self-contained |
| 511 | // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU. |
| 512 | return true; |
| 513 | } |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 514 | auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk); |
| 515 | for (auto& partition : disk_config()) { |
| 516 | auto partition_age = cvd::FileModificationTime(partition.image_file_path); |
| 517 | if (partition_age >= composite_age) { |
| 518 | LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", " |
| 519 | << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\""; |
| 520 | return true; |
| 521 | } |
| 522 | } |
| 523 | return false; |
| 524 | } |
| 525 | |
Ram Muthiah | 3db0a56 | 2019-08-26 13:35:19 -0700 | [diff] [blame] | 526 | bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path, |
| 527 | const std::string& ramdisk_b_path) { |
| 528 | // clear out file of any pre-existing content |
| 529 | std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc); |
| 530 | std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary); |
| 531 | std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary); |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 532 | |
Ram Muthiah | 3db0a56 | 2019-08-26 13:35:19 -0700 | [diff] [blame] | 533 | 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] | 534 | return false; |
| 535 | } |
| 536 | |
Ram Muthiah | 3db0a56 | 2019-08-26 13:35:19 -0700 | [diff] [blame] | 537 | new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf(); |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
| 540 | |
Cody Schuffelen | c3fd6e5 | 2019-08-30 15:14:07 -0700 | [diff] [blame] | 541 | void CreateCompositeDisk(const vsoc::CuttlefishConfig& config) { |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 542 | if (FLAGS_composite_disk.empty()) { |
| 543 | LOG(FATAL) << "asked to create composite disk, but path was empty"; |
| 544 | } |
Cody Schuffelen | c3fd6e5 | 2019-08-30 15:14:07 -0700 | [diff] [blame] | 545 | if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) { |
| 546 | std::string header_path = config.PerInstancePath("gpt_header.img"); |
| 547 | std::string footer_path = config.PerInstancePath("gpt_footer.img"); |
| 548 | create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk); |
| 549 | } else { |
| 550 | aggregate_image(disk_config(), FLAGS_composite_disk); |
| 551 | } |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | } // namespace |
| 555 | |
Cody Schuffelen | 605e685 | 2019-10-16 16:22:24 -0700 | [diff] [blame] | 556 | const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig( |
Cody Schuffelen | b737ef5 | 2019-09-25 14:54:38 -0700 | [diff] [blame] | 557 | int* argc, char*** argv, cvd::FetcherConfig fetcher_config) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 558 | if (!ParseCommandLineFlags(argc, argv)) { |
| 559 | LOG(ERROR) << "Failed to parse command arguments"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 560 | exit(AssemblerExitCodes::kArgumentParsingError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | // Clean up prior files before saving the config file (doing it after would |
| 564 | // delete it) |
| 565 | if (!CleanPriorFiles()) { |
| 566 | LOG(ERROR) << "Failed to clean prior files"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 567 | exit(AssemblerExitCodes::kPrioFilesCleanupError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 568 | } |
| 569 | // Create instance directory if it doesn't exist. |
| 570 | if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) { |
| 571 | LOG(INFO) << "Setting up " << FLAGS_instance_dir; |
| 572 | if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) { |
| 573 | LOG(ERROR) << "Failed to create instance directory: " |
| 574 | << FLAGS_instance_dir << ". Error: " << errno; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 575 | exit(AssemblerExitCodes::kInstanceDirCreationError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 579 | auto internal_dir = FLAGS_instance_dir + "/" + vsoc::kInternalDirName; |
| 580 | if (!cvd::DirectoryExists(internal_dir)) { |
| 581 | if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < |
| 582 | 0) { |
| 583 | LOG(ERROR) << "Failed to create internal instance directory: " |
| 584 | << internal_dir << ". Error: " << errno; |
| 585 | exit(AssemblerExitCodes::kInstanceDirCreationError); |
| 586 | } |
| 587 | } |
| 588 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 589 | if (!cvd::FileHasContent(FLAGS_boot_image)) { |
| 590 | LOG(ERROR) << "File not found: " << FLAGS_boot_image; |
| 591 | exit(cvd::kCuttlefishConfigurationInitError); |
| 592 | } |
| 593 | |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 594 | if (!cvd::FileHasContent(FLAGS_vendor_boot_image)) { |
| 595 | LOG(ERROR) << "File not found: " << FLAGS_vendor_boot_image; |
| 596 | exit(cvd::kCuttlefishConfigurationInitError); |
| 597 | } |
| 598 | |
| 599 | auto boot_img_unpacker = |
| 600 | cvd::BootImageUnpacker::FromImages(FLAGS_boot_image, |
| 601 | FLAGS_vendor_boot_image); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 602 | |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 603 | if (!InitializeCuttlefishConfiguration(*boot_img_unpacker, fetcher_config)) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 604 | LOG(ERROR) << "Failed to initialize configuration"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 605 | exit(AssemblerExitCodes::kCuttlefishConfigurationInitError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 606 | } |
| 607 | // Do this early so that the config object is ready for anything that needs it |
| 608 | auto config = vsoc::CuttlefishConfig::Get(); |
| 609 | if (!config) { |
| 610 | LOG(ERROR) << "Failed to obtain config singleton"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 611 | exit(AssemblerExitCodes::kCuttlefishConfigurationInitError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(), |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 615 | config->vendor_ramdisk_image_path(), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 616 | config->use_unpacked_kernel() |
| 617 | ? config->kernel_image_path() |
| 618 | : "")) { |
| 619 | LOG(ERROR) << "Failed to unpack boot image"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 620 | exit(AssemblerExitCodes::kBootImageUnpackError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 623 | // TODO(134522463) as part of the bootloader refactor, repack the vendor boot |
| 624 | // image and use the bootloader to load both the boot and vendor ramdisk. |
| 625 | // Until then, this hack to get gki modules into cuttlefish will suffice. |
| 626 | |
| 627 | // If a vendor ramdisk comes in via this mechanism, let it supercede the one |
| 628 | // in the vendor boot image. This flag is what kernel presubmit testing uses |
| 629 | // to pass in the kernel ramdisk. |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 630 | |
| 631 | // If no kernel is passed in or an initramfs is made available, the default |
| 632 | // vendor boot ramdisk or the initramfs provided should be appended to the |
| 633 | // boot ramdisk. If a kernel IS provided with no initramfs, it is safe to |
| 634 | // safe to assume that the kernel was built with no modules and expects no |
| 635 | // modules for cf to run properly. |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 636 | std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath); |
| 637 | std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel; |
| 638 | std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg); |
| 639 | std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk; |
| 640 | if(!foreign_kernel.size() || foreign_ramdisk.size()) { |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 641 | const std::string& vendor_ramdisk_path = |
| 642 | config->initramfs_path().size() ? config->initramfs_path() |
| 643 | : config->vendor_ramdisk_image_path(); |
| 644 | if(!ConcatRamdisks(config->final_ramdisk_path(), |
| 645 | config->ramdisk_image_path(), vendor_ramdisk_path)) { |
| 646 | LOG(ERROR) << "Failed to concatenate ramdisk and vendor ramdisk"; |
| 647 | exit(AssemblerExitCodes::kInitRamFsConcatError); |
| 648 | } |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 651 | if (config->decompress_kernel()) { |
| 652 | if (!DecompressKernel(config->kernel_image_path(), |
| 653 | config->decompressed_kernel_image_path())) { |
| 654 | LOG(ERROR) << "Failed to decompress kernel"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 655 | exit(AssemblerExitCodes::kKernelDecompressError); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 659 | ValidateAdbModeFlag(*config); |
| 660 | |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 661 | // Create misc if necessary |
| 662 | if (!InitializeMiscImage(FLAGS_misc_image)) { |
| 663 | exit(cvd::kCuttlefishConfigurationInitError); |
| 664 | } |
| 665 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 666 | // Create data if necessary |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 667 | if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) { |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 668 | exit(cvd::kCuttlefishConfigurationInitError); |
| 669 | } |
| 670 | |
Cody Schuffelen | 69c1959 | 2019-05-31 16:00:30 -0700 | [diff] [blame] | 671 | if (!cvd::FileExists(FLAGS_metadata_image)) { |
| 672 | CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none"); |
| 673 | } |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 674 | |
Cody Schuffelen | b737ef5 | 2019-09-25 14:54:38 -0700 | [diff] [blame] | 675 | if (SuperImageNeedsRebuilding(fetcher_config, *config)) { |
| 676 | if (!RebuildSuperImage(fetcher_config, *config, FLAGS_super_image)) { |
| 677 | LOG(ERROR) << "Super image rebuilding requested but could not be completed."; |
| 678 | exit(cvd::kCuttlefishConfigurationInitError); |
| 679 | } |
| 680 | } |
| 681 | |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 682 | if (ShouldCreateCompositeDisk()) { |
Cody Schuffelen | c3fd6e5 | 2019-08-30 15:14:07 -0700 | [diff] [blame] | 683 | CreateCompositeDisk(*config); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 686 | // Check that the files exist |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 687 | for (const auto& file : config->virtual_disk_paths()) { |
Alistair Strachan | 050ca93 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 688 | if (!file.empty() && !cvd::FileHasContent(file.c_str())) { |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 689 | LOG(ERROR) << "File not found: " << file; |
| 690 | exit(cvd::kCuttlefishConfigurationInitError); |
| 691 | } |
| 692 | } |
| 693 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 694 | return config; |
| 695 | } |
| 696 | |
| 697 | std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) { |
| 698 | return config.PerInstancePath("cuttlefish_config.json"); |
| 699 | } |