Last active
August 31, 2017 08:01
-
-
Save bboozzoo/01b8b09d16462db11766083633bfa632 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from __future__ import print_function | |
import subprocess | |
import os | |
import signal | |
import time | |
MENDER_QEMU_PATH = os.path.join(os.path.dirname(__file__), "mender-qemu") | |
def set_new_pg(): | |
try: | |
print('new process', os.getpid(),'group', os.getpgrp(), 'session', os.getsid(0)) | |
os.setsid() | |
print('current group', os.getpgrp(), 'session', os.getsid(0)) | |
except Exception as exc: | |
print(exc) | |
class ProcessGroupPopen(subprocess.Popen): | |
"""Wrapper for subprocess.Popen that starts the underlying process in a | |
separate process group""" | |
def __init__(self, *args, **kwargs): | |
# for Python > 3.2 it's enough to set start_new_session=True | |
super(ProcessGroupPopen, self).__init__(*args, | |
preexec_fn=set_new_pg, | |
**kwargs) | |
def __signal(self, sig): | |
os.killpg(self.pid, sig) | |
def terminate(self): | |
self.__signal(signal.SIGTERM) | |
def kill(self): | |
self.__signal(signal.SIGKILL) | |
def start_qemu(qenv=None): | |
"""Start qemu and return a subprocess.Popen object corresponding to a running | |
qemu process. `qenv` is a dict of environment variables that will be added | |
to `subprocess.Popen(..,env=)`. | |
Once qemu is stated, a connection over ssh will attempted, so the returned | |
process is actually a qemu instance with fully booted guest os. | |
The helper uses `meta-mender-qemu/scripts/mender-qemu` to start qemu, thus | |
you can use `VEXPRESS_IMG`, `QEMU_DRIVE` and other environment variables to | |
override the default behavior. | |
""" | |
env = dict(os.environ) | |
if qenv: | |
env.update(qenv) | |
print('starting process') | |
# proc = subprocess.Popen([MENDER_QEMU_PATH, "-snapshot"], | |
# preexec_fn=set_new_pg, | |
# env=env) | |
proc = ProcessGroupPopen([MENDER_QEMU_PATH, "-snapshot"], | |
env=env) | |
print('started, wait 30s before terminating') | |
time.sleep(30) | |
print('terminating') | |
proc.terminate() | |
proc.wait() | |
if __name__ == '__main__': | |
start_qemu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment