Patchwork [RFC,5/8] lib/oeqa/utils/qemurunner.py: class to handle qemu instance

login
register
mail settings
Submitter Stanacar, StefanX
Date June 28, 2013, 10:04 a.m.
Message ID <3047815ee6268db95c3916960eb31508fb7729f6.1372413711.git.stefanx.stanacar@intel.com>
Download mbox | patch
Permalink /patch/52545/
State New
Headers show

Comments

Stanacar, StefanX - June 28, 2013, 10:04 a.m.
From: Radu Moisan <radu.moisan@intel.com>

Handles qemu instances (launch, kill, restart, serial connection, logging)
Launch is blocking until login prompt and returns to the task. A qemu
serial connection is used to save the boot log and get the ip from the image.
Changed runqemu script not to error out when using custom serial option.

Signed-off-by: Radu Moisan <radu.moisan@intel.com>
Signed-off-by: Stefan Stanacar <stefanx.stanacar@intel.com>
---
 meta/lib/oeqa/utils/qemurunner.py | 160 ++++++++++++++++++++++++++++++++++++++
 scripts/runqemu                   |   2 +-
 2 files changed, 161 insertions(+), 1 deletion(-)
 create mode 100644 meta/lib/oeqa/utils/qemurunner.py
Colin Walters - Aug. 5, 2013, 7:50 p.m.
On Fri, 2013-06-28 at 13:04 +0300, Stefan Stanacar wrote:

> +        self.streampath = '/tmp/qemuconnection.%s' % os.getpid()

That's a security problem on shared machines.

> +                bb.note("Reached login banner")
> +                console.write("root\n")
> +                (index, match, text) = console.expect([r"(root@[\w-]+:~#)"],10)

So I forget if I've mentioned this here, but what I do for the
gnome-ostree testing is at boot time, use a qcow2 overlay disk to write
a custom systemd service that exports the journal over a virtio-serial
channel.  Then I look for specific MESSAGE_IDs in the journal.

This is extremely reliable, no parsing of log messages etc.

See:
https://rwmj.wordpress.com/2013/07/19/half-baked-ideas-ocr-vm-console-to-diagnose-state-and-errors/
Stanacar, StefanX - Aug. 29, 2013, 11:18 a.m.
Hi Colin,

On Mon, 2013-08-05 at 21:50 +0200, Colin Walters wrote:
> On Fri, 2013-06-28 at 13:04 +0300, Stefan Stanacar wrote:
> 
> > +        self.streampath = '/tmp/qemuconnection.%s' % os.getpid()
> 
> That's a security problem on shared machines.

I know this is a late reply, sorry, I missed the email back then.
This has been changed since then to something else (but the reasons were
different)
http://git.yoctoproject.org/cgit/cgit.cgi/poky/commit/?id=0ba78c1162bb125850a0ee504ca6fbe5bf21247f
That's a tcp socket localhost only now, random high port (calls bind
with 127.0.0.1, 0 so the os chooses the port).


> 
> > +                bb.note("Reached login banner")
> > +                console.write("root\n")
> > +                (index, match, text) = console.expect([r"(root@[\w-]+:~#)"],10)
> 
> So I forget if I've mentioned this here, but what I do for the
> gnome-ostree testing is at boot time, use a qcow2 overlay disk to write
> a custom systemd service that exports the journal over a virtio-serial
> channel.  Then I look for specific MESSAGE_IDs in the journal.
> 
> This is extremely reliable, no parsing of log messages etc.
> 

That sounds really cool, nice job! 
I might be wrong but doesn't that require virtio support in the target
kernel (which is something we can't expect to have)?

Cheers,
Stefan

> See:
> https://rwmj.wordpress.com/2013/07/19/half-baked-ideas-ocr-vm-console-to-diagnose-state-and-errors/
> 
>
Colin Walters - Aug. 29, 2013, 11:35 a.m.
On Thu, 2013-08-29 at 11:18 +0000, Stanacar, StefanX wrote:

> That sounds really cool, nice job! 
> I might be wrong but doesn't that require virtio support in the target
> kernel (which is something we can't expect to have)?

Yes...although you could do the same over TCP, it'd just require more
gymnastics on the host and guest side.   The driver is just 12k here on
this RHEL6 box; I have it built in to the kernel for my OE target so I
don't know how large it is offhand there.

Patch

diff --git a/meta/lib/oeqa/utils/qemurunner.py b/meta/lib/oeqa/utils/qemurunner.py
new file mode 100644
index 0000000..e201e25
--- /dev/null
+++ b/meta/lib/oeqa/utils/qemurunner.py
@@ -0,0 +1,160 @@ 
+import subprocess
+import optparse
+import sys
+import os
+import time
+import signal
+import re
+import bb
+from oeqa.utils.oetelnetlib import oeTelnet
+
+class QemuRunner:
+
+    def __init__(self, machine, rootfs, display = None, tmpdir = None, logfile = None):
+        # Popen object
+        self.runqemu = None
+
+        self.machine = machine
+        self.rootfs = rootfs
+
+        self.streampath = '/tmp/qemuconnection.%s' % os.getpid()
+        self.qemuparams = 'bootparams="console=ttyS0" qemuparams="-snapshot -serial unix:%s,server,nowait"' % self.streampath
+        self.qemupid = None
+        self.ip = None
+
+        self.display = display
+        self.tmpdir = tmpdir
+        self.logfile = logfile
+
+    def launch(self, qemuparams = None):
+
+        if qemuparams:
+            self.qemuparams = self.qemuparams[:-1] + " " + qemuparams + " " + '\"'
+
+        if self.display:
+            os.environ["DISPLAY"] = self.display
+        if not os.path.exists(self.rootfs):
+            bb.error("Invalid rootfs %s" % self.rootfs)
+            return False
+        if not os.path.exists(self.tmpdir):
+            bb.error("Invalid TMPDIR path %s" % self.tmpdir)
+            return False
+        else:
+            os.environ["OE_TMPDIR"] = self.tmpdir
+
+        launch_cmd = 'runqemu %s %s %s' % (self.machine, self.rootfs, self.qemuparams)
+        self.runqemu = subprocess.Popen(launch_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,preexec_fn=os.setpgrp)
+
+        bb.note("runqemu started, pid is %s" % self.runqemu.pid)
+        # wait at most 30 seconds until qemu pid appears
+        bb.note("waiting at most 30 seconds for qemu pid")
+        endtime = time.time() + 30
+        while not self.is_alive() and time.time() < endtime:
+            time.sleep(0.5)
+
+        if self.is_alive():
+            bb.note("qemu started - qemu procces pid is %s" % self.qemupid)
+
+            console = oeTelnet(self.streampath, self.logfile)
+            bb.note("Waiting at most 120 seconds for login banner")
+            (match, text) = console.read_all_timeout("login:", 120)
+
+            if match:
+                bb.note("Reached login banner")
+                console.write("root\n")
+                (index, match, text) = console.expect([r"(root@[\w-]+:~#)"],10)
+                if not match:
+                    bb.note("Couldn't get prompt, all I got was:\n%s" % match.group(0))
+                    return False
+                console.write("ip addr show eth0 | sed -n '3p' | awk '{ print $2 }' | cut -f 1 -d \"/\"\n")
+                (index, match, text) = console.expect([r"((?:[0-9]{1,3}\.){3}[0-9]{1,3})"],10)
+                console.close()
+                if match:
+                    self.ip = match.group(0)
+                    bb.note("Ip found: %s" % self.ip)
+                else:
+                    bb.note("Couldn't determine ip, all I got was:\n%s" % text)
+                    return False
+            else:
+                console.close()
+                bb.note("Target didn't reached login boot in 120 seconds")
+                lines = "\n".join(text.splitlines()[-5:])
+                bb.note("Last 5 lines of text:\n%s" % lines)
+                bb.note("Check full boot log: %s" % self.logfile)
+                return False
+        else:
+            bb.note("Qemu pid didn't appeared in 30 seconds")
+            self.runqemu.terminate()
+            self.runqemu.kill()
+            bb.note("Output from runqemu: %s " % self.runqemu.stdout.read())
+            self.runqemu.stdout.close()
+            return False
+
+        return self.is_alive()
+
+
+    def kill(self):
+        if self.runqemu:
+            os.kill(-self.runqemu.pid,signal.SIGTERM)
+        self.qemupid = None
+        self.ip = None
+        if os.path.exists(self.streampath):
+            os.remove(self.streampath)
+
+    def restart(self, qemuparams = None):
+        if self.is_alive():
+            self.kill()
+        bb.note("Qemu Restart required...")
+        return self.launch(qemuparams)
+
+    def is_alive(self):
+        qemu_child = self.find_child(str(self.runqemu.pid))
+        if qemu_child:
+            self.qemupid = qemu_child[0]
+            return os.path.exists("/proc/" + str(self.qemupid))
+        return False
+
+    def find_child(self,parent_pid):
+        #
+        # Walk the process tree from the process specified looking for a qemu-system. Return its [pid'cmd]
+        #
+        ps = subprocess.Popen(['ps', 'axww', '-o', 'pid,ppid,command'], stdout=subprocess.PIPE).communicate()[0]
+        processes = ps.split('\n')
+        nfields = len(processes[0].split()) - 1
+        pids = {}
+        commands = {}
+        for row in processes[1:]:
+            data = row.split(None, nfields)
+            if len(data) != 3:
+                continue
+            if data[1] not in pids:
+                pids[data[1]] = []
+
+            pids[data[1]].append(data[0])
+            commands[data[0]] = data[2]
+
+        if parent_pid not in pids:
+            sys.stderr.write("No children found matching %s\n" % parent_pid)
+            return []
+
+        parents = []
+        newparents = pids[parent_pid]
+        while newparents:
+            next = []
+            for p in newparents:
+                if p in pids:
+                    for n in pids[p]:
+                        if n not in parents and n not in next:
+                            next.append(n)
+                if p not in parents:
+                    parents.append(p)
+                    newparents = next
+        #print "Children matching %s:" % str(parents)
+        for p in parents:
+            # Need to be careful here since runqemu-internal runs "ldd qemu-system-xxxx"
+            # Also, old versions of ldd (2.11) run "LD_XXXX qemu-system-xxxx"
+            basecmd = commands[p].split()[0]
+            basecmd = os.path.basename(basecmd)
+            if "qemu-system" in basecmd and "192.168" in commands[p]:
+                return [int(p),commands[p]]
+
diff --git a/scripts/runqemu b/scripts/runqemu
index f2eb2e1..406092b 100755
--- a/scripts/runqemu
+++ b/scripts/runqemu
@@ -156,7 +156,7 @@  while true; do
             serial_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-serial\)'`
             kvm_option=`expr "$SCRIPT_QEMU_EXTRA_OPT" : '.*\(-enable-kvm\)'`
             [ ! -z "$serial_option" -o ! -z "$kvm_option" ] && \
-                error "Please use simplified serial or kvm options instead"
+                echo "Please use simplified serial or kvm options instead"
             ;;
         "bootparams="*)
             SCRIPT_KERNEL_OPT="$SCRIPT_KERNEL_OPT ${arg##bootparams=}"