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