forked from myugan/firecracker-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocess.py
More file actions
438 lines (366 loc) · 15.4 KB
/
process.py
File metadata and controls
438 lines (366 loc) · 15.4 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
431
432
433
434
435
436
437
438
import os
import time
import psutil
import subprocess
from datetime import datetime
from firecracker.logger import Logger
from firecracker.config import MicroVMConfig
from firecracker.exceptions import ProcessError
from tenacity import (
retry,
stop_after_delay,
wait_fixed,
retry_if_exception_type,
stop_after_attempt,
)
class ProcessManager:
"""Manages process-related operations for Firecracker microVMs."""
def __init__(self, verbose: bool = False, level: str = "INFO"):
self._logger = Logger(level=level, verbose=verbose)
self._config = MicroVMConfig()
self._config.verbose = verbose
def start(self, id: str, args: list) -> int:
"""Start a Firecracker process.
Args:
id (str): The ID of the Firecracker VM
args (list): List of command arguments
Returns:
int: Process ID if successful
Raises:
ProcessError: If process fails to start or becomes defunct
"""
try:
cmd = [self._config.binary_path] + args
log_path = f"{self._config.data_path}/{id}/firecracker.log"
pid_path = f"{self._config.data_path}/{id}/firecracker.pid"
# Get parent's process group
parent_pgid = os.getpgid(0)
process = subprocess.Popen(
cmd,
stdout=open(log_path, "w"),
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL,
# Explicitly set to parent's group
preexec_fn=lambda: os.setpgid(0, parent_pgid),
)
time.sleep(0.5)
if process.poll() is not None:
raise ProcessError("Firecracker process exited during startup")
proc = psutil.Process(process.pid)
if proc.status() == psutil.STATUS_ZOMBIE:
raise ProcessError("Firecracker process became defunct")
try:
proc.wait(timeout=1)
except psutil.TimeoutExpired:
pass
except psutil.NoSuchProcess:
raise ProcessError("Firecracker process disappeared during startup")
with open(pid_path, "w") as f:
f.write(str(process.pid))
if self._logger.verbose:
self._logger.debug(
f"Firecracker process started with PID: {process.pid}"
)
return process.pid
except Exception as e:
raise ProcessError(f"Failed to start Firecracker: {str(e)}")
@retry(
stop=stop_after_delay(3),
wait=wait_fixed(0.5),
retry=retry_if_exception_type(ProcessError),
)
def is_running(self, id: str) -> bool:
"""Check if Firecracker is running."""
try:
if os.path.exists(f"{self._config.data_path}/{id}/firecracker.pid"):
with open(f"{self._config.data_path}/{id}/firecracker.pid", "r") as f:
pid = int(f.read().strip())
try:
os.kill(pid, 0)
if self._logger.verbose:
self._logger.debug(f"Firecracker is running with PID: {pid}")
return True
except OSError:
if self._logger.verbose:
self._logger.info("Firecracker is not running (stale PID file)")
os.remove(f"{self._config.data_path}/{id}/firecracker.pid")
return False
else:
if self._logger.verbose:
self._logger.info("Firecracker is not running")
return False
except Exception as e:
if self._logger.verbose:
self._logger.error(f"Error checking status: {e}")
return False
@retry(
stop=stop_after_attempt(5),
wait=wait_fixed(1),
retry=retry_if_exception_type((ProcessError, OSError)),
)
def stop(self, id: str) -> bool:
"""Stop Firecracker with retry mechanism.
Args:
id (str): The ID of the Firecracker VM
Returns:
bool: True if successfully stopped, False otherwise
Raises:
ProcessError: If process fails to stop after retries
"""
try:
if os.path.exists(f"{self._config.data_path}/{id}/firecracker.pid"):
with open(f"{self._config.data_path}/{id}/firecracker.pid", "r") as f:
original_pid = int(f.read().strip())
try:
# Try to stop using the PID from file
if self._try_stop_process(original_pid, id):
self._cleanup_files(id)
return True
except ProcessError as e:
if self._logger.verbose:
self._logger.warn(
f"Failed to stop Firecracker (1st attempt): {e}"
)
# If PID-based stop failed, search for actual running process
if self._logger.verbose:
self._logger.info(
f"PID {original_pid} not found, searching for running Firecracker process for VM {id}"
)
actual_pid = self._find_running_process(id)
if actual_pid:
if self._logger.verbose:
self._logger.info(
f"Found running Firecracker process {actual_pid} for VM {id}"
)
if self._try_stop_process(actual_pid, id):
self._cleanup_files(id)
return True
else:
if self._logger.verbose:
self._logger.info(
f"No running Firecracker process found for VM {id}"
)
# Clean up files even if no process found
self._cleanup_files(id)
return True
else:
# Clean up files even if no process found
self._cleanup_files(id)
if self._logger.verbose:
self._logger.info("Firecracker is not running (no PID file)")
return False
except Exception as e:
if self._logger.verbose:
self._logger.error(f"Error stopping Firecracker: {e}")
raise ProcessError(f"Failed to stop Firecracker {id}: {str(e)}")
def _try_stop_process(self, pid: int, id: str) -> bool:
"""Try to stop a specific process by PID.
Args:
pid (int): Process ID to stop
id (str): VM ID for logging
Returns:
bool: True if process was successfully stopped
Raises:
ProcessError: If process fails to stop
"""
def wait_for_process_death(pid: int, timeout: float = 5.0) -> bool:
"""Wait for process to die with timeout."""
start_time = time.time()
while time.time() - start_time < timeout:
try:
os.kill(pid, 0) # Check if process exists
time.sleep(1) # Wait 1 second before next check
except OSError as e:
if e.errno == 3: # ESRCH - No such process
return True # Process is dead
else:
raise ProcessError(f"Error checking process {pid}: {e}")
return False # Timeout reached
try:
# First check if process exists
os.kill(pid, 0)
except OSError as e:
if e.errno == 3: # ESRCH - No such process
if self._logger.verbose:
self._logger.info(f"Firecracker process {pid} already terminated")
return True
else:
raise ProcessError(f"Failed to check process {pid}: {e}")
# Process exists, try graceful shutdown first
try:
os.kill(pid, 15) # SIGTERM
if self._logger.verbose:
self._logger.debug(
f"Sent SIGTERM to process {pid}, waiting for termination..."
)
# Wait for process to die after SIGTERM
if wait_for_process_death(pid, timeout=2):
if self._logger.verbose:
self._logger.info(
f"Firecracker process {pid} terminated after SIGTERM"
)
return True
else:
if self._logger.verbose:
self._logger.warn(
f"Process {pid} did not terminate after SIGTERM, using SIGKILL"
)
except OSError as e:
if e.errno == 3: # ESRCH - No such process
if self._logger.verbose:
self._logger.info(
f"Firecracker process {pid} terminated after SIGTERM"
)
return True
else:
raise ProcessError(f"Failed to send SIGTERM to process {pid}: {e}")
# Process still running after SIGTERM, try force kill
try:
os.kill(pid, 9) # SIGKILL
if self._logger.verbose:
self._logger.debug(
f"Sent SIGKILL to process {pid}, waiting for termination..."
)
# Wait for process to die after SIGKILL
if wait_for_process_death(pid, timeout=5):
if self._logger.verbose:
self._logger.info(
f"Firecracker process {pid} force killed with SIGKILL"
)
return True
else:
raise ProcessError(
f"Firecracker process {pid} still running after SIGKILL timeout"
)
except OSError as e:
if e.errno == 3: # ESRCH - No such process
if self._logger.verbose:
self._logger.info(
f"Firecracker process {pid} terminated after SIGKILL"
)
return True
else:
raise ProcessError(f"Failed to kill process {pid}: {e}")
def _find_running_process(self, id: str) -> int:
"""Find the actual running Firecracker process for a given VM ID.
Args:
id (str): The VM ID to search for
Returns:
int: Process ID if found, None otherwise
"""
try:
socket_path = f"{self._config.data_path}/{id}/firecracker.socket"
for pid in psutil.pids():
try:
proc = psutil.Process(pid)
if proc.name() == "firecracker":
cmdline = proc.cmdline()
if cmdline and len(cmdline) > 1:
# Check if this process uses the same socket path
for i, arg in enumerate(cmdline):
if arg == "--api-sock" and i + 1 < len(cmdline):
if cmdline[i + 1] == socket_path:
return pid
except (
psutil.NoSuchProcess,
psutil.AccessDenied,
psutil.ZombieProcess,
):
continue
except Exception as e:
if self._logger.verbose:
self._logger.warn(f"Error searching for running process: {e}")
return None
def _cleanup_files(self, id: str):
"""Clean up PID and socket files for a VM.
Args:
id (str): The VM ID
"""
# Clean up PID file
pid_file = f"{self._config.data_path}/{id}/firecracker.pid"
if os.path.exists(pid_file):
try:
os.remove(pid_file)
if self._logger.verbose:
self._logger.debug(f"Removed PID file for VM {id}")
except OSError as e:
if self._logger.verbose:
self._logger.warn(f"Failed to remove PID file: {e}")
# Clean up socket file
socket_path = f"{self._config.data_path}/{id}/firecracker.socket"
if os.path.exists(socket_path):
try:
os.remove(socket_path)
if self._logger.verbose:
self._logger.debug(f"Removed socket file: {socket_path}")
except OSError as e:
if self._logger.verbose:
self._logger.warn(f"Failed to remove socket file: {e}")
def get_pid(self, id: str) -> tuple:
"""Get the PID of the Firecracker process.
Args:
id (str): The ID of the Firecracker VM
Returns:
tuple: (pid, create_time) if process is found and running
Raises:
ProcessError: If the process is not found or not running
"""
try:
pid_file = f"{self._config.data_path}/{id}/firecracker.pid"
if not os.path.exists(pid_file):
raise ProcessError(f"No PID file found for VM {id}")
with open(pid_file, "r") as f:
pid = int(f.read().strip())
try:
process = psutil.Process(pid)
if not process.is_running():
os.remove(pid_file)
raise ProcessError(f"Firecracker process {pid} is not running")
if process.name() != "firecracker":
os.remove(pid_file)
raise ProcessError(f"Process {pid} is not a Firecracker process")
create_time = datetime.fromtimestamp(process.create_time()).strftime(
"%Y-%m-%d %H:%M:%S"
)
if self._logger.verbose:
self._logger.debug(
f"Found Firecracker process {pid} created at {create_time}"
)
return pid, create_time
except psutil.NoSuchProcess:
os.remove(pid_file)
raise ProcessError(f"Firecracker process {pid} is not running")
except psutil.AccessDenied:
raise ProcessError(f"Access denied to process {pid}")
except psutil.TimeoutExpired:
raise ProcessError(f"Timeout while checking process {pid}")
except Exception as e:
raise ProcessError(f"Failed to get Firecracker PID: {str(e)}")
def get_pids(self) -> list:
"""
Get all PIDs of the Firecracker processes that have --api-sock parameter.
Returns:
list: List of process IDs (integers)
"""
pid_list = []
try:
for pid in psutil.pids():
try:
proc = psutil.Process(pid)
if proc.name() == "firecracker":
cmdline = proc.cmdline()
if cmdline and len(cmdline) > 1 and "--api-sock" in cmdline:
pid_list.append(pid)
except (
psutil.NoSuchProcess,
psutil.AccessDenied,
psutil.ZombieProcess,
):
continue
except Exception as e:
raise ProcessError(f"Failed to get Firecracker processes: {str(e)}")
return pid_list
@staticmethod
def wait_process_running(process: psutil.Process):
"""Wait for a process to run."""
assert process.is_running()