blob: 2d6007ef5ea998b7f47e9b8a73cbd51657c8fca6 [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");
46DEFINE_int32(num_screen_buffers, 3, "The number of screen buffers");
47DEFINE_string(kernel_path, "",
48 "Path to the kernel. Overrides the one from the boot image");
Ram Muthiahaad97c52019-08-14 17:05:01 -070049DEFINE_string(initramfs_path, "", "Path to the initramfs");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080050DEFINE_bool(decompress_kernel, false,
Jorge E. Moreira36294042019-06-07 15:23:18 -070051 "Whether to decompress the kernel image.");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080052DEFINE_string(kernel_decompresser_executable,
53 vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"),
54 "Path to the extract-vmlinux executable.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080055DEFINE_string(extra_kernel_cmdline, "",
56 "Additional flags to put on the kernel command line");
57DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070058DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen79d4c192019-12-04 15:13:26 -080059 "Whether to run in enforcing mode (non permissive).");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080060DEFINE_bool(guest_audit_security, true,
61 "Whether to log security audits.");
Yifan Hong19d713e2019-05-01 14:12:07 -070062DEFINE_string(boot_image, "",
63 "Location of cuttlefish boot image. If empty it is assumed to be "
64 "boot.img in the directory specified by -system_image_dir.");
Ram Muthiah1aa02f82019-10-22 20:26:28 +000065DEFINE_string(vendor_boot_image, "",
66 "Location of cuttlefish vendor boot image. If empty it is assumed to "
67 "be vendor_boot.img in the directory specified by -system_image_dir.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080068DEFINE_int32(memory_mb, 2048,
69 "Total amount of memory available for guest, MB.");
Cody Schuffelen7a184512019-10-30 16:52:55 -070070DEFINE_string(mobile_interface, GetPerInstanceDefault("cvd-mbr-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080071 "Network interface to use for mobile networking");
Cody Schuffelen7a184512019-10-30 16:52:55 -070072DEFINE_string(mobile_tap_name, GetPerInstanceDefault("cvd-mtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080073 "The name of the tap interface to use for mobile");
Cody Schuffelen7a184512019-10-30 16:52:55 -070074DEFINE_string(serial_number, GetPerInstanceDefault("CUTTLEFISHCVD"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080075 "Serial number to use for the device");
76DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
77 "A directory to put all instance specific files");
78DEFINE_string(
Alistair Strachan5ab095c2019-05-23 20:41:20 +000079 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080080 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070081DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070082 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
83 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070084DEFINE_string(wayland_socket, "",
Greg Hartmanbc1bed42019-04-05 20:02:00 -070085 "Location of the wayland socket to use for drm_virgl gpu_mode.");
Greg Hartmaneb04ac52019-07-16 16:44:18 -070086DEFINE_string(x_display, "",
87 "X display to use for drm_virgl gpu_mode.");
Greg Hartmana3c552d2019-03-28 18:20:48 -070088
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080089DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
90 "Location of the system partition images.");
Alistair Strachan050ca932018-11-27 12:41:19 -080091DEFINE_string(super_image, "", "Location of the super partition image.");
Yifan Hong19d713e2019-05-01 14:12:07 -070092DEFINE_string(misc_image, "",
93 "Location of the misc partition image. If the image does not "
94 "exist, a blank new misc partition image is created.");
David Anderson64581ce2019-10-03 17:52:22 -070095DEFINE_string(composite_disk, "", "Location of the composite disk image. "
96 "If empty, a composite disk is not used.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080097
98DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
99 " host kernel. This is only used during transition of our clients."
100 " Will be deprecated soon.");
101DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process.");
102DEFINE_string(vnc_server_binary,
103 vsoc::DefaultHostArtifactsPath("bin/vnc_server"),
104 "Location of the vnc server binary.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800105DEFINE_string(virtual_usb_manager_binary,
106 vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager"),
107 "Location of the virtual usb manager binary.");
108DEFINE_string(kernel_log_monitor_binary,
109 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"),
110 "Location of the log monitor binary.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800111DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444),
112 "The port on which the vnc server should listen");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800113DEFINE_string(socket_forward_proxy_binary,
114 vsoc::DefaultHostArtifactsPath("bin/socket_forward_proxy"),
115 "Location of the socket_forward_proxy binary.");
116DEFINE_string(socket_vsock_proxy_binary,
117 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"),
118 "Location of the socket_vsock_proxy binary.");
Cody Schuffelen10743332019-04-15 16:50:49 -0700119DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen63d10052019-02-26 12:21:53 -0800120 "Mode for ADB connection. Can be 'usb' for USB forwarding, "
121 "'tunnel' for a TCP connection tunneled through VSoC, "
122 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
123 "'native_vsock' for a direct connection to the guest ADB over "
124 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
125 "the guest ADB server, or a comma separated list of types as in "
126 "'usb,tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800127DEFINE_bool(run_adb_connector, true,
128 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000129 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800130DEFINE_string(adb_connector_binary,
131 vsoc::DefaultHostArtifactsPath("bin/adb_connector"),
132 "Location of the adb_connector binary. Only relevant if "
Matthias Maennich454d7872019-02-06 16:35:17 +0000133 "-run_adb_connector is true");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800134DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb");
Cody Schuffelen7a184512019-10-30 16:52:55 -0700135DEFINE_string(wifi_tap_name, GetPerInstanceDefault("cvd-wtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800136 "The name of the tap interface to use for wifi");
137DEFINE_int32(vsock_guest_cid,
138 vsoc::GetDefaultPerInstanceVsockCid(),
139 "Guest identifier for vsock. Disabled if under 3.");
140
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800141DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
142 "UUID to use for the device. Random if not specified");
143DEFINE_bool(daemon, false,
144 "Run cuttlefish in background, the launcher exits on boot "
145 "completed/failed");
146
147DEFINE_string(device_title, "", "Human readable name for the instance, "
148 "used by the vnc_server for its server title");
149DEFINE_string(setupwizard_mode, "DISABLED",
150 "One of DISABLED,OPTIONAL,REQUIRED");
151
152DEFINE_string(qemu_binary,
153 "/usr/bin/qemu-system-x86_64",
154 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800155DEFINE_string(crosvm_binary,
156 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
157 "The Crosvm binary to use");
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700158DEFINE_string(console_forwarder_binary,
159 vsoc::DefaultHostArtifactsPath("bin/console_forwarder"),
160 "The Console Forwarder 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_receiver_binary,
163 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"),
164 "Binary for the logcat server");
165DEFINE_string(logcat_mode, "", "How to send android's log messages from "
166 "guest to host. One of [serial, vsock]");
167DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
168 "The port for logcat over vsock");
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700169DEFINE_string(config_server_binary,
170 vsoc::DefaultHostArtifactsPath("bin/config_server"),
171 "Binary for the configuration server");
172DEFINE_int32(config_server_port, vsoc::GetPerInstanceDefault(4680),
173 "The (vsock) port for the configuration server");
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800174DEFINE_int32(frames_vsock_port, vsoc::GetPerInstanceDefault(5580),
175 "The vsock port to receive frames from the guest on");
Ram Muthiah6e9c98c2019-05-28 15:18:27 -0700176DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on "
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700177 "both the guest and the host");
178DEFINE_string(tombstone_receiver_binary,
179 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"),
180 "Binary for the tombstone server");
181DEFINE_int32(tombstone_receiver_port, vsoc::GetPerInstanceDefault(5630),
182 "The vsock port for tombstones");
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800183DEFINE_int32(keyboard_server_port, GetPerInstanceDefault(5540),
184 "The port on which the vsock keyboard server should listen");
185DEFINE_int32(touch_server_port, GetPerInstanceDefault(5640),
186 "The port on which the vsock touch server should listen");
Cody Schuffelen1300f122019-05-28 18:24:34 -0700187DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
188DEFINE_string(bootloader, "", "Bootloader binary path");
David Anderson637bfd62019-09-26 13:23:21 -0700189DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, "
190 "the slot will be chosen based on the misc partition if using a "
191 "bootloader. It will default to 'a' if empty and not using a "
192 "bootloader.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700193
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800194namespace {
195
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700196const std::string kKernelDefaultPath = "kernel";
197const std::string kInitramfsImg = "initramfs.img";
198const std::string kRamdiskConcatExt = ".concat";
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700199
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800200bool ResolveInstanceFiles() {
201 if (FLAGS_system_image_dir.empty()) {
202 LOG(ERROR) << "--system_image_dir must be specified.";
203 return false;
204 }
205
206 // If user did not specify location of either of these files, expect them to
207 // be placed in --system_image_dir location.
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800208 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
209 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
210 google::FlagSettingMode::SET_FLAGS_DEFAULT);
211 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
212 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
213 google::FlagSettingMode::SET_FLAGS_DEFAULT);
214 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
215 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
216 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900217 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
218 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
219 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800220 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
221 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
222 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Yifan Hong19d713e2019-05-01 14:12:07 -0700223 std::string default_misc_image = FLAGS_system_image_dir + "/misc.img";
224 SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(),
225 google::FlagSettingMode::SET_FLAGS_DEFAULT);
David Anderson64581ce2019-10-03 17:52:22 -0700226 std::string default_composite_disk = FLAGS_system_image_dir + "/composite.img";
227 SetCommandLineOptionWithMode("composite_disk", default_composite_disk.c_str(),
228 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000229 std::string default_vendor_boot_image = FLAGS_system_image_dir
230 + "/vendor_boot.img";
231 SetCommandLineOptionWithMode("vendor_boot_image",
232 default_vendor_boot_image.c_str(),
233 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800234
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800235 return true;
236}
237
238std::string GetCuttlefishEnvPath() {
239 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
240}
241
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700242int GetHostPort() {
243 constexpr int kFirstHostPort = 6520;
244 return vsoc::GetPerInstanceDefault(kFirstHostPort);
245}
246
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800247// Initializes the config object and saves it to file. It doesn't return it, all
248// further uses of the config should happen through the singleton
249bool InitializeCuttlefishConfiguration(
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700250 const cvd::BootImageUnpacker& boot_image_unpacker,
251 const cvd::FetcherConfig& fetcher_config) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800252 vsoc::CuttlefishConfig tmp_config_obj;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800253 // Set this first so that calls to PerInstancePath below are correct
254 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
255 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
256 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
257 return false;
258 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700259 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
260 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
261 return false;
262 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800263 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700264 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800265 if (vm_manager::VmManager::ConfigureGpuMode(tmp_config_obj.vm_manager(),
266 tmp_config_obj.gpu_mode()).empty()) {
Greg Hartmana3c552d2019-03-28 18:20:48 -0700267 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
268 " does not work with vm_manager=" << FLAGS_vm_manager;
269 return false;
270 }
271 tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket);
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700272 tmp_config_obj.set_x_display(FLAGS_x_display);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800273
274 tmp_config_obj.set_serial_number(FLAGS_serial_number);
275
276 tmp_config_obj.set_cpus(FLAGS_cpus);
277 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
278
279 tmp_config_obj.set_dpi(FLAGS_dpi);
280 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
281 tmp_config_obj.set_x_res(FLAGS_x_res);
282 tmp_config_obj.set_y_res(FLAGS_y_res);
283 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
284 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
285 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelenf4a1cdb2019-11-13 16:51:16 -0800286 std::vector<std::string> adb = android::base::Split(FLAGS_adb_mode, ",");
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800287 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700288 tmp_config_obj.set_host_port(GetHostPort());
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800289 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
290
291 tmp_config_obj.set_device_title(FLAGS_device_title);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700292 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
293 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
294 if (foreign_kernel.size()) {
295 tmp_config_obj.set_kernel_image_path(foreign_kernel);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800296 tmp_config_obj.set_use_unpacked_kernel(false);
297 } else {
298 tmp_config_obj.set_kernel_image_path(
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700299 tmp_config_obj.PerInstancePath(kKernelDefaultPath.c_str()));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800300 tmp_config_obj.set_use_unpacked_kernel(true);
301 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800302 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
303 if (FLAGS_decompress_kernel) {
304 tmp_config_obj.set_decompressed_kernel_image_path(
305 tmp_config_obj.PerInstancePath("vmlinux"));
306 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800307
308 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000309 auto vendor_ramdisk_path = tmp_config_obj.PerInstancePath("vendor_ramdisk.img");
Cody Schuffelen1e645542019-11-19 15:44:22 -0800310 if (!boot_image_unpacker.HasRamdiskImage()) {
311 LOG(INFO) << "A ramdisk is required, but the boot image did not have one.";
312 return false;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800313 }
314
Cody Schuffelen2de6ead2019-12-04 16:16:47 -0800315 tmp_config_obj.set_boot_image_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
316 tmp_config_obj.set_loop_max_part(FLAGS_loop_max_part);
317 tmp_config_obj.set_guest_enforce_security(FLAGS_guest_enforce_security);
318 tmp_config_obj.set_guest_audit_security(FLAGS_guest_audit_security);
319 tmp_config_obj.set_extra_kernel_cmdline(FLAGS_extra_kernel_cmdline);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800320
Cody Schuffelen2ced6f12019-11-19 15:54:10 -0800321 tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk});
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700322
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800323 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Ram Muthiahd512c132019-10-23 17:20:45 -0700324 tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path);
325
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700326 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
327 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
328 if (foreign_kernel.size() && !foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700329 // If there's a kernel that's passed in without an initramfs, that implies
330 // user error or a kernel built with no modules. In either case, let's
331 // choose to avoid loading the modules from the vendor ramdisk which are
332 // built for the default cf kernel. Once boot occurs, user error will
333 // become obvious.
334 tmp_config_obj.set_final_ramdisk_path(ramdisk_path);
335 } else {
336 tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt);
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700337 if(foreign_ramdisk.size()) {
338 tmp_config_obj.set_initramfs_path(foreign_ramdisk);
Ram Muthiahd512c132019-10-23 17:20:45 -0700339 }
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700340 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800341
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700342 if (tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) > 0) {
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700343 tmp_config_obj.set_usb_v1_socket_name(
344 tmp_config_obj.PerInstanceInternalPath("usb-v1"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800345 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700346 tmp_config_obj.set_usb_ip_socket_name(
347 tmp_config_obj.PerInstanceInternalPath("usb-ip"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800348 }
349
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700350 tmp_config_obj.set_kernel_log_pipe_name(
351 tmp_config_obj.PerInstanceInternalPath("kernel-log-pipe"));
352 tmp_config_obj.set_console_pipe_name(
353 tmp_config_obj.PerInstanceInternalPath("console-pipe"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800354 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
355 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
356 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800357 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700358 tmp_config_obj.set_config_server_binary(FLAGS_config_server_binary);
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700359 tmp_config_obj.set_launcher_log_path(
360 tmp_config_obj.PerInstancePath("launcher.log"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800361 tmp_config_obj.set_launcher_monitor_socket_path(
362 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
363
364 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
365 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
366
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800367 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
368
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800369 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
370
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800371 tmp_config_obj.set_uuid(FLAGS_uuid);
372
373 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800374 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700375 tmp_config_obj.set_console_forwarder_binary(FLAGS_console_forwarder_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800376 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
377
378 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
379 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
380 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
381
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800382 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
383 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
384 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
385 tmp_config_obj.set_virtual_usb_manager_binary(
386 FLAGS_virtual_usb_manager_binary);
387 tmp_config_obj.set_socket_forward_proxy_binary(
388 FLAGS_socket_forward_proxy_binary);
389 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
390 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800391
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800392 tmp_config_obj.set_data_policy(FLAGS_data_policy);
393 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
394 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
395
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700396 if(tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) == 0) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800397 tmp_config_obj.disable_usb_adb();
398 }
399
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800400 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
401 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700402 tmp_config_obj.set_config_server_port(FLAGS_config_server_port);
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800403 tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800404
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700405 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
406 tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port);
407 tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary);
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700408
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800409 tmp_config_obj.set_touch_socket_port(FLAGS_touch_server_port);
410 tmp_config_obj.set_keyboard_socket_port(FLAGS_keyboard_server_port);
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800411
Cody Schuffelen1300f122019-05-28 18:24:34 -0700412 tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
413 tmp_config_obj.set_bootloader(FLAGS_bootloader);
414
David Anderson637bfd62019-09-26 13:23:21 -0700415 if (!FLAGS_boot_slot.empty()) {
416 tmp_config_obj.set_boot_slot(FLAGS_boot_slot);
417 }
418
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800419 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
420
421 auto config_file = GetConfigFilePath(tmp_config_obj);
422 auto config_link = vsoc::GetGlobalConfigFileLink();
423 // Save the config object before starting any host process
424 if (!tmp_config_obj.SaveToFile(config_file)) {
425 LOG(ERROR) << "Unable to save config object";
426 return false;
427 }
428 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
429 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
430 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
431 << ": " << strerror(errno);
432 return false;
433 }
434
435 return true;
436}
437
438void SetDefaultFlagsForQemu() {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800439 auto default_instance_dir =
440 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
441 SetCommandLineOptionWithMode("instance_dir",
442 default_instance_dir.c_str(),
443 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800444 // TODO(b/144119457) Use the serial port.
445 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800446 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800447}
448
449void SetDefaultFlagsForCrosvm() {
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800450 auto default_instance_dir =
451 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
452 SetCommandLineOptionWithMode("instance_dir",
453 default_instance_dir.c_str(),
454 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700455 SetCommandLineOptionWithMode("wayland_socket",
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700456 "",
457 google::FlagSettingMode::SET_FLAGS_DEFAULT);
458 SetCommandLineOptionWithMode("x_display",
459 getenv("DISPLAY"),
Greg Hartmana3c552d2019-03-28 18:20:48 -0700460 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800461 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
462 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800463}
464
465bool ParseCommandLineFlags(int* argc, char*** argv) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800466 google::ParseCommandLineNonHelpFlags(argc, argv, true);
467 bool invalid_manager = false;
468 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
469 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800470 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
471 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800472 } else {
473 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
474 << std::endl;
475 invalid_manager = true;
476 }
477 google::HandleCommandLineHelpFlags();
478 if (invalid_manager) {
479 return false;
480 }
481 // Set the env variable to empty (in case the caller passed a value for it).
482 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
483
484 return ResolveInstanceFiles();
485}
486
487bool CleanPriorFiles() {
488 // Everything on the instance directory
489 std::string prior_files = FLAGS_instance_dir + "/*";
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800490 // The environment file
491 prior_files += " " + GetCuttlefishEnvPath();
492 // The global link to the config file
493 prior_files += " " + vsoc::GetGlobalConfigFileLink();
494 LOG(INFO) << "Assuming prior files of " << prior_files;
495 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
496 int rval = std::system(fuser_cmd.c_str());
497 // fuser returns 0 if any of the files are open
498 if (WEXITSTATUS(rval) == 0) {
499 LOG(ERROR) << "Clean aborted: files are in use";
500 return false;
501 }
502 std::string clean_command = "rm -rf " + prior_files;
503 rval = std::system(clean_command.c_str());
504 if (WEXITSTATUS(rval) != 0) {
505 LOG(ERROR) << "Remove of files failed";
506 return false;
507 }
508 return true;
509}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800510
511bool DecompressKernel(const std::string& src, const std::string& dst) {
512 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
513 decomp_cmd.AddParameter(src);
514 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
515 if (!output_file->IsOpen()) {
516 LOG(ERROR) << "Unable to create decompressed image file: "
517 << output_file->StrError();
518 return false;
519 }
520 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
521 auto decomp_proc = decomp_cmd.Start(false);
522 return decomp_proc.Started() && decomp_proc.Wait() == 0;
523}
Cody Schuffelen78824ed2019-09-06 17:43:15 -0700524
525void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) {
526 auto adb_modes = config.adb_mode();
527 adb_modes.erase(vsoc::AdbMode::Unknown);
528 if (adb_modes.size() < 1) {
529 LOG(INFO) << "ADB not enabled";
530 }
531}
532
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800533} // namespace
534
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700535namespace {
536
537std::vector<ImagePartition> disk_config() {
538 std::vector<ImagePartition> partitions;
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700539 partitions.push_back(ImagePartition {
540 .label = "super",
541 .image_file_path = FLAGS_super_image,
542 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700543 partitions.push_back(ImagePartition {
544 .label = "userdata",
545 .image_file_path = FLAGS_data_image,
546 });
547 partitions.push_back(ImagePartition {
548 .label = "cache",
549 .image_file_path = FLAGS_cache_image,
550 });
551 partitions.push_back(ImagePartition {
552 .label = "metadata",
553 .image_file_path = FLAGS_metadata_image,
554 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700555 partitions.push_back(ImagePartition {
556 .label = "boot",
557 .image_file_path = FLAGS_boot_image,
558 });
Yifan Hong19d713e2019-05-01 14:12:07 -0700559 partitions.push_back(ImagePartition {
560 .label = "misc",
561 .image_file_path = FLAGS_misc_image
562 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700563 return partitions;
564}
565
566bool ShouldCreateCompositeDisk() {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700567 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
568 // The crosvm implementation is very fast to rebuild but also more brittle due to being split
569 // into multiple files. The QEMU implementation is slow to build, but completely self-contained
570 // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU.
571 return true;
572 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700573 auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk);
574 for (auto& partition : disk_config()) {
575 auto partition_age = cvd::FileModificationTime(partition.image_file_path);
576 if (partition_age >= composite_age) {
577 LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", "
578 << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\"";
579 return true;
580 }
581 }
582 return false;
583}
584
Ram Muthiah3db0a562019-08-26 13:35:19 -0700585bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path,
586 const std::string& ramdisk_b_path) {
587 // clear out file of any pre-existing content
588 std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
589 std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary);
590 std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700591
Ram Muthiah3db0a562019-08-26 13:35:19 -0700592 if(!new_ramdisk.is_open() || !ramdisk_a.is_open() || !ramdisk_b.is_open()) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700593 return false;
594 }
595
Ram Muthiah3db0a562019-08-26 13:35:19 -0700596 new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
Ram Muthiahaad97c52019-08-14 17:05:01 -0700597 return true;
598}
599
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700600void CreateCompositeDisk(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700601 if (FLAGS_composite_disk.empty()) {
602 LOG(FATAL) << "asked to create composite disk, but path was empty";
603 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700604 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
605 std::string header_path = config.PerInstancePath("gpt_header.img");
606 std::string footer_path = config.PerInstancePath("gpt_footer.img");
607 create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk);
608 } else {
609 aggregate_image(disk_config(), FLAGS_composite_disk);
610 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700611}
612
613} // namespace
614
Cody Schuffelen605e6852019-10-16 16:22:24 -0700615const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700616 int* argc, char*** argv, cvd::FetcherConfig fetcher_config) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800617 if (!ParseCommandLineFlags(argc, argv)) {
618 LOG(ERROR) << "Failed to parse command arguments";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700619 exit(AssemblerExitCodes::kArgumentParsingError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800620 }
621
622 // Clean up prior files before saving the config file (doing it after would
623 // delete it)
624 if (!CleanPriorFiles()) {
625 LOG(ERROR) << "Failed to clean prior files";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700626 exit(AssemblerExitCodes::kPrioFilesCleanupError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800627 }
628 // Create instance directory if it doesn't exist.
629 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
630 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
631 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
632 LOG(ERROR) << "Failed to create instance directory: "
633 << FLAGS_instance_dir << ". Error: " << errno;
Cody Schuffelene71fa352019-09-10 16:11:58 -0700634 exit(AssemblerExitCodes::kInstanceDirCreationError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800635 }
636 }
637
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700638 auto internal_dir = FLAGS_instance_dir + "/" + vsoc::kInternalDirName;
639 if (!cvd::DirectoryExists(internal_dir)) {
640 if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) <
641 0) {
642 LOG(ERROR) << "Failed to create internal instance directory: "
643 << internal_dir << ". Error: " << errno;
644 exit(AssemblerExitCodes::kInstanceDirCreationError);
645 }
646 }
647
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800648 if (!cvd::FileHasContent(FLAGS_boot_image)) {
649 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
650 exit(cvd::kCuttlefishConfigurationInitError);
651 }
652
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000653 if (!cvd::FileHasContent(FLAGS_vendor_boot_image)) {
654 LOG(ERROR) << "File not found: " << FLAGS_vendor_boot_image;
655 exit(cvd::kCuttlefishConfigurationInitError);
656 }
657
658 auto boot_img_unpacker =
659 cvd::BootImageUnpacker::FromImages(FLAGS_boot_image,
660 FLAGS_vendor_boot_image);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800661
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700662 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker, fetcher_config)) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800663 LOG(ERROR) << "Failed to initialize configuration";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700664 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800665 }
666 // Do this early so that the config object is ready for anything that needs it
667 auto config = vsoc::CuttlefishConfig::Get();
668 if (!config) {
669 LOG(ERROR) << "Failed to obtain config singleton";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700670 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800671 }
672
673 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000674 config->vendor_ramdisk_image_path(),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800675 config->use_unpacked_kernel()
676 ? config->kernel_image_path()
677 : "")) {
678 LOG(ERROR) << "Failed to unpack boot image";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700679 exit(AssemblerExitCodes::kBootImageUnpackError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800680 }
681
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000682 // TODO(134522463) as part of the bootloader refactor, repack the vendor boot
683 // image and use the bootloader to load both the boot and vendor ramdisk.
684 // Until then, this hack to get gki modules into cuttlefish will suffice.
685
686 // If a vendor ramdisk comes in via this mechanism, let it supercede the one
687 // in the vendor boot image. This flag is what kernel presubmit testing uses
688 // to pass in the kernel ramdisk.
Ram Muthiahd512c132019-10-23 17:20:45 -0700689
690 // If no kernel is passed in or an initramfs is made available, the default
691 // vendor boot ramdisk or the initramfs provided should be appended to the
692 // boot ramdisk. If a kernel IS provided with no initramfs, it is safe to
693 // safe to assume that the kernel was built with no modules and expects no
694 // modules for cf to run properly.
Cody Schuffelencd3563a2019-10-29 16:35:06 -0700695 std::string discovered_kernel = fetcher_config.FindCvdFileWithSuffix(kKernelDefaultPath);
696 std::string foreign_kernel = FLAGS_kernel_path.size() ? FLAGS_kernel_path : discovered_kernel;
697 std::string discovered_ramdisk = fetcher_config.FindCvdFileWithSuffix(kInitramfsImg);
698 std::string foreign_ramdisk = FLAGS_initramfs_path.size () ? FLAGS_initramfs_path : discovered_ramdisk;
699 if(!foreign_kernel.size() || foreign_ramdisk.size()) {
Ram Muthiahd512c132019-10-23 17:20:45 -0700700 const std::string& vendor_ramdisk_path =
701 config->initramfs_path().size() ? config->initramfs_path()
702 : config->vendor_ramdisk_image_path();
703 if(!ConcatRamdisks(config->final_ramdisk_path(),
704 config->ramdisk_image_path(), vendor_ramdisk_path)) {
705 LOG(ERROR) << "Failed to concatenate ramdisk and vendor ramdisk";
706 exit(AssemblerExitCodes::kInitRamFsConcatError);
707 }
Ram Muthiahaad97c52019-08-14 17:05:01 -0700708 }
709
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800710 if (config->decompress_kernel()) {
711 if (!DecompressKernel(config->kernel_image_path(),
712 config->decompressed_kernel_image_path())) {
713 LOG(ERROR) << "Failed to decompress kernel";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700714 exit(AssemblerExitCodes::kKernelDecompressError);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800715 }
716 }
717
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800718 ValidateAdbModeFlag(*config);
719
Yifan Hong19d713e2019-05-01 14:12:07 -0700720 // Create misc if necessary
721 if (!InitializeMiscImage(FLAGS_misc_image)) {
722 exit(cvd::kCuttlefishConfigurationInitError);
723 }
724
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800725 // Create data if necessary
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700726 if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800727 exit(cvd::kCuttlefishConfigurationInitError);
728 }
729
Cody Schuffelen69c19592019-05-31 16:00:30 -0700730 if (!cvd::FileExists(FLAGS_metadata_image)) {
731 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
732 }
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900733
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700734 if (SuperImageNeedsRebuilding(fetcher_config, *config)) {
735 if (!RebuildSuperImage(fetcher_config, *config, FLAGS_super_image)) {
736 LOG(ERROR) << "Super image rebuilding requested but could not be completed.";
737 exit(cvd::kCuttlefishConfigurationInitError);
738 }
739 }
740
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700741 if (ShouldCreateCompositeDisk()) {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700742 CreateCompositeDisk(*config);
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700743 }
744
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800745 // Check that the files exist
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700746 for (const auto& file : config->virtual_disk_paths()) {
Alistair Strachan050ca932018-11-27 12:41:19 -0800747 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800748 LOG(ERROR) << "File not found: " << file;
749 exit(cvd::kCuttlefishConfigurationInitError);
750 }
751 }
752
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800753 return config;
754}
755
756std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
757 return config.PerInstancePath("cuttlefish_config.json");
758}