1#
2# Copyright (C) 2016 Intel Corporation
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import sys
9import signal
10import time
11import glob
12import subprocess
13from collections import defaultdict
14
15from .ssh import OESSHTarget
16from oeqa.utils.qemurunner import QemuRunner
17
18supported_fstypes = ['ext3', 'ext4', 'cpio.gz', 'wic']
19
20class OEQemuTarget(OESSHTarget):
21    def __init__(self, logger, server_ip, timeout=300, user='root',
22            port=None, machine='', rootfs='', kernel='', kvm=False, slirp=False,
23            dump_dir='', display='', bootlog='',
24            tmpdir='', dir_image='', boottime=60, serial_ports=2,
25            boot_patterns = defaultdict(str), ovmf=False, tmpfsdir=None, **kwargs):
26
27        super(OEQemuTarget, self).__init__(logger, None, server_ip, timeout,
28                user, port)
29
30        self.server_ip = server_ip
31        self.server_port = 0
32        self.machine = machine
33        self.rootfs = rootfs
34        self.kernel = kernel
35        self.kvm = kvm
36        self.ovmf = ovmf
37        self.use_slirp = slirp
38        self.boot_patterns = boot_patterns
39        self.dump_dir = dump_dir
40        self.bootlog = bootlog
41
42        self.runner = QemuRunner(machine=machine, rootfs=rootfs, tmpdir=tmpdir,
43                                 deploy_dir_image=dir_image, display=display,
44                                 logfile=bootlog, boottime=boottime,
45                                 use_kvm=kvm, use_slirp=slirp, dump_dir=dump_dir, logger=logger,
46                                 serial_ports=serial_ports, boot_patterns = boot_patterns,
47                                 use_ovmf=ovmf, tmpfsdir=tmpfsdir)
48
49    def start(self, params=None, extra_bootparams=None, runqemuparams=''):
50        if self.use_slirp and not self.server_ip:
51            self.logger.error("Could not start qemu with slirp without server ip - provide 'TEST_SERVER_IP'")
52            raise RuntimeError("FAILED to start qemu - check the task log and the boot log")
53        if self.runner.start(params, extra_bootparams=extra_bootparams, runqemuparams=runqemuparams):
54            self.ip = self.runner.ip
55            if self.use_slirp:
56                target_ip_port = self.runner.ip.split(':')
57                if len(target_ip_port) == 2:
58                    target_ip = target_ip_port[0]
59                    port = target_ip_port[1]
60                    self.ip = target_ip
61                    self.ssh = self.ssh + ['-p', port]
62                    self.scp = self.scp + ['-P', port]
63                else:
64                    self.logger.error("Could not get host machine port to connect qemu with slirp, ssh will not be "
65                                      "able to connect to qemu with slirp")
66            if self.runner.server_ip:
67                self.server_ip = self.runner.server_ip
68        else:
69            self.stop()
70            # Display the first 20 lines of top and
71            # last 20 lines of the bootlog when the
72            # target is not being booted up.
73            topfile = glob.glob(self.dump_dir + "/*_qemu/host_*_top")
74            msg = "\n\n===== start: snippet =====\n\n"
75            for f in topfile:
76                msg += "file: %s\n\n" % f
77                with open(f) as tf:
78                    for x in range(20):
79                        msg += next(tf)
80            msg += "\n\n===== end: snippet =====\n\n"
81            blcmd = ["tail", "-20", self.bootlog]
82            msg += "===== start: snippet =====\n\n"
83            try:
84                out = subprocess.check_output(blcmd, stderr=subprocess.STDOUT, timeout=1).decode('utf-8')
85                msg += "file: %s\n\n" % self.bootlog
86                msg += out
87            except (subprocess.CalledProcessError, subprocess.TimeoutExpired, FileNotFoundError) as err:
88                msg += "Error running command: %s\n%s\n" % (blcmd, err)
89            msg += "\n\n===== end: snippet =====\n"
90
91            raise RuntimeError("FAILED to start qemu - check the task log and the boot log %s" % (msg))
92
93    def stop(self):
94        self.runner.stop()
95