diff options
authorChen Qi <>2018-06-07 15:52:46 +0800
committerRichard Purdie <>2018-06-17 08:37:49 +0100
commit5035737cdb0c65d1ff523e3e089608b6e1606989 (patch)
parent7fe7a1229daca9b97604050dde507c8a5355d77d (diff)
runqemu: add SIGTERM handler to make sure things are cleaned up
Add SIGTERM handler so that runqemu could clean things up correctly when receving such signal. This problem was originally observed when running testimage. On some hosts, after running testimage task, the user has to manually operate on the tap interface (e.g. `sudo ip link del tap0') in order for the next runqemu command to launch successfully. The problem is about runqemu, SIGTERM and network manager on the host. In testimage task, the runqemu process will receive SIGTERM. In such situation, its cleanup() function is not run, resulting in tap interface not cleaned up. On some hosts, the network manager will bring down the tap interface automatically, thus this problem. I saw this problem on Fedora21. I think we'd better just clean up the tap interface ourselves. So this patch adds to runqemu a SIGTERM handler, in which the actual qemu process is terminated and other things cleaned up. Signed-off-by: Chen Qi <> Signed-off-by: Richard Purdie <>
1 files changed, 22 insertions, 1 deletions
diff --git a/scripts/runqemu b/scripts/runqemu
index d998494063..de42d0f323 100755
--- a/scripts/runqemu
+++ b/scripts/runqemu
@@ -27,6 +27,7 @@ import fcntl
import shutil
import glob
import configparser
+import signal
class RunQemuError(Exception):
"""Custom exception to raise on known errors."""
@@ -233,6 +234,10 @@ class BaseConfig(object):
# slirp qemus are running.
self.mac_tap = "52:54:00:12:34:"
self.mac_slirp = "52:54:00:12:35:"
+ # pid of the actual qemu process
+ self.qemupid = None
+ # avoid cleanup twice
+ self.cleaned = False
def acquire_lock(self, error=True):
logger.debug("Acquiring lockfile %s..." % self.lock)
@@ -1200,10 +1205,18 @@ class BaseConfig(object):
cmd = "%s %s" % (self.qemu_opt, kernel_opts)'Running %s\n' % cmd)
process = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
+ self.qemupid =
if process.wait():
logger.error("Failed to run qemu: %s",
def cleanup(self):
+ if self.cleaned:
+ return
+ # avoid dealing with SIGTERM when cleanup function is running
+ signal.signal(signal.SIGTERM, signal.SIG_IGN)
+"Cleaning up")
if self.cleantap:
cmd = 'sudo %s %s %s' % (self.qemuifdown, self.tap, self.bindir_native)
logger.debug('Running %s' % cmd)
@@ -1227,6 +1240,8 @@ class BaseConfig(object):
shutil.rmtree('%s.pseudo_state' % self.rootfs)
+ self.cleaned = True
def load_bitbake_env(self, mach=None):
if self.bitbake_e:
@@ -1282,6 +1297,13 @@ def main():
return 0
config = BaseConfig()
+ def sigterm_handler(signum, frame):
+"SIGTERM received")
+ os.kill(config.qemupid, signal.SIGTERM)
+ config.cleanup()
+ signal.signal(signal.SIGTERM, sigterm_handler)
@@ -1300,7 +1322,6 @@ def main():
return 1
- print("Cleanup")
if __name__ == "__main__":