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