-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualManager.cc
114 lines (94 loc) · 3.07 KB
/
VirtualManager.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
#include "VirtualManager.hh"
#include <stdexcept>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <tinyxml.h>
VirtualManager::VirtualManager()
{
/* Connect to hypervisor */
conn = virConnectOpen(NULL);
if (conn == NULL) {
throw std::runtime_error("Unable to connect to hypervisor");
}
}
VirtualManager::~VirtualManager()
{
virConnectClose(conn);
}
std::vector<std::string> VirtualManager::listVms()
{
std::vector<std::string> ret;
/* List domains */
int numVms = virConnectNumOfDomains(conn);
int vms[numVms];
numVms = virConnectListDomains(conn, vms, numVms);
if (numVms == -1) {
throw std::runtime_error("Call to virConnectListDomains() failed.");
}
for (int i = 0; i < numVms; i++) {
virDomainPtr domain = virDomainLookupByID(conn, vms[i]);
ret.push_back(virDomainGetName(domain));
}
return ret;
}
std::string VirtualManager::lookUpVmByMac(const unsigned char lookedUpMac[6])
{
std::string ret;
/* List domains */
int numVms = virConnectNumOfDomains(conn);
int vms[numVms];
numVms = virConnectListDomains(conn, vms, numVms);
if (numVms == -1) {
throw std::runtime_error("Call to virConnectListDomains() failed.");
}
for (int i = 0; i < numVms; i++) {
virDomainPtr domain = virDomainLookupByID(conn, vms[i]);
if (domain == NULL) continue;
char *xmlDesc = virDomainGetXMLDesc(domain, 0);
if (xmlDesc == NULL) continue;
TiXmlDocument doc;
doc.Parse(xmlDesc);
TiXmlElement *devicesElement = NULL, *interfaceElement = NULL, *macElement = NULL;
const char *macAddress = NULL;
devicesElement = doc.RootElement()->FirstChildElement("devices");
if (devicesElement)
interfaceElement = devicesElement->FirstChildElement("interface");
if (interfaceElement)
macElement = interfaceElement->FirstChildElement("mac");
if (macElement)
macAddress = macElement->Attribute("address");
if (!macAddress) continue;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
unsigned char currentMac[6];
sscanf(macAddress, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
¤tMac[0], ¤tMac[1], ¤tMac[2], ¤tMac[3], ¤tMac[4], ¤tMac[5]);
#pragma GCC diagnostic pop
if (memcmp(currentMac, lookedUpMac, 6) == 0)
return virDomainGetName(domain);
}
throw std::runtime_error("No VM with the requested MAC");
}
void VirtualManager::setVmCap(std::string name, int cap)
{
virDomainPtr domain = virDomainLookupByName(conn, name.c_str());
if (domain == NULL) {
throw std::runtime_error(std::string() +
"Unable to find domain by name: " +
virGetLastError()->message);
}
int nvcpus = cap / 100 + ((cap % 100 == 0) ? 0 : 1);
virDomainSetVcpus(domain, nvcpus);
virTypedParameter param;
strcpy(param.field, VIR_DOMAIN_SCHEDULER_CAP);
param.type = VIR_TYPED_PARAM_UINT;
param.value.ui = cap;
if (virDomainSetSchedulerParameters(domain, ¶m, 1) == -1) {
throw std::runtime_error(std::string() +
"Unable to find domain by name: " +
virGetLastError()->message);
}
int numParams = 10; /* way too many */
virTypedParameter params[numParams];
virDomainGetSchedulerParameters(domain, params, &numParams);
}