Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 1 | /* |
| 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. Moreira | 46429a9 | 2019-02-12 17:21:32 -0800 | [diff] [blame] | 24 | #include "common/libs/utils/network.h" |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 25 | #include "common/libs/utils/subprocess.h" |
| 26 | #include "host/libs/config/cuttlefish_config.h" |
| 27 | #include "host/libs/vm_manager/qemu_manager.h" |
| 28 | |
| 29 | namespace vm_manager { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | std::string GetControlSocketPath(const vsoc::CuttlefishConfig* config) { |
| 34 | return config->PerInstancePath("crosvm_control.sock"); |
| 35 | } |
| 36 | |
| 37 | cvd::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. Moreira | 46429a9 | 2019-02-12 17:21:32 -0800 | [diff] [blame] | 42 | void 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. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 52 | } // namespace |
| 53 | |
| 54 | const std::string CrosvmManager::name() { return "crosvm"; } |
| 55 | |
| 56 | CrosvmManager::CrosvmManager(const vsoc::CuttlefishConfig* config) |
| 57 | : VmManager(config) {} |
| 58 | |
| 59 | cvd::Command CrosvmManager::StartCommand() { |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 60 | // 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 Strachan | 73427de | 2019-04-17 09:49:01 -0700 | [diff] [blame] | 71 | if (!config_->ramdisk_image_path().empty()) { |
| 72 | command.AddParameter("--initrd=", config_->ramdisk_image_path()); |
| 73 | } |
Alistair Strachan | 252d98b | 2019-03-04 17:58:06 -0800 | [diff] [blame] | 74 | command.AddParameter("--null-audio"); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 75 | command.AddParameter("--mem=", config_->memory_mb()); |
| 76 | command.AddParameter("--cpus=", config_->cpus()); |
| 77 | command.AddParameter("--params=", config_->kernel_cmdline_as_string()); |
Jorge E. Moreira | 3fad450 | 2019-02-28 14:16:38 -0800 | [diff] [blame] | 78 | command.AddParameter("--rwdisk=", config_->system_image_path()); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 79 | command.AddParameter("--rwdisk=", config_->data_image_path()); |
| 80 | command.AddParameter("--rwdisk=", config_->cache_image_path()); |
Paul Trautrim | ba8f8e9 | 2019-03-12 17:55:48 +0900 | [diff] [blame] | 81 | command.AddParameter("--rwdisk=", config_->metadata_image_path()); |
Alistair Strachan | 17c19e8 | 2019-04-17 09:37:09 -0700 | [diff] [blame^] | 82 | command.AddParameter("--rwdisk=", config_->vendor_image_path()); |
Alistair Strachan | 5782ce4 | 2019-03-28 22:05:13 -0700 | [diff] [blame] | 83 | command.AddParameter("--rwdisk=", config_->product_image_path()); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 84 | command.AddParameter("--socket=", GetControlSocketPath(config_)); |
| 85 | command.AddParameter("--android-fstab=", config_->gsi_fstab_path()); |
Jorge E. Moreira | c87c2c7 | 2019-03-06 16:12:23 -0800 | [diff] [blame] | 86 | 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. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 89 | |
Jorge E. Moreira | 46429a9 | 2019-02-12 17:21:32 -0800 | [diff] [blame] | 90 | AddTapFdParameter(&command, config_->wifi_tap_name()); |
| 91 | AddTapFdParameter(&command, config_->mobile_tap_name()); |
| 92 | |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 93 | // 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. Moreira | 664325e | 2019-03-12 16:52:26 -0700 | [diff] [blame] | 110 | 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. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 117 | // This needs to be the last parameter |
Jorge E. Moreira | 80ddd7f | 2019-02-04 16:30:13 -0800 | [diff] [blame] | 118 | command.AddParameter(config_->GetKernelImageToUse()); |
Jorge E. Moreira | ba62662 | 2019-01-28 17:47:50 -0800 | [diff] [blame] | 119 | |
| 120 | return command; |
| 121 | } |
| 122 | |
| 123 | bool 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 Strachan | 73427de | 2019-04-17 09:49:01 -0700 | [diff] [blame] | 134 | |