blob: 400a6861177dd58f9cc1f3c1873e1a3743dbd9bf [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
3#include <iostream>
Ram Muthiahaad97c52019-08-14 17:05:01 -07004#include <fstream>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08005
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -08006#include <android-base/strings.h>
Cody Schuffelen20ecaca2019-01-29 17:43:38 -08007#include <gflags/gflags.h>
8#include <glog/logging.h>
9
10#include "common/libs/utils/environment.h"
11#include "common/libs/utils/files.h"
Cody Schuffelen147b88e2019-09-09 16:00:11 -070012#include "host/commands/assemble_cvd/boot_image_unpacker.h"
13#include "host/commands/assemble_cvd/data_image.h"
14#include "host/commands/assemble_cvd/image_aggregator.h"
15#include "host/commands/assemble_cvd/assembler_defs.h"
Cody Schuffelenb737ef52019-09-25 14:54:38 -070016#include "host/commands/assemble_cvd/super_image_mixer.h"
Cody Schuffelen605e6852019-10-16 16:22:24 -070017#include "host/libs/config/fetcher_config.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080018#include "host/libs/vm_manager/crosvm_manager.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080019#include "host/libs/vm_manager/qemu_manager.h"
20#include "host/libs/vm_manager/vm_manager.h"
21
22using vsoc::GetPerInstanceDefault;
Cody Schuffelene71fa352019-09-10 16:11:58 -070023using cvd::AssemblerExitCodes;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080024
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080025DEFINE_string(cache_image, "", "Location of the cache partition image.");
Paul Trautrimba8f8e92019-03-12 17:55:48 +090026DEFINE_string(metadata_image, "", "Location of the metadata partition image "
27 "to be generated.");
28DEFINE_int32(blank_metadata_image_mb, 16,
29 "The size of the blank metadata image to generate, MB.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080030DEFINE_int32(cpus, 2, "Virtual CPU count.");
31DEFINE_string(data_image, "", "Location of the data partition image.");
32DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
33 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
34 "'always_create'.");
35DEFINE_int32(blank_data_image_mb, 0,
36 "The size of the blank data image to generate, MB.");
Cody Schuffelenbdca7b82019-12-04 20:09:22 +000037DEFINE_string(blank_data_image_fmt, "ext4",
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080038 "The fs format for the blank data image. Used with mkfs.");
39DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000040 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080041
42DEFINE_int32(x_res, 720, "Width of the screen in pixels");
43DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
44DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
45DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080046DEFINE_string(kernel_path, "",
47 "Path to the kernel. Overrides the one from the boot image");
Ram Muthiahaad97c52019-08-14 17:05:01 -070048DEFINE_string(initramfs_path, "", "Path to the initramfs");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080049DEFINE_bool(decompress_kernel, false,
Jorge E. Moreira36294042019-06-07 15:23:18 -070050 "Whether to decompress the kernel image.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080051DEFINE_string(extra_kernel_cmdline, "",
52 "Additional flags to put on the kernel command line");
53DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070054DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen79d4c192019-12-04 15:13:26 -080055 "Whether to run in enforcing mode (non permissive).");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080056DEFINE_bool(guest_audit_security, true,
57 "Whether to log security audits.");
Yifan Hong19d713e2019-05-01 14:12:07 -070058DEFINE_string(boot_image, "",
59 "Location of cuttlefish boot image. If empty it is assumed to be "
60 "boot.img in the directory specified by -system_image_dir.");
Ram Muthiah1aa02f82019-10-22 20:26:28 +000061DEFINE_string(vendor_boot_image, "",
62 "Location of cuttlefish vendor boot image. If empty it is assumed to "
63 "be vendor_boot.img in the directory specified by -system_image_dir.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080064DEFINE_int32(memory_mb, 2048,
65 "Total amount of memory available for guest, MB.");
Cody Schuffelen7a184512019-10-30 16:52:55 -070066DEFINE_string(mobile_interface, GetPerInstanceDefault("cvd-mbr-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080067 "Network interface to use for mobile networking");
Cody Schuffelen7a184512019-10-30 16:52:55 -070068DEFINE_string(mobile_tap_name, GetPerInstanceDefault("cvd-mtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080069 "The name of the tap interface to use for mobile");
Cody Schuffelen7a184512019-10-30 16:52:55 -070070DEFINE_string(serial_number, GetPerInstanceDefault("CUTTLEFISHCVD"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080071 "Serial number to use for the device");
72DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
73 "A directory to put all instance specific files");
74DEFINE_string(
Alistair Strachan5ab095c2019-05-23 20:41:20 +000075 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080076 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070077DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070078 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
79 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070080DEFINE_string(wayland_socket, "",
Greg Hartmanbc1bed42019-04-05 20:02:00 -070081 "Location of the wayland socket to use for drm_virgl gpu_mode.");
Greg Hartmaneb04ac52019-07-16 16:44:18 -070082DEFINE_string(x_display, "",
83 "X display to use for drm_virgl gpu_mode.");
Greg Hartmana3c552d2019-03-28 18:20:48 -070084
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080085DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
86 "Location of the system partition images.");
Alistair Strachan050ca932018-11-27 12:41:19 -080087DEFINE_string(super_image, "", "Location of the super partition image.");
Yifan Hong19d713e2019-05-01 14:12:07 -070088DEFINE_string(misc_image, "",
89 "Location of the misc partition image. If the image does not "
90 "exist, a blank new misc partition image is created.");
David Anderson64581ce2019-10-03 17:52:22 -070091DEFINE_string(composite_disk, "", "Location of the composite disk image. "
92 "If empty, a composite disk is not used.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080093
94DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
95 " host kernel. This is only used during transition of our clients."
96 " Will be deprecated soon.");
97DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080098DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444),
99 "The port on which the vnc server should listen");
Cody Schuffelen10743332019-04-15 16:50:49 -0700100DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen3c35fd82019-12-11 18:39:46 -0800101 "Mode for ADB connection."
Cody Schuffelen63d10052019-02-26 12:21:53 -0800102 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
103 "'native_vsock' for a direct connection to the guest ADB over "
104 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
105 "the guest ADB server, or a comma separated list of types as in "
Cody Schuffelen3c35fd82019-12-11 18:39:46 -0800106 "'native_vsock,vsock_half_tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800107DEFINE_bool(run_adb_connector, true,
108 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000109 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen7a184512019-10-30 16:52:55 -0700110DEFINE_string(wifi_tap_name, GetPerInstanceDefault("cvd-wtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800111 "The name of the tap interface to use for wifi");
112DEFINE_int32(vsock_guest_cid,
113 vsoc::GetDefaultPerInstanceVsockCid(),
114 "Guest identifier for vsock. Disabled if under 3.");
115
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800116DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
117 "UUID to use for the device. Random if not specified");
118DEFINE_bool(daemon, false,
119 "Run cuttlefish in background, the launcher exits on boot "
120 "completed/failed");
121
122DEFINE_string(device_title, "", "Human readable name for the instance, "
123 "used by the vnc_server for its server title");
124DEFINE_string(setupwizard_mode, "DISABLED",
125 "One of DISABLED,OPTIONAL,REQUIRED");
126
127DEFINE_string(qemu_binary,
128 "/usr/bin/qemu-system-x86_64",
129 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800130DEFINE_string(crosvm_binary,
131 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
132 "The Crosvm binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800133DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800134DEFINE_string(logcat_mode, "", "How to send android's log messages from "
135 "guest to host. One of [serial, vsock]");
Ram Muthiah6e9c98c2019-05-28 15:18:27 -0700136DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on "
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700137 "both the guest and the host");
Cody Schuffelen1300f122019-05-28 18:24:34 -0700138DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
139DEFINE_string(bootloader, "", "Bootloader binary path");
David Anderson637bfd62019-09-26 13:23:21 -0700140DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, "
141 "the slot will be chosen based on the misc partition if using a "
142 "bootloader. It will default to 'a' if empty and not using a "
143 "bootloader.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700144
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800145namespace {
146
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700147const std::string kKernelDefaultPath = "kernel";
148const std::string kInitramfsImg = "initramfs.img";
149const std::string kRamdiskConcatExt = ".concat";
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700150
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800151bool ResolveInstanceFiles() {
152 if (FLAGS_system_image_dir.empty()) {
153 LOG(ERROR) << "--system_image_dir must be specified.";
154 return false;
155 }
156
157 // If user did not specify location of either of these files, expect them to
158 // be placed in --system_image_dir location.
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800159 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
160 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
161 google::FlagSettingMode::SET_FLAGS_DEFAULT);
162 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
163 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
164 google::FlagSettingMode::SET_FLAGS_DEFAULT);
165 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
166 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
167 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900168 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
169 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
170 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800171 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
172 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
173 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Yifan Hong19d713e2019-05-01 14:12:07 -0700174 std::string default_misc_image = FLAGS_system_image_dir + "/misc.img";
175 SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(),
176 google::FlagSettingMode::SET_FLAGS_DEFAULT);
David Anderson64581ce2019-10-03 17:52:22 -0700177 std::string default_composite_disk = FLAGS_system_image_dir + "/composite.img";
178 SetCommandLineOptionWithMode("composite_disk", default_composite_disk.c_str(),
179 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000180 std::string default_vendor_boot_image = FLAGS_system_image_dir
181 + "/vendor_boot.img";
182 SetCommandLineOptionWithMode("vendor_boot_image",
183 default_vendor_boot_image.c_str(),
184 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800185
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800186 return true;
187}
188
189std::string GetCuttlefishEnvPath() {
190 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
191}
192
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700193int GetHostPort() {
194 constexpr int kFirstHostPort = 6520;
195 return vsoc::GetPerInstanceDefault(kFirstHostPort);
196}
197
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800198// Initializes the config object and saves it to file. It doesn't return it, all
199// further uses of the config should happen through the singleton
200bool InitializeCuttlefishConfiguration(
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700201 const cvd::BootImageUnpacker& boot_image_unpacker,
202 const cvd::FetcherConfig& fetcher_config) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800203 vsoc::CuttlefishConfig tmp_config_obj;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800204 // Set this first so that calls to PerInstancePath below are correct
205 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
206 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
207 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
208 return false;
209 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700210 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
211 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
212 return false;
213 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800214 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700215 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800216 if (vm_manager::VmManager::ConfigureGpuMode(tmp_config_obj.vm_manager(),
217 tmp_config_obj.gpu_mode()).empty()) {
Greg Hartmana3c552d2019-03-28 18:20:48 -0700218 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
219 " does not work with vm_manager=" << FLAGS_vm_manager;
220 return false;
221 }
222 tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket);
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700223 tmp_config_obj.set_x_display(FLAGS_x_display);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800224
225 tmp_config_obj.set_serial_number(FLAGS_serial_number);
226
227 tmp_config_obj.set_cpus(FLAGS_cpus);
228 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
229
230 tmp_config_obj.set_dpi(FLAGS_dpi);
231 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
232 tmp_config_obj.set_x_res(FLAGS_x_res);
233 tmp_config_obj.set_y_res(FLAGS_y_res);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800234 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
235 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -0800236 std::vector<std::string> adb = android::base::Split(FLAGS_adb_mode, ",");
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800237 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700238 tmp_config_obj.set_host_port(GetHostPort());
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800239 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
240
241 tmp_config_obj.set_device_title(FLAGS_device_title);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700242 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
243 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
244 if (foreign_kernel.size()) {
245 tmp_config_obj.set_kernel_image_path(foreign_kernel);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800246 tmp_config_obj.set_use_unpacked_kernel(false);
247 } else {
248 tmp_config_obj.set_kernel_image_path(
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700249 tmp_config_obj.PerInstancePath(kKernelDefaultPath.c_str()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800250 tmp_config_obj.set_use_unpacked_kernel(true);
251 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800252 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
253 if (FLAGS_decompress_kernel) {
254 tmp_config_obj.set_decompressed_kernel_image_path(
255 tmp_config_obj.PerInstancePath("vmlinux"));
256 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800257
258 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000259 auto vendor_ramdisk_path = tmp_config_obj.PerInstancePath("vendor_ramdisk.img");
Cody Schuffelen1e645542019-11-19 15:44:22 -0800260 if (!boot_image_unpacker.HasRamdiskImage()) {
261 LOG(INFO) << "A ramdisk is required, but the boot image did not have one.";
262 return false;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800263 }
264
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800265 tmp_config_obj.set_boot_image_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
266 tmp_config_obj.set_loop_max_part(FLAGS_loop_max_part);
267 tmp_config_obj.set_guest_enforce_security(FLAGS_guest_enforce_security);
268 tmp_config_obj.set_guest_audit_security(FLAGS_guest_audit_security);
269 tmp_config_obj.set_extra_kernel_cmdline(FLAGS_extra_kernel_cmdline);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800270
Cody Schuffelen2ced6f12019-11-19 15:54:10 -0800271 tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk});
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700272
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800273 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Ram Muthiahd512c132019-10-23 17:20:45 -0700274 tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path);
275
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700276 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
277 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
278 if (foreign_kernel.size() && !foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700279 // If there's a kernel that's passed in without an initramfs, that implies
280 // user error or a kernel built with no modules. In either case, let's
281 // choose to avoid loading the modules from the vendor ramdisk which are
282 // built for the default cf kernel. Once boot occurs, user error will
283 // become obvious.
284 tmp_config_obj.set_final_ramdisk_path(ramdisk_path);
285 } else {
286 tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700287 if(foreign_ramdisk.size()) {
288 tmp_config_obj.set_initramfs_path(foreign_ramdisk);
Ram Muthiahd512c132019-10-23 17:20:45 -0700289 }
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700290 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800291
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800292 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800293 tmp_config_obj.set_logcat_receiver_binary(
294 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"));
295 tmp_config_obj.set_config_server_binary(
296 vsoc::DefaultHostArtifactsPath("bin/config_server"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800297
298 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
299 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
300
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800301 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
302
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800303 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
304
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800305 tmp_config_obj.set_uuid(FLAGS_uuid);
306
307 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800308 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800309 tmp_config_obj.set_console_forwarder_binary(
310 vsoc::DefaultHostArtifactsPath("bin/console_forwarder"));
311 tmp_config_obj.set_kernel_log_monitor_binary(
312 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800313
314 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800315 tmp_config_obj.set_vnc_server_binary(
316 vsoc::DefaultHostArtifactsPath("bin/vnc_server"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800317 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
318
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800319 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
320 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800321 tmp_config_obj.set_adb_connector_binary(
322 vsoc::DefaultHostArtifactsPath("bin/adb_connector"));
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800323 tmp_config_obj.set_socket_vsock_proxy_binary(
324 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800325 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800326
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800327 tmp_config_obj.set_data_policy(FLAGS_data_policy);
328 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
329 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
330
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800331 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800332
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700333 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800334 tmp_config_obj.set_tombstone_receiver_binary(
335 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"));
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700336
Cody Schuffelen1300f122019-05-28 18:24:34 -0700337 tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
338 tmp_config_obj.set_bootloader(FLAGS_bootloader);
339
David Anderson637bfd62019-09-26 13:23:21 -0700340 if (!FLAGS_boot_slot.empty()) {
341 tmp_config_obj.set_boot_slot(FLAGS_boot_slot);
342 }
343
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800344 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
345
346 auto config_file = GetConfigFilePath(tmp_config_obj);
347 auto config_link = vsoc::GetGlobalConfigFileLink();
348 // Save the config object before starting any host process
349 if (!tmp_config_obj.SaveToFile(config_file)) {
350 LOG(ERROR) << "Unable to save config object";
351 return false;
352 }
353 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
354 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
355 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
356 << ": " << strerror(errno);
357 return false;
358 }
359
360 return true;
361}
362
363void SetDefaultFlagsForQemu() {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800364 auto default_instance_dir =
365 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
366 SetCommandLineOptionWithMode("instance_dir",
367 default_instance_dir.c_str(),
368 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800369 // TODO(b/144119457) Use the serial port.
370 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800371 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800372}
373
374void SetDefaultFlagsForCrosvm() {
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800375 auto default_instance_dir =
376 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
377 SetCommandLineOptionWithMode("instance_dir",
378 default_instance_dir.c_str(),
379 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700380 SetCommandLineOptionWithMode("wayland_socket",
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700381 "",
382 google::FlagSettingMode::SET_FLAGS_DEFAULT);
383 SetCommandLineOptionWithMode("x_display",
384 getenv("DISPLAY"),
Greg Hartmana3c552d2019-03-28 18:20:48 -0700385 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800386 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
387 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800388}
389
390bool ParseCommandLineFlags(int* argc, char*** argv) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800391 google::ParseCommandLineNonHelpFlags(argc, argv, true);
392 bool invalid_manager = false;
393 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
394 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800395 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
396 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800397 } else {
398 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
399 << std::endl;
400 invalid_manager = true;
401 }
402 google::HandleCommandLineHelpFlags();
403 if (invalid_manager) {
404 return false;
405 }
406 // Set the env variable to empty (in case the caller passed a value for it).
407 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
408
409 return ResolveInstanceFiles();
410}
411
412bool CleanPriorFiles() {
413 // Everything on the instance directory
414 std::string prior_files = FLAGS_instance_dir + "/*";
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800415 // The environment file
416 prior_files += " " + GetCuttlefishEnvPath();
417 // The global link to the config file
418 prior_files += " " + vsoc::GetGlobalConfigFileLink();
419 LOG(INFO) << "Assuming prior files of " << prior_files;
420 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
421 int rval = std::system(fuser_cmd.c_str());
422 // fuser returns 0 if any of the files are open
423 if (WEXITSTATUS(rval) == 0) {
424 LOG(ERROR) << "Clean aborted: files are in use";
425 return false;
426 }
427 std::string clean_command = "rm -rf " + prior_files;
428 rval = std::system(clean_command.c_str());
429 if (WEXITSTATUS(rval) != 0) {
430 LOG(ERROR) << "Remove of files failed";
431 return false;
432 }
433 return true;
434}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800435
436bool DecompressKernel(const std::string& src, const std::string& dst) {
Cody Schuffelen3dff6982019-12-05 16:24:12 -0800437 cvd::Command decomp_cmd(vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"));
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800438 decomp_cmd.AddParameter(src);
439 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
440 if (!output_file->IsOpen()) {
441 LOG(ERROR) << "Unable to create decompressed image file: "
442 << output_file->StrError();
443 return false;
444 }
445 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
Cody Schuffelene5670872019-12-10 15:04:59 -0800446 auto decomp_proc = decomp_cmd.Start();
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800447 return decomp_proc.Started() && decomp_proc.Wait() == 0;
448}
Cody Schuffelen78824ed2019-09-06 17:43:15 -0700449
450void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) {
451 auto adb_modes = config.adb_mode();
452 adb_modes.erase(vsoc::AdbMode::Unknown);
453 if (adb_modes.size() < 1) {
454 LOG(INFO) << "ADB not enabled";
455 }
456}
457
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800458} // namespace
459
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700460namespace {
461
462std::vector<ImagePartition> disk_config() {
463 std::vector<ImagePartition> partitions;
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700464 partitions.push_back(ImagePartition {
465 .label = "super",
466 .image_file_path = FLAGS_super_image,
467 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700468 partitions.push_back(ImagePartition {
469 .label = "userdata",
470 .image_file_path = FLAGS_data_image,
471 });
472 partitions.push_back(ImagePartition {
473 .label = "cache",
474 .image_file_path = FLAGS_cache_image,
475 });
476 partitions.push_back(ImagePartition {
477 .label = "metadata",
478 .image_file_path = FLAGS_metadata_image,
479 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700480 partitions.push_back(ImagePartition {
481 .label = "boot",
482 .image_file_path = FLAGS_boot_image,
483 });
Yifan Hong19d713e2019-05-01 14:12:07 -0700484 partitions.push_back(ImagePartition {
485 .label = "misc",
486 .image_file_path = FLAGS_misc_image
487 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700488 return partitions;
489}
490
491bool ShouldCreateCompositeDisk() {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700492 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
493 // The crosvm implementation is very fast to rebuild but also more brittle due to being split
494 // into multiple files. The QEMU implementation is slow to build, but completely self-contained
495 // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU.
496 return true;
497 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700498 auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk);
499 for (auto& partition : disk_config()) {
500 auto partition_age = cvd::FileModificationTime(partition.image_file_path);
501 if (partition_age >= composite_age) {
502 LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", "
503 << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\"";
504 return true;
505 }
506 }
507 return false;
508}
509
Ram Muthiah3db0a562019-08-26 13:35:19 -0700510bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path,
511 const std::string& ramdisk_b_path) {
512 // clear out file of any pre-existing content
513 std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
514 std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary);
515 std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700516
Ram Muthiah3db0a562019-08-26 13:35:19 -0700517 if(!new_ramdisk.is_open() || !ramdisk_a.is_open() || !ramdisk_b.is_open()) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700518 return false;
519 }
520
Ram Muthiah3db0a562019-08-26 13:35:19 -0700521 new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
Ram Muthiahaad97c52019-08-14 17:05:01 -0700522 return true;
523}
524
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700525void CreateCompositeDisk(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700526 if (FLAGS_composite_disk.empty()) {
527 LOG(FATAL) << "asked to create composite disk, but path was empty";
528 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700529 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
530 std::string header_path = config.PerInstancePath("gpt_header.img");
531 std::string footer_path = config.PerInstancePath("gpt_footer.img");
532 create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk);
533 } else {
534 aggregate_image(disk_config(), FLAGS_composite_disk);
535 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700536}
537
538} // namespace
539
Cody Schuffelen605e6852019-10-16 16:22:24 -0700540const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700541 int* argc, char*** argv, cvd::FetcherConfig fetcher_config) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800542 if (!ParseCommandLineFlags(argc, argv)) {
543 LOG(ERROR) << "Failed to parse command arguments";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700544 exit(AssemblerExitCodes::kArgumentParsingError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800545 }
546
547 // Clean up prior files before saving the config file (doing it after would
548 // delete it)
549 if (!CleanPriorFiles()) {
550 LOG(ERROR) << "Failed to clean prior files";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700551 exit(AssemblerExitCodes::kPrioFilesCleanupError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800552 }
553 // Create instance directory if it doesn't exist.
554 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
555 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
556 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
557 LOG(ERROR) << "Failed to create instance directory: "
558 << FLAGS_instance_dir << ". Error: " << errno;
Cody Schuffelene71fa352019-09-10 16:11:58 -0700559 exit(AssemblerExitCodes::kInstanceDirCreationError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800560 }
561 }
562
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700563 auto internal_dir = FLAGS_instance_dir + "/" + vsoc::kInternalDirName;
564 if (!cvd::DirectoryExists(internal_dir)) {
565 if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) <
566 0) {
567 LOG(ERROR) << "Failed to create internal instance directory: "
568 << internal_dir << ". Error: " << errno;
569 exit(AssemblerExitCodes::kInstanceDirCreationError);
570 }
571 }
572
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800573 if (!cvd::FileHasContent(FLAGS_boot_image)) {
574 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
575 exit(cvd::kCuttlefishConfigurationInitError);
576 }
577
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000578 if (!cvd::FileHasContent(FLAGS_vendor_boot_image)) {
579 LOG(ERROR) << "File not found: " << FLAGS_vendor_boot_image;
580 exit(cvd::kCuttlefishConfigurationInitError);
581 }
582
583 auto boot_img_unpacker =
584 cvd::BootImageUnpacker::FromImages(FLAGS_boot_image,
585 FLAGS_vendor_boot_image);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800586
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700587 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker, fetcher_config)) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800588 LOG(ERROR) << "Failed to initialize configuration";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700589 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800590 }
591 // Do this early so that the config object is ready for anything that needs it
592 auto config = vsoc::CuttlefishConfig::Get();
593 if (!config) {
594 LOG(ERROR) << "Failed to obtain config singleton";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700595 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800596 }
597
598 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000599 config->vendor_ramdisk_image_path(),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800600 config->use_unpacked_kernel()
601 ? config->kernel_image_path()
602 : "")) {
603 LOG(ERROR) << "Failed to unpack boot image";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700604 exit(AssemblerExitCodes::kBootImageUnpackError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800605 }
606
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000607 // TODO(134522463) as part of the bootloader refactor, repack the vendor boot
608 // image and use the bootloader to load both the boot and vendor ramdisk.
609 // Until then, this hack to get gki modules into cuttlefish will suffice.
610
611 // If a vendor ramdisk comes in via this mechanism, let it supercede the one
612 // in the vendor boot image. This flag is what kernel presubmit testing uses
613 // to pass in the kernel ramdisk.
Ram Muthiahd512c132019-10-23 17:20:45 -0700614
615 // If no kernel is passed in or an initramfs is made available, the default
616 // vendor boot ramdisk or the initramfs provided should be appended to the
617 // boot ramdisk. If a kernel IS provided with no initramfs, it is safe to
618 // safe to assume that the kernel was built with no modules and expects no
619 // modules for cf to run properly.
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700620 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
621 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
622 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
623 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
624 if(!foreign_kernel.size() || foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700625 const std::string& vendor_ramdisk_path =
626 config->initramfs_path().size() ? config->initramfs_path()
627 : config->vendor_ramdisk_image_path();
628 if(!ConcatRamdisks(config->final_ramdisk_path(),
629 config->ramdisk_image_path(), vendor_ramdisk_path)) {
630 LOG(ERROR) << "Failed to concatenate ramdisk and vendor ramdisk";
631 exit(AssemblerExitCodes::kInitRamFsConcatError);
632 }
Ram Muthiahaad97c52019-08-14 17:05:01 -0700633 }
634
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800635 if (config->decompress_kernel()) {
636 if (!DecompressKernel(config->kernel_image_path(),
637 config->decompressed_kernel_image_path())) {
638 LOG(ERROR) << "Failed to decompress kernel";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700639 exit(AssemblerExitCodes::kKernelDecompressError);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800640 }
641 }
642
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800643 ValidateAdbModeFlag(*config);
644
Yifan Hong19d713e2019-05-01 14:12:07 -0700645 // Create misc if necessary
646 if (!InitializeMiscImage(FLAGS_misc_image)) {
647 exit(cvd::kCuttlefishConfigurationInitError);
648 }
649
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800650 // Create data if necessary
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700651 if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800652 exit(cvd::kCuttlefishConfigurationInitError);
653 }
654
Cody Schuffelen69c19592019-05-31 16:00:30 -0700655 if (!cvd::FileExists(FLAGS_metadata_image)) {
656 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
657 }
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900658
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700659 if (SuperImageNeedsRebuilding(fetcher_config, *config)) {
660 if (!RebuildSuperImage(fetcher_config, *config, FLAGS_super_image)) {
661 LOG(ERROR) << "Super image rebuilding requested but could not be completed.";
662 exit(cvd::kCuttlefishConfigurationInitError);
663 }
664 }
665
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700666 if (ShouldCreateCompositeDisk()) {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700667 CreateCompositeDisk(*config);
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700668 }
669
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800670 // Check that the files exist
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700671 for (const auto& file : config->virtual_disk_paths()) {
Alistair Strachan050ca932018-11-27 12:41:19 -0800672 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800673 LOG(ERROR) << "File not found: " << file;
674 exit(cvd::kCuttlefishConfigurationInitError);
675 }
676 }
677
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800678 return config;
679}
680
681std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
682 return config.PerInstancePath("cuttlefish_config.json");
683}