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 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 3 | #include <dirent.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 6 | #include <sys/statvfs.h> |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 7 | #include <unistd.h> |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 8 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 9 | #include <algorithm> |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 10 | #include <iostream> |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 11 | #include <fstream> |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 12 | |
Cody Schuffelen | f4a1cdb | 2019-11-13 16:51:16 -0800 | [diff] [blame] | 13 | #include <android-base/strings.h> |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 14 | #include <gflags/gflags.h> |
| 15 | #include <glog/logging.h> |
| 16 | |
| 17 | #include "common/libs/utils/environment.h" |
| 18 | #include "common/libs/utils/files.h" |
Cody Schuffelen | 147b88e | 2019-09-09 16:00:11 -0700 | [diff] [blame] | 19 | #include "host/commands/assemble_cvd/boot_image_unpacker.h" |
| 20 | #include "host/commands/assemble_cvd/data_image.h" |
| 21 | #include "host/commands/assemble_cvd/image_aggregator.h" |
| 22 | #include "host/commands/assemble_cvd/assembler_defs.h" |
Cody Schuffelen | b737ef5 | 2019-09-25 14:54:38 -0700 | [diff] [blame] | 23 | #include "host/commands/assemble_cvd/super_image_mixer.h" |
Cody Schuffelen | 605e685 | 2019-10-16 16:22:24 -0700 | [diff] [blame] | 24 | #include "host/libs/config/fetcher_config.h" |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 25 | #include "host/libs/vm_manager/crosvm_manager.h" |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 26 | #include "host/libs/vm_manager/qemu_manager.h" |
| 27 | #include "host/libs/vm_manager/vm_manager.h" |
| 28 | |
Cody Schuffelen | 47c5785 | 2019-12-13 13:50:50 -0800 | [diff] [blame] | 29 | using vsoc::ForCurrentInstance; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 30 | using cvd::AssemblerExitCodes; |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 31 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 32 | DEFINE_string(cache_image, "", "Location of the cache partition image."); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 33 | DEFINE_string(metadata_image, "", "Location of the metadata partition image " |
| 34 | "to be generated."); |
| 35 | DEFINE_int32(blank_metadata_image_mb, 16, |
| 36 | "The size of the blank metadata image to generate, MB."); |
Alistair Delva | 3bba628 | 2020-05-14 13:52:00 -0700 | [diff] [blame] | 37 | DEFINE_int32(blank_sdcard_image_mb, 2048, |
| 38 | "The size of the blank sdcard image to generate, MB."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 39 | DEFINE_int32(cpus, 2, "Virtual CPU count."); |
| 40 | DEFINE_string(data_image, "", "Location of the data partition image."); |
| 41 | DEFINE_string(data_policy, "use_existing", "How to handle userdata partition." |
| 42 | " Either 'use_existing', 'create_if_missing', 'resize_up_to', or " |
| 43 | "'always_create'."); |
| 44 | DEFINE_int32(blank_data_image_mb, 0, |
| 45 | "The size of the blank data image to generate, MB."); |
Alistair Delva | 4c70e26 | 2019-10-15 14:33:59 -0700 | [diff] [blame] | 46 | DEFINE_string(blank_data_image_fmt, "f2fs", |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 47 | "The fs format for the blank data image. Used with mkfs."); |
| 48 | DEFINE_string(qemu_gdb, "", |
Matthias Maennich | 454d787 | 2019-02-06 16:35:17 +0000 | [diff] [blame] | 49 | "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 50 | |
| 51 | DEFINE_int32(x_res, 720, "Width of the screen in pixels"); |
| 52 | DEFINE_int32(y_res, 1280, "Height of the screen in pixels"); |
| 53 | DEFINE_int32(dpi, 160, "Pixels per inch for the screen"); |
| 54 | DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 55 | DEFINE_string(kernel_path, "", |
| 56 | "Path to the kernel. Overrides the one from the boot image"); |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 57 | DEFINE_string(initramfs_path, "", "Path to the initramfs"); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 58 | DEFINE_bool(decompress_kernel, false, |
Jorge E. Moreira | 3629404 | 2019-06-07 15:23:18 -0700 | [diff] [blame] | 59 | "Whether to decompress the kernel image."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 60 | DEFINE_string(extra_kernel_cmdline, "", |
| 61 | "Additional flags to put on the kernel command line"); |
| 62 | DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions"); |
Jorge E. Moreira | 300f97f | 2019-04-09 13:48:01 -0700 | [diff] [blame] | 63 | DEFINE_bool(guest_enforce_security, true, |
Cody Schuffelen | 79d4c19 | 2019-12-04 15:13:26 -0800 | [diff] [blame] | 64 | "Whether to run in enforcing mode (non permissive)."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 65 | DEFINE_bool(guest_audit_security, true, |
| 66 | "Whether to log security audits."); |
Alistair Delva | 33b861f | 2019-12-26 12:23:59 -0800 | [diff] [blame] | 67 | DEFINE_bool(guest_force_normal_boot, true, |
| 68 | "Whether to force the boot sequence to skip recovery."); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 69 | DEFINE_string(boot_image, "", |
| 70 | "Location of cuttlefish boot image. If empty it is assumed to be " |
| 71 | "boot.img in the directory specified by -system_image_dir."); |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 72 | DEFINE_string(vendor_boot_image, "", |
| 73 | "Location of cuttlefish vendor boot image. If empty it is assumed to " |
| 74 | "be vendor_boot.img in the directory specified by -system_image_dir."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 75 | DEFINE_int32(memory_mb, 2048, |
| 76 | "Total amount of memory available for guest, MB."); |
Cody Schuffelen | 47c5785 | 2019-12-13 13:50:50 -0800 | [diff] [blame] | 77 | DEFINE_string(serial_number, ForCurrentInstance("CUTTLEFISHCVD"), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 78 | "Serial number to use for the device"); |
Cody Schuffelen | ab6d345 | 2019-12-13 15:54:27 -0800 | [diff] [blame] | 79 | DEFINE_string(assembly_dir, |
| 80 | cvd::StringFromEnv("HOME", ".") + "/cuttlefish_assembly", |
| 81 | "A directory to put generated files common between instances"); |
| 82 | DEFINE_string(instance_dir, |
| 83 | cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime", |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 84 | "A directory to put all instance specific files"); |
| 85 | DEFINE_string( |
Alistair Strachan | 5ab095c | 2019-05-23 20:41:20 +0000 | [diff] [blame] | 86 | vm_manager, vm_manager::CrosvmManager::name(), |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 87 | "What virtual machine manager to use, one of {qemu_cli, crosvm}"); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 88 | DEFINE_string( |
Greg Hartman | bc1bed4 | 2019-04-05 20:02:00 -0700 | [diff] [blame] | 89 | gpu_mode, vsoc::kGpuModeGuestSwiftshader, |
| 90 | "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}"); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 91 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 92 | DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""), |
| 93 | "Location of the system partition images."); |
Alistair Strachan | 050ca93 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 94 | DEFINE_string(super_image, "", "Location of the super partition image."); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 95 | DEFINE_string(misc_image, "", |
| 96 | "Location of the misc partition image. If the image does not " |
| 97 | "exist, a blank new misc partition image is created."); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 98 | |
| 99 | DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to" |
| 100 | " host kernel. This is only used during transition of our clients." |
| 101 | " Will be deprecated soon."); |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 102 | DEFINE_bool(start_vnc_server, false, "Whether to start the vnc server process. " |
| 103 | "The VNC server runs at port 6443 + i for " |
| 104 | "the vsoc-i user or CUTTLEFISH_INSTANCE=i, " |
| 105 | "starting from 1."); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 106 | |
Jorge E. Moreira | 692ca66 | 2020-02-19 15:36:26 -0800 | [diff] [blame] | 107 | DEFINE_bool(start_webrtc, false, "[Experimental] Whether to start the webrtc process."); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 108 | |
| 109 | DEFINE_string( |
| 110 | webrtc_assets_dir, |
Jorge E. Moreira | 1f65a4a | 2019-11-08 15:23:32 -0800 | [diff] [blame] | 111 | vsoc::DefaultHostArtifactsPath("usr/share/webrtc/assets"), |
Jorge E. Moreira | 692ca66 | 2020-02-19 15:36:26 -0800 | [diff] [blame] | 112 | "[Experimental] Path to WebRTC webpage assets."); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 113 | |
| 114 | DEFINE_string( |
| 115 | webrtc_certs_dir, |
Jorge E. Moreira | 1f65a4a | 2019-11-08 15:23:32 -0800 | [diff] [blame] | 116 | vsoc::DefaultHostArtifactsPath("usr/share/webrtc/certs"), |
Jorge E. Moreira | 692ca66 | 2020-02-19 15:36:26 -0800 | [diff] [blame] | 117 | "[Experimental] Path to WebRTC certificates directory."); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 118 | |
| 119 | DEFINE_string( |
| 120 | webrtc_public_ip, |
Jorge E. Moreira | 1f65a4a | 2019-11-08 15:23:32 -0800 | [diff] [blame] | 121 | "127.0.0.1", |
Jorge E. Moreira | 692ca66 | 2020-02-19 15:36:26 -0800 | [diff] [blame] | 122 | "[Experimental] Public IPv4 address of your server, a.b.c.d format"); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 123 | |
| 124 | DEFINE_bool( |
| 125 | webrtc_enable_adb_websocket, |
| 126 | false, |
Jorge E. Moreira | 692ca66 | 2020-02-19 15:36:26 -0800 | [diff] [blame] | 127 | "[Experimental] If enabled, exposes local adb service through a websocket."); |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 128 | |
Cody Schuffelen | 1074333 | 2019-04-15 16:50:49 -0700 | [diff] [blame] | 129 | DEFINE_string(adb_mode, "vsock_half_tunnel", |
Cody Schuffelen | 3c35fd8 | 2019-12-11 18:39:46 -0800 | [diff] [blame] | 130 | "Mode for ADB connection." |
Cody Schuffelen | 63d1005 | 2019-02-26 12:21:53 -0800 | [diff] [blame] | 131 | "'vsock_tunnel' for a TCP connection tunneled through vsock, " |
| 132 | "'native_vsock' for a direct connection to the guest ADB over " |
| 133 | "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to " |
| 134 | "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] | 135 | "'native_vsock,vsock_half_tunnel'"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 136 | DEFINE_bool(run_adb_connector, true, |
| 137 | "Maintain adb connection by sending 'adb connect' commands to the " |
Matthias Maennich | 454d787 | 2019-02-06 16:35:17 +0000 | [diff] [blame] | 138 | "server. Only relevant with -adb_mode=tunnel or vsock_tunnel"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 139 | |
Cody Schuffelen | 47c5785 | 2019-12-13 13:50:50 -0800 | [diff] [blame] | 140 | DEFINE_string(uuid, vsoc::ForCurrentInstance(vsoc::kDefaultUuidPrefix), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 141 | "UUID to use for the device. Random if not specified"); |
| 142 | DEFINE_bool(daemon, false, |
| 143 | "Run cuttlefish in background, the launcher exits on boot " |
| 144 | "completed/failed"); |
| 145 | |
| 146 | DEFINE_string(device_title, "", "Human readable name for the instance, " |
| 147 | "used by the vnc_server for its server title"); |
| 148 | DEFINE_string(setupwizard_mode, "DISABLED", |
| 149 | "One of DISABLED,OPTIONAL,REQUIRED"); |
| 150 | |
| 151 | DEFINE_string(qemu_binary, |
| 152 | "/usr/bin/qemu-system-x86_64", |
| 153 | "The qemu binary to use"); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 154 | DEFINE_string(crosvm_binary, |
| 155 | vsoc::DefaultHostArtifactsPath("bin/crosvm"), |
| 156 | "The Crosvm binary to use"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 157 | DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process"); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 158 | DEFINE_string(logcat_mode, "", "How to send android's log messages from " |
| 159 | "guest to host. One of [serial, vsock]"); |
Ram Muthiah | 6e9c98c | 2019-05-28 15:18:27 -0700 | [diff] [blame] | 160 | DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on " |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 161 | "both the guest and the host"); |
Cody Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 162 | DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader"); |
| 163 | DEFINE_string(bootloader, "", "Bootloader binary path"); |
David Anderson | 637bfd6 | 2019-09-26 13:23:21 -0700 | [diff] [blame] | 164 | DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, " |
| 165 | "the slot will be chosen based on the misc partition if using a " |
| 166 | "bootloader. It will default to 'a' if empty and not using a " |
| 167 | "bootloader."); |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 168 | DEFINE_int32(num_instances, 1, "Number of Android guests to launch"); |
| 169 | DEFINE_bool(resume, true, "Resume using the disk from the last session, if " |
| 170 | "possible. i.e., if --noresume is passed, the disk " |
| 171 | "will be reset to the state it was initially launched " |
| 172 | "in. This flag is ignored if the underlying partition " |
| 173 | "images have been updated since the first launch."); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 174 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 175 | namespace { |
| 176 | |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 177 | const std::string kKernelDefaultPath = "kernel"; |
| 178 | const std::string kInitramfsImg = "initramfs.img"; |
| 179 | const std::string kRamdiskConcatExt = ".concat"; |
Cody Schuffelen | 9fd9547 | 2019-10-04 13:49:57 -0700 | [diff] [blame] | 180 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 181 | bool ResolveInstanceFiles() { |
| 182 | if (FLAGS_system_image_dir.empty()) { |
| 183 | LOG(ERROR) << "--system_image_dir must be specified."; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | // If user did not specify location of either of these files, expect them to |
| 188 | // be placed in --system_image_dir location. |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 189 | std::string default_boot_image = FLAGS_system_image_dir + "/boot.img"; |
| 190 | SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(), |
| 191 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 192 | std::string default_cache_image = FLAGS_system_image_dir + "/cache.img"; |
| 193 | SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(), |
| 194 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 195 | std::string default_data_image = FLAGS_system_image_dir + "/userdata.img"; |
| 196 | SetCommandLineOptionWithMode("data_image", default_data_image.c_str(), |
| 197 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 198 | std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img"; |
| 199 | SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(), |
| 200 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Alistair Strachan | 050ca93 | 2018-11-27 12:41:19 -0800 | [diff] [blame] | 201 | std::string default_super_image = FLAGS_system_image_dir + "/super.img"; |
| 202 | SetCommandLineOptionWithMode("super_image", default_super_image.c_str(), |
| 203 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 204 | std::string default_misc_image = FLAGS_system_image_dir + "/misc.img"; |
| 205 | SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(), |
| 206 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 207 | std::string default_vendor_boot_image = FLAGS_system_image_dir |
| 208 | + "/vendor_boot.img"; |
| 209 | SetCommandLineOptionWithMode("vendor_boot_image", |
| 210 | default_vendor_boot_image.c_str(), |
| 211 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 212 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
| 216 | std::string GetCuttlefishEnvPath() { |
| 217 | return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh"; |
| 218 | } |
| 219 | |
Cody Schuffelen | 2168c24 | 2019-12-13 16:15:27 -0800 | [diff] [blame] | 220 | std::string GetLegacyConfigFilePath(const vsoc::CuttlefishConfig& config) { |
| 221 | return config.ForDefaultInstance().PerInstancePath("cuttlefish_config.json"); |
| 222 | } |
| 223 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 224 | int NumStreamers() { |
| 225 | auto start_flags = {FLAGS_start_vnc_server, FLAGS_start_webrtc}; |
| 226 | return std::count(start_flags.begin(), start_flags.end(), true); |
| 227 | } |
| 228 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 229 | std::string StrForInstance(const std::string& prefix, int num) { |
| 230 | std::ostringstream stream; |
| 231 | stream << prefix << std::setfill('0') << std::setw(2) << num; |
| 232 | return stream.str(); |
| 233 | } |
| 234 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 235 | // Initializes the config object and saves it to file. It doesn't return it, all |
| 236 | // further uses of the config should happen through the singleton |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 237 | vsoc::CuttlefishConfig InitializeCuttlefishConfiguration( |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 238 | const cvd::BootImageUnpacker& boot_image_unpacker, |
| 239 | const cvd::FetcherConfig& fetcher_config) { |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 240 | // At most one streamer can be started. |
| 241 | CHECK(NumStreamers() <= 1); |
| 242 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 243 | vsoc::CuttlefishConfig tmp_config_obj; |
Cody Schuffelen | ab6d345 | 2019-12-13 15:54:27 -0800 | [diff] [blame] | 244 | tmp_config_obj.set_assembly_dir(FLAGS_assembly_dir); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 245 | if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 246 | LOG(FATAL) << "Invalid vm_manager: " << FLAGS_vm_manager; |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 247 | } |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 248 | if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 249 | LOG(FATAL) << "Invalid vm_manager: " << FLAGS_vm_manager; |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 250 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 251 | tmp_config_obj.set_vm_manager(FLAGS_vm_manager); |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 252 | tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode); |
Cody Schuffelen | 2de6ead | 2019-12-04 16:16:47 -0800 | [diff] [blame] | 253 | if (vm_manager::VmManager::ConfigureGpuMode(tmp_config_obj.vm_manager(), |
| 254 | tmp_config_obj.gpu_mode()).empty()) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 255 | LOG(FATAL) << "Invalid gpu_mode=" << FLAGS_gpu_mode << |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 256 | " does not work with vm_manager=" << FLAGS_vm_manager; |
Greg Hartman | a3c552d | 2019-03-28 18:20:48 -0700 | [diff] [blame] | 257 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 258 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 259 | tmp_config_obj.set_cpus(FLAGS_cpus); |
| 260 | tmp_config_obj.set_memory_mb(FLAGS_memory_mb); |
| 261 | |
| 262 | tmp_config_obj.set_dpi(FLAGS_dpi); |
| 263 | tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode); |
| 264 | tmp_config_obj.set_x_res(FLAGS_x_res); |
| 265 | tmp_config_obj.set_y_res(FLAGS_y_res); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 266 | tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz); |
| 267 | tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb); |
Cody Schuffelen | f4a1cdb | 2019-11-13 16:51:16 -0800 | [diff] [blame] | 268 | std::vector<std::string> adb = android::base::Split(FLAGS_adb_mode, ","); |
Cody Schuffelen | 90b2fb2 | 2019-02-28 18:55:21 -0800 | [diff] [blame] | 269 | tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end())); |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 270 | std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath); |
| 271 | std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel; |
| 272 | if (foreign_kernel.size()) { |
| 273 | tmp_config_obj.set_kernel_image_path(foreign_kernel); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 274 | tmp_config_obj.set_use_unpacked_kernel(false); |
| 275 | } else { |
| 276 | tmp_config_obj.set_kernel_image_path( |
Cody Schuffelen | f53ded1 | 2019-12-13 16:05:22 -0800 | [diff] [blame] | 277 | tmp_config_obj.AssemblyPath(kKernelDefaultPath.c_str())); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 278 | tmp_config_obj.set_use_unpacked_kernel(true); |
| 279 | } |
Alistair Delva | 3aac3e1 | 2020-05-13 12:17:02 -0700 | [diff] [blame] | 280 | |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 281 | tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel); |
Alistair Delva | 3aac3e1 | 2020-05-13 12:17:02 -0700 | [diff] [blame] | 282 | if (tmp_config_obj.decompress_kernel()) { |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 283 | tmp_config_obj.set_decompressed_kernel_image_path( |
Cody Schuffelen | f53ded1 | 2019-12-13 16:05:22 -0800 | [diff] [blame] | 284 | tmp_config_obj.AssemblyPath("vmlinux")); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 285 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 286 | |
Cody Schuffelen | f53ded1 | 2019-12-13 16:05:22 -0800 | [diff] [blame] | 287 | auto ramdisk_path = tmp_config_obj.AssemblyPath("ramdisk.img"); |
| 288 | auto vendor_ramdisk_path = tmp_config_obj.AssemblyPath("vendor_ramdisk.img"); |
Cody Schuffelen | 1e64554 | 2019-11-19 15:44:22 -0800 | [diff] [blame] | 289 | if (!boot_image_unpacker.HasRamdiskImage()) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 290 | LOG(FATAL) << "A ramdisk is required, but the boot image did not have one."; |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Cody Schuffelen | 2de6ead | 2019-12-04 16:16:47 -0800 | [diff] [blame] | 293 | tmp_config_obj.set_boot_image_kernel_cmdline(boot_image_unpacker.kernel_cmdline()); |
| 294 | tmp_config_obj.set_loop_max_part(FLAGS_loop_max_part); |
| 295 | tmp_config_obj.set_guest_enforce_security(FLAGS_guest_enforce_security); |
| 296 | tmp_config_obj.set_guest_audit_security(FLAGS_guest_audit_security); |
Alistair Delva | 33b861f | 2019-12-26 12:23:59 -0800 | [diff] [blame] | 297 | 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] | 298 | tmp_config_obj.set_extra_kernel_cmdline(FLAGS_extra_kernel_cmdline); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 299 | |
| 300 | tmp_config_obj.set_ramdisk_image_path(ramdisk_path); |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 301 | tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path); |
| 302 | |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 303 | std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg); |
| 304 | std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk; |
| 305 | if (foreign_kernel.size() && !foreign_ramdisk.size()) { |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 306 | // If there's a kernel that's passed in without an initramfs, that implies |
| 307 | // user error or a kernel built with no modules. In either case, let's |
| 308 | // choose to avoid loading the modules from the vendor ramdisk which are |
| 309 | // built for the default cf kernel. Once boot occurs, user error will |
| 310 | // become obvious. |
| 311 | tmp_config_obj.set_final_ramdisk_path(ramdisk_path); |
| 312 | } else { |
| 313 | tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt); |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 314 | if(foreign_ramdisk.size()) { |
| 315 | tmp_config_obj.set_initramfs_path(foreign_ramdisk); |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 316 | } |
Cody Schuffelen | 9fd9547 | 2019-10-04 13:49:57 -0700 | [diff] [blame] | 317 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 318 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 319 | tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 320 | tmp_config_obj.set_logcat_receiver_binary( |
| 321 | vsoc::DefaultHostArtifactsPath("bin/logcat_receiver")); |
| 322 | tmp_config_obj.set_config_server_binary( |
| 323 | vsoc::DefaultHostArtifactsPath("bin/config_server")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 324 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 325 | tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 326 | tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 327 | tmp_config_obj.set_console_forwarder_binary( |
| 328 | vsoc::DefaultHostArtifactsPath("bin/console_forwarder")); |
| 329 | tmp_config_obj.set_kernel_log_monitor_binary( |
| 330 | vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 331 | |
| 332 | tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 333 | tmp_config_obj.set_vnc_server_binary( |
| 334 | vsoc::DefaultHostArtifactsPath("bin/vnc_server")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 335 | |
Jorge E. Moreira | e049b79 | 2019-12-18 18:17:48 -0800 | [diff] [blame] | 336 | tmp_config_obj.set_enable_webrtc(FLAGS_start_webrtc); |
| 337 | tmp_config_obj.set_webrtc_binary( |
| 338 | vsoc::DefaultHostArtifactsPath("bin/webRTC")); |
| 339 | tmp_config_obj.set_webrtc_assets_dir(FLAGS_webrtc_assets_dir); |
| 340 | tmp_config_obj.set_webrtc_public_ip(FLAGS_webrtc_public_ip); |
| 341 | tmp_config_obj.set_webrtc_certs_dir(FLAGS_webrtc_certs_dir); |
| 342 | |
| 343 | tmp_config_obj.set_webrtc_enable_adb_websocket( |
| 344 | FLAGS_webrtc_enable_adb_websocket); |
| 345 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 346 | tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses); |
| 347 | tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 348 | tmp_config_obj.set_adb_connector_binary( |
| 349 | vsoc::DefaultHostArtifactsPath("bin/adb_connector")); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 350 | tmp_config_obj.set_socket_vsock_proxy_binary( |
| 351 | vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy")); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 352 | tmp_config_obj.set_run_as_daemon(FLAGS_daemon); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 353 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 354 | tmp_config_obj.set_data_policy(FLAGS_data_policy); |
| 355 | tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb); |
| 356 | tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt); |
| 357 | |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 358 | tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode); |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 359 | |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 360 | tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver); |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 361 | tmp_config_obj.set_tombstone_receiver_binary( |
| 362 | vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver")); |
Ram Muthiah | 792e2ad | 2019-04-19 11:19:46 -0700 | [diff] [blame] | 363 | |
Cody Schuffelen | 1300f12 | 2019-05-28 18:24:34 -0700 | [diff] [blame] | 364 | tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader); |
| 365 | tmp_config_obj.set_bootloader(FLAGS_bootloader); |
| 366 | |
David Anderson | 637bfd6 | 2019-09-26 13:23:21 -0700 | [diff] [blame] | 367 | if (!FLAGS_boot_slot.empty()) { |
| 368 | tmp_config_obj.set_boot_slot(FLAGS_boot_slot); |
| 369 | } |
| 370 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 371 | tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath()); |
| 372 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 373 | std::vector<int> instance_nums; |
| 374 | for (int i = 0; i < FLAGS_num_instances; i++) { |
| 375 | instance_nums.push_back(vsoc::GetInstance() + i); |
| 376 | } |
| 377 | |
| 378 | for (const auto& num : instance_nums) { |
| 379 | auto instance = tmp_config_obj.ForInstance(num); |
| 380 | auto const_instance = const_cast<const vsoc::CuttlefishConfig&>(tmp_config_obj) |
| 381 | .ForInstance(num); |
| 382 | // Set this first so that calls to PerInstancePath below are correct |
| 383 | instance.set_instance_dir(FLAGS_instance_dir + "." + std::to_string(num)); |
| 384 | instance.set_serial_number(FLAGS_serial_number + std::to_string(num)); |
| 385 | |
| 386 | instance.set_mobile_bridge_name(StrForInstance("cvd-mbr-", num)); |
| 387 | instance.set_mobile_tap_name(StrForInstance("cvd-mtap-", num)); |
| 388 | |
| 389 | instance.set_wifi_tap_name(StrForInstance("cvd-wtap-", num)); |
| 390 | |
| 391 | instance.set_vsock_guest_cid(3 + num - 1); |
| 392 | |
| 393 | instance.set_uuid(FLAGS_uuid); |
| 394 | |
| 395 | instance.set_vnc_server_port(6444 + num - 1); |
| 396 | instance.set_host_port(6520 + num - 1); |
| 397 | instance.set_adb_ip_and_port("127.0.0.1:" + std::to_string(6520 + num - 1)); |
| 398 | |
| 399 | instance.set_device_title(FLAGS_device_title); |
| 400 | |
Alistair Delva | 3bba628 | 2020-05-14 13:52:00 -0700 | [diff] [blame] | 401 | instance.set_virtual_disk_paths({ |
| 402 | const_instance.PerInstancePath("overlay.img"), |
| 403 | const_instance.sdcard_path(), |
| 404 | }); |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return tmp_config_obj; |
| 408 | } |
| 409 | |
| 410 | bool SaveConfig(const vsoc::CuttlefishConfig& tmp_config_obj) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 411 | auto config_file = GetConfigFilePath(tmp_config_obj); |
| 412 | auto config_link = vsoc::GetGlobalConfigFileLink(); |
| 413 | // Save the config object before starting any host process |
| 414 | if (!tmp_config_obj.SaveToFile(config_file)) { |
| 415 | LOG(ERROR) << "Unable to save config object"; |
| 416 | return false; |
| 417 | } |
Cody Schuffelen | 2168c24 | 2019-12-13 16:15:27 -0800 | [diff] [blame] | 418 | auto legacy_config_file = GetLegacyConfigFilePath(tmp_config_obj); |
| 419 | if (!tmp_config_obj.SaveToFile(legacy_config_file)) { |
| 420 | LOG(ERROR) << "Unable to save legacy config object"; |
| 421 | return false; |
| 422 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 423 | setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true); |
| 424 | if (symlink(config_file.c_str(), config_link.c_str()) != 0) { |
| 425 | LOG(ERROR) << "Failed to create symlink to config file at " << config_link |
| 426 | << ": " << strerror(errno); |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | void SetDefaultFlagsForQemu() { |
Cody Schuffelen | 038d4d9 | 2019-11-04 15:09:03 -0800 | [diff] [blame] | 434 | // TODO(b/144119457) Use the serial port. |
| 435 | SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode, |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 436 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void SetDefaultFlagsForCrosvm() { |
Jorge E. Moreira | fd10cae | 2019-02-19 15:35:42 -0800 | [diff] [blame] | 440 | SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode, |
| 441 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Alistair Delva | 3aac3e1 | 2020-05-13 12:17:02 -0700 | [diff] [blame] | 442 | |
| 443 | // Crosvm requires a specific setting for kernel decompression; it must be |
| 444 | // on for aarch64 and off for x86, no other mode is supported. |
| 445 | bool decompress_kernel = false; |
| 446 | if (cvd::HostArch() == "aarch64") { |
| 447 | decompress_kernel = true; |
| 448 | } |
| 449 | SetCommandLineOptionWithMode("decompress_kernel", |
| 450 | (decompress_kernel ? "true" : "false"), |
| 451 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | bool ParseCommandLineFlags(int* argc, char*** argv) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 455 | google::ParseCommandLineNonHelpFlags(argc, argv, true); |
| 456 | bool invalid_manager = false; |
| 457 | if (FLAGS_vm_manager == vm_manager::QemuManager::name()) { |
| 458 | SetDefaultFlagsForQemu(); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 459 | } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) { |
| 460 | SetDefaultFlagsForCrosvm(); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 461 | } else { |
| 462 | std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager |
| 463 | << std::endl; |
| 464 | invalid_manager = true; |
| 465 | } |
Jorge E. Moreira | 1f65a4a | 2019-11-08 15:23:32 -0800 | [diff] [blame] | 466 | if (NumStreamers() == 0) { |
| 467 | // This makes the vnc server the default streamer unless the user requests |
| 468 | // another via a --star_<streamer> flag, while at the same time it's |
| 469 | // possible to run without any streamer by setting --start_vnc_server=false. |
| 470 | SetCommandLineOptionWithMode("start_vnc_server", "true", |
| 471 | google::FlagSettingMode::SET_FLAGS_DEFAULT); |
| 472 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 473 | google::HandleCommandLineHelpFlags(); |
| 474 | if (invalid_manager) { |
| 475 | return false; |
| 476 | } |
| 477 | // Set the env variable to empty (in case the caller passed a value for it). |
| 478 | unsetenv(vsoc::kCuttlefishConfigEnvVarName); |
| 479 | |
| 480 | return ResolveInstanceFiles(); |
| 481 | } |
| 482 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 483 | std::string cpp_basename(const std::string& str) { |
| 484 | char* copy = strdup(str.c_str()); // basename may modify its argument |
| 485 | std::string ret(basename(copy)); |
| 486 | free(copy); |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | bool CleanPriorFiles(const std::string& path, const std::set<std::string>& preserving) { |
| 491 | if (preserving.count(cpp_basename(path))) { |
| 492 | LOG(INFO) << "Preserving: " << path; |
| 493 | return true; |
| 494 | } |
| 495 | struct stat statbuf; |
| 496 | if (lstat(path.c_str(), &statbuf) < 0) { |
| 497 | int error_num = errno; |
| 498 | if (error_num == ENOENT) { |
| 499 | return true; |
| 500 | } else { |
| 501 | LOG(ERROR) << "Could not stat \"" << path << "\": " << strerror(error_num); |
| 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | if ((statbuf.st_mode & S_IFMT) != S_IFDIR) { |
| 506 | LOG(INFO) << "Deleting: " << path; |
| 507 | if (unlink(path.c_str()) < 0) { |
| 508 | int error_num = errno; |
| 509 | LOG(ERROR) << "Could not unlink \"" << path << "\", error was " << strerror(error_num); |
| 510 | return false; |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(path.c_str()), closedir); |
| 515 | if (!dir) { |
| 516 | int error_num = errno; |
| 517 | LOG(ERROR) << "Could not clean \"" << path << "\": error was " << strerror(error_num); |
| 518 | return false; |
| 519 | } |
| 520 | for (auto entity = readdir(dir.get()); entity != nullptr; entity = readdir(dir.get())) { |
| 521 | std::string entity_name(entity->d_name); |
| 522 | if (entity_name == "." || entity_name == "..") { |
| 523 | continue; |
| 524 | } |
| 525 | std::string entity_path = path + "/" + entity_name; |
| 526 | if (!CleanPriorFiles(entity_path.c_str(), preserving)) { |
| 527 | return false; |
| 528 | } |
| 529 | } |
| 530 | if (rmdir(path.c_str()) < 0) { |
| 531 | if (!(errno == EEXIST || errno == ENOTEMPTY)) { |
| 532 | // If EEXIST or ENOTEMPTY, probably because a file was preserved |
| 533 | int error_num = errno; |
| 534 | LOG(ERROR) << "Could not rmdir \"" << path << "\", error was " << strerror(error_num); |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | bool CleanPriorFiles(const std::vector<std::string>& paths, const std::set<std::string>& preserving) { |
| 542 | std::string prior_files; |
| 543 | for (auto path : paths) { |
| 544 | struct stat statbuf; |
| 545 | if (stat(path.c_str(), &statbuf) < 0 && errno != ENOENT) { |
| 546 | // If ENOENT, it doesn't exist yet, so there is no work to do' |
| 547 | int error_num = errno; |
| 548 | LOG(ERROR) << "Could not stat \"" << path << "\": " << strerror(error_num); |
| 549 | return false; |
| 550 | } |
| 551 | bool is_directory = (statbuf.st_mode & S_IFMT) == S_IFDIR; |
| 552 | prior_files += (is_directory ? (path + "/*") : path) + " "; |
| 553 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 554 | LOG(INFO) << "Assuming prior files of " << prior_files; |
Alistair Delva | 7f09b73 | 2020-01-30 18:18:12 -0800 | [diff] [blame] | 555 | std::string lsof_cmd = "lsof -t " + prior_files + " >/dev/null 2>&1"; |
| 556 | int rval = std::system(lsof_cmd.c_str()); |
| 557 | // lsof returns 0 if any of the files are open |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 558 | if (WEXITSTATUS(rval) == 0) { |
| 559 | LOG(ERROR) << "Clean aborted: files are in use"; |
| 560 | return false; |
| 561 | } |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 562 | for (const auto& path : paths) { |
| 563 | if (!CleanPriorFiles(path, preserving)) { |
| 564 | LOG(ERROR) << "Remove of file under \"" << path << "\" failed"; |
| 565 | return false; |
| 566 | } |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 567 | } |
| 568 | return true; |
| 569 | } |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 570 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 571 | bool CleanPriorFiles(const vsoc::CuttlefishConfig& config, const std::set<std::string>& preserving) { |
| 572 | std::vector<std::string> paths = { |
| 573 | // Everything in the assembly directory |
| 574 | FLAGS_assembly_dir, |
| 575 | // The environment file |
| 576 | GetCuttlefishEnvPath(), |
| 577 | // The global link to the config file |
| 578 | vsoc::GetGlobalConfigFileLink(), |
| 579 | }; |
| 580 | for (const auto& instance : config.Instances()) { |
| 581 | paths.push_back(instance.instance_dir()); |
| 582 | } |
| 583 | paths.push_back(FLAGS_instance_dir); |
| 584 | return CleanPriorFiles(paths, preserving); |
| 585 | } |
| 586 | |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 587 | bool DecompressKernel(const std::string& src, const std::string& dst) { |
Cody Schuffelen | 3dff698 | 2019-12-05 16:24:12 -0800 | [diff] [blame] | 588 | cvd::Command decomp_cmd(vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux")); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 589 | decomp_cmd.AddParameter(src); |
| 590 | auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666); |
| 591 | if (!output_file->IsOpen()) { |
| 592 | LOG(ERROR) << "Unable to create decompressed image file: " |
| 593 | << output_file->StrError(); |
| 594 | return false; |
| 595 | } |
| 596 | decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file); |
Cody Schuffelen | e567087 | 2019-12-10 15:04:59 -0800 | [diff] [blame] | 597 | auto decomp_proc = decomp_cmd.Start(); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 598 | return decomp_proc.Started() && decomp_proc.Wait() == 0; |
| 599 | } |
Cody Schuffelen | 78824ed | 2019-09-06 17:43:15 -0700 | [diff] [blame] | 600 | |
| 601 | void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) { |
| 602 | auto adb_modes = config.adb_mode(); |
| 603 | adb_modes.erase(vsoc::AdbMode::Unknown); |
| 604 | if (adb_modes.size() < 1) { |
| 605 | LOG(INFO) << "ADB not enabled"; |
| 606 | } |
| 607 | } |
| 608 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 609 | } // namespace |
| 610 | |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 611 | namespace { |
| 612 | |
| 613 | std::vector<ImagePartition> disk_config() { |
| 614 | std::vector<ImagePartition> partitions; |
Cody Schuffelen | 4f7e441 | 2019-08-30 16:10:59 -0700 | [diff] [blame] | 615 | partitions.push_back(ImagePartition { |
Alistair Delva | af8ad62 | 2020-04-24 17:38:12 -0700 | [diff] [blame^] | 616 | .label = "misc", |
| 617 | .image_file_path = FLAGS_misc_image, |
| 618 | }); |
| 619 | partitions.push_back(ImagePartition { |
| 620 | .label = "boot_a", |
| 621 | .image_file_path = FLAGS_boot_image, |
| 622 | }); |
| 623 | partitions.push_back(ImagePartition { |
| 624 | .label = "boot_b", |
| 625 | .image_file_path = FLAGS_boot_image, |
| 626 | }); |
| 627 | partitions.push_back(ImagePartition { |
| 628 | .label = "vendor_boot_a", |
| 629 | .image_file_path = FLAGS_vendor_boot_image, |
| 630 | }); |
| 631 | partitions.push_back(ImagePartition { |
| 632 | .label = "vendor_boot_b", |
| 633 | .image_file_path = FLAGS_vendor_boot_image, |
| 634 | }); |
| 635 | partitions.push_back(ImagePartition { |
Cody Schuffelen | 4f7e441 | 2019-08-30 16:10:59 -0700 | [diff] [blame] | 636 | .label = "super", |
| 637 | .image_file_path = FLAGS_super_image, |
| 638 | }); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 639 | partitions.push_back(ImagePartition { |
| 640 | .label = "userdata", |
| 641 | .image_file_path = FLAGS_data_image, |
| 642 | }); |
| 643 | partitions.push_back(ImagePartition { |
| 644 | .label = "cache", |
| 645 | .image_file_path = FLAGS_cache_image, |
| 646 | }); |
| 647 | partitions.push_back(ImagePartition { |
| 648 | .label = "metadata", |
| 649 | .image_file_path = FLAGS_metadata_image, |
| 650 | }); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 651 | return partitions; |
| 652 | } |
| 653 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 654 | std::chrono::system_clock::time_point LastUpdatedInputDisk() { |
| 655 | std::chrono::system_clock::time_point ret; |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 656 | for (auto& partition : disk_config()) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 657 | auto partition_mod_time = cvd::FileModificationTime(partition.image_file_path); |
| 658 | if (partition_mod_time > ret) { |
| 659 | ret = partition_mod_time; |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 662 | return ret; |
| 663 | } |
| 664 | |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 665 | bool ShouldCreateCompositeDisk(const vsoc::CuttlefishConfig& config) { |
| 666 | if (!cvd::FileExists(config.composite_disk_path())) { |
| 667 | return true; |
| 668 | } |
| 669 | auto composite_age = cvd::FileModificationTime(config.composite_disk_path()); |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 670 | return composite_age < LastUpdatedInputDisk(); |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Ram Muthiah | 3db0a56 | 2019-08-26 13:35:19 -0700 | [diff] [blame] | 673 | bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path, |
| 674 | const std::string& ramdisk_b_path) { |
| 675 | // clear out file of any pre-existing content |
| 676 | std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc); |
| 677 | std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary); |
| 678 | std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary); |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 679 | |
Ram Muthiah | 3db0a56 | 2019-08-26 13:35:19 -0700 | [diff] [blame] | 680 | 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] | 681 | return false; |
| 682 | } |
| 683 | |
Ram Muthiah | 3db0a56 | 2019-08-26 13:35:19 -0700 | [diff] [blame] | 684 | new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf(); |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 685 | return true; |
| 686 | } |
| 687 | |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 688 | off_t AvailableSpaceAtPath(const std::string& path) { |
| 689 | struct statvfs vfs; |
| 690 | if (statvfs(path.c_str(), &vfs) != 0) { |
| 691 | int error_num = errno; |
| 692 | LOG(ERROR) << "Could not find space available at " << path << ", error was " |
| 693 | << strerror(error_num); |
| 694 | return 0; |
| 695 | } |
| 696 | return vfs.f_bsize * vfs.f_bavail; // block size * free blocks for unprivileged users |
| 697 | } |
| 698 | |
| 699 | off_t USERDATA_IMAGE_RESERVED = 4l * (1l << 30l); // 4 GiB |
| 700 | off_t AGGREGATE_IMAGE_RESERVED = 12l * (1l << 30l); // 12 GiB |
| 701 | |
| 702 | bool CreateCompositeDisk(const vsoc::CuttlefishConfig& config) { |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 703 | if (!cvd::SharedFD::Open(config.composite_disk_path().c_str(), O_WRONLY | O_CREAT, 0644)->IsOpen()) { |
| 704 | LOG(ERROR) << "Could not ensure " << config.composite_disk_path() << " exists"; |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 705 | return false; |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 706 | } |
Cody Schuffelen | c3fd6e5 | 2019-08-30 15:14:07 -0700 | [diff] [blame] | 707 | if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) { |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 708 | auto existing_size = cvd::FileSize(FLAGS_data_image); |
| 709 | auto available_space = AvailableSpaceAtPath(FLAGS_data_image); |
| 710 | if (available_space < USERDATA_IMAGE_RESERVED - existing_size) { |
| 711 | // TODO(schuffelen): Duplicate this check in run_cvd when it can run on a separate machine |
| 712 | LOG(ERROR) << "Not enough space in fs containing " << FLAGS_data_image; |
| 713 | LOG(ERROR) << "Wanted " << (USERDATA_IMAGE_RESERVED - existing_size); |
| 714 | LOG(ERROR) << "Got " << available_space; |
| 715 | return false; |
| 716 | } |
Cody Schuffelen | a08d9dd | 2019-12-16 17:55:40 -0800 | [diff] [blame] | 717 | std::string header_path = config.AssemblyPath("gpt_header.img"); |
| 718 | std::string footer_path = config.AssemblyPath("gpt_footer.img"); |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 719 | CreateCompositeDisk(disk_config(), header_path, footer_path, config.composite_disk_path()); |
Cody Schuffelen | c3fd6e5 | 2019-08-30 15:14:07 -0700 | [diff] [blame] | 720 | } else { |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 721 | auto existing_size = cvd::FileSize(config.composite_disk_path()); |
| 722 | auto available_space = AvailableSpaceAtPath(config.composite_disk_path()); |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 723 | if (available_space < AGGREGATE_IMAGE_RESERVED - existing_size) { |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 724 | LOG(ERROR) << "Not enough space to create " << config.composite_disk_path(); |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 725 | LOG(ERROR) << "Wanted " << (AGGREGATE_IMAGE_RESERVED - existing_size); |
| 726 | LOG(ERROR) << "Got " << available_space; |
| 727 | return false; |
| 728 | } |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 729 | AggregateImage(disk_config(), config.composite_disk_path()); |
Cody Schuffelen | c3fd6e5 | 2019-08-30 15:14:07 -0700 | [diff] [blame] | 730 | } |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 731 | return true; |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | } // namespace |
| 735 | |
Cody Schuffelen | 605e685 | 2019-10-16 16:22:24 -0700 | [diff] [blame] | 736 | const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig( |
Cody Schuffelen | b737ef5 | 2019-09-25 14:54:38 -0700 | [diff] [blame] | 737 | int* argc, char*** argv, cvd::FetcherConfig fetcher_config) { |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 738 | if (!ParseCommandLineFlags(argc, argv)) { |
| 739 | LOG(ERROR) << "Failed to parse command arguments"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 740 | exit(AssemblerExitCodes::kArgumentParsingError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 741 | } |
| 742 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 743 | auto boot_img_unpacker = |
| 744 | cvd::BootImageUnpacker::FromImages(FLAGS_boot_image, |
| 745 | FLAGS_vendor_boot_image); |
| 746 | { |
| 747 | // The config object is created here, but only exists in memory until the |
| 748 | // SaveConfig line below. Don't launch cuttlefish subprocesses between these |
| 749 | // two operations, as those will assume they can read the config object from |
| 750 | // disk. |
| 751 | auto config = InitializeCuttlefishConfiguration(*boot_img_unpacker, fetcher_config); |
| 752 | std::set<std::string> preserving; |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 753 | if (FLAGS_resume && ShouldCreateCompositeDisk(config)) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 754 | LOG(WARNING) << "Requested resuming a previous session (the default behavior) " |
| 755 | << "but the base images have changed under the overlay, making the " |
| 756 | << "overlay incompatible. Wiping the overlay files."; |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 757 | } else if (FLAGS_resume && !ShouldCreateCompositeDisk(config)) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 758 | preserving.insert("overlay.img"); |
| 759 | preserving.insert("gpt_header.img"); |
| 760 | preserving.insert("gpt_footer.img"); |
| 761 | preserving.insert("composite.img"); |
Alistair Delva | 3bba628 | 2020-05-14 13:52:00 -0700 | [diff] [blame] | 762 | preserving.insert("sdcard.img"); |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 763 | preserving.insert("access-kregistry"); |
A. Cody Schuffelen | c17acda | 2020-01-23 15:54:40 -0800 | [diff] [blame] | 764 | } |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 765 | if (!CleanPriorFiles(config, preserving)) { |
| 766 | LOG(ERROR) << "Failed to clean prior files"; |
| 767 | exit(AssemblerExitCodes::kPrioFilesCleanupError); |
| 768 | } |
| 769 | // Create assembly directory if it doesn't exist. |
| 770 | if (!cvd::DirectoryExists(FLAGS_assembly_dir.c_str())) { |
| 771 | LOG(INFO) << "Setting up " << FLAGS_assembly_dir; |
| 772 | if (mkdir(FLAGS_assembly_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 |
| 773 | && errno != EEXIST) { |
| 774 | LOG(ERROR) << "Failed to create assembly directory: " |
| 775 | << FLAGS_assembly_dir << ". Error: " << errno; |
| 776 | exit(AssemblerExitCodes::kAssemblyDirCreationError); |
| 777 | } |
| 778 | } |
| 779 | for (const auto& instance : config.Instances()) { |
| 780 | // Create instance directory if it doesn't exist. |
| 781 | if (!cvd::DirectoryExists(instance.instance_dir().c_str())) { |
| 782 | LOG(INFO) << "Setting up " << FLAGS_instance_dir << ".N"; |
| 783 | if (mkdir(instance.instance_dir().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 |
| 784 | && errno != EEXIST) { |
| 785 | LOG(ERROR) << "Failed to create instance directory: " |
| 786 | << FLAGS_instance_dir << ". Error: " << errno; |
| 787 | exit(AssemblerExitCodes::kInstanceDirCreationError); |
| 788 | } |
| 789 | } |
| 790 | auto internal_dir = instance.instance_dir() + "/" + vsoc::kInternalDirName; |
| 791 | if (!cvd::DirectoryExists(internal_dir)) { |
| 792 | if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 |
| 793 | && errno != EEXIST) { |
| 794 | LOG(ERROR) << "Failed to create internal instance directory: " |
| 795 | << internal_dir << ". Error: " << errno; |
| 796 | exit(AssemblerExitCodes::kInstanceDirCreationError); |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | if (!SaveConfig(config)) { |
| 801 | LOG(ERROR) << "Failed to initialize configuration"; |
| 802 | exit(AssemblerExitCodes::kCuttlefishConfigurationInitError); |
Jorge E. Moreira | 1e8e2f1 | 2019-10-07 18:09:49 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 806 | std::string first_instance = FLAGS_instance_dir + "." + std::to_string(vsoc::GetInstance()); |
| 807 | if (symlink(first_instance.c_str(), FLAGS_instance_dir.c_str()) < 0) { |
| 808 | LOG(ERROR) << "Could not symlink \"" << first_instance << "\" to \"" << FLAGS_instance_dir << "\""; |
| 809 | exit(cvd::kCuttlefishConfigurationInitError); |
A. Cody Schuffelen | 3a60a87 | 2020-01-23 19:03:50 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 812 | if (!cvd::FileHasContent(FLAGS_boot_image)) { |
| 813 | LOG(ERROR) << "File not found: " << FLAGS_boot_image; |
| 814 | exit(cvd::kCuttlefishConfigurationInitError); |
| 815 | } |
| 816 | |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 817 | if (!cvd::FileHasContent(FLAGS_vendor_boot_image)) { |
| 818 | LOG(ERROR) << "File not found: " << FLAGS_vendor_boot_image; |
| 819 | exit(cvd::kCuttlefishConfigurationInitError); |
| 820 | } |
| 821 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 822 | // Do this early so that the config object is ready for anything that needs it |
| 823 | auto config = vsoc::CuttlefishConfig::Get(); |
| 824 | if (!config) { |
| 825 | LOG(ERROR) << "Failed to obtain config singleton"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 826 | exit(AssemblerExitCodes::kCuttlefishConfigurationInitError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(), |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 830 | config->vendor_ramdisk_image_path(), |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 831 | config->use_unpacked_kernel() |
| 832 | ? config->kernel_image_path() |
| 833 | : "")) { |
| 834 | LOG(ERROR) << "Failed to unpack boot image"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 835 | exit(AssemblerExitCodes::kBootImageUnpackError); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Ram Muthiah | 1aa02f8 | 2019-10-22 20:26:28 +0000 | [diff] [blame] | 838 | // TODO(134522463) as part of the bootloader refactor, repack the vendor boot |
| 839 | // image and use the bootloader to load both the boot and vendor ramdisk. |
| 840 | // Until then, this hack to get gki modules into cuttlefish will suffice. |
| 841 | |
| 842 | // If a vendor ramdisk comes in via this mechanism, let it supercede the one |
| 843 | // in the vendor boot image. This flag is what kernel presubmit testing uses |
| 844 | // to pass in the kernel ramdisk. |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 845 | |
| 846 | // If no kernel is passed in or an initramfs is made available, the default |
| 847 | // vendor boot ramdisk or the initramfs provided should be appended to the |
| 848 | // boot ramdisk. If a kernel IS provided with no initramfs, it is safe to |
| 849 | // safe to assume that the kernel was built with no modules and expects no |
| 850 | // modules for cf to run properly. |
Cody Schuffelen | cd3563a | 2019-10-29 16:35:06 -0700 | [diff] [blame] | 851 | std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath); |
| 852 | std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel; |
| 853 | std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg); |
| 854 | std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk; |
| 855 | if(!foreign_kernel.size() || foreign_ramdisk.size()) { |
Ram Muthiah | d512c13 | 2019-10-23 17:20:45 -0700 | [diff] [blame] | 856 | const std::string& vendor_ramdisk_path = |
| 857 | config->initramfs_path().size() ? config->initramfs_path() |
| 858 | : config->vendor_ramdisk_image_path(); |
| 859 | if(!ConcatRamdisks(config->final_ramdisk_path(), |
| 860 | config->ramdisk_image_path(), vendor_ramdisk_path)) { |
| 861 | LOG(ERROR) << "Failed to concatenate ramdisk and vendor ramdisk"; |
| 862 | exit(AssemblerExitCodes::kInitRamFsConcatError); |
| 863 | } |
Ram Muthiah | aad97c5 | 2019-08-14 17:05:01 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 866 | if (config->decompress_kernel()) { |
| 867 | if (!DecompressKernel(config->kernel_image_path(), |
| 868 | config->decompressed_kernel_image_path())) { |
| 869 | LOG(ERROR) << "Failed to decompress kernel"; |
Cody Schuffelen | e71fa35 | 2019-09-10 16:11:58 -0700 | [diff] [blame] | 870 | exit(AssemblerExitCodes::kKernelDecompressError); |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 874 | ValidateAdbModeFlag(*config); |
| 875 | |
Yifan Hong | 19d713e | 2019-05-01 14:12:07 -0700 | [diff] [blame] | 876 | // Create misc if necessary |
| 877 | if (!InitializeMiscImage(FLAGS_misc_image)) { |
| 878 | exit(cvd::kCuttlefishConfigurationInitError); |
| 879 | } |
| 880 | |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 881 | // Create data if necessary |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 882 | if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) { |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 883 | exit(cvd::kCuttlefishConfigurationInitError); |
| 884 | } |
| 885 | |
Cody Schuffelen | 69c1959 | 2019-05-31 16:00:30 -0700 | [diff] [blame] | 886 | if (!cvd::FileExists(FLAGS_metadata_image)) { |
| 887 | CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none"); |
| 888 | } |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 889 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 890 | for (const auto& instance : config->Instances()) { |
| 891 | if (!cvd::FileExists(instance.access_kregistry_path())) { |
Alistair Delva | 2df23d6 | 2020-05-14 11:26:02 -0700 | [diff] [blame] | 892 | CreateBlankImage(instance.access_kregistry_path(), 2 /* mb */, "none"); |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 893 | } |
Alistair Delva | 3bba628 | 2020-05-14 13:52:00 -0700 | [diff] [blame] | 894 | |
| 895 | if (!cvd::FileExists(instance.sdcard_path())) { |
| 896 | CreateBlankImage(instance.sdcard_path(), |
| 897 | FLAGS_blank_sdcard_image_mb, "sdcard"); |
| 898 | } |
Kenny Root | 1f50ee6 | 2020-01-24 12:06:19 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Cody Schuffelen | b737ef5 | 2019-09-25 14:54:38 -0700 | [diff] [blame] | 901 | if (SuperImageNeedsRebuilding(fetcher_config, *config)) { |
| 902 | if (!RebuildSuperImage(fetcher_config, *config, FLAGS_super_image)) { |
| 903 | LOG(ERROR) << "Super image rebuilding requested but could not be completed."; |
| 904 | exit(cvd::kCuttlefishConfigurationInitError); |
| 905 | } |
| 906 | } |
| 907 | |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 908 | if (ShouldCreateCompositeDisk(*config)) { |
Cody Schuffelen | 25c4643 | 2020-01-14 13:49:52 -0800 | [diff] [blame] | 909 | if (!CreateCompositeDisk(*config)) { |
| 910 | exit(cvd::kDiskSpaceError); |
| 911 | } |
Cody Schuffelen | 3c99f5b | 2019-06-14 17:26:01 -0700 | [diff] [blame] | 912 | } |
| 913 | |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 914 | for (auto instance : config->Instances()) { |
| 915 | auto overlay_path = instance.PerInstancePath("overlay.img"); |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 916 | if (!cvd::FileExists(overlay_path) || ShouldCreateCompositeDisk(*config) || !FLAGS_resume |
| 917 | || cvd::FileModificationTime(overlay_path) < cvd::FileModificationTime(config->composite_disk_path())) { |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 918 | if (FLAGS_resume) { |
| 919 | LOG(WARNING) << "Requested to continue an existing session, but the overlay was " |
| 920 | << "newer than its underlying composite disk. Wiping the overlay."; |
| 921 | } |
A. Cody Schuffelen | 4c28713 | 2020-02-07 17:04:23 -0800 | [diff] [blame] | 922 | CreateQcowOverlay(config->crosvm_binary(), config->composite_disk_path(), overlay_path); |
Alistair Delva | 2df23d6 | 2020-05-14 11:26:02 -0700 | [diff] [blame] | 923 | CreateBlankImage(instance.access_kregistry_path(), 2 /* mb */, "none"); |
A. Cody Schuffelen | 9e2f73e | 2020-02-04 17:19:54 -0800 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | for (auto instance : config->Instances()) { |
| 928 | // Check that the files exist |
| 929 | for (const auto& file : instance.virtual_disk_paths()) { |
| 930 | if (!file.empty() && !cvd::FileHasContent(file.c_str())) { |
| 931 | LOG(ERROR) << "File not found: " << file; |
| 932 | exit(cvd::kCuttlefishConfigurationInitError); |
| 933 | } |
Cody Schuffelen | 2b51bab | 2019-01-29 18:14:48 -0800 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 937 | return config; |
| 938 | } |
| 939 | |
| 940 | std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) { |
Cody Schuffelen | 2168c24 | 2019-12-13 16:15:27 -0800 | [diff] [blame] | 941 | return config.AssemblyPath("cuttlefish_config.json"); |
Cody Schuffelen | 20ecaca | 2019-01-29 17:43:38 -0800 | [diff] [blame] | 942 | } |