-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathqemu-agent-guest-exec
executable file
·52 lines (47 loc) · 1.26 KB
/
qemu-agent-guest-exec
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
#!/usr/bin/python3
'''
Execute a command inside the guest.
'''
import base64
import json
import libvirt
import libvirt_qemu
import logging
import sys
import time
domain_name = sys.argv[1]
command_path = sys.argv[2]
command_args = sys.argv[3:]
try:
connection = libvirt.open(None)
except libvirt.libvirtError:
logging.exception('Failed to open connection to the hypervisor')
exit(1)
try:
domain = connection.lookupByName(domain_name)
except libvirt.libvirtError:
logging.exception(f'Domain {domain_name} is not running')
exit(1)
# execute a command inside the guest.
# see https://libvirt.org/html/libvirt-libvirt-qemu.html#virDomainQemuAgentCommand
command = json.dumps({
'execute': 'guest-exec',
'arguments': {
'path': command_path,
'arg': command_args,
'capture-output': True,
}
})
result = json.loads(libvirt_qemu.qemuAgentCommand(domain, command, -2, 0))
command = json.dumps({
'execute': 'guest-exec-status',
'arguments': {
'pid': result['return']['pid'],
}
})
while True:
result = json.loads(libvirt_qemu.qemuAgentCommand(domain, command, -2, 0))
if result['return']['exited']:
print(base64.b64decode(result['return']['out-data']).decode('utf-8'))
break
time.sleep(0.1)