From patchwork Wed Jun 19 19:40:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 167255 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp1127194ilk; Wed, 19 Jun 2019 12:43:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqz7cLqjGNtXWB8702jNnwmL1X1Yly1TJnCrbyKPeGWK3Krmtj1Q29f8lwhcg+EYfrwqRi7a X-Received: by 2002:a50:cc47:: with SMTP id n7mr14719565edi.58.1560973408489; Wed, 19 Jun 2019 12:43:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560973408; cv=none; d=google.com; s=arc-20160816; b=IXAE625BjPRkbG02ryfwdb0XATfUrBjgbCXXVR2RQyAAm9PwTw9v9yEFsD6FiFdhWd 13I7Cmex9DgIUzYkaHrIt3j9K3zy+Y/K9EWCOKliUamxx8Kmoz61pyyhfJ8KvVAAw6ac JOy4vPgNClmjT61Wr4lQmd9QjM9wyM7LNeSeXib/CYbwty0xQ5cgZRTA0KnWmB9t0RTb kRdM73vHSMIKAwp8kd0X8VcGGvFMwF0Indt8TZiMXzqgj7jems/YSNxA0NTK1bzIS52j 2U0KOu/Kjo4c+JWdRoTS6DB+er0lEmMfLZQzNT9yuORtKsjlyFdT5OY1hDWUKb3nSAi1 JZvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=L6XVojZ9d06YYw7Qa0xt+OkZV+Iz6HolpHL92sjbbUE=; b=g2cbKijNDjpiM6OkHG3iuR7rfQ7yPkhg0mUBgr8QSN+iqvzuU76xLF+v+mmuwSE9pS OJgK1XgRGXTRRcrhwXK7JAxYzQdH6VqyHm68iMIZj90LsoOGmM+ndOscnYYYFM8p37iH 01/HDskLEjmb2j4yENDrrIdvORKbsUIlvX1RgjZeXlMB6M4WN5qfvky8IAeoCxzhBZmC TXLdjfwDU0ZrYpGZBFug57RrWfwhMOGcOP/42AOQz7jXIDKKgGnhbGxCyRaHOlI9d85N iA/9Q+JF4ko8qrik829I+BPF6iAnPr+2KhTldAHrdr1A8DG8G6X2d8lRdHJg5QpIREMd mOSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=SMXD36Bf; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id q55si15253005eda.257.2019.06.19.12.43.28 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Jun 2019 12:43:28 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=SMXD36Bf; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:41434 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hdgUR-00015C-Dr for patch@linaro.org; Wed, 19 Jun 2019 15:43:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53976) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hdgRf-0006Aa-4E for qemu-devel@nongnu.org; Wed, 19 Jun 2019 15:40:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hdgRc-0000BF-P3 for qemu-devel@nongnu.org; Wed, 19 Jun 2019 15:40:34 -0400 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]:35085) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hdgRc-00008r-I1 for qemu-devel@nongnu.org; Wed, 19 Jun 2019 15:40:32 -0400 Received: by mail-wm1-x329.google.com with SMTP id c6so723874wml.0 for ; Wed, 19 Jun 2019 12:40:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=L6XVojZ9d06YYw7Qa0xt+OkZV+Iz6HolpHL92sjbbUE=; b=SMXD36BfIHTjJWHy9gfisDuwrT/MWMlDBAWWvldMH67j0POs+IOigPjdaJb1R8c/yV 1pnrQiqU/YcilMGhjQ9ObG85L5cZalaVUvNvkoVHEkYqoZ/jUppI55jvWOWX1BXcC7nY iXnWD3IYZi7rw4bXLSTxFxLgt5fRANOmYr6EPWJ1+AAXGIWF8zGwV19Afi8FCS/IN0rZ PVQ7JQUWGRPkKQu+/+CJ36rT03gYCEV0T20S7zb04pX6k4rvvm90w1yNcrFBVfKidFpJ QDGd7XCGJEAAJ9egPvG85Q3MlobVtvXVCJkXppgarFpiHzm0icVkUwjVDeaJLM+x5+zR AyBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=L6XVojZ9d06YYw7Qa0xt+OkZV+Iz6HolpHL92sjbbUE=; b=oWdpqS6GKN8Y/AvfJSFmdGiZW/UwX3aMG39qMiZ1z0OII7ydcA/VQQcZ263MMIquQ4 wX5FLYJn/ty/5OGZC9xKjxHxK5C24CjtEVdcI+XcieGIFKzPJFdDMzRNhP5nZpiDRIAm E15DJLnBcRoGgJu0PxtaiAyj2pLjKNtzJcBl/6rSZ+SfD25FjyeIu9kGWT+VxNTpAanc sb++511A2K0vVSkh+6PoocSlDpKY31D6hWqXFNx061zWXwztxERhvOQW1hHhtd/N/EjN JkZI7ZKE+mu6RPFesE3H9F8+eo3Sw3rzrDOn+OH8i5lKu1hrMte2bKsalmo/k4FqLKcK Kbdg== X-Gm-Message-State: APjAAAU4n1mvwIN6MzdnmzRWlPZLxJfAaSvRPTQEaTlYZyxsA+KVTlm3 hgdOfraCkeFQk62lWat16JXpeQ== X-Received: by 2002:a1c:a848:: with SMTP id r69mr9323555wme.12.1560973229821; Wed, 19 Jun 2019 12:40:29 -0700 (PDT) Received: from zen.linaroharston ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id o8sm19763322wrj.71.2019.06.19.12.40.24 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 19 Jun 2019 12:40:27 -0700 (PDT) Received: from zen.linaroharston. (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 4687D1FF9A; Wed, 19 Jun 2019 20:40:22 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Wed, 19 Jun 2019 20:40:14 +0100 Message-Id: <20190619194021.8240-11-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190619194021.8240-1-alex.bennee@linaro.org> References: <20190619194021.8240-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::329 Subject: [Qemu-devel] [PATCH v1 10/17] tests/vm: serial console support helpers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , =?utf-8?q?Alex_Benn=C3=A9e?= , Thomas Huth , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Gerd Hoffmann Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Gerd Hoffmann Add a bunch of helpers to talk to the guest using the serial console. Also drop the hard-coded -serial parameter for the vm so QEMUMachine.set_console() actually works. Signed-off-by: Gerd Hoffmann Tested-by: Thomas Huth Reviewed-by: Philippe Mathieu-Daudé Tested-by: Philippe Mathieu-Daudé Message-Id: <20190617043858.8290-7-kraxel@redhat.com> [AJB: added tags] Signed-off-by: Alex Bennée --- tests/vm/basevm.py | 86 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) -- 2.20.1 diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py index f27178f3c7..592a344773 100755 --- a/tests/vm/basevm.py +++ b/tests/vm/basevm.py @@ -2,10 +2,11 @@ # # VM testing base class # -# Copyright 2017 Red Hat Inc. +# Copyright 2017-2019 Red Hat Inc. # # Authors: # Fam Zheng +# Gerd Hoffmann # # This code is licensed under the GPL version 2 or later. See # the COPYING file in the top-level directory. @@ -13,7 +14,9 @@ from __future__ import print_function import os +import re import sys +import socket import logging import time import datetime @@ -79,8 +82,7 @@ class BaseVM(object): "-cpu", "max", "-netdev", "user,id=vnet,hostfwd=:127.0.0.1:0-:22", "-device", "virtio-net-pci,netdev=vnet", - "-vnc", "127.0.0.1:0,to=20", - "-serial", "file:%s" % os.path.join(self._tmpdir, "serial.out")] + "-vnc", "127.0.0.1:0,to=20"] if vcpus and vcpus > 1: self._args += ["-smp", "%d" % vcpus] if kvm_available(self.arch): @@ -162,6 +164,8 @@ class BaseVM(object): logging.debug("QEMU args: %s", " ".join(args)) qemu_bin = os.environ.get("QEMU", "qemu-system-" + self.arch) guest = QEMUMachine(binary=qemu_bin, args=args) + guest.set_machine('pc') + guest.set_console() try: guest.launch() except: @@ -184,6 +188,82 @@ class BaseVM(object): raise Exception("Cannot find ssh port from 'info usernet':\n%s" % \ usernet_info) + def console_init(self, timeout = 120): + vm = self._guest + vm.console_socket.settimeout(timeout) + + def console_log(self, text): + for line in re.split("[\r\n]", text): + # filter out terminal escape sequences + line = re.sub("\x1b\[[0-9;?]*[a-zA-Z]", "", line) + line = re.sub("\x1b\([0-9;?]*[a-zA-Z]", "", line) + # replace unprintable chars + line = re.sub("\x1b", "", line) + line = re.sub("[\x00-\x1f]", ".", line) + line = re.sub("[\x80-\xff]", ".", line) + if line == "": + continue + # log console line + sys.stderr.write("con recv: %s\n" % line) + + def console_wait(self, expect): + vm = self._guest + output = "" + while True: + try: + chars = vm.console_socket.recv(1) + except socket.timeout: + sys.stderr.write("console: *** read timeout ***\n") + sys.stderr.write("console: waiting for: '%s'\n" % expect) + sys.stderr.write("console: line buffer:\n") + sys.stderr.write("\n") + self.console_log(output.rstrip()) + sys.stderr.write("\n") + raise + output += chars.decode("latin1") + if expect in output: + break + if "\r" in output or "\n" in output: + lines = re.split("[\r\n]", output) + output = lines.pop() + if self.debug: + self.console_log("\n".join(lines)) + if self.debug: + self.console_log(output) + + def console_send(self, command): + vm = self._guest + if self.debug: + logline = re.sub("\n", "", command) + logline = re.sub("[\x00-\x1f]", ".", logline) + sys.stderr.write("con send: %s\n" % logline) + for char in list(command): + vm.console_socket.send(char.encode("utf-8")) + time.sleep(0.01) + + def console_wait_send(self, wait, command): + self.console_wait(wait) + self.console_send(command) + + def console_ssh_init(self, prompt, user, pw): + sshkey_cmd = "echo '%s' > .ssh/authorized_keys\n" % SSH_PUB_KEY.rstrip() + self.console_wait_send("login:", "%s\n" % user) + self.console_wait_send("Password:", "%s\n" % pw) + self.console_wait_send(prompt, "mkdir .ssh\n") + self.console_wait_send(prompt, sshkey_cmd) + self.console_wait_send(prompt, "chmod 755 .ssh\n") + self.console_wait_send(prompt, "chmod 644 .ssh/authorized_keys\n") + + def console_sshd_config(self, prompt): + self.console_wait(prompt) + self.console_send("echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config\n") + for var in self.envvars: + self.console_wait(prompt) + self.console_send("echo 'AcceptEnv %s' >> /etc/ssh/sshd_config\n" % var) + + def print_step(self, text): + sys.stderr.write("### %s ...\n" % text) + def wait_ssh(self, seconds=300): starttime = datetime.datetime.now() endtime = starttime + datetime.timedelta(seconds=seconds)