1 //
2 // Copyright (C) 2021 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 <optional>
19 #include <string>
20 #include <utility>
21 
22 #include "common/libs/fs/shared_fd.h"
23 #include "common/libs/utils/result.h"
24 #include "common/libs/utils/subprocess.h"
25 #include "host/libs/vm_manager/pci.h"
26 
27 namespace cuttlefish {
28 
29 class CrosvmBuilder {
30  public:
31   CrosvmBuilder();
32 
33   void ApplyProcessRestarter(const std::string& crosvm_binary,
34                              const std::string& first_time_argument,
35                              int exit_code);
36   void AddControlSocket(const std::string&, const std::string&);
37 
38   void AddHvcSink();
39   void AddHvcReadOnly(const std::string& output, bool console = false);
40   void AddHvcReadWrite(const std::string& output, const std::string& input);
41 
42   void AddReadOnlyDisk(const std::string& path);
43   void AddReadWriteDisk(const std::string& path);
44 
45   void AddSerialSink();
46   void AddSerialConsoleReadOnly(const std::string& output);
47   void AddSerialConsoleReadWrite(const std::string& output,
48                                  const std::string& input, bool earlycon);
49   // [[deprecated("do not add any more users")]]
50   void AddSerial(const std::string& output, const std::string& input);
51 
52 #ifdef __linux__
53   SharedFD AddTap(const std::string& tap_name,
54                   std::optional<std::string_view> mac = std::nullopt,
55                   const std::optional<pci::Address>& pci = std::nullopt);
56 #endif
57 
58   int HvcNum();
59 
60   /**
61    * Configures the crosvm to start with --restore=<guest snapshot path>
62    */
63   Result<void> SetToRestoreFromSnapshot(const std::string& snapshot_dir_path,
64                                         const std::string& instance_id,
65                                         const std::string& snapshot_name);
66 
67   Command& Cmd();
68 
69  private:
70   Command command_;
71   int hvc_num_;
72   int serial_num_;
73 };
74 
75 }  // namespace cuttlefish
76