blob: af7d336d0b46aa73e269051cb83d505a23001408 [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 Schuffelenb737ef52019-09-25 14:54:38 -070017#include "host/commands/assemble_cvd/super_image_mixer.h"
Cody Schuffelen605e6852019-10-16 16:22:24 -070018#include "host/libs/config/fetcher_config.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080019#include "host/libs/vm_manager/crosvm_manager.h"
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080020#include "host/libs/vm_manager/qemu_manager.h"
21#include "host/libs/vm_manager/vm_manager.h"
22
23using vsoc::GetPerInstanceDefault;
Cody Schuffelene71fa352019-09-10 16:11:58 -070024using cvd::AssemblerExitCodes;
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080025
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080026DEFINE_string(cache_image, "", "Location of the cache partition image.");
Paul Trautrimba8f8e92019-03-12 17:55:48 +090027DEFINE_string(metadata_image, "", "Location of the metadata partition image "
28 "to be generated.");
29DEFINE_int32(blank_metadata_image_mb, 16,
30 "The size of the blank metadata image to generate, MB.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080031DEFINE_int32(cpus, 2, "Virtual CPU count.");
32DEFINE_string(data_image, "", "Location of the data partition image.");
33DEFINE_string(data_policy, "use_existing", "How to handle userdata partition."
34 " Either 'use_existing', 'create_if_missing', 'resize_up_to', or "
35 "'always_create'.");
36DEFINE_int32(blank_data_image_mb, 0,
37 "The size of the blank data image to generate, MB.");
38DEFINE_string(blank_data_image_fmt, "ext4",
39 "The fs format for the blank data image. Used with mkfs.");
40DEFINE_string(qemu_gdb, "",
Matthias Maennich454d7872019-02-06 16:35:17 +000041 "Debug flag to pass to qemu. e.g. -qemu_gdb=tcp::1234");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080042
43DEFINE_int32(x_res, 720, "Width of the screen in pixels");
44DEFINE_int32(y_res, 1280, "Height of the screen in pixels");
45DEFINE_int32(dpi, 160, "Pixels per inch for the screen");
46DEFINE_int32(refresh_rate_hz, 60, "Screen refresh rate in Hertz");
47DEFINE_int32(num_screen_buffers, 3, "The number of screen buffers");
48DEFINE_string(kernel_path, "",
49 "Path to the kernel. Overrides the one from the boot image");
Ram Muthiahaad97c52019-08-14 17:05:01 -070050DEFINE_string(initramfs_path, "", "Path to the initramfs");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080051DEFINE_bool(decompress_kernel, false,
Jorge E. Moreira36294042019-06-07 15:23:18 -070052 "Whether to decompress the kernel image.");
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -080053DEFINE_string(kernel_decompresser_executable,
54 vsoc::DefaultHostArtifactsPath("bin/extract-vmlinux"),
55 "Path to the extract-vmlinux executable.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080056DEFINE_string(extra_kernel_cmdline, "",
57 "Additional flags to put on the kernel command line");
58DEFINE_int32(loop_max_part, 7, "Maximum number of loop partitions");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080059DEFINE_string(androidboot_console, "ttyS1",
60 "Console device for the Android framework");
Jorge E. Moreiraba626622019-01-28 17:47:50 -080061DEFINE_string(
62 hardware_name, "",
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -080063 "The codename of the device's hardware, one of {cutf_ivsh, cutf_cvm}");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080064DEFINE_string(guest_security, "selinux",
65 "The security module to use in the guest");
Jorge E. Moreira300f97f2019-04-09 13:48:01 -070066DEFINE_bool(guest_enforce_security, true,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080067 "Whether to run in enforcing mode (non permissive). Ignored if "
68 "-guest_security is empty.");
69DEFINE_bool(guest_audit_security, true,
70 "Whether to log security audits.");
Yifan Hong19d713e2019-05-01 14:12:07 -070071DEFINE_string(boot_image, "",
72 "Location of cuttlefish boot image. If empty it is assumed to be "
73 "boot.img in the directory specified by -system_image_dir.");
Ram Muthiah1aa02f82019-10-22 20:26:28 +000074DEFINE_string(vendor_boot_image, "",
75 "Location of cuttlefish vendor boot image. If empty it is assumed to "
76 "be vendor_boot.img in the directory specified by -system_image_dir.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080077DEFINE_int32(memory_mb, 2048,
78 "Total amount of memory available for guest, MB.");
Cody Schuffelen7a184512019-10-30 16:52:55 -070079DEFINE_string(mempath, vsoc::GetDefaultMempath(),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080080 "Target location for the shmem file.");
Cody Schuffelen7a184512019-10-30 16:52:55 -070081DEFINE_string(mobile_interface, GetPerInstanceDefault("cvd-mbr-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080082 "Network interface to use for mobile networking");
Cody Schuffelen7a184512019-10-30 16:52:55 -070083DEFINE_string(mobile_tap_name, GetPerInstanceDefault("cvd-mtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080084 "The name of the tap interface to use for mobile");
Cody Schuffelen7a184512019-10-30 16:52:55 -070085DEFINE_string(serial_number, GetPerInstanceDefault("CUTTLEFISHCVD"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -080086 "Serial number to use for the device");
87DEFINE_string(instance_dir, "", // default handled on ParseCommandLine
88 "A directory to put all instance specific files");
89DEFINE_string(
Alistair Strachan5ab095c2019-05-23 20:41:20 +000090 vm_manager, vm_manager::CrosvmManager::name(),
Jorge E. Moreiraba626622019-01-28 17:47:50 -080091 "What virtual machine manager to use, one of {qemu_cli, crosvm}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070092DEFINE_string(
Greg Hartmanbc1bed42019-04-05 20:02:00 -070093 gpu_mode, vsoc::kGpuModeGuestSwiftshader,
94 "What gpu configuration to use, one of {guest_swiftshader, drm_virgl}");
Greg Hartmana3c552d2019-03-28 18:20:48 -070095DEFINE_string(wayland_socket, "",
Greg Hartmanbc1bed42019-04-05 20:02:00 -070096 "Location of the wayland socket to use for drm_virgl gpu_mode.");
Greg Hartmaneb04ac52019-07-16 16:44:18 -070097DEFINE_string(x_display, "",
98 "X display to use for drm_virgl gpu_mode.");
Greg Hartmana3c552d2019-03-28 18:20:48 -070099
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800100DEFINE_string(system_image_dir, vsoc::DefaultGuestImagePath(""),
101 "Location of the system partition images.");
Alistair Strachan050ca932018-11-27 12:41:19 -0800102DEFINE_string(super_image, "", "Location of the super partition image.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700103DEFINE_string(misc_image, "",
104 "Location of the misc partition image. If the image does not "
105 "exist, a blank new misc partition image is created.");
David Anderson64581ce2019-10-03 17:52:22 -0700106DEFINE_string(composite_disk, "", "Location of the composite disk image. "
107 "If empty, a composite disk is not used.");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800108
109DEFINE_bool(deprecated_boot_completed, false, "Log boot completed message to"
110 " host kernel. This is only used during transition of our clients."
111 " Will be deprecated soon.");
112DEFINE_bool(start_vnc_server, true, "Whether to start the vnc server process.");
113DEFINE_string(vnc_server_binary,
114 vsoc::DefaultHostArtifactsPath("bin/vnc_server"),
115 "Location of the vnc server binary.");
Alistair Strachan7eeddf42019-03-04 18:13:34 -0800116DEFINE_bool(start_stream_audio, false,
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800117 "Whether to start the stream audio process.");
118DEFINE_string(stream_audio_binary,
119 vsoc::DefaultHostArtifactsPath("bin/stream_audio"),
120 "Location of the stream_audio binary.");
121DEFINE_string(virtual_usb_manager_binary,
122 vsoc::DefaultHostArtifactsPath("bin/virtual_usb_manager"),
123 "Location of the virtual usb manager binary.");
124DEFINE_string(kernel_log_monitor_binary,
125 vsoc::DefaultHostArtifactsPath("bin/kernel_log_monitor"),
126 "Location of the log monitor binary.");
127DEFINE_string(ivserver_binary,
128 vsoc::DefaultHostArtifactsPath("bin/ivserver"),
129 "Location of the ivshmem server binary.");
130DEFINE_int32(vnc_server_port, GetPerInstanceDefault(6444),
131 "The port on which the vnc server should listen");
132DEFINE_int32(stream_audio_port, GetPerInstanceDefault(7444),
133 "The port on which stream_audio should listen.");
134DEFINE_string(socket_forward_proxy_binary,
135 vsoc::DefaultHostArtifactsPath("bin/socket_forward_proxy"),
136 "Location of the socket_forward_proxy binary.");
137DEFINE_string(socket_vsock_proxy_binary,
138 vsoc::DefaultHostArtifactsPath("bin/socket_vsock_proxy"),
139 "Location of the socket_vsock_proxy binary.");
Cody Schuffelen10743332019-04-15 16:50:49 -0700140DEFINE_string(adb_mode, "vsock_half_tunnel",
Cody Schuffelen63d10052019-02-26 12:21:53 -0800141 "Mode for ADB connection. Can be 'usb' for USB forwarding, "
142 "'tunnel' for a TCP connection tunneled through VSoC, "
143 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
144 "'native_vsock' for a direct connection to the guest ADB over "
145 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
146 "the guest ADB server, or a comma separated list of types as in "
147 "'usb,tunnel'");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800148DEFINE_bool(run_adb_connector, true,
149 "Maintain adb connection by sending 'adb connect' commands to the "
Matthias Maennich454d7872019-02-06 16:35:17 +0000150 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800151DEFINE_string(adb_connector_binary,
152 vsoc::DefaultHostArtifactsPath("bin/adb_connector"),
153 "Location of the adb_connector binary. Only relevant if "
Matthias Maennich454d7872019-02-06 16:35:17 +0000154 "-run_adb_connector is true");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800155DEFINE_int32(vhci_port, GetPerInstanceDefault(0), "VHCI port to use for usb");
Cody Schuffelen7a184512019-10-30 16:52:55 -0700156DEFINE_string(wifi_tap_name, GetPerInstanceDefault("cvd-wtap-"),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800157 "The name of the tap interface to use for wifi");
158DEFINE_int32(vsock_guest_cid,
159 vsoc::GetDefaultPerInstanceVsockCid(),
160 "Guest identifier for vsock. Disabled if under 3.");
161
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800162DEFINE_string(uuid, vsoc::GetPerInstanceDefault(vsoc::kDefaultUuidPrefix),
163 "UUID to use for the device. Random if not specified");
164DEFINE_bool(daemon, false,
165 "Run cuttlefish in background, the launcher exits on boot "
166 "completed/failed");
167
168DEFINE_string(device_title, "", "Human readable name for the instance, "
169 "used by the vnc_server for its server title");
170DEFINE_string(setupwizard_mode, "DISABLED",
171 "One of DISABLED,OPTIONAL,REQUIRED");
172
173DEFINE_string(qemu_binary,
174 "/usr/bin/qemu-system-x86_64",
175 "The qemu binary to use");
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800176DEFINE_string(crosvm_binary,
177 vsoc::DefaultHostArtifactsPath("bin/crosvm"),
178 "The Crosvm binary to use");
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700179DEFINE_string(console_forwarder_binary,
180 vsoc::DefaultHostArtifactsPath("bin/console_forwarder"),
181 "The Console Forwarder binary to use");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800182DEFINE_bool(restart_subprocesses, true, "Restart any crashed host process");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800183DEFINE_string(e2e_test_binary,
184 vsoc::DefaultHostArtifactsPath("bin/host_region_e2e_test"),
185 "Location of the region end to end test binary");
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800186DEFINE_string(logcat_receiver_binary,
187 vsoc::DefaultHostArtifactsPath("bin/logcat_receiver"),
188 "Binary for the logcat server");
189DEFINE_string(logcat_mode, "", "How to send android's log messages from "
190 "guest to host. One of [serial, vsock]");
191DEFINE_int32(logcat_vsock_port, vsoc::GetPerInstanceDefault(5620),
192 "The port for logcat over vsock");
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700193DEFINE_string(config_server_binary,
194 vsoc::DefaultHostArtifactsPath("bin/config_server"),
195 "Binary for the configuration server");
196DEFINE_int32(config_server_port, vsoc::GetPerInstanceDefault(4680),
197 "The (vsock) port for the configuration server");
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800198DEFINE_int32(frames_vsock_port, vsoc::GetPerInstanceDefault(5580),
199 "The vsock port to receive frames from the guest on");
Ram Muthiah6e9c98c2019-05-28 15:18:27 -0700200DEFINE_bool(enable_tombstone_receiver, true, "Enables the tombstone logger on "
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700201 "both the guest and the host");
202DEFINE_string(tombstone_receiver_binary,
203 vsoc::DefaultHostArtifactsPath("bin/tombstone_receiver"),
204 "Binary for the tombstone server");
205DEFINE_int32(tombstone_receiver_port, vsoc::GetPerInstanceDefault(5630),
206 "The vsock port for tombstones");
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800207DEFINE_int32(keyboard_server_port, GetPerInstanceDefault(5540),
208 "The port on which the vsock keyboard server should listen");
209DEFINE_int32(touch_server_port, GetPerInstanceDefault(5640),
210 "The port on which the vsock touch server should listen");
Cody Schuffelen1300f122019-05-28 18:24:34 -0700211DEFINE_bool(use_bootloader, false, "Boots the device using a bootloader");
212DEFINE_string(bootloader, "", "Bootloader binary path");
David Anderson637bfd62019-09-26 13:23:21 -0700213DEFINE_string(boot_slot, "", "Force booting into the given slot. If empty, "
214 "the slot will be chosen based on the misc partition if using a "
215 "bootloader. It will default to 'a' if empty and not using a "
216 "bootloader.");
Yifan Hong19d713e2019-05-01 14:12:07 -0700217
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800218namespace {
219
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700220std::string kRamdiskConcatExt = ".concat";
221
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800222template<typename S, typename T>
223static std::string concat(const S& s, const T& t) {
224 std::ostringstream os;
225 os << s << t;
226 return os.str();
227}
228
229bool ResolveInstanceFiles() {
230 if (FLAGS_system_image_dir.empty()) {
231 LOG(ERROR) << "--system_image_dir must be specified.";
232 return false;
233 }
234
235 // If user did not specify location of either of these files, expect them to
236 // be placed in --system_image_dir location.
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800237 std::string default_boot_image = FLAGS_system_image_dir + "/boot.img";
238 SetCommandLineOptionWithMode("boot_image", default_boot_image.c_str(),
239 google::FlagSettingMode::SET_FLAGS_DEFAULT);
240 std::string default_cache_image = FLAGS_system_image_dir + "/cache.img";
241 SetCommandLineOptionWithMode("cache_image", default_cache_image.c_str(),
242 google::FlagSettingMode::SET_FLAGS_DEFAULT);
243 std::string default_data_image = FLAGS_system_image_dir + "/userdata.img";
244 SetCommandLineOptionWithMode("data_image", default_data_image.c_str(),
245 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900246 std::string default_metadata_image = FLAGS_system_image_dir + "/metadata.img";
247 SetCommandLineOptionWithMode("metadata_image", default_metadata_image.c_str(),
248 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Alistair Strachan050ca932018-11-27 12:41:19 -0800249 std::string default_super_image = FLAGS_system_image_dir + "/super.img";
250 SetCommandLineOptionWithMode("super_image", default_super_image.c_str(),
251 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Yifan Hong19d713e2019-05-01 14:12:07 -0700252 std::string default_misc_image = FLAGS_system_image_dir + "/misc.img";
253 SetCommandLineOptionWithMode("misc_image", default_misc_image.c_str(),
254 google::FlagSettingMode::SET_FLAGS_DEFAULT);
David Anderson64581ce2019-10-03 17:52:22 -0700255 std::string default_composite_disk = FLAGS_system_image_dir + "/composite.img";
256 SetCommandLineOptionWithMode("composite_disk", default_composite_disk.c_str(),
257 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000258 std::string default_vendor_boot_image = FLAGS_system_image_dir
259 + "/vendor_boot.img";
260 SetCommandLineOptionWithMode("vendor_boot_image",
261 default_vendor_boot_image.c_str(),
262 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800263
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800264 return true;
265}
266
267std::string GetCuttlefishEnvPath() {
268 return cvd::StringFromEnv("HOME", ".") + "/.cuttlefish.sh";
269}
270
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700271int GetHostPort() {
272 constexpr int kFirstHostPort = 6520;
273 return vsoc::GetPerInstanceDefault(kFirstHostPort);
274}
275
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800276// Initializes the config object and saves it to file. It doesn't return it, all
277// further uses of the config should happen through the singleton
278bool InitializeCuttlefishConfiguration(
279 const cvd::BootImageUnpacker& boot_image_unpacker) {
280 vsoc::CuttlefishConfig tmp_config_obj;
281 auto& memory_layout = *vsoc::VSoCMemoryLayout::Get();
282 // Set this first so that calls to PerInstancePath below are correct
283 tmp_config_obj.set_instance_dir(FLAGS_instance_dir);
284 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
285 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
286 return false;
287 }
Greg Hartmana3c552d2019-03-28 18:20:48 -0700288 if (!vm_manager::VmManager::IsValidName(FLAGS_vm_manager)) {
289 LOG(ERROR) << "Invalid vm_manager: " << FLAGS_vm_manager;
290 return false;
291 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800292 tmp_config_obj.set_vm_manager(FLAGS_vm_manager);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700293 tmp_config_obj.set_gpu_mode(FLAGS_gpu_mode);
294 if (!vm_manager::VmManager::ConfigureGpuMode(&tmp_config_obj)) {
295 LOG(ERROR) << "Invalid gpu_mode=" << FLAGS_gpu_mode <<
296 " does not work with vm_manager=" << FLAGS_vm_manager;
297 return false;
298 }
299 tmp_config_obj.set_wayland_socket(FLAGS_wayland_socket);
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700300 tmp_config_obj.set_x_display(FLAGS_x_display);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800301
Alistair Strachan050ca932018-11-27 12:41:19 -0800302 vm_manager::VmManager::ConfigureBootDevices(&tmp_config_obj);
303
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800304 tmp_config_obj.set_serial_number(FLAGS_serial_number);
305
306 tmp_config_obj.set_cpus(FLAGS_cpus);
307 tmp_config_obj.set_memory_mb(FLAGS_memory_mb);
308
309 tmp_config_obj.set_dpi(FLAGS_dpi);
310 tmp_config_obj.set_setupwizard_mode(FLAGS_setupwizard_mode);
311 tmp_config_obj.set_x_res(FLAGS_x_res);
312 tmp_config_obj.set_y_res(FLAGS_y_res);
313 tmp_config_obj.set_num_screen_buffers(FLAGS_num_screen_buffers);
314 tmp_config_obj.set_refresh_rate_hz(FLAGS_refresh_rate_hz);
315 tmp_config_obj.set_gdb_flag(FLAGS_qemu_gdb);
Cody Schuffelen90b2fb22019-02-28 18:55:21 -0800316 std::vector<std::string> adb = cvd::StrSplit(FLAGS_adb_mode, ',');
317 tmp_config_obj.set_adb_mode(std::set<std::string>(adb.begin(), adb.end()));
Cody Schuffelene0d0c462019-09-09 14:13:01 -0700318 tmp_config_obj.set_host_port(GetHostPort());
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800319 tmp_config_obj.set_adb_ip_and_port("127.0.0.1:" + std::to_string(GetHostPort()));
320
321 tmp_config_obj.set_device_title(FLAGS_device_title);
322 if (FLAGS_kernel_path.size()) {
323 tmp_config_obj.set_kernel_image_path(FLAGS_kernel_path);
324 tmp_config_obj.set_use_unpacked_kernel(false);
325 } else {
326 tmp_config_obj.set_kernel_image_path(
327 tmp_config_obj.PerInstancePath("kernel"));
328 tmp_config_obj.set_use_unpacked_kernel(true);
329 }
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800330 tmp_config_obj.set_decompress_kernel(FLAGS_decompress_kernel);
331 if (FLAGS_decompress_kernel) {
332 tmp_config_obj.set_decompressed_kernel_image_path(
333 tmp_config_obj.PerInstancePath("vmlinux"));
334 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800335
336 auto ramdisk_path = tmp_config_obj.PerInstancePath("ramdisk.img");
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000337 auto vendor_ramdisk_path = tmp_config_obj.PerInstancePath("vendor_ramdisk.img");
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800338 bool use_ramdisk = boot_image_unpacker.HasRamdiskImage();
339 if (!use_ramdisk) {
340 LOG(INFO) << "No ramdisk present; assuming system-as-root build";
341 ramdisk_path = "";
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000342 vendor_ramdisk_path = "";
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800343 }
344
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800345 tmp_config_obj.add_kernel_cmdline(boot_image_unpacker.kernel_cmdline());
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700346
347 if (use_ramdisk) {
348 if (FLAGS_composite_disk.empty()) {
349 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab");
350 } else {
Alistair Delva73ad5ec2019-08-30 15:37:19 -0700351 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab.composite");
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700352 }
353 } else {
354 if (FLAGS_composite_disk.empty()) {
355 tmp_config_obj.add_kernel_cmdline("root=/dev/vda");
356 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab");
357 } else {
358 tmp_config_obj.add_kernel_cmdline("root=/dev/vda1");
Alistair Delva73ad5ec2019-08-30 15:37:19 -0700359 tmp_config_obj.add_kernel_cmdline("androidboot.fstab_name=fstab.composite");
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700360 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800361 }
Cody Schuffelen88b82d52019-05-29 14:16:03 -0700362
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800363 tmp_config_obj.add_kernel_cmdline("init=/init");
364 tmp_config_obj.add_kernel_cmdline(
365 concat("androidboot.serialno=", FLAGS_serial_number));
366 tmp_config_obj.add_kernel_cmdline("mac80211_hwsim.radios=0");
367 tmp_config_obj.add_kernel_cmdline(concat("androidboot.lcd_density=", FLAGS_dpi));
368 tmp_config_obj.add_kernel_cmdline(
369 concat("androidboot.setupwizard_mode=", FLAGS_setupwizard_mode));
370 tmp_config_obj.add_kernel_cmdline(concat("loop.max_part=", FLAGS_loop_max_part));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800371 if (!FLAGS_androidboot_console.empty()) {
372 tmp_config_obj.add_kernel_cmdline(
373 concat("androidboot.console=", FLAGS_androidboot_console));
374 }
375 if (!FLAGS_hardware_name.empty()) {
376 tmp_config_obj.add_kernel_cmdline(
377 concat("androidboot.hardware=", FLAGS_hardware_name));
378 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800379 if (FLAGS_logcat_mode == cvd::kLogcatVsockMode) {
380 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_logcat_port=",
381 FLAGS_logcat_vsock_port));
382 }
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700383 tmp_config_obj.add_kernel_cmdline(concat("androidboot.cuttlefish_config_server_port=",
384 FLAGS_config_server_port));
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800385 tmp_config_obj.set_hardware_name(FLAGS_hardware_name);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800386 if (!FLAGS_guest_security.empty()) {
387 tmp_config_obj.add_kernel_cmdline(concat("security=", FLAGS_guest_security));
388 if (FLAGS_guest_enforce_security) {
389 tmp_config_obj.add_kernel_cmdline("enforcing=1");
390 } else {
391 tmp_config_obj.add_kernel_cmdline("enforcing=0");
392 tmp_config_obj.add_kernel_cmdline("androidboot.selinux=permissive");
393 }
394 if (FLAGS_guest_audit_security) {
395 tmp_config_obj.add_kernel_cmdline("audit=1");
396 } else {
397 tmp_config_obj.add_kernel_cmdline("audit=0");
398 }
399 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800400 if (FLAGS_extra_kernel_cmdline.size()) {
401 tmp_config_obj.add_kernel_cmdline(FLAGS_extra_kernel_cmdline);
402 }
403
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700404 if (!FLAGS_composite_disk.empty()) {
405 tmp_config_obj.set_virtual_disk_paths({FLAGS_composite_disk});
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700406 } else {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700407 tmp_config_obj.set_virtual_disk_paths({
408 FLAGS_super_image,
409 FLAGS_data_image,
410 FLAGS_cache_image,
411 FLAGS_metadata_image,
412 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700413 }
414
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800415 tmp_config_obj.set_ramdisk_image_path(ramdisk_path);
Ram Muthiahd512c132019-10-23 17:20:45 -0700416 tmp_config_obj.set_vendor_ramdisk_image_path(vendor_ramdisk_path);
417
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000418 // Boot as recovery is set so normal boot needs to be forced every boot
419 tmp_config_obj.add_kernel_cmdline("androidboot.force_normal_boot=1");
Ram Muthiahd512c132019-10-23 17:20:45 -0700420
421 if (FLAGS_kernel_path.size() && !FLAGS_initramfs_path.size()) {
422 // If there's a kernel that's passed in without an initramfs, that implies
423 // user error or a kernel built with no modules. In either case, let's
424 // choose to avoid loading the modules from the vendor ramdisk which are
425 // built for the default cf kernel. Once boot occurs, user error will
426 // become obvious.
427 tmp_config_obj.set_final_ramdisk_path(ramdisk_path);
428 } else {
429 tmp_config_obj.set_final_ramdisk_path(ramdisk_path + kRamdiskConcatExt);
430 if(FLAGS_initramfs_path.size()) {
431 tmp_config_obj.set_initramfs_path(FLAGS_initramfs_path);
432 }
Cody Schuffelen9fd95472019-10-04 13:49:57 -0700433 }
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800434
435 tmp_config_obj.set_mempath(FLAGS_mempath);
436 tmp_config_obj.set_ivshmem_qemu_socket_path(
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700437 tmp_config_obj.PerInstanceInternalPath("ivshmem_socket_qemu"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800438 tmp_config_obj.set_ivshmem_client_socket_path(
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700439 tmp_config_obj.PerInstanceInternalPath("ivshmem_socket_client"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800440 tmp_config_obj.set_ivshmem_vector_count(memory_layout.GetRegions().size());
441
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700442 if (tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) > 0) {
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700443 tmp_config_obj.set_usb_v1_socket_name(
444 tmp_config_obj.PerInstanceInternalPath("usb-v1"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800445 tmp_config_obj.set_vhci_port(FLAGS_vhci_port);
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700446 tmp_config_obj.set_usb_ip_socket_name(
447 tmp_config_obj.PerInstanceInternalPath("usb-ip"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800448 }
449
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700450 tmp_config_obj.set_kernel_log_pipe_name(
451 tmp_config_obj.PerInstanceInternalPath("kernel-log-pipe"));
452 tmp_config_obj.set_console_pipe_name(
453 tmp_config_obj.PerInstanceInternalPath("console-pipe"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800454 tmp_config_obj.set_deprecated_boot_completed(FLAGS_deprecated_boot_completed);
455 tmp_config_obj.set_console_path(tmp_config_obj.PerInstancePath("console"));
456 tmp_config_obj.set_logcat_path(tmp_config_obj.PerInstancePath("logcat"));
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800457 tmp_config_obj.set_logcat_receiver_binary(FLAGS_logcat_receiver_binary);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700458 tmp_config_obj.set_config_server_binary(FLAGS_config_server_binary);
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700459 tmp_config_obj.set_launcher_log_path(
460 tmp_config_obj.PerInstancePath("launcher.log"));
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800461 tmp_config_obj.set_launcher_monitor_socket_path(
462 tmp_config_obj.PerInstancePath("launcher_monitor.sock"));
463
464 tmp_config_obj.set_mobile_bridge_name(FLAGS_mobile_interface);
465 tmp_config_obj.set_mobile_tap_name(FLAGS_mobile_tap_name);
466
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800467 tmp_config_obj.set_wifi_tap_name(FLAGS_wifi_tap_name);
468
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800469 tmp_config_obj.set_vsock_guest_cid(FLAGS_vsock_guest_cid);
470
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800471 tmp_config_obj.set_uuid(FLAGS_uuid);
472
473 tmp_config_obj.set_qemu_binary(FLAGS_qemu_binary);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800474 tmp_config_obj.set_crosvm_binary(FLAGS_crosvm_binary);
Jorge E. Moreira13cbd982019-06-06 16:06:34 -0700475 tmp_config_obj.set_console_forwarder_binary(FLAGS_console_forwarder_binary);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800476 tmp_config_obj.set_ivserver_binary(FLAGS_ivserver_binary);
477 tmp_config_obj.set_kernel_log_monitor_binary(FLAGS_kernel_log_monitor_binary);
478
479 tmp_config_obj.set_enable_vnc_server(FLAGS_start_vnc_server);
480 tmp_config_obj.set_vnc_server_binary(FLAGS_vnc_server_binary);
481 tmp_config_obj.set_vnc_server_port(FLAGS_vnc_server_port);
482
483 tmp_config_obj.set_enable_stream_audio(FLAGS_start_stream_audio);
484 tmp_config_obj.set_stream_audio_binary(FLAGS_stream_audio_binary);
485 tmp_config_obj.set_stream_audio_port(FLAGS_stream_audio_port);
486
487 tmp_config_obj.set_restart_subprocesses(FLAGS_restart_subprocesses);
488 tmp_config_obj.set_run_adb_connector(FLAGS_run_adb_connector);
489 tmp_config_obj.set_adb_connector_binary(FLAGS_adb_connector_binary);
490 tmp_config_obj.set_virtual_usb_manager_binary(
491 FLAGS_virtual_usb_manager_binary);
492 tmp_config_obj.set_socket_forward_proxy_binary(
493 FLAGS_socket_forward_proxy_binary);
494 tmp_config_obj.set_socket_vsock_proxy_binary(FLAGS_socket_vsock_proxy_binary);
495 tmp_config_obj.set_run_as_daemon(FLAGS_daemon);
Cody Schuffelen5b809d42019-10-31 14:05:09 -0700496 tmp_config_obj.set_run_e2e_test(false);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800497 tmp_config_obj.set_e2e_test_binary(FLAGS_e2e_test_binary);
498
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800499 tmp_config_obj.set_data_policy(FLAGS_data_policy);
500 tmp_config_obj.set_blank_data_image_mb(FLAGS_blank_data_image_mb);
501 tmp_config_obj.set_blank_data_image_fmt(FLAGS_blank_data_image_fmt);
502
Cody Schuffelen75ef8872019-09-06 17:35:54 -0700503 if(tmp_config_obj.adb_mode().count(vsoc::AdbMode::Usb) == 0) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800504 tmp_config_obj.disable_usb_adb();
505 }
506
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800507 tmp_config_obj.set_logcat_mode(FLAGS_logcat_mode);
508 tmp_config_obj.set_logcat_vsock_port(FLAGS_logcat_vsock_port);
Jorge E. Moreiraf1f7cb32019-04-15 18:44:41 -0700509 tmp_config_obj.set_config_server_port(FLAGS_config_server_port);
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800510 tmp_config_obj.set_frames_vsock_port(FLAGS_frames_vsock_port);
Jorge E. Moreira1859ccc2019-05-14 15:15:27 -0700511 if (!tmp_config_obj.enable_ivserver() && tmp_config_obj.enable_vnc_server()) {
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -0800512 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_frames_port=",
513 FLAGS_frames_vsock_port));
514 }
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800515
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700516 tmp_config_obj.set_enable_tombstone_receiver(FLAGS_enable_tombstone_receiver);
517 tmp_config_obj.set_tombstone_receiver_port(FLAGS_tombstone_receiver_port);
518 tmp_config_obj.set_tombstone_receiver_binary(FLAGS_tombstone_receiver_binary);
519 if (FLAGS_enable_tombstone_receiver) {
520 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=1");
521 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_tombstone_port="
522 ,FLAGS_tombstone_receiver_port));
Alistair Strachan050ca932018-11-27 12:41:19 -0800523 // TODO (b/128842613) populate a cid flag to read the host CID during
Ram Muthiah792e2ad2019-04-19 11:19:46 -0700524 // runtime
525 } else {
526 tmp_config_obj.add_kernel_cmdline("androidboot.tombstone_transmit=0");
527 }
528
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800529 tmp_config_obj.set_touch_socket_port(FLAGS_touch_server_port);
530 tmp_config_obj.set_keyboard_socket_port(FLAGS_keyboard_server_port);
531 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
532 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_touch_port=",
533 FLAGS_touch_server_port));
534 tmp_config_obj.add_kernel_cmdline(concat("androidboot.vsock_keyboard_port=",
535 FLAGS_keyboard_server_port));
536 }
537
Cody Schuffelen1300f122019-05-28 18:24:34 -0700538 tmp_config_obj.set_use_bootloader(FLAGS_use_bootloader);
539 tmp_config_obj.set_bootloader(FLAGS_bootloader);
540
David Anderson637bfd62019-09-26 13:23:21 -0700541 if (!FLAGS_boot_slot.empty()) {
542 tmp_config_obj.set_boot_slot(FLAGS_boot_slot);
543 }
544
545 if (!FLAGS_use_bootloader) {
546 std::string slot_suffix;
547 if (FLAGS_boot_slot.empty()) {
548 slot_suffix = "_a";
549 } else {
550 slot_suffix = "_" + FLAGS_boot_slot;
551 }
552 tmp_config_obj.add_kernel_cmdline("androidboot.slot_suffix=" + slot_suffix);
553 }
554
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800555 tmp_config_obj.set_cuttlefish_env_path(GetCuttlefishEnvPath());
556
557 auto config_file = GetConfigFilePath(tmp_config_obj);
558 auto config_link = vsoc::GetGlobalConfigFileLink();
559 // Save the config object before starting any host process
560 if (!tmp_config_obj.SaveToFile(config_file)) {
561 LOG(ERROR) << "Unable to save config object";
562 return false;
563 }
564 setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
565 if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
566 LOG(ERROR) << "Failed to create symlink to config file at " << config_link
567 << ": " << strerror(errno);
568 return false;
569 }
570
571 return true;
572}
573
574void SetDefaultFlagsForQemu() {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800575 auto default_instance_dir =
576 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
577 SetCommandLineOptionWithMode("instance_dir",
578 default_instance_dir.c_str(),
579 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen5b809d42019-10-31 14:05:09 -0700580 // TODO(b/144111429): Consolidate to one hardware name
581 SetCommandLineOptionWithMode("hardware_name", "cutf_cvm",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800582 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen038d4d92019-11-04 15:09:03 -0800583 // TODO(b/144119457) Use the serial port.
584 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800585 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800586}
587
588void SetDefaultFlagsForCrosvm() {
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800589 auto default_instance_dir =
590 cvd::StringFromEnv("HOME", ".") + "/cuttlefish_runtime";
591 SetCommandLineOptionWithMode("instance_dir",
592 default_instance_dir.c_str(),
593 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Greg Hartmana3c552d2019-03-28 18:20:48 -0700594 SetCommandLineOptionWithMode("wayland_socket",
Greg Hartmaneb04ac52019-07-16 16:44:18 -0700595 "",
596 google::FlagSettingMode::SET_FLAGS_DEFAULT);
597 SetCommandLineOptionWithMode("x_display",
598 getenv("DISPLAY"),
Greg Hartmana3c552d2019-03-28 18:20:48 -0700599 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen5b809d42019-10-31 14:05:09 -0700600 // TODO(b/144111429): Consolidate to one hardware name
Jorge E. Moreirafd1a6b02019-02-27 16:25:00 -0800601 SetCommandLineOptionWithMode("hardware_name", "cutf_cvm",
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800602 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Jorge E. Moreirafd10cae2019-02-19 15:35:42 -0800603 SetCommandLineOptionWithMode("logcat_mode", cvd::kLogcatVsockMode,
604 google::FlagSettingMode::SET_FLAGS_DEFAULT);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800605}
606
607bool ParseCommandLineFlags(int* argc, char*** argv) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800608 google::ParseCommandLineNonHelpFlags(argc, argv, true);
609 bool invalid_manager = false;
610 if (FLAGS_vm_manager == vm_manager::QemuManager::name()) {
611 SetDefaultFlagsForQemu();
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800612 } else if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
613 SetDefaultFlagsForCrosvm();
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800614 } else {
615 std::cerr << "Unknown Virtual Machine Manager: " << FLAGS_vm_manager
616 << std::endl;
617 invalid_manager = true;
618 }
619 google::HandleCommandLineHelpFlags();
620 if (invalid_manager) {
621 return false;
622 }
623 // Set the env variable to empty (in case the caller passed a value for it).
624 unsetenv(vsoc::kCuttlefishConfigEnvVarName);
625
626 return ResolveInstanceFiles();
627}
628
629bool CleanPriorFiles() {
630 // Everything on the instance directory
631 std::string prior_files = FLAGS_instance_dir + "/*";
632 // The shared memory file
633 prior_files += " " + FLAGS_mempath;
634 // The environment file
635 prior_files += " " + GetCuttlefishEnvPath();
636 // The global link to the config file
637 prior_files += " " + vsoc::GetGlobalConfigFileLink();
638 LOG(INFO) << "Assuming prior files of " << prior_files;
639 std::string fuser_cmd = "fuser " + prior_files + " 2> /dev/null";
640 int rval = std::system(fuser_cmd.c_str());
641 // fuser returns 0 if any of the files are open
642 if (WEXITSTATUS(rval) == 0) {
643 LOG(ERROR) << "Clean aborted: files are in use";
644 return false;
645 }
646 std::string clean_command = "rm -rf " + prior_files;
647 rval = std::system(clean_command.c_str());
648 if (WEXITSTATUS(rval) != 0) {
649 LOG(ERROR) << "Remove of files failed";
650 return false;
651 }
652 return true;
653}
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800654
655bool DecompressKernel(const std::string& src, const std::string& dst) {
656 cvd::Command decomp_cmd(FLAGS_kernel_decompresser_executable);
657 decomp_cmd.AddParameter(src);
658 auto output_file = cvd::SharedFD::Creat(dst.c_str(), 0666);
659 if (!output_file->IsOpen()) {
660 LOG(ERROR) << "Unable to create decompressed image file: "
661 << output_file->StrError();
662 return false;
663 }
664 decomp_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut, output_file);
665 auto decomp_proc = decomp_cmd.Start(false);
666 return decomp_proc.Started() && decomp_proc.Wait() == 0;
667}
Cody Schuffelen78824ed2019-09-06 17:43:15 -0700668
669void ValidateAdbModeFlag(const vsoc::CuttlefishConfig& config) {
670 auto adb_modes = config.adb_mode();
671 adb_modes.erase(vsoc::AdbMode::Unknown);
672 if (adb_modes.size() < 1) {
673 LOG(INFO) << "ADB not enabled";
674 }
675}
676
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800677} // namespace
678
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700679namespace {
680
681std::vector<ImagePartition> disk_config() {
682 std::vector<ImagePartition> partitions;
Cody Schuffelen4f7e4412019-08-30 16:10:59 -0700683 partitions.push_back(ImagePartition {
684 .label = "super",
685 .image_file_path = FLAGS_super_image,
686 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700687 partitions.push_back(ImagePartition {
688 .label = "userdata",
689 .image_file_path = FLAGS_data_image,
690 });
691 partitions.push_back(ImagePartition {
692 .label = "cache",
693 .image_file_path = FLAGS_cache_image,
694 });
695 partitions.push_back(ImagePartition {
696 .label = "metadata",
697 .image_file_path = FLAGS_metadata_image,
698 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700699 partitions.push_back(ImagePartition {
700 .label = "boot",
701 .image_file_path = FLAGS_boot_image,
702 });
Yifan Hong19d713e2019-05-01 14:12:07 -0700703 partitions.push_back(ImagePartition {
704 .label = "misc",
705 .image_file_path = FLAGS_misc_image
706 });
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700707 return partitions;
708}
709
710bool ShouldCreateCompositeDisk() {
711 if (FLAGS_composite_disk.empty()) {
712 return false;
713 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700714 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
715 // The crosvm implementation is very fast to rebuild but also more brittle due to being split
716 // into multiple files. The QEMU implementation is slow to build, but completely self-contained
717 // at that point. Therefore, always rebuild on crosvm but check if it is necessary for QEMU.
718 return true;
719 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700720 auto composite_age = cvd::FileModificationTime(FLAGS_composite_disk);
721 for (auto& partition : disk_config()) {
722 auto partition_age = cvd::FileModificationTime(partition.image_file_path);
723 if (partition_age >= composite_age) {
724 LOG(INFO) << "composite disk age was \"" << std::chrono::system_clock::to_time_t(composite_age) << "\", "
725 << "partition age was \"" << std::chrono::system_clock::to_time_t(partition_age) << "\"";
726 return true;
727 }
728 }
729 return false;
730}
731
Ram Muthiah3db0a562019-08-26 13:35:19 -0700732bool ConcatRamdisks(const std::string& new_ramdisk_path, const std::string& ramdisk_a_path,
733 const std::string& ramdisk_b_path) {
734 // clear out file of any pre-existing content
735 std::ofstream new_ramdisk(new_ramdisk_path, std::ios_base::binary | std::ios_base::trunc);
736 std::ifstream ramdisk_a(ramdisk_a_path, std::ios_base::binary);
737 std::ifstream ramdisk_b(ramdisk_b_path, std::ios_base::binary);
Ram Muthiahaad97c52019-08-14 17:05:01 -0700738
Ram Muthiah3db0a562019-08-26 13:35:19 -0700739 if(!new_ramdisk.is_open() || !ramdisk_a.is_open() || !ramdisk_b.is_open()) {
Ram Muthiahaad97c52019-08-14 17:05:01 -0700740 return false;
741 }
742
Ram Muthiah3db0a562019-08-26 13:35:19 -0700743 new_ramdisk << ramdisk_a.rdbuf() << ramdisk_b.rdbuf();
Ram Muthiahaad97c52019-08-14 17:05:01 -0700744 return true;
745}
746
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700747void CreateCompositeDisk(const vsoc::CuttlefishConfig& config) {
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700748 if (FLAGS_composite_disk.empty()) {
749 LOG(FATAL) << "asked to create composite disk, but path was empty";
750 }
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700751 if (FLAGS_vm_manager == vm_manager::CrosvmManager::name()) {
752 std::string header_path = config.PerInstancePath("gpt_header.img");
753 std::string footer_path = config.PerInstancePath("gpt_footer.img");
754 create_composite_disk(disk_config(), header_path, footer_path, FLAGS_composite_disk);
755 } else {
756 aggregate_image(disk_config(), FLAGS_composite_disk);
757 }
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700758}
759
760} // namespace
761
Cody Schuffelen605e6852019-10-16 16:22:24 -0700762const vsoc::CuttlefishConfig* InitFilesystemAndCreateConfig(
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700763 int* argc, char*** argv, cvd::FetcherConfig fetcher_config) {
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800764 if (!ParseCommandLineFlags(argc, argv)) {
765 LOG(ERROR) << "Failed to parse command arguments";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700766 exit(AssemblerExitCodes::kArgumentParsingError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800767 }
768
769 // Clean up prior files before saving the config file (doing it after would
770 // delete it)
771 if (!CleanPriorFiles()) {
772 LOG(ERROR) << "Failed to clean prior files";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700773 exit(AssemblerExitCodes::kPrioFilesCleanupError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800774 }
775 // Create instance directory if it doesn't exist.
776 if (!cvd::DirectoryExists(FLAGS_instance_dir.c_str())) {
777 LOG(INFO) << "Setting up " << FLAGS_instance_dir;
778 if (mkdir(FLAGS_instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
779 LOG(ERROR) << "Failed to create instance directory: "
780 << FLAGS_instance_dir << ". Error: " << errno;
Cody Schuffelene71fa352019-09-10 16:11:58 -0700781 exit(AssemblerExitCodes::kInstanceDirCreationError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800782 }
783 }
784
Jorge E. Moreira1e8e2f12019-10-07 18:09:49 -0700785 auto internal_dir = FLAGS_instance_dir + "/" + vsoc::kInternalDirName;
786 if (!cvd::DirectoryExists(internal_dir)) {
787 if (mkdir(internal_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) <
788 0) {
789 LOG(ERROR) << "Failed to create internal instance directory: "
790 << internal_dir << ". Error: " << errno;
791 exit(AssemblerExitCodes::kInstanceDirCreationError);
792 }
793 }
794
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800795 if (!cvd::FileHasContent(FLAGS_boot_image)) {
796 LOG(ERROR) << "File not found: " << FLAGS_boot_image;
797 exit(cvd::kCuttlefishConfigurationInitError);
798 }
799
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000800 if (!cvd::FileHasContent(FLAGS_vendor_boot_image)) {
801 LOG(ERROR) << "File not found: " << FLAGS_vendor_boot_image;
802 exit(cvd::kCuttlefishConfigurationInitError);
803 }
804
805 auto boot_img_unpacker =
806 cvd::BootImageUnpacker::FromImages(FLAGS_boot_image,
807 FLAGS_vendor_boot_image);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800808
809 if (!InitializeCuttlefishConfiguration(*boot_img_unpacker)) {
810 LOG(ERROR) << "Failed to initialize configuration";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700811 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800812 }
813 // Do this early so that the config object is ready for anything that needs it
814 auto config = vsoc::CuttlefishConfig::Get();
815 if (!config) {
816 LOG(ERROR) << "Failed to obtain config singleton";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700817 exit(AssemblerExitCodes::kCuttlefishConfigurationInitError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800818 }
819
820 if (!boot_img_unpacker->Unpack(config->ramdisk_image_path(),
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000821 config->vendor_ramdisk_image_path(),
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800822 config->use_unpacked_kernel()
823 ? config->kernel_image_path()
824 : "")) {
825 LOG(ERROR) << "Failed to unpack boot image";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700826 exit(AssemblerExitCodes::kBootImageUnpackError);
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800827 }
828
Ram Muthiah1aa02f82019-10-22 20:26:28 +0000829 // TODO(134522463) as part of the bootloader refactor, repack the vendor boot
830 // image and use the bootloader to load both the boot and vendor ramdisk.
831 // Until then, this hack to get gki modules into cuttlefish will suffice.
832
833 // If a vendor ramdisk comes in via this mechanism, let it supercede the one
834 // in the vendor boot image. This flag is what kernel presubmit testing uses
835 // to pass in the kernel ramdisk.
Ram Muthiahd512c132019-10-23 17:20:45 -0700836
837 // If no kernel is passed in or an initramfs is made available, the default
838 // vendor boot ramdisk or the initramfs provided should be appended to the
839 // boot ramdisk. If a kernel IS provided with no initramfs, it is safe to
840 // safe to assume that the kernel was built with no modules and expects no
841 // modules for cf to run properly.
842 if(!FLAGS_kernel_path.size() || FLAGS_initramfs_path.size()) {
843 const std::string& vendor_ramdisk_path =
844 config->initramfs_path().size() ? config->initramfs_path()
845 : config->vendor_ramdisk_image_path();
846 if(!ConcatRamdisks(config->final_ramdisk_path(),
847 config->ramdisk_image_path(), vendor_ramdisk_path)) {
848 LOG(ERROR) << "Failed to concatenate ramdisk and vendor ramdisk";
849 exit(AssemblerExitCodes::kInitRamFsConcatError);
850 }
Ram Muthiahaad97c52019-08-14 17:05:01 -0700851 }
852
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800853 if (config->decompress_kernel()) {
854 if (!DecompressKernel(config->kernel_image_path(),
855 config->decompressed_kernel_image_path())) {
856 LOG(ERROR) << "Failed to decompress kernel";
Cody Schuffelene71fa352019-09-10 16:11:58 -0700857 exit(AssemblerExitCodes::kKernelDecompressError);
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800858 }
859 }
860
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800861 ValidateAdbModeFlag(*config);
862
Yifan Hong19d713e2019-05-01 14:12:07 -0700863 // Create misc if necessary
864 if (!InitializeMiscImage(FLAGS_misc_image)) {
865 exit(cvd::kCuttlefishConfigurationInitError);
866 }
867
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800868 // Create data if necessary
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700869 if (!ApplyDataImagePolicy(*config, FLAGS_data_image)) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800870 exit(cvd::kCuttlefishConfigurationInitError);
871 }
872
Cody Schuffelen69c19592019-05-31 16:00:30 -0700873 if (!cvd::FileExists(FLAGS_metadata_image)) {
874 CreateBlankImage(FLAGS_metadata_image, FLAGS_blank_metadata_image_mb, "none");
875 }
Paul Trautrimba8f8e92019-03-12 17:55:48 +0900876
Cody Schuffelenb737ef52019-09-25 14:54:38 -0700877 if (SuperImageNeedsRebuilding(fetcher_config, *config)) {
878 if (!RebuildSuperImage(fetcher_config, *config, FLAGS_super_image)) {
879 LOG(ERROR) << "Super image rebuilding requested but could not be completed.";
880 exit(cvd::kCuttlefishConfigurationInitError);
881 }
882 }
883
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700884 if (ShouldCreateCompositeDisk()) {
Cody Schuffelenc3fd6e52019-08-30 15:14:07 -0700885 CreateCompositeDisk(*config);
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700886 }
887
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800888 // Check that the files exist
Cody Schuffelen3c99f5b2019-06-14 17:26:01 -0700889 for (const auto& file : config->virtual_disk_paths()) {
Alistair Strachan050ca932018-11-27 12:41:19 -0800890 if (!file.empty() && !cvd::FileHasContent(file.c_str())) {
Cody Schuffelen2b51bab2019-01-29 18:14:48 -0800891 LOG(ERROR) << "File not found: " << file;
892 exit(cvd::kCuttlefishConfigurationInitError);
893 }
894 }
895
Cody Schuffelen20ecaca2019-01-29 17:43:38 -0800896 return config;
897}
898
899std::string GetConfigFilePath(const vsoc::CuttlefishConfig& config) {
900 return config.PerInstancePath("cuttlefish_config.json");
901}