/* * This source code is released into the public domain. */ module; #include #include #include #include import nihil; export module liblfvm:vm; import :vm_config; namespace lfvm { /* * Create the bhyve arguments list for a VM configuration. */ auto get_bhyve_args(vm_config const &config) -> nihil::argv { using namespace std::literals; auto args = std::vector(); // UUID args.emplace_back(std::format("-U {}", to_string(config.uuid()))); // CPUs args.emplace_back(std::format("-c {}", config.ncpus())); // Memory args.emplace_back(std::format("-m {}", config.memory_size())); if (config.destroy_on_poweroff()) args.emplace_back("-D"sv); if (config.wire_memory()) args.emplace_back("-S"sv); if (config.include_memory_in_core()) args.emplace_back("-C"sv); if (config.yield_on_halt()) args.emplace_back("-H"sv); if (config.exit_on_pause()) args.emplace_back("-P"sv); if (config.rtc_is_utc()) args.emplace_back("-u"sv); // VM name args.emplace_back(config.name()); std::cerr << "args: ["; for (auto &&arg : args) std::cerr << arg << ' '; std::cerr << "]\n"; return nihil::argv(std::from_range, std::move(args)); } /* * Represents a VM which can be started and stopped. * Destroying the VM object will also destroy the VM. */ export struct vm final { // Create a new VM object. This doesn't create the actual VM. vm(vm_config config) : _config(std::move(config)) { } ~vm() { // TODO: Destroy the VM. } // Get the configuration for this VM. auto config(this vm const &self) -> vm_config const & { return self._config; } // Start the VM and wait until bhyve exits. auto run(this vm &self) -> void { auto argv = get_bhyve_args(self.config()); } private: vm_config _config; }; } // namespace lfvm