-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathqemu.py
155 lines (119 loc) · 4.48 KB
/
qemu.py
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
"""
Wraps a QEMU virtual machine.
"""
import asyncio
import logging
import os
from pathlib import Path
import shlex
import subprocess
from . import Container, ContainerConfig
class QEMU(Container):
"""
Represents a qemu virtual machine.
"""
def __init__(self, cfg):
super().__init__(cfg)
self.manage = False
self.process = None
self.running_image = None
@classmethod
def config(cls, machine_id, cfgdata, cfgpath):
cfg = ContainerConfig(machine_id, cfgdata, cfgpath)
base_img = Path(cfgdata["base_image"])
if not base_img.is_absolute():
base_img = cfgpath / base_img
cfg.base_image = base_img.absolute()
overlay_img = Path(cfgdata["overlay_image"])
if not overlay_img.is_absolute():
overlay_img = cfgpath / overlay_img
cfg.overlay_image = overlay_img.absolute()
cfg.command = cfgdata["command"]
if not cfg.base_image.is_file():
raise FileNotFoundError("base image: %s" % cfg.base_image)
return cfg
async def prepare(self, manage=False):
self.manage = manage
if not self.manage:
# create a temporary runimage
idx = 0
while True:
tmpimage = Path(str(self.cfg.overlay_image) + "_%02d" % idx)
if not tmpimage.is_file():
break
idx += 1
self.running_image = tmpimage
command = [
"qemu-img", "create",
"-o", "backing_file=" + str(self.cfg.base_image),
"-f", "qcow2",
str(self.running_image),
]
proc = await asyncio.create_subprocess_exec(*command)
try:
ret = await asyncio.wait_for(proc.wait(), timeout=60)
except asyncio.TimeoutError as exc:
raise RuntimeError("timeout when creating "
"overlay image") from exc
if ret != 0:
raise RuntimeError(f"could not create overlay image: "
f"qemu-img returned {ret}")
else:
# TODO: even in management mode, create a cow image,
# but in the end merge it back into a new image and
# perform an atomic rename(2) in order to atomically
# replace the VM.
# currently, builds that were triggered while the VM
# is being managed may use a corrupted image.
# TODO: disallow multiple management connections at once.
logging.info("QEMU VM launching in management mode!")
# to manage, use the base image to run
self.running_image = str(self.cfg.base_image)
async def launch(self):
if self.running_image is None:
raise RuntimeError("runimage was not prepared!")
logging.debug("Launching VM which shall listen "
"on ssh port %d", self.ssh_port)
command = []
for part in shlex.split(self.cfg.command):
part = part.replace("{IMAGENAME}", str(self.running_image))
part = part.replace("{SSHPORT}", str(self.ssh_port))
command.append(part)
self.process = await asyncio.create_subprocess_exec(
*command,
stdin=subprocess.PIPE,
stdout=None,
stderr=None
)
self.process.stdin.close()
async def is_running(self):
if self.process:
return self.process.returncode is None
return False
async def wait_for_shutdown(self, timeout):
if not self.process:
return
try:
await asyncio.wait_for(self.process.wait(), timeout)
return True
except asyncio.TimeoutError:
logging.warning("shutdown wait timed out.")
return False
async def terminate(self):
if not self.process:
return
if self.process.returncode is not None:
return
try:
self.process.terminate()
await asyncio.wait_for(self.process.wait(), timeout=10)
except asyncio.TimeoutError:
self.process.kill()
await self.process.wait()
self.process = None
async def cleanup(self):
if not self.manage and self.running_image is not None:
try:
os.unlink(str(self.running_image))
except FileNotFoundError:
pass