blob: 2fe27634ec42a848efc3e42deec093d3293bea15 [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"
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.");
37DEFINE_string(blank_data_image_fmt, "ext4",
38 "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");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080058DEFINE_string(androidboot_console, "ttyS1",
59 "Console device for the Android framework");
Jorge E. Moreiraba626622019-01-28 17:47:50 -080060DEFINE_string(
61 hardware_name, "",
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -080062 "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080063DEFINE_string(guest_security, "selinux",
64 "The security module to use in the guest");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070065DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080066 "Whether to run in enforcing mode (non permissive). Ignored if "
67 "-guest_security is empty.");
68DEFINE_bool(guest_audit_security, true,
69 "Whether to log security audits.");
Yifan Hong19d713e2019-05-01 14:12:07 -070070DEFINE_string(boot_image, "",
71 "Location of cuttlefish boot image. If empty it is assumed to be "
72 "boot.img in the directory specified by -system_image_dir.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080073DEFINE_int32(memory_mb, 2048,
74 "Total amount of memory available for guest, MB.");
75std::string g_default_mempath{vsoc::GetDefaultMempath()};
76DEFINE_string(mempath, g_default_mempath.c_str(),
77 "Target location for the shmem file.");
78DEFINE_string(mobile_interface, "", // default handled on ParseCommandLine
79 "Network interface to use for mobile networking");
80DEFINE_string(mobile_tap_name, "", // default handled on ParseCommandLine
81 "The name of the tap interface to use for mobile");
82std::string g_default_serial_number{GetPerInstanceDefault("CUTTLEFISHCVD")};
83DEFINE_string(serial_number, g_default_serial_number.c_str(),
84 "Serial number to use for the device");
85DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
86 "A directory to put all instance specific files");
87DEFINE_string(
Alistair Strachan5ab095c2019-05-23 20:41:20 +000088 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080089 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070090DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070091 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
92 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070093DEFINE_string(wayland_socket, "",
Greg Hartmanbc1bed42019-04-05 20:02:00 -070094 "Location of the wayland socket to use for drm_virgl gpu_mode.");
Greg Hartmaneb04ac52019-07-16 16:44:18 -070095DEFINE_string(x_display, "",
96 "X display to use for drm_virgl gpu_mode.");
Greg Hartmana3c552d2019-03-28 18:20:48 -070097
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080098DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
99 "Location of the system partition images.");
Alistair Strachan050ca932018-11-27 12:41:19 -0800100DEFINE_string(super_image, "", "Location of the super partition image.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700101DEFINE_string(misc_image, "",
102 "Location of the misc partition image. If the image does not "
103 "exist, a blank new misc partition image is created.");
David Anderson64581ce2019-10-03 17:52:22 -0700104DEFINE_string(composite_disk, "", "Location of the composite disk image. "
105 "If empty, a composite disk is not used.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800106
107DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
108 " host kernel. This is only used during transition of our clients."
109 " Will be deprecated soon.");
110DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process.");
111DEFINE_string(vnc_server_binary,
112 vsoc::DefaultHostArtifactsPath("bin/vnc_server"),
113 "Location of the vnc server binary.");
Alistair Strachan7eeddf42019-03-04 18:13:34 -0800114DEFINE_bool(start_stream_audio, false,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800115 "Whether to start the stream audio process.");
116DEFINE_string(stream_audio_binary,
117 vsoc::DefaultHostArtifactsPath("bin/stream_audio"),
118 "Location of the stream_audio binary.");
119DEFINE_string(virtual_usb_manager_binary,
120 vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager"),
121 "Location of the virtual usb manager binary.");
122DEFINE_string(kernel_log_monitor_binary,
123 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"),
124 "Location of the log monitor binary.");
125DEFINE_string(ivserver_binary,
126 vsoc::DefaultHostArtifactsPath("bin/ivserver"),
127 "Location of the ivshmem server binary.");
128DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444),
129 "The port on which the vnc server should listen");
130DEFINE_int32(stream_audio_port, GetPerInstanceDefault(7444),
131 "The port on which stream_audio should listen.");
132DEFINE_string(socket_forward_proxy_binary,
133 vsoc::DefaultHostArtifactsPath("bin/socket_forward_proxy"),
134 "Location of the socket_forward_proxy binary.");
135DEFINE_string(socket_vsock_proxy_binary,
136 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"),
137 "Location of the socket_vsock_proxy binary.");
Cody Schuffelen10743332019-04-15 16:50:49 -0700138DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen63d10052019-02-26 12:21:53 -0800139 "Mode for ADB connection. Can be 'usb' for USB forwarding, "
140 "'tunnel' for a TCP connection tunneled through VSoC, "
141 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
142 "'native_vsock' for a direct connection to the guest ADB over "
143 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
144 "the guest ADB server, or a comma separated list of types as in "
145 "'usb,tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800146DEFINE_bool(run_adb_connector, true,
147 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000148 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800149DEFINE_string(adb_connector_binary,
150 vsoc::DefaultHostArtifactsPath("bin/adb_connector"),
151 "Location of the adb_connector binary. Only relevant if "
Matthias Maennich454d7872019-02-06 16:35:17 +0000152 "-run_adb_connector is true");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800153DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800154DEFINE_string(wifi_tap_name, "", // default handled on ParseCommandLine
155 "The name of the tap interface to use for wifi");
156DEFINE_int32(vsock_guest_cid,
157 vsoc::GetDefaultPerInstanceVsockCid(),
158 "Guest identifier for vsock. Disabled if under 3.");
159
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800160DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
161 "UUID to use for the device. Random if not specified");
162DEFINE_bool(daemon, false,
163 "Run cuttlefish in background, the launcher exits on boot "
164 "completed/failed");
165
166DEFINE_string(device_title, "", "Human readable name for the instance, "
167 "used by the vnc_server for its server title");
168DEFINE_string(setupwizard_mode, "DISABLED",
169 "One of DISABLED,OPTIONAL,REQUIRED");
170
171DEFINE_string(qemu_binary,
172 "/usr/bin/qemu-system-x86_64",
173 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800174DEFINE_string(crosvm_binary,
175 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
176 "The Crosvm binary to use");
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700177DEFINE_string(console_forwarder_binary,
178 vsoc::DefaultHostArtifactsPath("bin/console_forwarder"),
179 "The Console Forwarder binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800180DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
181DEFINE_bool(run_e2e_test, true, "Run e2e test after device launches");
182DEFINE_string(e2e_test_binary,
183 vsoc::DefaultHostArtifactsPath("bin/host_region_e2e_test"),
184 "Location of the region end to end test binary");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800185DEFINE_string(logcat_receiver_binary,
186 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"),
187 "Binary for the logcat server");
188DEFINE_string(logcat_mode, "", "How to send android's log messages from "
189 "guest to host. One of [serial, vsock]");
190DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
191 "The port for logcat over vsock");
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700192DEFINE_string(config_server_binary,
193 vsoc::DefaultHostArtifactsPath("bin/config_server"),
194 "Binary for the configuration server");
195DEFINE_int32(config_server_port, vsoc::GetPerInstanceDefault(4680),
196 "The (vsock) port for the configuration server");
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800197DEFINE_int32(frames_vsock_port, vsoc::GetPerInstanceDefault(5580),
198 "The vsock port to receive frames from the guest on");
Ram Muthiah6e9c98c2019-05-28 15:18:27 -0700199DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on "
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700200 "both the guest and the host");
201DEFINE_string(tombstone_receiver_binary,
202 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"),
203 "Binary for the tombstone server");
204DEFINE_int32(tombstone_receiver_port, vsoc::GetPerInstanceDefault(5630),
205 "The vsock port for tombstones");
Cody Schuffelen1300f122019-05-28 18:24:34 -0700206DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
207DEFINE_string(bootloader, "", "Bootloader binary path");
Yifan Hong19d713e2019-05-01 14:12:07 -0700208
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800209namespace {
210
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700211std::string kRamdiskConcatExt = ".concat";
212
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800213template<typename S, typename T>
214static std::string concat(const S& s, const T& t) {
215 std::ostringstream os;
216 os << s << t;
217 return os.str();
218}
219
220bool ResolveInstanceFiles() {
221 if (FLAGS_system_image_dir.empty()) {
222 LOG(ERROR) << "--system_image_dir must be specified.";
223 return false;
224 }
225
226 // If user did not specify location of either of these files, expect them to
227 // be placed in --system_image_dir location.
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800228 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
229 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
230 google::FlagSettingMode::SET_FLAGS_DEFAULT);
231 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
232 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
233 google::FlagSettingMode::SET_FLAGS_DEFAULT);
234 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
235 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
236 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900237 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
238 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
239 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800240 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
241 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
242 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Yifan Hong19d713e2019-05-01 14:12:07 -0700243 std::string default_misc_image = FLAGS_system_image_dir + "/misc.img";
244 SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(),
245 google::FlagSettingMode::SET_FLAGS_DEFAULT);
David Anderson64581ce2019-10-03 17:52:22 -0700246 std::string default_composite_disk = FLAGS_system_image_dir + "/composite.img";
247 SetCommandLineOptionWithMode("composite_disk", default_composite_disk.c_str(),
248 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800249
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800250 return true;
251}
252
253std::string GetCuttlefishEnvPath() {
254 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
255}
256
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700257int GetHostPort() {
258 constexpr int kFirstHostPort = 6520;
259 return vsoc::GetPerInstanceDefault(kFirstHostPort);
260}
261
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800262// Initializes the config object and saves it to file. It doesn't return it, all
263// further uses of the config should happen through the singleton
264bool InitializeCuttlefishConfiguration(
265 const cvd::BootImageUnpacker& boot_image_unpacker) {
266 vsoc::CuttlefishConfig tmp_config_obj;
267 auto& memory_layout = *vsoc::VSoCMemoryLayout::Get();
268 // Set this first so that calls to PerInstancePath below are correct
269 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
270 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
271 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
272 return false;
273 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700274 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
275 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
276 return false;
277 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800278 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700279 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
280 if (!vm_manager::VmManager::ConfigureGpuMode(&tmp_config_obj)) {
281 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
282 " does not work with vm_manager=" << FLAGS_vm_manager;
283 return false;
284 }
285 tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket);
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700286 tmp_config_obj.set_x_display(FLAGS_x_display);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800287
Alistair Strachan050ca932018-11-27 12:41:19 -0800288 vm_manager::VmManager::ConfigureBootDevices(&tmp_config_obj);
289
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800290 tmp_config_obj.set_serial_number(FLAGS_serial_number);
291
292 tmp_config_obj.set_cpus(FLAGS_cpus);
293 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
294
295 tmp_config_obj.set_dpi(FLAGS_dpi);
296 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
297 tmp_config_obj.set_x_res(FLAGS_x_res);
298 tmp_config_obj.set_y_res(FLAGS_y_res);
299 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
300 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
301 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800302 std::vector<std::string> adb = cvd::StrSplit(FLAGS_adb_mode, ',');
303 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700304 tmp_config_obj.set_host_port(GetHostPort());
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800305 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
306
307 tmp_config_obj.set_device_title(FLAGS_device_title);
308 if (FLAGS_kernel_path.size()) {
309 tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
310 tmp_config_obj.set_use_unpacked_kernel(false);
311 } else {
312 tmp_config_obj.set_kernel_image_path(
313 tmp_config_obj.PerInstancePath("kernel"));
314 tmp_config_obj.set_use_unpacked_kernel(true);
315 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800316 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
317 if (FLAGS_decompress_kernel) {
318 tmp_config_obj.set_decompressed_kernel_image_path(
319 tmp_config_obj.PerInstancePath("vmlinux"));
320 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800321
322 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
323 bool use_ramdisk = boot_image_unpacker.HasRamdiskImage();
324 if (!use_ramdisk) {
325 LOG(INFO) << "No ramdisk present; assuming system-as-root build";
326 ramdisk_path = "";
327 }
328
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800329 tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700330
331 if (use_ramdisk) {
332 if (FLAGS_composite_disk.empty()) {
333 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab");
334 } else {
Alistair Delva73ad5ec2019-08-30 15:37:19 -0700335 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab.composite");
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700336 }
337 } else {
338 if (FLAGS_composite_disk.empty()) {
339 tmp_config_obj.add_kernel_cmdline("root=/dev/vda");
340 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab");
341 } else {
342 tmp_config_obj.add_kernel_cmdline("root=/dev/vda1");
Alistair Delva73ad5ec2019-08-30 15:37:19 -0700343 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab.composite");
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700344 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800345 }
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700346
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800347 tmp_config_obj.add_kernel_cmdline("init=/init");
348 tmp_config_obj.add_kernel_cmdline(
349 concat("androidboot.serialno=", FLAGS_serial_number));
350 tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0");
351 tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi));
352 tmp_config_obj.add_kernel_cmdline(
353 concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode));
354 tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800355 if (!FLAGS_androidboot_console.empty()) {
356 tmp_config_obj.add_kernel_cmdline(
357 concat("androidboot.console=", FLAGS_androidboot_console));
358 }
359 if (!FLAGS_hardware_name.empty()) {
360 tmp_config_obj.add_kernel_cmdline(
361 concat("androidboot.hardware=", FLAGS_hardware_name));
362 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800363 if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) {
364 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=",
365 FLAGS_logcat_vsock_port));
366 }
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700367 tmp_config_obj.add_kernel_cmdline(concat("androidboot.cuttlefish_config_server_port=",
368 FLAGS_config_server_port));
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800369 tmp_config_obj.set_hardware_name(FLAGS_hardware_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800370 if (!FLAGS_guest_security.empty()) {
371 tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security));
372 if (FLAGS_guest_enforce_security) {
373 tmp_config_obj.add_kernel_cmdline("enforcing=1");
374 } else {
375 tmp_config_obj.add_kernel_cmdline("enforcing=0");
376 tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive");
377 }
378 if (FLAGS_guest_audit_security) {
379 tmp_config_obj.add_kernel_cmdline("audit=1");
380 } else {
381 tmp_config_obj.add_kernel_cmdline("audit=0");
382 }
383 }
384 if (FLAGS_run_e2e_test) {
385 tmp_config_obj.add_kernel_cmdline("androidboot.vsoc_e2e_test=1");
386 }
387 if (FLAGS_extra_kernel_cmdline.size()) {
388 tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline);
389 }
390
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700391 if (!FLAGS_composite_disk.empty()) {
392 tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk});
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700393 } else {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700394 tmp_config_obj.set_virtual_disk_paths({
395 FLAGS_super_image,
396 FLAGS_data_image,
397 FLAGS_cache_image,
398 FLAGS_metadata_image,
399 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700400 }
401
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800402 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700403 if(FLAGS_initramfs_path.size() > 0) {
404 tmp_config_obj.set_initramfs_path(FLAGS_initramfs_path);
405 tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt);
406 } else {
407 tmp_config_obj.set_final_ramdisk_path(ramdisk_path);
408 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800409
410 tmp_config_obj.set_mempath(FLAGS_mempath);
411 tmp_config_obj.set_ivshmem_qemu_socket_path(
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700412 tmp_config_obj.PerInstanceInternalPath("ivshmem_socket_qemu"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800413 tmp_config_obj.set_ivshmem_client_socket_path(
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700414 tmp_config_obj.PerInstanceInternalPath("ivshmem_socket_client"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800415 tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size());
416
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700417 if (tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) > 0) {
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700418 tmp_config_obj.set_usb_v1_socket_name(
419 tmp_config_obj.PerInstanceInternalPath("usb-v1"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800420 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700421 tmp_config_obj.set_usb_ip_socket_name(
422 tmp_config_obj.PerInstanceInternalPath("usb-ip"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800423 }
424
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700425 tmp_config_obj.set_kernel_log_pipe_name(
426 tmp_config_obj.PerInstanceInternalPath("kernel-log-pipe"));
427 tmp_config_obj.set_console_pipe_name(
428 tmp_config_obj.PerInstanceInternalPath("console-pipe"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800429 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
430 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
431 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800432 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700433 tmp_config_obj.set_config_server_binary(FLAGS_config_server_binary);
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700434 tmp_config_obj.set_launcher_log_path(
435 tmp_config_obj.PerInstancePath("launcher.log"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800436 tmp_config_obj.set_launcher_monitor_socket_path(
437 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
438
439 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
440 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
441
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800442 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
443
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800444 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
445
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800446 tmp_config_obj.set_uuid(FLAGS_uuid);
447
448 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800449 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700450 tmp_config_obj.set_console_forwarder_binary(FLAGS_console_forwarder_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800451 tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
452 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
453
454 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
455 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
456 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
457
458 tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio);
459 tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary);
460 tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port);
461
462 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
463 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
464 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
465 tmp_config_obj.set_virtual_usb_manager_binary(
466 FLAGS_virtual_usb_manager_binary);
467 tmp_config_obj.set_socket_forward_proxy_binary(
468 FLAGS_socket_forward_proxy_binary);
469 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
470 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
471 tmp_config_obj.set_run_e2e_test(FLAGS_run_e2e_test);
472 tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary);
473
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800474 tmp_config_obj.set_data_policy(FLAGS_data_policy);
475 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
476 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
477
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700478 if(tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) == 0) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800479 tmp_config_obj.disable_usb_adb();
480 }
481
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800482 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
483 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700484 tmp_config_obj.set_config_server_port(FLAGS_config_server_port);
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800485 tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port);
Jorge E. Moreira1859ccc2019-05-14 15:15:27 -0700486 if (!tmp_config_obj.enable_ivserver() && tmp_config_obj.enable_vnc_server()) {
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800487 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_frames_port=",
488 FLAGS_frames_vsock_port));
489 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800490
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700491 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
492 tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port);
493 tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary);
494 if (FLAGS_enable_tombstone_receiver) {
495 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=1");
496 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_tombstone_port="
497 ,FLAGS_tombstone_receiver_port));
Alistair Strachan050ca932018-11-27 12:41:19 -0800498 // TODO (b/128842613) populate a cid flag to read the host CID during
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700499 // runtime
500 } else {
501 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=0");
502 }
503
Cody Schuffelen1300f122019-05-28 18:24:34 -0700504 tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
505 tmp_config_obj.set_bootloader(FLAGS_bootloader);
506
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800507 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
508
509 auto config_file = GetConfigFilePath(tmp_config_obj);
510 auto config_link = vsoc::GetGlobalConfigFileLink();
511 // Save the config object before starting any host process
512 if (!tmp_config_obj.SaveToFile(config_file)) {
513 LOG(ERROR) << "Unable to save config object";
514 return false;
515 }
516 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
517 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
518 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
519 << ": " << strerror(errno);
520 return false;
521 }
522
523 return true;
524}
525
526void SetDefaultFlagsForQemu() {
527 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
528 SetCommandLineOptionWithMode("mobile_interface",
529 default_mobile_interface.c_str(),
530 google::FlagSettingMode::SET_FLAGS_DEFAULT);
531 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
532 SetCommandLineOptionWithMode("mobile_tap_name",
533 default_mobile_tap_name.c_str(),
534 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800535 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
536 SetCommandLineOptionWithMode("wifi_tap_name",
537 default_wifi_tap_name.c_str(),
538 google::FlagSettingMode::SET_FLAGS_DEFAULT);
539 auto default_instance_dir =
540 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
541 SetCommandLineOptionWithMode("instance_dir",
542 default_instance_dir.c_str(),
543 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800544 SetCommandLineOptionWithMode("hardware_name", "cutf_ivsh",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800545 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800546 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatSerialMode,
547 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800548}
549
550void SetDefaultFlagsForCrosvm() {
551 auto default_mobile_interface = GetPerInstanceDefault("cvd-mbr-");
552 SetCommandLineOptionWithMode("mobile_interface",
553 default_mobile_interface.c_str(),
554 google::FlagSettingMode::SET_FLAGS_DEFAULT);
555 auto default_mobile_tap_name = GetPerInstanceDefault("cvd-mtap-");
556 SetCommandLineOptionWithMode("mobile_tap_name",
557 default_mobile_tap_name.c_str(),
558 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800559 auto default_wifi_tap_name = GetPerInstanceDefault("cvd-wtap-");
560 SetCommandLineOptionWithMode("wifi_tap_name",
561 default_wifi_tap_name.c_str(),
562 google::FlagSettingMode::SET_FLAGS_DEFAULT);
563 auto default_instance_dir =
564 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
565 SetCommandLineOptionWithMode("instance_dir",
566 default_instance_dir.c_str(),
567 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700568 SetCommandLineOptionWithMode("wayland_socket",
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700569 "",
570 google::FlagSettingMode::SET_FLAGS_DEFAULT);
571 SetCommandLineOptionWithMode("x_display",
572 getenv("DISPLAY"),
Greg Hartmana3c552d2019-03-28 18:20:48 -0700573 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800574 SetCommandLineOptionWithMode("hardware_name", "cutf_cvm",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800575 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreira84d93c12019-02-05 12:02:29 -0800576 SetCommandLineOptionWithMode("run_e2e_test", "false",
577 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800578 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
579 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800580}
581
582bool ParseCommandLineFlags(int* argc, char*** argv) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800583 google::ParseCommandLineNonHelpFlags(argc, argv, true);
584 bool invalid_manager = false;
585 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
586 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800587 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
588 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800589 } else {
590 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
591 << std::endl;
592 invalid_manager = true;
593 }
594 google::HandleCommandLineHelpFlags();
595 if (invalid_manager) {
596 return false;
597 }
598 // Set the env variable to empty (in case the caller passed a value for it).
599 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
600
601 return ResolveInstanceFiles();
602}
603
604bool CleanPriorFiles() {
605 // Everything on the instance directory
606 std::string prior_files = FLAGS_instance_dir + "/*";
607 // The shared memory file
608 prior_files += " " + FLAGS_mempath;
609 // The environment file
610 prior_files += " " + GetCuttlefishEnvPath();
611 // The global link to the config file
612 prior_files += " " + vsoc::GetGlobalConfigFileLink();
613 LOG(INFO) << "Assuming prior files of " << prior_files;
614 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
615 int rval = std::system(fuser_cmd.c_str());
616 // fuser returns 0 if any of the files are open
617 if (WEXITSTATUS(rval) == 0) {
618 LOG(ERROR) << "Clean aborted: files are in use";
619 return false;
620 }
621 std::string clean_command = "rm -rf " + prior_files;
622 rval = std::system(clean_command.c_str());
623 if (WEXITSTATUS(rval) != 0) {
624 LOG(ERROR) << "Remove of files failed";
625 return false;
626 }
627 return true;
628}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800629
630bool DecompressKernel(const std::string& src, const std::string& dst) {
631 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
632 decomp_cmd.AddParameter(src);
633 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
634 if (!output_file->IsOpen()) {
635 LOG(ERROR) << "Unable to create decompressed image file: "
636 << output_file->StrError();
637 return false;
638 }
639 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
640 auto decomp_proc = decomp_cmd.Start(false);
641 return decomp_proc.Started() && decomp_proc.Wait() == 0;
642}
Cody Schuffelen78824ed2019-09-06 17:43:15 -0700643
644void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) {
645 auto adb_modes = config.adb_mode();
646 adb_modes.erase(vsoc::AdbMode::Unknown);
647 if (adb_modes.size() < 1) {
648 LOG(INFO) << "ADB not enabled";
649 }
650}
651
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800652} // namespace
653
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700654namespace {
655
656std::vector<ImagePartition> disk_config() {
657 std::vector<ImagePartition> partitions;
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700658 partitions.push_back(ImagePartition {
659 .label = "super",
660 .image_file_path = FLAGS_super_image,
661 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700662 partitions.push_back(ImagePartition {
663 .label = "userdata",
664 .image_file_path = FLAGS_data_image,
665 });
666 partitions.push_back(ImagePartition {
667 .label = "cache",
668 .image_file_path = FLAGS_cache_image,
669 });
670 partitions.push_back(ImagePartition {
671 .label = "metadata",
672 .image_file_path = FLAGS_metadata_image,
673 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700674 partitions.push_back(ImagePartition {
675 .label = "boot",
676 .image_file_path = FLAGS_boot_image,
677 });
Yifan Hong19d713e2019-05-01 14:12:07 -0700678 partitions.push_back(ImagePartition {
679 .label = "misc",
680 .image_file_path = FLAGS_misc_image
681 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700682 return partitions;
683}
684
685bool ShouldCreateCompositeDisk() {
686 if (FLAGS_composite_disk.empty()) {
687 return false;
688 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700689 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
690 // The crosvm implementation is very fast to rebuild but also more brittle due to being split
691 // into multiple files. The QEMU implementation is slow to build, but completely self-contained
692 // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU.
693 return true;
694 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700695 auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk);
696 for (auto& partition : disk_config()) {
697 auto partition_age = cvd::FileModificationTime(partition.image_file_path);
698 if (partition_age >= composite_age) {
699 LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", "
700 << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\"";
701 return true;
702 }
703 }
704 return false;
705}
706
Ram Muthiah3db0a562019-08-26 13:35:19 -0700707bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path,
708 const std::string& ramdisk_b_path) {
709 // clear out file of any pre-existing content
710 std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
711 std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary);
712 std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700713
Ram Muthiah3db0a562019-08-26 13:35:19 -0700714 if(!new_ramdisk.is_open() || !ramdisk_a.is_open() || !ramdisk_b.is_open()) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700715 return false;
716 }
717
Ram Muthiah3db0a562019-08-26 13:35:19 -0700718 new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
Ram Muthiahaad97c52019-08-14 17:05:01 -0700719 return true;
720}
721
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700722void CreateCompositeDisk(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700723 if (FLAGS_composite_disk.empty()) {
724 LOG(FATAL) << "asked to create composite disk, but path was empty";
725 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700726 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
727 std::string header_path = config.PerInstancePath("gpt_header.img");
728 std::string footer_path = config.PerInstancePath("gpt_footer.img");
729 create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk);
730 } else {
731 aggregate_image(disk_config(), FLAGS_composite_disk);
732 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700733}
734
735} // namespace
736
Cody Schuffelen605e6852019-10-16 16:22:24 -0700737const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(
738 int* argc, char*** argv, cvd::FetcherConfig) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800739 if (!ParseCommandLineFlags(argc, argv)) {
740 LOG(ERROR) << "Failed to parse command arguments";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700741 exit(AssemblerExitCodes::kArgumentParsingError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800742 }
743
744 // Clean up prior files before saving the config file (doing it after would
745 // delete it)
746 if (!CleanPriorFiles()) {
747 LOG(ERROR) << "Failed to clean prior files";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700748 exit(AssemblerExitCodes::kPrioFilesCleanupError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800749 }
750 // Create instance directory if it doesn't exist.
751 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
752 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
753 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
754 LOG(ERROR) << "Failed to create instance directory: "
755 << FLAGS_instance_dir << ". Error: " << errno;
Cody Schuffelene71fa352019-09-10 16:11:58 -0700756 exit(AssemblerExitCodes::kInstanceDirCreationError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800757 }
758 }
759
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700760 auto internal_dir = FLAGS_instance_dir + "/" + vsoc::kInternalDirName;
761 if (!cvd::DirectoryExists(internal_dir)) {
762 if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) <
763 0) {
764 LOG(ERROR) << "Failed to create internal instance directory: "
765 << internal_dir << ". Error: " << errno;
766 exit(AssemblerExitCodes::kInstanceDirCreationError);
767 }
768 }
769
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800770 if (!cvd::FileHasContent(FLAGS_boot_image)) {
771 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
772 exit(cvd::kCuttlefishConfigurationInitError);
773 }
774
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800775 auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image);
776
777 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) {
778 LOG(ERROR) << "Failed to initialize configuration";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700779 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800780 }
781 // Do this early so that the config object is ready for anything that needs it
782 auto config = vsoc::CuttlefishConfig::Get();
783 if (!config) {
784 LOG(ERROR) << "Failed to obtain config singleton";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700785 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800786 }
787
788 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
789 config->use_unpacked_kernel()
790 ? config->kernel_image_path()
791 : "")) {
792 LOG(ERROR) << "Failed to unpack boot image";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700793 exit(AssemblerExitCodes::kBootImageUnpackError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800794 }
795
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700796 if(config->initramfs_path().size() != 0) {
797 if(!ConcatRamdisks(config->final_ramdisk_path(), config->ramdisk_image_path(),
798 config->initramfs_path())) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700799 LOG(ERROR) << "Failed to concatenate ramdisk and initramfs";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700800 exit(AssemblerExitCodes::kInitRamFsConcatError);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700801 }
802 }
803
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800804 if (config->decompress_kernel()) {
805 if (!DecompressKernel(config->kernel_image_path(),
806 config->decompressed_kernel_image_path())) {
807 LOG(ERROR) << "Failed to decompress kernel";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700808 exit(AssemblerExitCodes::kKernelDecompressError);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800809 }
810 }
811
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800812 ValidateAdbModeFlag(*config);
813
Yifan Hong19d713e2019-05-01 14:12:07 -0700814 // Create misc if necessary
815 if (!InitializeMiscImage(FLAGS_misc_image)) {
816 exit(cvd::kCuttlefishConfigurationInitError);
817 }
818
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800819 // Create data if necessary
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700820 if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800821 exit(cvd::kCuttlefishConfigurationInitError);
822 }
823
Cody Schuffelen69c19592019-05-31 16:00:30 -0700824 if (!cvd::FileExists(FLAGS_metadata_image)) {
825 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
826 }
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900827
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700828 if (ShouldCreateCompositeDisk()) {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700829 CreateCompositeDisk(*config);
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700830 }
831
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800832 // Check that the files exist
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700833 for (const auto& file : config->virtual_disk_paths()) {
Alistair Strachan050ca932018-11-27 12:41:19 -0800834 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800835 LOG(ERROR) << "File not found: " << file;
836 exit(cvd::kCuttlefishConfigurationInitError);
837 }
838 }
839
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800840 return config;
841}
842
843std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
844 return config.PerInstancePath("cuttlefish_config.json");
845}