blob: 7406a147739669ff11e311f26bcf49b646dd5718 [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
6#include <gflags/gflags.h>
7#include <glog/logging.h>
8
Cody Schuffelen90b2fb22019-02-28 18:55:21 -08009#include "common/libs/strings/str_split.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080010#include "common/libs/utils/environment.h"
11#include "common/libs/utils/files.h"
12#include "common/vsoc/lib/vsoc_memory.h"
Cody Schuffelen147b88e2019-09-09 16:00:11 -070013#include "host/commands/assemble_cvd/boot_image_unpacker.h"
14#include "host/commands/assemble_cvd/data_image.h"
15#include "host/commands/assemble_cvd/image_aggregator.h"
16#include "host/commands/assemble_cvd/assembler_defs.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080017#include "host/libs/vm_manager/crosvm_manager.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080018#include "host/libs/vm_manager/qemu_manager.h"
19#include "host/libs/vm_manager/vm_manager.h"
20
21using vsoc::GetPerInstanceDefault;
22using cvd::LauncherExitCodes;
23
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080024DEFINE_string(cache_image, "", "Location of the cache partition image.");
Paul Trautrimba8f8e92019-03-12 17:55:48 +090025DEFINE_string(metadata_image, "", "Location of the metadata partition image "
26 "to be generated.");
27DEFINE_int32(blank_metadata_image_mb, 16,
28 "The size of the blank metadata image to generate, MB.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080029DEFINE_int32(cpus, 2, "Virtual CPU count.");
30DEFINE_string(data_image, "", "Location of the data partition image.");
31DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
32 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
33 "'always_create'.");
34DEFINE_int32(blank_data_image_mb, 0,
35 "The size of the blank data image to generate, MB.");
36DEFINE_string(blank_data_image_fmt, "ext4",
37 "The fs format for the blank data image. Used with mkfs.");
38DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000039 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080040
41DEFINE_int32(x_res, 720, "Width of the screen in pixels");
42DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
43DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
44DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
45DEFINE_int32(num_screen_buffers, 3, "The number of screen buffers");
46DEFINE_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.");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080051DEFINE_string(kernel_decompresser_executable,
52 vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"),
53 "Path to the extract-vmlinux executable.");
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");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080057DEFINE_string(androidboot_console, "ttyS1",
58 "Console device for the Android framework");
Jorge E. Moreiraba626622019-01-28 17:47:50 -080059DEFINE_string(
60 hardware_name, "",
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -080061 "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080062DEFINE_string(guest_security, "selinux",
63 "The security module to use in the guest");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070064DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080065 "Whether to run in enforcing mode (non permissive). Ignored if "
66 "-guest_security is empty.");
67DEFINE_bool(guest_audit_security, true,
68 "Whether to log security audits.");
Yifan Hong19d713e2019-05-01 14:12:07 -070069DEFINE_string(boot_image, "",
70 "Location of cuttlefish boot image. If empty it is assumed to be "
71 "boot.img in the directory specified by -system_image_dir.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080072DEFINE_int32(memory_mb, 2048,
73 "Total amount of memory available for guest, MB.");
74std::string g_default_mempath{vsoc::GetDefaultMempath()};
75DEFINE_string(mempath, g_default_mempath.c_str(),
76 "Target location for the shmem file.");
77DEFINE_string(mobile_interface, "", // default handled on ParseCommandLine
78 "Network interface to use for mobile networking");
79DEFINE_string(mobile_tap_name, "", // default handled on ParseCommandLine
80 "The name of the tap interface to use for mobile");
81std::string g_default_serial_number{GetPerInstanceDefault("CUTTLEFISHCVD")};
82DEFINE_string(serial_number, g_default_serial_number.c_str(),
83 "Serial number to use for the device");
84DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
85 "A directory to put all instance specific files");
86DEFINE_string(
Alistair Strachan5ab095c2019-05-23 20:41:20 +000087 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080088 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070089DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070090 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
91 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070092DEFINE_string(wayland_socket, "",
Greg Hartmanbc1bed42019-04-05 20:02:00 -070093 "Location of the wayland socket to use for drm_virgl gpu_mode.");
Greg Hartmaneb04ac52019-07-16 16:44:18 -070094DEFINE_string(x_display, "",
95 "X display to use for drm_virgl gpu_mode.");
Greg Hartmana3c552d2019-03-28 18:20:48 -070096
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080097DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
98 "Location of the system partition images.");
Alistair Strachan050ca932018-11-27 12:41:19 -080099DEFINE_string(super_image, "", "Location of the super partition image.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700100DEFINE_string(misc_image, "",
101 "Location of the misc partition image. If the image does not "
102 "exist, a blank new misc partition image is created.");
Cody Schuffelen68728802019-09-23 22:22:19 +0000103DEFINE_string(composite_disk, "", "Location of the composite disk image.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800104
105DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
106 " host kernel. This is only used during transition of our clients."
107 " Will be deprecated soon.");
108DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process.");
109DEFINE_string(vnc_server_binary,
110 vsoc::DefaultHostArtifactsPath("bin/vnc_server"),
111 "Location of the vnc server binary.");
Alistair Strachan7eeddf42019-03-04 18:13:34 -0800112DEFINE_bool(start_stream_audio, false,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800113 "Whether to start the stream audio process.");
114DEFINE_string(stream_audio_binary,
115 vsoc::DefaultHostArtifactsPath("bin/stream_audio"),
116 "Location of the stream_audio binary.");
117DEFINE_string(virtual_usb_manager_binary,
118 vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager"),
119 "Location of the virtual usb manager binary.");
120DEFINE_string(kernel_log_monitor_binary,
121 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"),
122 "Location of the log monitor binary.");
123DEFINE_string(ivserver_binary,
124 vsoc::DefaultHostArtifactsPath("bin/ivserver"),
125 "Location of the ivshmem server binary.");
126DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444),
127 "The port on which the vnc server should listen");
128DEFINE_int32(stream_audio_port, GetPerInstanceDefault(7444),
129 "The port on which stream_audio should listen.");
130DEFINE_string(socket_forward_proxy_binary,
131 vsoc::DefaultHostArtifactsPath("bin/socket_forward_proxy"),
132 "Location of the socket_forward_proxy binary.");
133DEFINE_string(socket_vsock_proxy_binary,
134 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"),
135 "Location of the socket_vsock_proxy binary.");
Cody Schuffelen10743332019-04-15 16:50:49 -0700136DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen63d10052019-02-26 12:21:53 -0800137 "Mode for ADB connection. Can be 'usb' for USB forwarding, "
138 "'tunnel' for a TCP connection tunneled through VSoC, "
139 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
140 "'native_vsock' for a direct connection to the guest ADB over "
141 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
142 "the guest ADB server, or a comma separated list of types as in "
143 "'usb,tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800144DEFINE_bool(run_adb_connector, true,
145 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000146 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800147DEFINE_string(adb_connector_binary,
148 vsoc::DefaultHostArtifactsPath("bin/adb_connector"),
149 "Location of the adb_connector binary. Only relevant if "
Matthias Maennich454d7872019-02-06 16:35:17 +0000150 "-run_adb_connector is true");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800151DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800152DEFINE_string(wifi_tap_name, "", // default handled on ParseCommandLine
153 "The name of the tap interface to use for wifi");
154DEFINE_int32(vsock_guest_cid,
155 vsoc::GetDefaultPerInstanceVsockCid(),
156 "Guest identifier for vsock. Disabled if under 3.");
157
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800158DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
159 "UUID to use for the device. Random if not specified");
160DEFINE_bool(daemon, false,
161 "Run cuttlefish in background, the launcher exits on boot "
162 "completed/failed");
163
164DEFINE_string(device_title, "", "Human readable name for the instance, "
165 "used by the vnc_server for its server title");
166DEFINE_string(setupwizard_mode, "DISABLED",
167 "One of DISABLED,OPTIONAL,REQUIRED");
168
169DEFINE_string(qemu_binary,
170 "/usr/bin/qemu-system-x86_64",
171 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800172DEFINE_string(crosvm_binary,
173 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
174 "The Crosvm binary to use");
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700175DEFINE_string(console_forwarder_binary,
176 vsoc::DefaultHostArtifactsPath("bin/console_forwarder"),
177 "The Console Forwarder binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800178DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
179DEFINE_bool(run_e2e_test, true, "Run e2e test after device launches");
180DEFINE_string(e2e_test_binary,
181 vsoc::DefaultHostArtifactsPath("bin/host_region_e2e_test"),
182 "Location of the region end to end test binary");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800183DEFINE_string(logcat_receiver_binary,
184 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"),
185 "Binary for the logcat server");
186DEFINE_string(logcat_mode, "", "How to send android's log messages from "
187 "guest to host. One of [serial, vsock]");
188DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
189 "The port for logcat over vsock");
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700190DEFINE_string(config_server_binary,
191 vsoc::DefaultHostArtifactsPath("bin/config_server"),
192 "Binary for the configuration server");
193DEFINE_int32(config_server_port, vsoc::GetPerInstanceDefault(4680),
194 "The (vsock) port for the configuration server");
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800195DEFINE_int32(frames_vsock_port, vsoc::GetPerInstanceDefault(5580),
196 "The vsock port to receive frames from the guest on");
Ram Muthiah6e9c98c2019-05-28 15:18:27 -0700197DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on "
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700198 "both the guest and the host");
199DEFINE_string(tombstone_receiver_binary,
200 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"),
201 "Binary for the tombstone server");
202DEFINE_int32(tombstone_receiver_port, vsoc::GetPerInstanceDefault(5630),
203 "The vsock port for tombstones");
Cody Schuffelen1300f122019-05-28 18:24:34 -0700204DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
205DEFINE_string(bootloader, "", "Bootloader binary path");
Yifan Hong19d713e2019-05-01 14:12:07 -0700206
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800207namespace {
208
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700209std::string kRamdiskConcatExt = ".concat";
210
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800211template<typename S, typename T>
212static std::string concat(const S& s, const T& t) {
213 std::ostringstream os;
214 os << s << t;
215 return os.str();
216}
217
218bool ResolveInstanceFiles() {
219 if (FLAGS_system_image_dir.empty()) {
220 LOG(ERROR) << "--system_image_dir must be specified.";
221 return false;
222 }
223
224 // If user did not specify location of either of these files, expect them to
225 // be placed in --system_image_dir location.
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800226 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
227 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
228 google::FlagSettingMode::SET_FLAGS_DEFAULT);
229 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
230 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
231 google::FlagSettingMode::SET_FLAGS_DEFAULT);
232 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
233 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
234 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900235 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
236 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
237 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800238 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
239 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
240 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Yifan Hong19d713e2019-05-01 14:12:07 -0700241 std::string default_misc_image = FLAGS_system_image_dir + "/misc.img";
242 SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(),
243 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800244
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800245 return true;
246}
247
248std::string GetCuttlefishEnvPath() {
249 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
250}
251
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700252int GetHostPort() {
253 constexpr int kFirstHostPort = 6520;
254 return vsoc::GetPerInstanceDefault(kFirstHostPort);
255}
256
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800257// Initializes the config object and saves it to file. It doesn't return it, all
258// further uses of the config should happen through the singleton
259bool InitializeCuttlefishConfiguration(
260 const cvd::BootImageUnpacker& boot_image_unpacker) {
261 vsoc::CuttlefishConfig tmp_config_obj;
262 auto& memory_layout = *vsoc::VSoCMemoryLayout::Get();
263 // Set this first so that calls to PerInstancePath below are correct
264 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
265 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
266 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
267 return false;
268 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700269 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
270 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
271 return false;
272 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800273 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700274 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
275 if (!vm_manager::VmManager::ConfigureGpuMode(&tmp_config_obj)) {
276 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
277 " does not work with vm_manager=" << FLAGS_vm_manager;
278 return false;
279 }
280 tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket);
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700281 tmp_config_obj.set_x_display(FLAGS_x_display);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800282
Alistair Strachan050ca932018-11-27 12:41:19 -0800283 vm_manager::VmManager::ConfigureBootDevices(&tmp_config_obj);
284
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800285 tmp_config_obj.set_serial_number(FLAGS_serial_number);
286
287 tmp_config_obj.set_cpus(FLAGS_cpus);
288 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
289
290 tmp_config_obj.set_dpi(FLAGS_dpi);
291 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
292 tmp_config_obj.set_x_res(FLAGS_x_res);
293 tmp_config_obj.set_y_res(FLAGS_y_res);
294 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
295 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
296 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800297 std::vector<std::string> adb = cvd::StrSplit(FLAGS_adb_mode, ',');
298 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700299 tmp_config_obj.set_host_port(GetHostPort());
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800300 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
301
302 tmp_config_obj.set_device_title(FLAGS_device_title);
303 if (FLAGS_kernel_path.size()) {
304 tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
305 tmp_config_obj.set_use_unpacked_kernel(false);
306 } else {
307 tmp_config_obj.set_kernel_image_path(
308 tmp_config_obj.PerInstancePath("kernel"));
309 tmp_config_obj.set_use_unpacked_kernel(true);
310 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800311 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
312 if (FLAGS_decompress_kernel) {
313 tmp_config_obj.set_decompressed_kernel_image_path(
314 tmp_config_obj.PerInstancePath("vmlinux"));
315 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800316
317 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
318 bool use_ramdisk = boot_image_unpacker.HasRamdiskImage();
319 if (!use_ramdisk) {
320 LOG(INFO) << "No ramdisk present; assuming system-as-root build";
321 ramdisk_path = "";
322 }
323
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800324 tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700325
326 if (use_ramdisk) {
327 if (FLAGS_composite_disk.empty()) {
328 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab");
329 } else {
Alistair Delva73ad5ec2019-08-30 15:37:19 -0700330 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab.composite");
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700331 }
332 } else {
333 if (FLAGS_composite_disk.empty()) {
334 tmp_config_obj.add_kernel_cmdline("root=/dev/vda");
335 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab");
336 } else {
337 tmp_config_obj.add_kernel_cmdline("root=/dev/vda1");
Alistair Delva73ad5ec2019-08-30 15:37:19 -0700338 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab.composite");
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700339 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800340 }
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700341
Alistair Strachan050ca932018-11-27 12:41:19 -0800342 if (!FLAGS_super_image.empty()) {
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700343 if (FLAGS_composite_disk.empty()) {
344 tmp_config_obj.add_kernel_cmdline("androidboot.super_partition=vda");
345 } else {
346 tmp_config_obj.add_kernel_cmdline("androidboot.super_partition=super");
347 }
Alistair Strachan050ca932018-11-27 12:41:19 -0800348 }
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700349
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800350 tmp_config_obj.add_kernel_cmdline("init=/init");
351 tmp_config_obj.add_kernel_cmdline(
352 concat("androidboot.serialno=", FLAGS_serial_number));
353 tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0");
354 tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi));
355 tmp_config_obj.add_kernel_cmdline(
356 concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode));
357 tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800358 if (!FLAGS_androidboot_console.empty()) {
359 tmp_config_obj.add_kernel_cmdline(
360 concat("androidboot.console=", FLAGS_androidboot_console));
361 }
362 if (!FLAGS_hardware_name.empty()) {
363 tmp_config_obj.add_kernel_cmdline(
364 concat("androidboot.hardware=", FLAGS_hardware_name));
365 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800366 if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) {
367 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=",
368 FLAGS_logcat_vsock_port));
369 }
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700370 tmp_config_obj.add_kernel_cmdline(concat("androidboot.cuttlefish_config_server_port=",
371 FLAGS_config_server_port));
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800372 tmp_config_obj.set_hardware_name(FLAGS_hardware_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800373 if (!FLAGS_guest_security.empty()) {
374 tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security));
375 if (FLAGS_guest_enforce_security) {
376 tmp_config_obj.add_kernel_cmdline("enforcing=1");
377 } else {
378 tmp_config_obj.add_kernel_cmdline("enforcing=0");
379 tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive");
380 }
381 if (FLAGS_guest_audit_security) {
382 tmp_config_obj.add_kernel_cmdline("audit=1");
383 } else {
384 tmp_config_obj.add_kernel_cmdline("audit=0");
385 }
386 }
387 if (FLAGS_run_e2e_test) {
388 tmp_config_obj.add_kernel_cmdline("androidboot.vsoc_e2e_test=1");
389 }
390 if (FLAGS_extra_kernel_cmdline.size()) {
391 tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline);
392 }
393
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700394 if (!FLAGS_composite_disk.empty()) {
395 tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk});
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700396 } else {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700397 tmp_config_obj.set_virtual_disk_paths({
398 FLAGS_super_image,
399 FLAGS_data_image,
400 FLAGS_cache_image,
401 FLAGS_metadata_image,
402 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700403 }
404
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800405 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700406 if(FLAGS_initramfs_path.size() > 0) {
407 tmp_config_obj.set_initramfs_path(FLAGS_initramfs_path);
408 tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt);
409 } else {
410 tmp_config_obj.set_final_ramdisk_path(ramdisk_path);
411 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800412
413 tmp_config_obj.set_mempath(FLAGS_mempath);
414 tmp_config_obj.set_ivshmem_qemu_socket_path(
415 tmp_config_obj.PerInstancePath("ivshmem_socket_qemu"));
416 tmp_config_obj.set_ivshmem_client_socket_path(
417 tmp_config_obj.PerInstancePath("ivshmem_socket_client"));
418 tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size());
419
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700420 if (tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) > 0) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800421 tmp_config_obj.set_usb_v1_socket_name(tmp_config_obj.PerInstancePath("usb-v1"));
422 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
423 tmp_config_obj.set_usb_ip_socket_name(tmp_config_obj.PerInstancePath("usb-ip"));
424 }
425
Jorge E. Moreirab1ec4352019-06-04 11:43:08 -0700426 tmp_config_obj.set_kernel_log_pipe_name(tmp_config_obj.PerInstancePath("kernel-log"));
Jorge E. Moreira7207f4b2019-09-25 10:30:29 -0700427 tmp_config_obj.set_console_pipe_name(tmp_config_obj.PerInstancePath("console-pipe"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800428 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
429 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
430 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800431 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700432 tmp_config_obj.set_config_server_binary(FLAGS_config_server_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800433 tmp_config_obj.set_launcher_log_path(tmp_config_obj.PerInstancePath("launcher.log"));
434 tmp_config_obj.set_launcher_monitor_socket_path(
435 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
436
437 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
438 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
439
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800440 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
441
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800442 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
443
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800444 tmp_config_obj.set_uuid(FLAGS_uuid);
445
446 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800447 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700448 tmp_config_obj.set_console_forwarder_binary(FLAGS_console_forwarder_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800449 tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
450 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
451
452 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
453 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
454 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
455
456 tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio);
457 tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary);
458 tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port);
459
460 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
461 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
462 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
463 tmp_config_obj.set_virtual_usb_manager_binary(
464 FLAGS_virtual_usb_manager_binary);
465 tmp_config_obj.set_socket_forward_proxy_binary(
466 FLAGS_socket_forward_proxy_binary);
467 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
468 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
469 tmp_config_obj.set_run_e2e_test(FLAGS_run_e2e_test);
470 tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary);
471
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800472 tmp_config_obj.set_data_policy(FLAGS_data_policy);
473 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
474 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
475
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700476 if(tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) == 0) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800477 tmp_config_obj.disable_usb_adb();
478 }
479
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800480 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
481 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700482 tmp_config_obj.set_config_server_port(FLAGS_config_server_port);
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800483 tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port);
Jorge E. Moreira1859ccc2019-05-14 15:15:27 -0700484 if (!tmp_config_obj.enable_ivserver() && tmp_config_obj.enable_vnc_server()) {
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800485 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_frames_port=",
486 FLAGS_frames_vsock_port));
487 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800488
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700489 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
490 tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port);
491 tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary);
492 if (FLAGS_enable_tombstone_receiver) {
493 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=1");
494 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_tombstone_port="
495 ,FLAGS_tombstone_receiver_port));
Alistair Strachan050ca932018-11-27 12:41:19 -0800496 // TODO (b/128842613) populate a cid flag to read the host CID during
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700497 // runtime
498 } else {
499 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=0");
500 }
501
Cody Schuffelen1300f122019-05-28 18:24:34 -0700502 tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
503 tmp_config_obj.set_bootloader(FLAGS_bootloader);
504
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800505 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
506
507 auto config_file = GetConfigFilePath(tmp_config_obj);
508 auto config_link = vsoc::GetGlobalConfigFileLink();
509 // Save the config object before starting any host process
510 if (!tmp_config_obj.SaveToFile(config_file)) {
511 LOG(ERROR) << "Unable to save config object";
512 return false;
513 }
514 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
515 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
516 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
517 << ": " << strerror(errno);
518 return false;
519 }
520
521 return true;
522}
523
524void SetDefaultFlagsForQemu() {
525 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
526 SetCommandLineOptionWithMode("mobile_interface",
527 default_mobile_interface.c_str(),
528 google::FlagSettingMode::SET_FLAGS_DEFAULT);
529 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
530 SetCommandLineOptionWithMode("mobile_tap_name",
531 default_mobile_tap_name.c_str(),
532 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800533 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
534 SetCommandLineOptionWithMode("wifi_tap_name",
535 default_wifi_tap_name.c_str(),
536 google::FlagSettingMode::SET_FLAGS_DEFAULT);
537 auto default_instance_dir =
538 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
539 SetCommandLineOptionWithMode("instance_dir",
540 default_instance_dir.c_str(),
541 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800542 SetCommandLineOptionWithMode("hardware_name", "cutf_ivsh",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800543 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800544 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatSerialMode,
545 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800546}
547
548void SetDefaultFlagsForCrosvm() {
549 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
550 SetCommandLineOptionWithMode("mobile_interface",
551 default_mobile_interface.c_str(),
552 google::FlagSettingMode::SET_FLAGS_DEFAULT);
553 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
554 SetCommandLineOptionWithMode("mobile_tap_name",
555 default_mobile_tap_name.c_str(),
556 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800557 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
558 SetCommandLineOptionWithMode("wifi_tap_name",
559 default_wifi_tap_name.c_str(),
560 google::FlagSettingMode::SET_FLAGS_DEFAULT);
561 auto default_instance_dir =
562 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
563 SetCommandLineOptionWithMode("instance_dir",
564 default_instance_dir.c_str(),
565 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700566 SetCommandLineOptionWithMode("wayland_socket",
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700567 "",
568 google::FlagSettingMode::SET_FLAGS_DEFAULT);
569 SetCommandLineOptionWithMode("x_display",
570 getenv("DISPLAY"),
Greg Hartmana3c552d2019-03-28 18:20:48 -0700571 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800572 SetCommandLineOptionWithMode("hardware_name", "cutf_cvm",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800573 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira84d93c12019-02-05 12:02:29 -0800574 SetCommandLineOptionWithMode("run_e2e_test", "false",
575 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800576 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
577 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800578}
579
580bool ParseCommandLineFlags(int* argc, char*** argv) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800581 google::ParseCommandLineNonHelpFlags(argc, argv, true);
582 bool invalid_manager = false;
583 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
584 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800585 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
586 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800587 } else {
588 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
589 << std::endl;
590 invalid_manager = true;
591 }
592 google::HandleCommandLineHelpFlags();
593 if (invalid_manager) {
594 return false;
595 }
596 // Set the env variable to empty (in case the caller passed a value for it).
597 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
598
599 return ResolveInstanceFiles();
600}
601
602bool CleanPriorFiles() {
603 // Everything on the instance directory
604 std::string prior_files = FLAGS_instance_dir + "/*";
605 // The shared memory file
606 prior_files += " " + FLAGS_mempath;
607 // The environment file
608 prior_files += " " + GetCuttlefishEnvPath();
609 // The global link to the config file
610 prior_files += " " + vsoc::GetGlobalConfigFileLink();
611 LOG(INFO) << "Assuming prior files of " << prior_files;
612 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
613 int rval = std::system(fuser_cmd.c_str());
614 // fuser returns 0 if any of the files are open
615 if (WEXITSTATUS(rval) == 0) {
616 LOG(ERROR) << "Clean aborted: files are in use";
617 return false;
618 }
619 std::string clean_command = "rm -rf " + prior_files;
620 rval = std::system(clean_command.c_str());
621 if (WEXITSTATUS(rval) != 0) {
622 LOG(ERROR) << "Remove of files failed";
623 return false;
624 }
625 return true;
626}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800627
628bool DecompressKernel(const std::string& src, const std::string& dst) {
629 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
630 decomp_cmd.AddParameter(src);
631 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
632 if (!output_file->IsOpen()) {
633 LOG(ERROR) << "Unable to create decompressed image file: "
634 << output_file->StrError();
635 return false;
636 }
637 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
638 auto decomp_proc = decomp_cmd.Start(false);
639 return decomp_proc.Started() && decomp_proc.Wait() == 0;
640}
Cody Schuffelen78824ed2019-09-06 17:43:15 -0700641
642void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) {
643 auto adb_modes = config.adb_mode();
644 adb_modes.erase(vsoc::AdbMode::Unknown);
645 if (adb_modes.size() < 1) {
646 LOG(INFO) << "ADB not enabled";
647 }
648}
649
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800650} // namespace
651
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700652namespace {
653
654std::vector<ImagePartition> disk_config() {
655 std::vector<ImagePartition> partitions;
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700656 partitions.push_back(ImagePartition {
657 .label = "super",
658 .image_file_path = FLAGS_super_image,
659 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700660 partitions.push_back(ImagePartition {
661 .label = "userdata",
662 .image_file_path = FLAGS_data_image,
663 });
664 partitions.push_back(ImagePartition {
665 .label = "cache",
666 .image_file_path = FLAGS_cache_image,
667 });
668 partitions.push_back(ImagePartition {
669 .label = "metadata",
670 .image_file_path = FLAGS_metadata_image,
671 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700672 partitions.push_back(ImagePartition {
673 .label = "boot",
674 .image_file_path = FLAGS_boot_image,
675 });
Yifan Hong19d713e2019-05-01 14:12:07 -0700676 partitions.push_back(ImagePartition {
677 .label = "misc",
678 .image_file_path = FLAGS_misc_image
679 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700680 return partitions;
681}
682
683bool ShouldCreateCompositeDisk() {
684 if (FLAGS_composite_disk.empty()) {
685 return false;
686 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700687 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
688 // The crosvm implementation is very fast to rebuild but also more brittle due to being split
689 // into multiple files. The QEMU implementation is slow to build, but completely self-contained
690 // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU.
691 return true;
692 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700693 auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk);
694 for (auto& partition : disk_config()) {
695 auto partition_age = cvd::FileModificationTime(partition.image_file_path);
696 if (partition_age >= composite_age) {
697 LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", "
698 << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\"";
699 return true;
700 }
701 }
702 return false;
703}
704
Ram Muthiah3db0a562019-08-26 13:35:19 -0700705bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path,
706 const std::string& ramdisk_b_path) {
707 // clear out file of any pre-existing content
708 std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
709 std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary);
710 std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700711
Ram Muthiah3db0a562019-08-26 13:35:19 -0700712 if(!new_ramdisk.is_open() || !ramdisk_a.is_open() || !ramdisk_b.is_open()) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700713 return false;
714 }
715
Ram Muthiah3db0a562019-08-26 13:35:19 -0700716 new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
Ram Muthiahaad97c52019-08-14 17:05:01 -0700717 return true;
718}
719
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700720void CreateCompositeDisk(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700721 if (FLAGS_composite_disk.empty()) {
722 LOG(FATAL) << "asked to create composite disk, but path was empty";
723 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700724 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
725 std::string header_path = config.PerInstancePath("gpt_header.img");
726 std::string footer_path = config.PerInstancePath("gpt_footer.img");
727 create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk);
728 } else {
729 aggregate_image(disk_config(), FLAGS_composite_disk);
730 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700731}
732
733} // namespace
734
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800735vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(int* argc, char*** argv) {
736 if (!ParseCommandLineFlags(argc, argv)) {
737 LOG(ERROR) << "Failed to parse command arguments";
738 exit(LauncherExitCodes::kArgumentParsingError);
739 }
740
741 // Clean up prior files before saving the config file (doing it after would
742 // delete it)
743 if (!CleanPriorFiles()) {
744 LOG(ERROR) << "Failed to clean prior files";
745 exit(LauncherExitCodes::kPrioFilesCleanupError);
746 }
747 // Create instance directory if it doesn't exist.
748 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
749 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
750 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
751 LOG(ERROR) << "Failed to create instance directory: "
752 << FLAGS_instance_dir << ". Error: " << errno;
753 exit(LauncherExitCodes::kInstanceDirCreationError);
754 }
755 }
756
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800757 if (!cvd::FileHasContent(FLAGS_boot_image)) {
758 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
759 exit(cvd::kCuttlefishConfigurationInitError);
760 }
761
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800762 auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image);
763
764 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) {
765 LOG(ERROR) << "Failed to initialize configuration";
766 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
767 }
768 // Do this early so that the config object is ready for anything that needs it
769 auto config = vsoc::CuttlefishConfig::Get();
770 if (!config) {
771 LOG(ERROR) << "Failed to obtain config singleton";
772 exit(LauncherExitCodes::kCuttlefishConfigurationInitError);
773 }
774
775 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
776 config->use_unpacked_kernel()
777 ? config->kernel_image_path()
778 : "")) {
779 LOG(ERROR) << "Failed to unpack boot image";
780 exit(LauncherExitCodes::kBootImageUnpackError);
781 }
782
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700783 if(config->initramfs_path().size() != 0) {
784 if(!ConcatRamdisks(config->final_ramdisk_path(), config->ramdisk_image_path(),
785 config->initramfs_path())) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700786 LOG(ERROR) << "Failed to concatenate ramdisk and initramfs";
787 exit(LauncherExitCodes::kInitRamFsConcatError);
788 }
789 }
790
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800791 if (config->decompress_kernel()) {
792 if (!DecompressKernel(config->kernel_image_path(),
793 config->decompressed_kernel_image_path())) {
794 LOG(ERROR) << "Failed to decompress kernel";
795 exit(LauncherExitCodes::kKernelDecompressError);
796 }
797 }
798
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800799 ValidateAdbModeFlag(*config);
800
Yifan Hong19d713e2019-05-01 14:12:07 -0700801 // Create misc if necessary
802 if (!InitializeMiscImage(FLAGS_misc_image)) {
803 exit(cvd::kCuttlefishConfigurationInitError);
804 }
805
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800806 // Create data if necessary
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700807 if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800808 exit(cvd::kCuttlefishConfigurationInitError);
809 }
810
Cody Schuffelen69c19592019-05-31 16:00:30 -0700811 if (!cvd::FileExists(FLAGS_metadata_image)) {
812 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
813 }
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900814
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700815 if (ShouldCreateCompositeDisk()) {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700816 CreateCompositeDisk(*config);
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700817 }
818
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800819 // Check that the files exist
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700820 for (const auto& file : config->virtual_disk_paths()) {
Alistair Strachan050ca932018-11-27 12:41:19 -0800821 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800822 LOG(ERROR) << "File not found: " << file;
823 exit(cvd::kCuttlefishConfigurationInitError);
824 }
825 }
826
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800827 return config;
828}
829
830std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
831 return config.PerInstancePath("cuttlefish_config.json");
832}