-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathvirtualization_resolver.cc
256 lines (232 loc) · 8.02 KB
/
virtualization_resolver.cc
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <internal/facts/linux/virtualization_resolver.hpp>
#include <internal/util/agent.hpp>
#include <facter/facts/scalar_value.hpp>
#include <facter/facts/collection.hpp>
#include <facter/facts/fact.hpp>
#include <facter/facts/vm.hpp>
#include <leatherman/file_util/file.hpp>
#include <leatherman/file_util/directory.hpp>
#include <leatherman/util/regex.hpp>
#include <leatherman/logging/logging.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace facter::facts;
using namespace facter::util;
using namespace leatherman::util;
using namespace leatherman::execution;
using namespace boost::filesystem;
namespace bs = boost::system;
namespace lth_file = leatherman::file_util;
namespace facter { namespace facts { namespace linux {
string virtualization_resolver::get_hypervisor(collection& facts)
{
// First check for Docker/LXC
string value = get_cgroup_vm();
// Next check for Google Compute Engine
if (value.empty()) {
value = get_gce_vm(facts);
}
// Next check based on the virt-what command
if (value.empty()) {
value = get_what_vm();
}
// Next check the vmware tool output
if (value.empty()) {
value = get_vmware_vm();
}
// Next check for OpenVZ
if (value.empty()) {
value = get_openvz_vm();
}
// Next check for VServer
if (value.empty()) {
value = get_vserver_vm();
}
// Next check for Xen
if (value.empty()) {
value = get_xen_vm();
}
// Next check other facts for the VM
if (value.empty()) {
value = get_fact_vm(facts);
}
// Lastly, resort to lspci to look for hardware related to certain VMs
if (value.empty()) {
value = get_lspci_vm();
}
return value;
}
string virtualization_resolver::get_cgroup_vm()
{
string value;
lth_file::each_line("/proc/1/cgroup", [&](string& line) {
vector<boost::iterator_range<string::iterator>> parts;
boost::split(parts, line, boost::is_any_of(":"), boost::token_compress_on);
if (parts.size() < 3) {
return true;
}
if (boost::contains(parts[2], boost::as_literal("/docker"))) {
value = vm::docker;
return false;
}
if (boost::contains(parts[2], boost::as_literal("/lxc"))) {
value = vm::lxc;
return false;
}
return true;
});
return value;
}
string virtualization_resolver::get_gce_vm(collection& facts)
{
auto vendor = facts.get<string_value>(fact::bios_vendor);
if (vendor && vendor->value().find("Google") != string::npos) {
return vm::gce;
}
return {};
}
string virtualization_resolver::get_what_vm()
{
string virt_what = agent::which("virt-what");
string value;
each_line(virt_what, [&](string& line) {
// Some versions of virt-what dump error/warning messages to stdout
if (boost::starts_with(line, "virt-what:")) {
return true;
}
// Take the first line that isn't an error/warning
// unless it's "xen", in which case we expect a second
// line with more useful information
if (line == "xen") {
return true;
}
value = move(line);
return false;
});
// Do some normalization of virt-what's output
if (!value.empty()) {
boost::to_lower(value);
if (value == "linux_vserver") {
return get_vserver_vm();
}
if (value == "xen-hvm") {
return vm::xen_hardware;
}
if (value == "xen-dom0") {
return vm::xen_privileged;
}
if (value == "xen-domu") {
return vm::xen_unprivileged;
}
if (value == "ibm_systemz") {
return vm::zlinux;
}
}
return value;
}
string virtualization_resolver::get_vserver_vm()
{
string value;
lth_file::each_line("/proc/self/status", [&](string& line) {
vector<boost::iterator_range<string::iterator>> parts;
boost::split(parts, line, boost::is_space(), boost::token_compress_on);
if (parts.size() != 2) {
return true;
}
if (parts[0] == boost::as_literal("s_context:") || parts[0] == boost::as_literal("VxID:")) {
if (parts[1] == boost::as_literal("0")) {
value = vm::vserver_host;
} else {
value = vm::vserver;
}
return false;
}
return true;
});
return value;
}
string virtualization_resolver::get_vmware_vm()
{
auto exec = execute("vmware", { "-v" });
if (!exec.success) {
return {};
}
vector<string> parts;
boost::split(parts, exec.output, boost::is_space(), boost::token_compress_on);
if (parts.size() < 2) {
return {};
}
boost::to_lower(parts[0]);
boost::to_lower(parts[1]);
return parts[0] + '_' + parts[1];
}
string virtualization_resolver::get_openvz_vm()
{
// Detect if it's a OpenVZ without being CloudLinux
bs::error_code ec;
if (!is_directory("/proc/vz", ec) ||
is_regular_file("/proc/lve/list", ec) ||
boost::filesystem::is_empty("/proc/vz", ec)) {
return {};
}
string value;
lth_file::each_line("/proc/self/status", [&](string& line) {
vector<boost::iterator_range<string::iterator>> parts;
boost::split(parts, line, boost::is_space(), boost::token_compress_on);
if (parts.size() != 2) {
return true;
}
if (parts[0] == boost::as_literal("envID:")) {
if (parts[1] == boost::as_literal("0")) {
value = vm::openvz_hn;
} else {
value = vm::openvz_ve;
}
return false;
}
return true;
});
return value;
}
string virtualization_resolver::get_xen_vm()
{
// Check for a required Xen file
bs::error_code ec;
if (exists("/dev/xen/evtchn", ec) && !ec) {
return vm::xen_privileged;
}
ec.clear();
if (exists("/proc/xen", ec) && !ec) {
return vm::xen_unprivileged;
}
ec.clear();
if (exists("/dev/xvda1", ec) && !ec) {
return vm::xen_unprivileged;
}
return {};
}
string virtualization_resolver::get_lspci_vm()
{
static vector<tuple<boost::regex, string>> vms = {
make_tuple(boost::regex("VM[wW]are"), string(vm::vmware)),
make_tuple(boost::regex("VirtualBox"), string(vm::virtualbox)),
make_tuple(boost::regex("1ab8:|[Pp]arallels"), string(vm::parallels)),
make_tuple(boost::regex("XenSource"), string(vm::xen_hardware)),
make_tuple(boost::regex("Microsoft Corporation Hyper-V"), string(vm::hyperv)),
make_tuple(boost::regex("Class 8007: Google, Inc"), string(vm::gce)),
make_tuple(boost::regex("virtio", boost::regex::icase), string(vm::kvm)),
};
string value;
each_line("lspci", [&](string& line) {
for (auto const& vm : vms) {
if (re_search(line, get<0>(vm))) {
value = get<1>(vm);
return false;
}
}
return true;
});
return value;
}
}}} // namespace facter::facts::linux