-
Notifications
You must be signed in to change notification settings - Fork 9
/
libvirt.cpp
234 lines (195 loc) · 5.44 KB
/
libvirt.cpp
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
/*
* PROJECT: ReactOS System Regression Testing Utility
* LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
* PURPOSE: Support for libvirt VMs - generic code only
* COPYRIGHT: Copyright 2008-2009 Christoph von Wittich <[email protected]>
* Copyright 2009 Colin Finck <[email protected]>
* Copyright 2012-2015 Pierre Schweitzer <[email protected]>
*/
#include "machine.h"
LibVirt::LibVirt()
{
vConn = NULL;
}
LibVirt::~LibVirt()
{
if (vConn)
virConnectClose(vConn);
}
bool LibVirt::IsMachineRunning(const char* name, bool destroy)
{
bool Ret;
virDomainPtr vDomPtr = NULL;
vDomPtr = virDomainLookupByName(vConn, name);
if (vDomPtr == 0)
return false;
Ret = (virDomainIsActive(vDomPtr) == 1);
if (!destroy)
{
virDomainFree(vDomPtr);
return Ret;
}
if (Ret)
Ret = (virDomainDestroy(vDomPtr) != 0);
if (!Ret)
{
for (unsigned int i = 0; i < 12; ++i)
{
if (virDomainUndefine(vDomPtr) == 0)
break;
sleep(5);
}
}
virDomainFree(vDomPtr);
return Ret;
}
void LibVirt::InitializeDisk()
{
FILE* file;
char qemu_img_cmdline[300];
/* If the HD image already exists, delete it */
if ((file = fopen(AppSettings.HardDiskImage, "r")))
{
fclose(file);
remove(AppSettings.HardDiskImage);
}
/* Create a new HD image */
if (AppSettings.VMType == TYPE_KVM)
{
sprintf(qemu_img_cmdline, "qemu-img create -f raw %s %dM",
AppSettings.HardDiskImage, AppSettings.ImageSize);
}
else if (AppSettings.VMType == TYPE_VMWARE_PLAYER)
{
sprintf(qemu_img_cmdline, "qemu-img create -f vmdk %s %dM",
AppSettings.HardDiskImage, AppSettings.ImageSize);
}
else if (AppSettings.VMType == TYPE_VIRTUALBOX)
{
sprintf(qemu_img_cmdline, "qemu-img create -f vdi %s %dM",
AppSettings.HardDiskImage, AppSettings.ImageSize);
}
Execute(qemu_img_cmdline);
}
bool LibVirt::LaunchMachine(const char* XmlFileName, const char* BootDevice)
{
xmlDocPtr xml = NULL;
xmlXPathObjectPtr obj = NULL;
xmlXPathContextPtr ctxt = NULL;
char* buffer;
int len = 0;
buffer = ReadFile(XmlFileName);
if (buffer == NULL)
return false;
xml = xmlReadDoc((const xmlChar *) buffer, "domain.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOWARNING);
if (!xml)
return false;
ctxt = xmlXPathNewContext(xml);
if (!ctxt)
return false;
obj = xmlXPathEval(BAD_CAST "/domain/os/boot", ctxt);
if ((obj != NULL) && (obj->type == XPATH_NODESET)
&& (obj->nodesetval != NULL) && (obj->nodesetval->nodeTab != NULL))
{
xmlSetProp(obj->nodesetval->nodeTab[0], BAD_CAST"dev", BAD_CAST BootDevice);
}
if (obj)
xmlXPathFreeObject(obj);
free(buffer);
xmlDocDumpMemory(xml, (xmlChar**) &buffer, &len);
xmlFreeDoc(xml);
xmlXPathFreeContext(ctxt);
vDom = virDomainDefineXML(vConn, buffer);
xmlFree((xmlChar*)buffer);
if (vDom)
{
if (!PrepareSerialPort())
{
return false;
}
if (virDomainCreate(vDom) != 0)
{
virDomainUndefine(vDom);
virDomainFree(vDom);
vDom = NULL;
return false;
}
else
{
/* workaround a bug in libvirt */
const char *name = virDomainGetName(vDom);
char *domname = strdup(name);
virDomainFree(vDom);
vDom = virDomainLookupByName(vConn, domname);
free(domname);
return true;
}
}
else
return false;
}
const char * LibVirt::GetMachineName() const
{
return virDomainGetName(vDom);
}
void LibVirt::ShutdownMachine()
{
virDomainInfo info;
/* Get VM info in order to shutdown.
* NB: In case the VM was properly shutdown by ReactOS,
* This will display an error in output.
* It can be safely ignored, all fine.
* Error message with KVM:
* libvir: QEMU error : Requested operation is not valid: domain is not running
*/
virDomainGetInfo(vDom, &info);
/* Shutdown the VM - if running */
if (info.state != VIR_DOMAIN_SHUTOFF)
{
/* We will first try a graceful shutdown */
virDomainReboot(vDom, VIR_DOMAIN_REBOOT_ACPI_POWER_BTN);
sleep(3);
/* Is the VM shut? */
virDomainGetInfo(vDom, &info);
/* Kill the VM - if running */
if (info.state != VIR_DOMAIN_SHUTOFF)
virDomainDestroy(vDom);
}
for (unsigned int i = 0; i < 12; ++i)
{
if (virDomainUndefine(vDom) == 0)
break;
sleep(5);
}
virDomainFree(vDom);
CloseSerialPort();
}
bool LibVirt::PrepareSerialPort()
{
// Do nothing
return true;
}
void LibVirt::CloseSerialPort()
{
// Do nothing
return;
}
bool LibVirt::IsConnected() const
{
return (vConn != NULL);
}
bool LibVirt::BreakToDebugger() const
{
int ret;
unsigned int keycodes[2];
/* If VM not running, do nothing */
if (vDom == NULL)
return false;
/* Otherwise, send tab + k */
keycodes[0] = 0x9; /* TAB */
keycodes[1] = 0x4B; /* K */
ret = virDomainSendKey(vDom, VIR_KEYCODE_SET_WIN32, 10, &keycodes[0], 2, 0);
return (ret == 0);
}