aboutsummaryrefslogtreecommitdiffstats
path: root/liblfvm/vm.ccm
blob: 0e5eb0b85845da516676719bd7536e326254aa61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * This source code is released into the public domain.
 */

module;

#include <iostream>
#include <ranges>
#include <string>
#include <vector>

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<std::string>();

	// 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