-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQEMUController.py
More file actions
430 lines (354 loc) · 17.1 KB
/
QEMUController.py
File metadata and controls
430 lines (354 loc) · 17.1 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import re
import os
import signal
import subprocess
import time
import gi
gi.require_version("Xdp", "1.0")
from gi.repository import Xdp
from loguru import logger as log
class QEMUController:
"""
A class to manage QEMU virtual machines with support for both direct QEMU
processes and libvirt-managed VMs.
"""
def __init__(self, qemu_bin_path="qemu-system-x86_64", libvirt_uri=None):
"""
Initialize the QemuVM manager.
Args:
qemu_bin_path (str): Path to the QEMU binary.
libvirt_uri (str, optional): URI for libvirt connection (e.g., 'qemu:///system')
If None, will try to detect the default.
"""
portal = Xdp.Portal.new()
self.flatpak = portal.running_under_flatpak()
self.qemu_bin_path = qemu_bin_path
self.libvirt_available = self._check_libvirt()
self.libvirt_uri = libvirt_uri
self.default_uri = self._get_default_uri() if self.libvirt_available else None
@log.catch
def _run_command(self, command: list[str]) -> subprocess.Popen:
"""Run a command with proper error handling"""
if self.flatpak:
command.insert(0, "flatpak-spawn")
command.insert(1, "--host")
try:
return subprocess.Popen(command, stdout=subprocess.PIPE, cwd="/")
except Exception as e:
log.error(f"An error occurred while running {command}: {e}")
return None
def _check_libvirt(self):
"""Check if libvirt tools are available"""
try:
process = self._run_command(["which", "virsh"])
if process:
result = process.wait()
return result == 0
return False
except Exception as e:
log.error(f"Error checking libvirt availability: {e}")
return False
def _get_default_uri(self):
"""Get the default URI used by libvirt"""
try:
process = self._run_command(["virsh", "uri"])
if process:
stdout, _ = process.communicate()
result = process.wait()
if result == 0:
return stdout.decode().strip()
return None
except Exception as e:
log.error(f"Error getting default URI: {e}")
return None
def _get_all_connections(self):
"""Get all available libvirt connections"""
connections = []
try:
# Try common URIs
uris = [
"qemu:///system", # System-level VMs
"qemu:///session", # User-level VMs
self.default_uri # Default connection
]
for uri in uris:
if uri and uri not in connections:
connections.append(uri)
return connections
except Exception as e:
log.error(f"Error getting connections: {e}")
return [self.default_uri] if self.default_uri else []
def get_vms(self, show_all_connections=True):
"""
Get a list of all VMs (both direct QEMU and libvirt-managed).
Args:
show_all_connections (bool): If True, list VMs from all possible libvirt connections.
Returns:
dict: Dictionary with 'qemu' and 'libvirt' keys, each containing VM information.
"""
vms = {'qemu': [], 'libvirt': {}}
# Get libvirt VMs first if available
if self.libvirt_available:
try:
connections = self._get_all_connections() if show_all_connections else [self.libvirt_uri or self.default_uri]
for uri in connections:
if not uri:
continue
vms['libvirt'][uri] = []
# Get running VMs
process = self._run_command(["virsh", "-c", uri, "list", "--name"])
if process:
stdout, _ = process.communicate()
result = process.wait()
if result == 0:
running_vms = [vm for vm in stdout.decode().splitlines() if vm.strip()]
for vm in running_vms:
vms['libvirt'][uri].append({"name": vm, "status": "Running"})
# Get stopped VMs
process = self._run_command(["virsh", "-c", uri, "list", "--inactive", "--name"])
if process:
stdout, _ = process.communicate()
result = process.wait()
if result == 0:
stopped_vms = [vm for vm in stdout.decode().splitlines() if vm.strip()]
for vm in stopped_vms:
vms['libvirt'][uri].append({"name": vm, "status": "Stopped"})
except Exception as e:
log.error(f"Error retrieving libvirt VM list: {e}")
# Now get direct QEMU VMs - exclude libvirt managed VMs
try:
process = self._run_command(["pgrep", "-fa", "qemu"])
if process:
stdout, _ = process.communicate()
result = process.wait()
# Get list of libvirt VM names for exclusion
libvirt_vm_names = set()
for uri_vms in vms['libvirt'].values():
for vm in uri_vms:
libvirt_vm_names.add(vm["name"])
for line in stdout.decode().splitlines():
# Look for the pattern: -name guest=
match = re.search(r'-name\\s+guest=([^,]+)', line)
if match:
vm_name = match.group(1)
# Only add if not already managed by libvirt
if vm_name not in libvirt_vm_names:
vms['qemu'].append({"name": vm_name, "status": "Running"})
except Exception as e:
log.error(f"Error retrieving QEMU VM list: {e}")
return vms
def get_vm_status(self, vm_name, uri):
"""
Check if a VM is running.
Args:
vm_name (str): Name of the VM to check.
uri (str): URI identifier - 'qemu' for direct QEMU VMs or libvirt URI.
Returns:
dict: Dictionary with status information including:
- 'status': 'Running', 'Stopped', or 'Unknown'
- 'type': 'qemu', 'libvirt', or None
- 'uri': URI if libvirt VM
"""
result = {'status': 'Unknown', 'type': None, 'uri': None}
# Handle direct QEMU VMs
if uri == 'qemu':
result['type'] = 'qemu'
try:
process = self._run_command(["pgrep", "-f", f"qemu.*{vm_name}"])
if process:
stdout, _ = process.communicate()
cmd_result = process.wait()
if cmd_result == 0:
result['status'] = 'Running'
else:
result['status'] = 'Stopped'
return result
except Exception as e:
log.error(f"Error checking QEMU VM status: {e}")
return result
# Handle libvirt VMs
elif self.libvirt_available and uri != 'qemu':
result['type'] = 'libvirt'
result['uri'] = uri
try:
# Check if VM exists in libvirt
cmd = ["virsh", "-c", uri, "domstate", vm_name]
process = self._run_command(cmd)
if process:
stdout, _ = process.communicate()
cmd_result = process.wait()
if cmd_result == 0:
state = stdout.decode().strip().lower()
if state == 'running':
result['status'] = 'Running'
elif state in ['shut off', 'inactive', 'paused', 'suspended']:
result['status'] = 'Stopped'
return result
except Exception as e:
log.error(f"Error checking libvirt VM status: {e}")
return result
def start_vm(self, vm_name, uri, vm_config=None):
"""
Start a VM (either direct QEMU or libvirt-managed).
Args:
vm_name (str): Name of the VM to start.
uri (str): URI identifier - 'qemu' for direct QEMU VMs or libvirt URI.
vm_config (dict, optional): Configuration parameters for direct QEMU VMs.
Ignored for libvirt VMs.
Returns:
bool: True if VM was started successfully, False otherwise.
"""
# Check current status
status_info = self.get_vm_status(vm_name, uri)
# If already running, nothing to do
if status_info['status'] == 'Running':
log.info(f"VM {vm_name} is already running")
return True
# Handle libvirt VMs
if uri != 'qemu' and self.libvirt_available:
try:
process = self._run_command(["virsh", "-c", uri, "start", vm_name])
if process:
stdout, stderr = process.communicate()
cmd_result = process.wait()
if cmd_result == 0:
time.sleep(2) # Give it a moment to start
return self.get_vm_status(vm_name, uri)['status'] == 'Running'
log.error(f"Failed to start libvirt VM {vm_name}: {stderr.decode() if stderr else ''}")
return False
return False
except Exception as e:
log.error(f"Error starting libvirt VM {vm_name}: {e}")
return False
# Handle direct QEMU VMs
elif uri == 'qemu':
try:
# Basic command to start a VM
cmd = [self.qemu_bin_path, "-name", f"guest={vm_name}"]
# Add additional configuration if provided
if vm_config:
for key, value in vm_config.items():
if isinstance(value, bool) and value:
cmd.append(f"-{key}")
else:
cmd.extend([f"-{key}", str(value)])
# Start QEMU process in the background
process = self._run_command(cmd)
if not process:
return False
# Give it a moment to start
time.sleep(2)
# Check if it's running
return self.get_vm_status(vm_name, uri)['status'] == 'Running'
except Exception as e:
log.error(f"Error starting direct QEMU VM {vm_name}: {e}")
return False
log.error(f"Invalid URI provided for VM {vm_name}")
return False
def stop_vm(self, vm_name, uri, force=False, timeout=3):
"""
Stop a running VM (either direct QEMU or libvirt-managed).
Args:
vm_name (str): Name of the VM to stop.
uri (str): URI identifier - 'qemu' for direct QEMU VMs or libvirt URI.
force (bool): If True, forcefully kill the VM. If False, attempt graceful shutdown.
timeout (int): Time in seconds to wait for graceful shutdown before forcing.
Returns:
bool: True if VM was stopped successfully, False otherwise.
"""
# Check current status
status_info = self.get_vm_status(vm_name, uri)
# If already stopped, nothing to do
if status_info['status'] == 'Stopped':
log.info(f"VM {vm_name} is already stopped")
return True
log.info(f"Stopping VM {vm_name} with status: {status_info}")
# Handle libvirt VMs
if uri != 'qemu' and status_info['type'] == 'libvirt':
try:
if force:
cmd = ["virsh", "-c", uri, "destroy", vm_name]
else:
cmd = ["virsh", "-c", uri, "shutdown", vm_name]
log.debug(f"Running command: {' '.join(cmd)}")
process = self._run_command(cmd)
if process:
stdout, stderr = process.communicate()
cmd_result = process.wait()
if cmd_result != 0:
log.error(f"Failed to stop libvirt VM {vm_name}: {stderr.decode() if stderr else ''}")
return False
# Wait for VM to stop if using graceful shutdown
if not force:
start_time = time.time()
while time.time() - start_time < timeout:
if self.get_vm_status(vm_name, uri)['status'] == 'Stopped':
return True
time.sleep(1)
# If still running after timeout, force kill
if self.get_vm_status(vm_name, uri)['status'] == 'Running':
log.warning(f"Timeout waiting for graceful shutdown, forcing VM {vm_name} to stop")
log.debug("Forcing shutdown")
force_process = self._run_command(["virsh", "-c", uri, "destroy", vm_name])
if force_process:
force_process.communicate()
time.sleep(0.2) # Give it a moment
return self.get_vm_status(vm_name, uri)['status'] == 'Stopped'
return False
except Exception as e:
log.error(f"Error stopping libvirt VM {vm_name}: {e}")
return False
# Handle direct QEMU VMs
elif uri == 'qemu' and status_info['type'] == 'qemu':
try:
# Find the process ID
process = self._run_command(["pgrep", "-f", f"qemu.*{vm_name}"])
if process:
stdout, _ = process.communicate()
result = process.wait()
if result != 0:
return True # Already stopped
pid = int(stdout.decode().strip())
if force:
# Force kill
os.kill(pid, signal.SIGKILL)
else:
# Try graceful shutdown first (SIGTERM)
os.kill(pid, signal.SIGTERM)
# Wait for VM to stop
start_time = time.time()
while time.time() - start_time < timeout:
if self.get_vm_status(vm_name, uri)['status'] == 'Stopped':
return True
time.sleep(1)
# If still running after timeout, force kill
if self.get_vm_status(vm_name, uri)['status'] == 'Running':
log.warning(f"Timeout waiting for graceful shutdown, forcing VM {vm_name} to stop")
os.kill(pid, signal.SIGKILL)
# Give it a moment
time.sleep(1)
# Check if it's stopped
return self.get_vm_status(vm_name, uri)['status'] == 'Stopped'
return False
except Exception as e:
log.error(f"Error stopping direct QEMU VM {vm_name}: {e}")
return False
# VM not found or URI and type don't match
else:
log.error(f"VM {vm_name} not found or URI type ({uri}) doesn't match VM type ({status_info['type']})")
return False
# Test code
if __name__ == "__main__":
qemu_controller = QEMUController()
# Get the default URI
print(f"Default libvirt URI: {qemu_controller.default_uri}")
# Get VMs from all connections
vms = qemu_controller.get_vms(show_all_connections=True)
# Print QEMU VMs
print("\\nQEMU VMs:", vms['qemu'])
# Print libvirt VMs by connection
print("\\nLibvirt VMs by connection:")
for uri, vm_list in vms['libvirt'].items():
print(f" URI: {uri}")
print(f" VMs: {vm_list}")
qemu_controller.stop_vm("Fedora39", "qemu:///system")