• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
17 
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include <common/libs/utils/subprocess.h>
25 #include <host/libs/config/cuttlefish_config.h>
26 
27 namespace vm_manager {
28 
29 // Superclass of every guest VM manager. It provides a static getter that
30 // returns the requested vm manager as a singleton.
31 class VmManager {
32  public:
33   // Returns the most suitable vm manager as a singleton. It may return nullptr
34   // if the requested vm manager is not supported by the current version of the
35   // host packages
36   static VmManager* Get(const std::string& vm_manager_name,
37                         const vsoc::CuttlefishConfig* config);
38   static bool IsValidName(const std::string& name);
39   static std::vector<std::string> ConfigureGpuMode(
40       const std::string& vmm_name, const std::string& gpu_mode);
41   static std::vector<std::string> ConfigureBootDevices(
42       const std::string& vmm_name);
43   static bool IsVmManagerSupported(const std::string& name);
44   static std::vector<std::string> GetValidNames();
45 
46   virtual ~VmManager() = default;
47 
48   virtual void WithFrontend(bool);
49   virtual void WithKernelCommandLine(const std::string&);
50 
51   // Starts the VMM. It will usually build a command and pass it to the
52   // command_starter function, although it may start more than one. The
53   // command_starter function allows to customize the way vmm commands are
54   // started/tracked/etc.
55   virtual std::vector<cvd::Command> StartCommands() = 0;
56 
57   virtual bool ValidateHostConfiguration(
58       std::vector<std::string>* config_commands) const;
59 
60  protected:
61   static bool UserInGroup(const std::string& group,
62                           std::vector<std::string>* config_commands);
63   static constexpr std::pair<int,int> invalid_linux_version =
64     std::pair<int,int>();
65   static std::pair<int,int> GetLinuxVersion();
66   static bool LinuxVersionAtLeast(std::vector<std::string>* config_commands,
67                                   const std::pair<int,int>& version,
68                                   int major, int minor);
69 
70   const vsoc::CuttlefishConfig* config_;
71   VmManager(const vsoc::CuttlefishConfig* config);
72 
73   bool frontend_enabled_;
74   std::string kernel_cmdline_;
75 
76  private:
77   struct VmManagerHelper {
78     // The singleton implementation
79     std::function<VmManager*(const vsoc::CuttlefishConfig*)> builder;
80     // Whether the host packages support this vm manager
81     std::function<bool()> support_checker;
82     std::function<std::vector<std::string>(const std::string&)> configure_gpu_mode;
83     std::function<std::vector<std::string>()> configure_boot_devices;
84   };
85   // Asociates a vm manager helper to every valid vm manager name
86   static std::map<std::string, VmManagerHelper> vm_manager_helpers_;
87 };
88 
89 }  // namespace vm_manager
90