1 /*
2 * Copyright (C) 2018 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/vm_manager.h"
18
19 #include <memory>
20
21 #include <glog/logging.h>
22
23 #include "common/libs/utils/users.h"
24 #include "host/libs/config/cuttlefish_config.h"
25 #include "host/libs/vm_manager/qemu_manager.h"
26 #include "host/libs/vm_manager/crosvm_manager.h"
27
28 namespace vm_manager {
29
VmManager(const vsoc::CuttlefishConfig * config)30 VmManager::VmManager(const vsoc::CuttlefishConfig* config)
31 : config_(config) {}
32
33 namespace{
34 template <typename T>
GetManagerSingleton(const vsoc::CuttlefishConfig * config)35 VmManager* GetManagerSingleton(const vsoc::CuttlefishConfig* config) {
36 static std::shared_ptr<VmManager> vm_manager(new T(config));
37 return vm_manager.get();
38 }
39 }
40
41 std::map<std::string, VmManager::VmManagerHelper>
42 VmManager::vm_manager_helpers_ = {
43 {
44 QemuManager::name(),
45 {
__anone8b419450202() 46 [](const vsoc::CuttlefishConfig* config) {
47 return GetManagerSingleton<QemuManager>(config);
48 },
__anone8b419450302() 49 []() { return vsoc::HostSupportsQemuCli(); },
__anone8b419450402() 50 [](vsoc::CuttlefishConfig* c) {
51 return QemuManager::ConfigureBootDevices(c);
52 }
53 },
54 },
55 {
56 CrosvmManager::name(),
57 {
__anone8b419450502() 58 [](const vsoc::CuttlefishConfig* config) {
59 return GetManagerSingleton<CrosvmManager>(config);
60 },
61 // Same as Qemu for the time being
__anone8b419450602() 62 []() { return vsoc::HostSupportsQemuCli(); },
__anone8b419450702() 63 [](vsoc::CuttlefishConfig* c) {
64 return CrosvmManager::ConfigureBootDevices(c);
65 }
66 }
67 }
68 };
69
Get(const std::string & vm_manager_name,const vsoc::CuttlefishConfig * config)70 VmManager* VmManager::Get(const std::string& vm_manager_name,
71 const vsoc::CuttlefishConfig* config) {
72 if (VmManager::IsValidName(vm_manager_name)) {
73 return vm_manager_helpers_[vm_manager_name].builder(config);
74 }
75 LOG(ERROR) << "Requested invalid VmManager: " << vm_manager_name;
76 return nullptr;
77 }
78
IsValidName(const std::string & name)79 bool VmManager::IsValidName(const std::string& name) {
80 return vm_manager_helpers_.count(name) > 0;
81 }
82
IsVmManagerSupported(const std::string & name)83 bool VmManager::IsVmManagerSupported(const std::string& name) {
84 return VmManager::IsValidName(name) &&
85 vm_manager_helpers_[name].support_checker();
86 }
87
ConfigureBootDevices(vsoc::CuttlefishConfig * config)88 void VmManager::ConfigureBootDevices(vsoc::CuttlefishConfig* config) {
89 auto it = vm_manager_helpers_.find(config->vm_manager());
90 if (it == vm_manager_helpers_.end()) {
91 return;
92 }
93 it->second.configure_boot_devices(config);
94 }
95
GetValidNames()96 std::vector<std::string> VmManager::GetValidNames() {
97 std::vector<std::string> ret = {};
98 for (const auto& key_val: vm_manager_helpers_) {
99 ret.push_back(key_val.first);
100 }
101 return ret;
102 }
103
UserInGroup(const std::string & group,std::vector<std::string> * config_commands)104 bool VmManager::UserInGroup(const std::string& group,
105 std::vector<std::string>* config_commands) {
106 if (!cvd::InGroup(group)) {
107 LOG(ERROR) << "User must be a member of " << group;
108 config_commands->push_back("# Add your user to the " + group + " group:");
109 config_commands->push_back("sudo usermod -aG " + group + " $USER");
110 return false;
111 }
112 return true;
113 }
114
ValidateHostConfiguration(std::vector<std::string> * config_commands) const115 bool VmManager::ValidateHostConfiguration(
116 std::vector<std::string>* config_commands) const {
117 // the check for cvdnetwork needs to happen even if the user is not in kvm, so
118 // we cant just say UserInGroup("kvm") && UserInGroup("cvdnetwork")
119 auto in_kvm = VmManager::UserInGroup("kvm", config_commands);
120 auto in_cvdnetwork = VmManager::UserInGroup("cvdnetwork", config_commands);
121 return in_kvm && in_cvdnetwork;
122 }
123 } // namespace vm_manager
124