blob: 2241b0e02e328d3c048920c2b7e507e5bd7a2b71 [file] [log] [blame]
Jorge E. Moreiraba626622019-01-28 17:47:50 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "host/libs/vm_manager/crosvm_manager.h"
18
19#include <string>
20#include <vector>
21
22#include <glog/logging.h>
23
Jorge E. Moreira46429a92019-02-12 17:21:32 -080024#include "common/libs/utils/network.h"
Jorge E. Moreiraba626622019-01-28 17:47:50 -080025#include "common/libs/utils/subprocess.h"
26#include "host/libs/config/cuttlefish_config.h"
27#include "host/libs/vm_manager/qemu_manager.h"
28
29namespace vm_manager {
30
31namespace {
32
33std::string GetControlSocketPath(const vsoc::CuttlefishConfig* config) {
34 return config->PerInstancePath("crosvm_control.sock");
35}
36
37cvd::SharedFD ConnectToLogMonitor(const std::string& log_monitor_name) {
38 return cvd::SharedFD::SocketLocalClient(log_monitor_name.c_str(), false,
39 SOCK_STREAM);
40}
41
Jorge E. Moreira46429a92019-02-12 17:21:32 -080042void AddTapFdParameter(cvd::Command* crosvm_cmd, const std::string& tap_name) {
43 auto tap_fd = cvd::OpenTapInterface(tap_name);
44 if (tap_fd->IsOpen()) {
45 crosvm_cmd->AddParameter("--tap-fd=", tap_fd);
46 } else {
47 LOG(ERROR) << "Unable to connect to " << tap_name << ": "
48 << tap_fd->StrError();
49 }
50}
51
Jorge E. Moreiraba626622019-01-28 17:47:50 -080052} // namespace
53
54const std::string CrosvmManager::name() { return "crosvm"; }
55
56CrosvmManager::CrosvmManager(const vsoc::CuttlefishConfig* config)
57 : VmManager(config) {}
58
59cvd::Command CrosvmManager::StartCommand() {
Jorge E. Moreiraba626622019-01-28 17:47:50 -080060 // TODO Add aarch64 support
61 // TODO Add the tap interfaces (--tap-fd)
62 // TODO Redirect logcat output
63
64 // Run crosvm directly instead of through a cf_crosvm.sh script. The kernel
65 // logs are on crosvm's standard output, so we need to redirect it to the log
66 // monitor socket, a helper script will print more than just the logs to
67 // standard output.
68 cvd::Command command(config_->crosvm_binary());
69 command.AddParameter("run");
70
Alistair Strachan73427de2019-04-17 09:49:01 -070071 if (!config_->ramdisk_image_path().empty()) {
72 command.AddParameter("--initrd=", config_->ramdisk_image_path());
73 }
Alistair Strachan252d98b2019-03-04 17:58:06 -080074 command.AddParameter("--null-audio");
Jorge E. Moreiraba626622019-01-28 17:47:50 -080075 command.AddParameter("--mem=", config_->memory_mb());
76 command.AddParameter("--cpus=", config_->cpus());
77 command.AddParameter("--params=", config_->kernel_cmdline_as_string());
Jorge E. Moreira3fad4502019-02-28 14:16:38 -080078 command.AddParameter("--rwdisk=", config_->system_image_path());
Jorge E. Moreiraba626622019-01-28 17:47:50 -080079 command.AddParameter("--rwdisk=", config_->data_image_path());
80 command.AddParameter("--rwdisk=", config_->cache_image_path());
Paul Trautrimba8f8e92019-03-12 17:55:48 +090081 command.AddParameter("--rwdisk=", config_->metadata_image_path());
Alistair Strachan17c19e82019-04-17 09:37:09 -070082 command.AddParameter("--rwdisk=", config_->vendor_image_path());
Alistair Strachan5782ce42019-03-28 22:05:13 -070083 command.AddParameter("--rwdisk=", config_->product_image_path());
Jorge E. Moreiraba626622019-01-28 17:47:50 -080084 command.AddParameter("--socket=", GetControlSocketPath(config_));
85 command.AddParameter("--android-fstab=", config_->gsi_fstab_path());
Jorge E. Moreirac87c2c72019-03-06 16:12:23 -080086 command.AddParameter("--single-touch=", config_->touch_socket_path(), ":",
87 config_->x_res(), ":", config_->y_res());
88 command.AddParameter("--keyboard=", config_->keyboard_socket_path());
Jorge E. Moreiraba626622019-01-28 17:47:50 -080089
Jorge E. Moreira46429a92019-02-12 17:21:32 -080090 AddTapFdParameter(&command, config_->wifi_tap_name());
91 AddTapFdParameter(&command, config_->mobile_tap_name());
92
Jorge E. Moreiraba626622019-01-28 17:47:50 -080093 // TODO remove this (use crosvm's seccomp files)
94 command.AddParameter("--disable-sandbox");
95
96 if (config_->vsock_guest_cid() >= 2) {
97 command.AddParameter("--cid=", config_->vsock_guest_cid());
98 }
99
100 auto kernel_log_connection =
101 ConnectToLogMonitor(config_->kernel_log_socket_name());
102 if (!kernel_log_connection->IsOpen()) {
103 LOG(WARNING) << "Unable to connect to log monitor: "
104 << kernel_log_connection->StrError();
105 } else {
106 command.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut,
107 kernel_log_connection);
108 }
109
Jorge E. Moreira664325e2019-03-12 16:52:26 -0700110 auto dev_null = cvd::SharedFD::Open("/dev/null", O_RDONLY);
111 if (dev_null->IsOpen()) {
112 command.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdIn, dev_null);
113 } else {
114 LOG(ERROR) << "Unable to open /dev/null for stdin redirection";
115 }
116
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800117 // This needs to be the last parameter
Jorge E. Moreira80ddd7f2019-02-04 16:30:13 -0800118 command.AddParameter(config_->GetKernelImageToUse());
Jorge E. Moreiraba626622019-01-28 17:47:50 -0800119
120 return command;
121}
122
123bool CrosvmManager::Stop() {
124 cvd::Command command(config_->crosvm_binary());
125 command.AddParameter("stop");
126 command.AddParameter(GetControlSocketPath(config_));
127
128 auto process = command.Start();
129
130 return process.Wait() == 0;
131}
132
133} // namespace vm_manager
Alistair Strachan73427de2019-04-17 09:49:01 -0700134