blob: 3750e6d33ab36d13d9784988364ee8dc36ec1365 [file] [log] [blame]
Cody Schuffelen147b88e2019-09-09 16:00:11 -07001#include "host/commands/assemble_cvd/flags.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08002
Cody Schuffelen25c46432020-01-14 13:49:52 -08003#include <sys/statvfs.h>
4
Jorge E. Moreirae049b792019-12-18 18:17:48 -08005#include <algorithm>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08006#include <iostream>
Ram Muthiahaad97c52019-08-14 17:05:01 -07007#include <fstream>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08008
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -08009#include <android-base/strings.h>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080010#include <gflags/gflags.h>
11#include <glog/logging.h>
12
13#include "common/libs/utils/environment.h"
14#include "common/libs/utils/files.h"
Cody Schuffelen147b88e2019-09-09 16:00:11 -070015#include "host/commands/assemble_cvd/boot_image_unpacker.h"
16#include "host/commands/assemble_cvd/data_image.h"
17#include "host/commands/assemble_cvd/image_aggregator.h"
18#include "host/commands/assemble_cvd/assembler_defs.h"
Cody Schuffelenb737ef52019-09-25 14:54:38 -070019#include "host/commands/assemble_cvd/super_image_mixer.h"
Cody Schuffelen605e6852019-10-16 16:22:24 -070020#include "host/libs/config/fetcher_config.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080021#include "host/libs/vm_manager/crosvm_manager.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080022#include "host/libs/vm_manager/qemu_manager.h"
23#include "host/libs/vm_manager/vm_manager.h"
24
Cody Schuffelen47c57852019-12-13 13:50:50 -080025using vsoc::ForCurrentInstance;
Cody Schuffelene71fa352019-09-10 16:11:58 -070026using cvd::AssemblerExitCodes;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080027
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080028DEFINE_string(cache_image, "", "Location of the cache partition image.");
Paul Trautrimba8f8e92019-03-12 17:55:48 +090029DEFINE_string(metadata_image, "", "Location of the metadata partition image "
30 "to be generated.");
31DEFINE_int32(blank_metadata_image_mb, 16,
32 "The size of the blank metadata image to generate, MB.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080033DEFINE_int32(cpus, 2, "Virtual CPU count.");
34DEFINE_string(data_image, "", "Location of the data partition image.");
35DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
36 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
37 "'always_create'.");
38DEFINE_int32(blank_data_image_mb, 0,
39 "The size of the blank data image to generate, MB.");
Cody Schuffelenbdca7b82019-12-04 20:09:22 +000040DEFINE_string(blank_data_image_fmt, "ext4",
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080041 "The fs format for the blank data image. Used with mkfs.");
42DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000043 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080044
45DEFINE_int32(x_res, 720, "Width of the screen in pixels");
46DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
47DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
48DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080049DEFINE_string(kernel_path, "",
50 "Path to the kernel. Overrides the one from the boot image");
Ram Muthiahaad97c52019-08-14 17:05:01 -070051DEFINE_string(initramfs_path, "", "Path to the initramfs");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080052DEFINE_bool(decompress_kernel, false,
Jorge E. Moreira36294042019-06-07 15:23:18 -070053 "Whether to decompress the kernel image.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080054DEFINE_string(extra_kernel_cmdline, "",
55 "Additional flags to put on the kernel command line");
56DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070057DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen79d4c192019-12-04 15:13:26 -080058 "Whether to run in enforcing mode (non permissive).");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080059DEFINE_bool(guest_audit_security, true,
60 "Whether to log security audits.");
Alistair Delva33b861f2019-12-26 12:23:59 -080061DEFINE_bool(guest_force_normal_boot, true,
62 "Whether to force the boot sequence to skip recovery.");
Yifan Hong19d713e2019-05-01 14:12:07 -070063DEFINE_string(boot_image, "",
64 "Location of cuttlefish boot image. If empty it is assumed to be "
65 "boot.img in the directory specified by -system_image_dir.");
Ram Muthiah1aa02f82019-10-22 20:26:28 +000066DEFINE_string(vendor_boot_image, "",
67 "Location of cuttlefish vendor boot image. If empty it is assumed to "
68 "be vendor_boot.img in the directory specified by -system_image_dir.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080069DEFINE_int32(memory_mb, 2048,
70 "Total amount of memory available for guest, MB.");
Cody Schuffelen47c57852019-12-13 13:50:50 -080071DEFINE_string(mobile_interface, ForCurrentInstance("cvd-mbr-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080072 "Network interface to use for mobile networking");
Cody Schuffelen47c57852019-12-13 13:50:50 -080073DEFINE_string(mobile_tap_name, ForCurrentInstance("cvd-mtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080074 "The name of the tap interface to use for mobile");
Cody Schuffelen47c57852019-12-13 13:50:50 -080075DEFINE_string(serial_number, ForCurrentInstance("CUTTLEFISHCVD"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080076 "Serial number to use for the device");
Cody Schuffelenab6d3452019-12-13 15:54:27 -080077DEFINE_string(assembly_dir,
78 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_assembly",
79 "A directory to put generated files common between instances");
80DEFINE_string(instance_dir,
81 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime",
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080082 "A directory to put all instance specific files");
83DEFINE_string(
Alistair Strachan5ab095c2019-05-23 20:41:20 +000084 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080085 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070086DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070087 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
88 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070089
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080090DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
91 "Location of the system partition images.");
Alistair Strachan050ca932018-11-27 12:41:19 -080092DEFINE_string(super_image, "", "Location of the super partition image.");
Yifan Hong19d713e2019-05-01 14:12:07 -070093DEFINE_string(misc_image, "",
94 "Location of the misc partition image. If the image does not "
95 "exist, a blank new misc partition image is created.");
David Anderson64581ce2019-10-03 17:52:22 -070096DEFINE_string(composite_disk, "", "Location of the composite disk image. "
97 "If empty, a composite disk is not used.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080098
99DEFINE_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.");
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800102DEFINE_bool(start_vnc_server, false, "Whether to start the vnc server process.");
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800103
104DEFINE_bool(start_webrtc, false, "Whether to start the webrtc process.");
105
106DEFINE_string(
107 webrtc_assets_dir,
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800108 vsoc::DefaultHostArtifactsPath("usr/share/webrtc/assets"),
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800109 "Path to WebRTC webpage assets.");
110
111DEFINE_string(
112 webrtc_certs_dir,
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800113 vsoc::DefaultHostArtifactsPath("usr/share/webrtc/certs"),
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800114 "Path to WebRTC certificates directory.");
115
116DEFINE_string(
117 webrtc_public_ip,
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800118 "127.0.0.1",
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800119 "Public IPv4 address of your server, a.b.c.d format");
120
121DEFINE_bool(
122 webrtc_enable_adb_websocket,
123 false,
124 "If enabled, exposes local adb service through a websocket.");
125
Cody Schuffelen47c57852019-12-13 13:50:50 -0800126DEFINE_int32(vnc_server_port, ForCurrentInstance(6444),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800127 "The port on which the vnc server should listen");
Cody Schuffelen10743332019-04-15 16:50:49 -0700128DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen3c35fd82019-12-11 18:39:46 -0800129 "Mode for ADB connection."
Cody Schuffelen63d10052019-02-26 12:21:53 -0800130 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
131 "'native_vsock' for a direct connection to the guest ADB over "
132 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
133 "the guest ADB server, or a comma separated list of types as in "
Cody Schuffelen3c35fd82019-12-11 18:39:46 -0800134 "'native_vsock,vsock_half_tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800135DEFINE_bool(run_adb_connector, true,
136 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000137 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen47c57852019-12-13 13:50:50 -0800138DEFINE_string(wifi_tap_name, ForCurrentInstance("cvd-wtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800139 "The name of the tap interface to use for wifi");
140DEFINE_int32(vsock_guest_cid,
141 vsoc::GetDefaultPerInstanceVsockCid(),
142 "Guest identifier for vsock. Disabled if under 3.");
143
Cody Schuffelen47c57852019-12-13 13:50:50 -0800144DEFINE_string(uuid, vsoc::ForCurrentInstance(vsoc::kDefaultUuidPrefix),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800145 "UUID to use for the device. Random if not specified");
146DEFINE_bool(daemon, false,
147 "Run cuttlefish in background, the launcher exits on boot "
148 "completed/failed");
149
150DEFINE_string(device_title, "", "Human readable name for the instance, "
151 "used by the vnc_server for its server title");
152DEFINE_string(setupwizard_mode, "DISABLED",
153 "One of DISABLED,OPTIONAL,REQUIRED");
154
155DEFINE_string(qemu_binary,
156 "/usr/bin/qemu-system-x86_64",
157 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800158DEFINE_string(crosvm_binary,
159 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
160 "The Crosvm binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800161DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800162DEFINE_string(logcat_mode, "", "How to send android's log messages from "
163 "guest to host. One of [serial, vsock]");
Ram Muthiah6e9c98c2019-05-28 15:18:27 -0700164DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on "
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700165 "both the guest and the host");
Cody Schuffelen1300f122019-05-28 18:24:34 -0700166DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
167DEFINE_string(bootloader, "", "Bootloader binary path");
David Anderson637bfd62019-09-26 13:23:21 -0700168DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, "
169 "the slot will be chosen based on the misc partition if using a "
170 "bootloader. It will default to 'a' if empty and not using a "
171 "bootloader.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700172
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800173namespace {
174
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700175const std::string kKernelDefaultPath = "kernel";
176const std::string kInitramfsImg = "initramfs.img";
177const std::string kRamdiskConcatExt = ".concat";
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700178
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800179bool ResolveInstanceFiles() {
180 if (FLAGS_system_image_dir.empty()) {
181 LOG(ERROR) << "--system_image_dir must be specified.";
182 return false;
183 }
184
185 // If user did not specify location of either of these files, expect them to
186 // be placed in --system_image_dir location.
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800187 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
188 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
189 google::FlagSettingMode::SET_FLAGS_DEFAULT);
190 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
191 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
192 google::FlagSettingMode::SET_FLAGS_DEFAULT);
193 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
194 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
195 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900196 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
197 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
198 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800199 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
200 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
201 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Yifan Hong19d713e2019-05-01 14:12:07 -0700202 std::string default_misc_image = FLAGS_system_image_dir + "/misc.img";
203 SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(),
204 google::FlagSettingMode::SET_FLAGS_DEFAULT);
David Anderson64581ce2019-10-03 17:52:22 -0700205 std::string default_composite_disk = FLAGS_system_image_dir + "/composite.img";
206 SetCommandLineOptionWithMode("composite_disk", default_composite_disk.c_str(),
207 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000208 std::string default_vendor_boot_image = FLAGS_system_image_dir
209 + "/vendor_boot.img";
210 SetCommandLineOptionWithMode("vendor_boot_image",
211 default_vendor_boot_image.c_str(),
212 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800213
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800214 return true;
215}
216
217std::string GetCuttlefishEnvPath() {
218 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
219}
220
Cody Schuffelen2168c242019-12-13 16:15:27 -0800221std::string GetLegacyConfigFilePath(const vsoc::CuttlefishConfig& config) {
222 return config.ForDefaultInstance().PerInstancePath("cuttlefish_config.json");
223}
224
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700225int GetHostPort() {
226 constexpr int kFirstHostPort = 6520;
Cody Schuffelen47c57852019-12-13 13:50:50 -0800227 return vsoc::ForCurrentInstance(kFirstHostPort);
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700228}
229
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800230int NumStreamers() {
231 auto start_flags = {FLAGS_start_vnc_server, FLAGS_start_webrtc};
232 return std::count(start_flags.begin(), start_flags.end(), true);
233}
234
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800235// 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
237bool InitializeCuttlefishConfiguration(
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700238 const cvd::BootImageUnpacker& boot_image_unpacker,
239 const cvd::FetcherConfig& fetcher_config) {
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800240 // At most one streamer can be started.
241 CHECK(NumStreamers() <= 1);
242
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800243 vsoc::CuttlefishConfig tmp_config_obj;
Cody Schuffelenab6d3452019-12-13 15:54:27 -0800244 tmp_config_obj.set_assembly_dir(FLAGS_assembly_dir);
Cody Schuffelen47c57852019-12-13 13:50:50 -0800245 auto instance = tmp_config_obj.ForDefaultInstance();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800246 // Set this first so that calls to PerInstancePath below are correct
Cody Schuffeleneb242c42019-12-13 15:16:50 -0800247 instance.set_instance_dir(FLAGS_instance_dir);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800248 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
249 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
250 return false;
251 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700252 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
253 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
254 return false;
255 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800256 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700257 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800258 if (vm_manager::VmManager::ConfigureGpuMode(tmp_config_obj.vm_manager(),
259 tmp_config_obj.gpu_mode()).empty()) {
Greg Hartmana3c552d2019-03-28 18:20:48 -0700260 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
261 " does not work with vm_manager=" << FLAGS_vm_manager;
262 return false;
263 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800264
Cody Schuffelen47c57852019-12-13 13:50:50 -0800265 instance.set_serial_number(FLAGS_serial_number);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800266
267 tmp_config_obj.set_cpus(FLAGS_cpus);
268 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
269
270 tmp_config_obj.set_dpi(FLAGS_dpi);
271 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
272 tmp_config_obj.set_x_res(FLAGS_x_res);
273 tmp_config_obj.set_y_res(FLAGS_y_res);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800274 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
275 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -0800276 std::vector<std::string> adb = android::base::Split(FLAGS_adb_mode, ",");
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800277 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelen47c57852019-12-13 13:50:50 -0800278 instance.set_host_port(GetHostPort());
279 instance.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800280
Cody Schuffelen47c57852019-12-13 13:50:50 -0800281 instance.set_device_title(FLAGS_device_title);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700282 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
283 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
284 if (foreign_kernel.size()) {
285 tmp_config_obj.set_kernel_image_path(foreign_kernel);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800286 tmp_config_obj.set_use_unpacked_kernel(false);
287 } else {
288 tmp_config_obj.set_kernel_image_path(
Cody Schuffelenf53ded12019-12-13 16:05:22 -0800289 tmp_config_obj.AssemblyPath(kKernelDefaultPath.c_str()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800290 tmp_config_obj.set_use_unpacked_kernel(true);
291 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800292 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
293 if (FLAGS_decompress_kernel) {
294 tmp_config_obj.set_decompressed_kernel_image_path(
Cody Schuffelenf53ded12019-12-13 16:05:22 -0800295 tmp_config_obj.AssemblyPath("vmlinux"));
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800296 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800297
Cody Schuffelenf53ded12019-12-13 16:05:22 -0800298 auto ramdisk_path = tmp_config_obj.AssemblyPath("ramdisk.img");
299 auto vendor_ramdisk_path = tmp_config_obj.AssemblyPath("vendor_ramdisk.img");
Cody Schuffelen1e645542019-11-19 15:44:22 -0800300 if (!boot_image_unpacker.HasRamdiskImage()) {
301 LOG(INFO) << "A ramdisk is required, but the boot image did not have one.";
302 return false;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800303 }
304
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800305 tmp_config_obj.set_boot_image_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
306 tmp_config_obj.set_loop_max_part(FLAGS_loop_max_part);
307 tmp_config_obj.set_guest_enforce_security(FLAGS_guest_enforce_security);
308 tmp_config_obj.set_guest_audit_security(FLAGS_guest_audit_security);
Alistair Delva33b861f2019-12-26 12:23:59 -0800309 tmp_config_obj.set_guest_force_normal_boot(FLAGS_guest_force_normal_boot);
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800310 tmp_config_obj.set_extra_kernel_cmdline(FLAGS_extra_kernel_cmdline);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800311
Cody Schuffelen2ced6f12019-11-19 15:54:10 -0800312 tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk});
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700313
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800314 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Ram Muthiahd512c132019-10-23 17:20:45 -0700315 tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path);
316
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700317 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
318 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
319 if (foreign_kernel.size() && !foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700320 // If there's a kernel that's passed in without an initramfs, that implies
321 // user error or a kernel built with no modules. In either case, let's
322 // choose to avoid loading the modules from the vendor ramdisk which are
323 // built for the default cf kernel. Once boot occurs, user error will
324 // become obvious.
325 tmp_config_obj.set_final_ramdisk_path(ramdisk_path);
326 } else {
327 tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700328 if(foreign_ramdisk.size()) {
329 tmp_config_obj.set_initramfs_path(foreign_ramdisk);
Ram Muthiahd512c132019-10-23 17:20:45 -0700330 }
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700331 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800332
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800333 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800334 tmp_config_obj.set_logcat_receiver_binary(
335 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"));
336 tmp_config_obj.set_config_server_binary(
337 vsoc::DefaultHostArtifactsPath("bin/config_server"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800338
Cody Schuffelen47c57852019-12-13 13:50:50 -0800339 instance.set_mobile_bridge_name(FLAGS_mobile_interface);
340 instance.set_mobile_tap_name(FLAGS_mobile_tap_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800341
Cody Schuffelen47c57852019-12-13 13:50:50 -0800342 instance.set_wifi_tap_name(FLAGS_wifi_tap_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800343
Cody Schuffelen47c57852019-12-13 13:50:50 -0800344 instance.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800345
Cody Schuffelen47c57852019-12-13 13:50:50 -0800346 instance.set_uuid(FLAGS_uuid);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800347
348 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800349 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800350 tmp_config_obj.set_console_forwarder_binary(
351 vsoc::DefaultHostArtifactsPath("bin/console_forwarder"));
352 tmp_config_obj.set_kernel_log_monitor_binary(
353 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800354
355 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800356 tmp_config_obj.set_vnc_server_binary(
357 vsoc::DefaultHostArtifactsPath("bin/vnc_server"));
Cody Schuffelen47c57852019-12-13 13:50:50 -0800358 instance.set_vnc_server_port(FLAGS_vnc_server_port);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800359
Jorge E. Moreirae049b792019-12-18 18:17:48 -0800360 tmp_config_obj.set_enable_webrtc(FLAGS_start_webrtc);
361 tmp_config_obj.set_webrtc_binary(
362 vsoc::DefaultHostArtifactsPath("bin/webRTC"));
363 tmp_config_obj.set_webrtc_assets_dir(FLAGS_webrtc_assets_dir);
364 tmp_config_obj.set_webrtc_public_ip(FLAGS_webrtc_public_ip);
365 tmp_config_obj.set_webrtc_certs_dir(FLAGS_webrtc_certs_dir);
366
367 tmp_config_obj.set_webrtc_enable_adb_websocket(
368 FLAGS_webrtc_enable_adb_websocket);
369
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800370 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
371 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800372 tmp_config_obj.set_adb_connector_binary(
373 vsoc::DefaultHostArtifactsPath("bin/adb_connector"));
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800374 tmp_config_obj.set_socket_vsock_proxy_binary(
375 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800376 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800377
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800378 tmp_config_obj.set_data_policy(FLAGS_data_policy);
379 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
380 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
381
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800382 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800383
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700384 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800385 tmp_config_obj.set_tombstone_receiver_binary(
386 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"));
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700387
Cody Schuffelen1300f122019-05-28 18:24:34 -0700388 tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
389 tmp_config_obj.set_bootloader(FLAGS_bootloader);
390
David Anderson637bfd62019-09-26 13:23:21 -0700391 if (!FLAGS_boot_slot.empty()) {
392 tmp_config_obj.set_boot_slot(FLAGS_boot_slot);
393 }
394
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800395 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
396
397 auto config_file = GetConfigFilePath(tmp_config_obj);
398 auto config_link = vsoc::GetGlobalConfigFileLink();
399 // Save the config object before starting any host process
400 if (!tmp_config_obj.SaveToFile(config_file)) {
401 LOG(ERROR) << "Unable to save config object";
402 return false;
403 }
Cody Schuffelen2168c242019-12-13 16:15:27 -0800404 auto legacy_config_file = GetLegacyConfigFilePath(tmp_config_obj);
405 if (!tmp_config_obj.SaveToFile(legacy_config_file)) {
406 LOG(ERROR) << "Unable to save legacy config object";
407 return false;
408 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800409 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
410 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
411 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
412 << ": " << strerror(errno);
413 return false;
414 }
415
416 return true;
417}
418
419void SetDefaultFlagsForQemu() {
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800420 // TODO(b/144119457) Use the serial port.
421 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800422 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800423}
424
425void SetDefaultFlagsForCrosvm() {
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800426 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
427 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800428}
429
430bool ParseCommandLineFlags(int* argc, char*** argv) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800431 google::ParseCommandLineNonHelpFlags(argc, argv, true);
432 bool invalid_manager = false;
433 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
434 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800435 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
436 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800437 } else {
438 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
439 << std::endl;
440 invalid_manager = true;
441 }
Jorge E. Moreira1f65a4a2019-11-08 15:23:32 -0800442 if (NumStreamers() == 0) {
443 // This makes the vnc server the default streamer unless the user requests
444 // another via a --star_<streamer> flag, while at the same time it's
445 // possible to run without any streamer by setting --start_vnc_server=false.
446 SetCommandLineOptionWithMode("start_vnc_server", "true",
447 google::FlagSettingMode::SET_FLAGS_DEFAULT);
448 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800449 google::HandleCommandLineHelpFlags();
450 if (invalid_manager) {
451 return false;
452 }
453 // Set the env variable to empty (in case the caller passed a value for it).
454 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
455
456 return ResolveInstanceFiles();
457}
458
459bool CleanPriorFiles() {
460 // Everything on the instance directory
461 std::string prior_files = FLAGS_instance_dir + "/*";
Cody Schuffelenab6d3452019-12-13 15:54:27 -0800462 // Everything in the assembly directory
463 prior_files += " " + FLAGS_assembly_dir + "/*";
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800464 // The environment file
465 prior_files += " " + GetCuttlefishEnvPath();
466 // The global link to the config file
467 prior_files += " " + vsoc::GetGlobalConfigFileLink();
468 LOG(INFO) << "Assuming prior files of " << prior_files;
469 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
470 int rval = std::system(fuser_cmd.c_str());
471 // fuser returns 0 if any of the files are open
472 if (WEXITSTATUS(rval) == 0) {
473 LOG(ERROR) << "Clean aborted: files are in use";
474 return false;
475 }
476 std::string clean_command = "rm -rf " + prior_files;
477 rval = std::system(clean_command.c_str());
478 if (WEXITSTATUS(rval) != 0) {
479 LOG(ERROR) << "Remove of files failed";
480 return false;
481 }
482 return true;
483}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800484
485bool DecompressKernel(const std::string& src, const std::string& dst) {
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800486 cvd::Command decomp_cmd(vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"));
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800487 decomp_cmd.AddParameter(src);
488 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
489 if (!output_file->IsOpen()) {
490 LOG(ERROR) << "Unable to create decompressed image file: "
491 << output_file->StrError();
492 return false;
493 }
494 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
Cody Schuffelene5670872019-12-10 15:04:59 -0800495 auto decomp_proc = decomp_cmd.Start();
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800496 return decomp_proc.Started() && decomp_proc.Wait() == 0;
497}
Cody Schuffelen78824ed2019-09-06 17:43:15 -0700498
499void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) {
500 auto adb_modes = config.adb_mode();
501 adb_modes.erase(vsoc::AdbMode::Unknown);
502 if (adb_modes.size() < 1) {
503 LOG(INFO) << "ADB not enabled";
504 }
505}
506
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800507} // namespace
508
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700509namespace {
510
511std::vector<ImagePartition> disk_config() {
512 std::vector<ImagePartition> partitions;
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700513 partitions.push_back(ImagePartition {
514 .label = "super",
515 .image_file_path = FLAGS_super_image,
516 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700517 partitions.push_back(ImagePartition {
518 .label = "userdata",
519 .image_file_path = FLAGS_data_image,
520 });
521 partitions.push_back(ImagePartition {
522 .label = "cache",
523 .image_file_path = FLAGS_cache_image,
524 });
525 partitions.push_back(ImagePartition {
526 .label = "metadata",
527 .image_file_path = FLAGS_metadata_image,
528 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700529 partitions.push_back(ImagePartition {
530 .label = "boot",
531 .image_file_path = FLAGS_boot_image,
532 });
Yifan Hong19d713e2019-05-01 14:12:07 -0700533 partitions.push_back(ImagePartition {
534 .label = "misc",
535 .image_file_path = FLAGS_misc_image
536 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700537 return partitions;
538}
539
540bool ShouldCreateCompositeDisk() {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700541 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
542 // The crosvm implementation is very fast to rebuild but also more brittle due to being split
543 // into multiple files. The QEMU implementation is slow to build, but completely self-contained
544 // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU.
545 return true;
546 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700547 auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk);
548 for (auto& partition : disk_config()) {
549 auto partition_age = cvd::FileModificationTime(partition.image_file_path);
550 if (partition_age >= composite_age) {
551 LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", "
552 << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\"";
553 return true;
554 }
555 }
556 return false;
557}
558
Ram Muthiah3db0a562019-08-26 13:35:19 -0700559bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path,
560 const std::string& ramdisk_b_path) {
561 // clear out file of any pre-existing content
562 std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
563 std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary);
564 std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700565
Ram Muthiah3db0a562019-08-26 13:35:19 -0700566 if(!new_ramdisk.is_open() || !ramdisk_a.is_open() || !ramdisk_b.is_open()) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700567 return false;
568 }
569
Ram Muthiah3db0a562019-08-26 13:35:19 -0700570 new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
Ram Muthiahaad97c52019-08-14 17:05:01 -0700571 return true;
572}
573
Cody Schuffelen25c46432020-01-14 13:49:52 -0800574off_t AvailableSpaceAtPath(const std::string& path) {
575 struct statvfs vfs;
576 if (statvfs(path.c_str(), &vfs) != 0) {
577 int error_num = errno;
578 LOG(ERROR) << "Could not find space available at " << path << ", error was "
579 << strerror(error_num);
580 return 0;
581 }
582 return vfs.f_bsize * vfs.f_bavail; // block size * free blocks for unprivileged users
583}
584
585off_t USERDATA_IMAGE_RESERVED = 4l * (1l << 30l); // 4 GiB
586off_t AGGREGATE_IMAGE_RESERVED = 12l * (1l << 30l); // 12 GiB
587
588bool CreateCompositeDisk(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700589 if (FLAGS_composite_disk.empty()) {
Cody Schuffelen25c46432020-01-14 13:49:52 -0800590 LOG(ERROR) << "asked to create composite disk, but path was empty";
591 return false;
592 }
593 if (!cvd::SharedFD::Open(FLAGS_composite_disk.c_str(), O_WRONLY | O_CREAT, 0644)->IsOpen()) {
594 LOG(ERROR) << "Could not ensure " << FLAGS_composite_disk << " exists";
595 return false;
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700596 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700597 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
Cody Schuffelen25c46432020-01-14 13:49:52 -0800598 auto existing_size = cvd::FileSize(FLAGS_data_image);
599 auto available_space = AvailableSpaceAtPath(FLAGS_data_image);
600 if (available_space < USERDATA_IMAGE_RESERVED - existing_size) {
601 // TODO(schuffelen): Duplicate this check in run_cvd when it can run on a separate machine
602 LOG(ERROR) << "Not enough space in fs containing " << FLAGS_data_image;
603 LOG(ERROR) << "Wanted " << (USERDATA_IMAGE_RESERVED - existing_size);
604 LOG(ERROR) << "Got " << available_space;
605 return false;
606 }
Cody Schuffelena08d9dd2019-12-16 17:55:40 -0800607 std::string header_path = config.AssemblyPath("gpt_header.img");
608 std::string footer_path = config.AssemblyPath("gpt_footer.img");
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700609 create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk);
610 } else {
Cody Schuffelen25c46432020-01-14 13:49:52 -0800611 auto existing_size = cvd::FileSize(FLAGS_composite_disk);
612 auto available_space = AvailableSpaceAtPath(FLAGS_composite_disk);
613 if (available_space < AGGREGATE_IMAGE_RESERVED - existing_size) {
614 LOG(ERROR) << "Not enough space to create " << FLAGS_composite_disk;
615 LOG(ERROR) << "Wanted " << (AGGREGATE_IMAGE_RESERVED - existing_size);
616 LOG(ERROR) << "Got " << available_space;
617 return false;
618 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700619 aggregate_image(disk_config(), FLAGS_composite_disk);
620 }
Cody Schuffelen25c46432020-01-14 13:49:52 -0800621 return true;
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700622}
623
624} // namespace
625
Cody Schuffelen605e6852019-10-16 16:22:24 -0700626const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700627 int* argc, char*** argv, cvd::FetcherConfig fetcher_config) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800628 if (!ParseCommandLineFlags(argc, argv)) {
629 LOG(ERROR) << "Failed to parse command arguments";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700630 exit(AssemblerExitCodes::kArgumentParsingError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800631 }
632
633 // Clean up prior files before saving the config file (doing it after would
634 // delete it)
635 if (!CleanPriorFiles()) {
636 LOG(ERROR) << "Failed to clean prior files";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700637 exit(AssemblerExitCodes::kPrioFilesCleanupError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800638 }
Cody Schuffelenab6d3452019-12-13 15:54:27 -0800639 // Create assembly directory if it doesn't exist.
640 if (!cvd::DirectoryExists(FLAGS_assembly_dir.c_str())) {
641 LOG(INFO) << "Setting up " << FLAGS_assembly_dir;
642 if (mkdir(FLAGS_assembly_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
643 LOG(ERROR) << "Failed to create assembly directory: "
644 << FLAGS_assembly_dir << ". Error: " << errno;
645 exit(AssemblerExitCodes::kAssemblyDirCreationError);
646 }
647 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800648 // Create instance directory if it doesn't exist.
649 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
650 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
651 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
652 LOG(ERROR) << "Failed to create instance directory: "
653 << FLAGS_instance_dir << ". Error: " << errno;
Cody Schuffelene71fa352019-09-10 16:11:58 -0700654 exit(AssemblerExitCodes::kInstanceDirCreationError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800655 }
656 }
657
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700658 auto internal_dir = FLAGS_instance_dir + "/" + vsoc::kInternalDirName;
659 if (!cvd::DirectoryExists(internal_dir)) {
660 if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) <
661 0) {
662 LOG(ERROR) << "Failed to create internal instance directory: "
663 << internal_dir << ". Error: " << errno;
664 exit(AssemblerExitCodes::kInstanceDirCreationError);
665 }
666 }
667
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800668 if (!cvd::FileHasContent(FLAGS_boot_image)) {
669 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
670 exit(cvd::kCuttlefishConfigurationInitError);
671 }
672
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000673 if (!cvd::FileHasContent(FLAGS_vendor_boot_image)) {
674 LOG(ERROR) << "File not found: " << FLAGS_vendor_boot_image;
675 exit(cvd::kCuttlefishConfigurationInitError);
676 }
677
678 auto boot_img_unpacker =
679 cvd::BootImageUnpacker::FromImages(FLAGS_boot_image,
680 FLAGS_vendor_boot_image);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800681
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700682 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker, fetcher_config)) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800683 LOG(ERROR) << "Failed to initialize configuration";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700684 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800685 }
686 // Do this early so that the config object is ready for anything that needs it
687 auto config = vsoc::CuttlefishConfig::Get();
688 if (!config) {
689 LOG(ERROR) << "Failed to obtain config singleton";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700690 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800691 }
692
693 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000694 config->vendor_ramdisk_image_path(),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800695 config->use_unpacked_kernel()
696 ? config->kernel_image_path()
697 : "")) {
698 LOG(ERROR) << "Failed to unpack boot image";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700699 exit(AssemblerExitCodes::kBootImageUnpackError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800700 }
701
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000702 // TODO(134522463) as part of the bootloader refactor, repack the vendor boot
703 // image and use the bootloader to load both the boot and vendor ramdisk.
704 // Until then, this hack to get gki modules into cuttlefish will suffice.
705
706 // If a vendor ramdisk comes in via this mechanism, let it supercede the one
707 // in the vendor boot image. This flag is what kernel presubmit testing uses
708 // to pass in the kernel ramdisk.
Ram Muthiahd512c132019-10-23 17:20:45 -0700709
710 // If no kernel is passed in or an initramfs is made available, the default
711 // vendor boot ramdisk or the initramfs provided should be appended to the
712 // boot ramdisk. If a kernel IS provided with no initramfs, it is safe to
713 // safe to assume that the kernel was built with no modules and expects no
714 // modules for cf to run properly.
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700715 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
716 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
717 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
718 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
719 if(!foreign_kernel.size() || foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700720 const std::string& vendor_ramdisk_path =
721 config->initramfs_path().size() ? config->initramfs_path()
722 : config->vendor_ramdisk_image_path();
723 if(!ConcatRamdisks(config->final_ramdisk_path(),
724 config->ramdisk_image_path(), vendor_ramdisk_path)) {
725 LOG(ERROR) << "Failed to concatenate ramdisk and vendor ramdisk";
726 exit(AssemblerExitCodes::kInitRamFsConcatError);
727 }
Ram Muthiahaad97c52019-08-14 17:05:01 -0700728 }
729
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800730 if (config->decompress_kernel()) {
731 if (!DecompressKernel(config->kernel_image_path(),
732 config->decompressed_kernel_image_path())) {
733 LOG(ERROR) << "Failed to decompress kernel";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700734 exit(AssemblerExitCodes::kKernelDecompressError);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800735 }
736 }
737
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800738 ValidateAdbModeFlag(*config);
739
Yifan Hong19d713e2019-05-01 14:12:07 -0700740 // Create misc if necessary
741 if (!InitializeMiscImage(FLAGS_misc_image)) {
742 exit(cvd::kCuttlefishConfigurationInitError);
743 }
744
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800745 // Create data if necessary
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700746 if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800747 exit(cvd::kCuttlefishConfigurationInitError);
748 }
749
Cody Schuffelen69c19592019-05-31 16:00:30 -0700750 if (!cvd::FileExists(FLAGS_metadata_image)) {
751 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
752 }
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900753
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700754 if (SuperImageNeedsRebuilding(fetcher_config, *config)) {
755 if (!RebuildSuperImage(fetcher_config, *config, FLAGS_super_image)) {
756 LOG(ERROR) << "Super image rebuilding requested but could not be completed.";
757 exit(cvd::kCuttlefishConfigurationInitError);
758 }
759 }
760
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700761 if (ShouldCreateCompositeDisk()) {
Cody Schuffelen25c46432020-01-14 13:49:52 -0800762 if (!CreateCompositeDisk(*config)) {
763 exit(cvd::kDiskSpaceError);
764 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700765 }
766
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800767 // Check that the files exist
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700768 for (const auto& file : config->virtual_disk_paths()) {
Alistair Strachan050ca932018-11-27 12:41:19 -0800769 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800770 LOG(ERROR) << "File not found: " << file;
771 exit(cvd::kCuttlefishConfigurationInitError);
772 }
773 }
774
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800775 return config;
776}
777
778std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen2168c242019-12-13 16:15:27 -0800779 return config.AssemblyPath("cuttlefish_config.json");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800780}