Home | History | Annotate | Download | only in vm_manager
      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 <string>
     20 #include <utility>
     21 #include <vector>
     22 
     23 #include <common/libs/utils/subprocess.h>
     24 #include <host/libs/config/cuttlefish_config.h>
     25 
     26 namespace vm_manager {
     27 
     28 // Superclass of every guest VM manager. It provides a static getter that
     29 // returns the requested vm manager as a singleton.
     30 class VmManager {
     31  public:
     32   // Returns the most suitable vm manager as a singleton. It may return nullptr
     33   // if the requested vm manager is not supported by the current version of the
     34   // host packages
     35   static VmManager* Get(const std::string& vm_manager_name,
     36                         const vsoc::CuttlefishConfig* config);
     37   static bool IsValidName(const std::string& name);
     38   static void ConfigureBootDevices(vsoc::CuttlefishConfig* config);
     39   static bool IsVmManagerSupported(const std::string& name);
     40   static std::vector<std::string> GetValidNames();
     41 
     42   virtual ~VmManager() = default;
     43 
     44   virtual cvd::Command StartCommand() = 0;
     45   virtual bool Stop() = 0;
     46 
     47   virtual bool ValidateHostConfiguration(
     48       std::vector<std::string>* config_commands) const;
     49 
     50  protected:
     51   static bool UserInGroup(const std::string& group,
     52                           std::vector<std::string>* config_commands);
     53   const vsoc::CuttlefishConfig* config_;
     54   VmManager(const vsoc::CuttlefishConfig* config);
     55 
     56 private:
     57   struct VmManagerHelper {
     58     // The singleton implementation
     59     std::function<VmManager*(const vsoc::CuttlefishConfig*)> builder;
     60     // Whether the host packages support this vm manager
     61     std::function<bool()> support_checker;
     62     std::function<void(vsoc::CuttlefishConfig*)> configure_boot_devices;
     63   };
     64   // Asociates a vm manager helper to every valid vm manager name
     65   static std::map<std::string, VmManagerHelper> vm_manager_helpers_;
     66 };
     67 
     68 }  // namespace vm_manager
     69