guest_memory_dump_analysis.py 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
"""
Integrity test of a big guest vmcore, using the dump-guest-memory QMP
command and the "crash" utility.

:copyright: 2013 Red Hat, Inc.
:author: Laszlo Ersek <lersek@redhat.com>

Related RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=990118
"""

import logging
import os
import gzip
import threading

16 17 18
from aexpect import ShellCmdError


L
Lucas Meneghel Rodrigues 已提交
19 20 21
REQ_GUEST_MEM = 4096        # exact size of guest RAM required
REQ_GUEST_ARCH = "x86_64"    # the only supported guest arch
REQ_GUEST_DF = 6144        # minimum guest disk space required
22
#     after package installation
L
Lucas Meneghel Rodrigues 已提交
23 24
LONG_TIMEOUT = 10 * 60       # timeout for long operations
VMCORE_BASE = "vmcore"    # basename of the host-side file the
25 26 27 28
#     guest vmcore is written to, .gz
#     suffix will be appended. No
#     metacharacters or leading dashes
#     please.
L
Lucas Meneghel Rodrigues 已提交
29 30
VMCORE_FD_NAME = "vmcore_fd"  # fd identifier used in the monitor
CRASH_SCRIPT = "crash.cmd"  # guest-side filename of the minimal
31
# crash script
32

L
Lucas Meneghel Rodrigues 已提交
33

34
def run(test, params, env):
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    """
    Verify the vmcore written by dump-guest-memory by a big guest.

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def check_requirements(vm, session):
        """
        Check guest RAM size and guest architecture.

        :param vm: virtual machine.
        :param session: login shell session.
        :raise: error.TestError if the test is misconfigured.
        """
        mem_size = vm.get_memory_size()
        if (mem_size != REQ_GUEST_MEM):
X
Xu Han 已提交
52 53 54
            test.error("the guest must have %d MB RAM exactly "
                       "(current: %d MB)" % (REQ_GUEST_MEM,
                                             mem_size))
55 56
        arch = session.cmd("uname -m").rstrip()
        if (arch != REQ_GUEST_ARCH):
X
Xu Han 已提交
57 58
            test.error("this test only supports %s guests "
                       "(current: %s)" % (REQ_GUEST_ARCH, arch))
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    def install_kernel_debuginfo(vm, session, login_timeout):
        """
        In the guest, install a kernel debuginfo package that matches
        the running kernel.

        Debuginfo packages are available for the most recent kernels
        only, so this step may need a kernel upgrade and a corresponding
        VM reboot. Also, the "debuginfo-install" yum utility is not good
        enough for this, because its exit status doesn't seem to reflect
        any failure to find a matching debuginfo package. Only "yum
        install" seems to do that, and only if an individual package is
        requested.

        :param vm: virtual machine. Can be None if the caller demands a
                debuginfo package for the running kernel.
        :param session: login shell session.
        :param login_timeout: passed to vm.reboot() as timeout. Can be
                None if vm is None.
        :return: If the debuginfo package has been successfully
                installed, None is returned. If no debuginfo package
                matching the running guest kernel is available.
                If vm is None, an exception is raised; otherwise, the
                guest kernel is upgraded, and a new session is returned
                for the rebooted guest. In this case the next call to
                this function should succeed, using the new session and
                with vm=None.
        :raise: error.TestError (guest uname command failed),
                ShellCmdError (unexpected guest yum command failure),
                exceptions from vm.reboot().
        """
        def install_matching_debuginfo(session):
            try:
                guest_kernel = session.cmd("uname -r").rstrip()
X
Xu Han 已提交
93
            except ShellCmdError as details:
X
Xu Han 已提交
94
                test.error("guest uname command failed: %s" % details)
95 96 97 98 99 100 101 102
            return session.cmd("yum -y install --enablerepo='*debuginfo' "
                               "kernel-debuginfo-%s" % guest_kernel,
                               timeout=LONG_TIMEOUT)

        try:
            output = install_matching_debuginfo(session)
            logging.debug("%s", output)
            new_sess = None
X
Xu Han 已提交
103
        except ShellCmdError as details:
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
            if (vm is None):
                raise
            logging.info("failed to install matching debuginfo, "
                         "upgrading kernel")
            logging.debug("shell error was: %s", details)
            output = session.cmd("yum -y upgrade kernel",
                                 timeout=LONG_TIMEOUT)
            logging.debug("%s", output)
            new_sess = vm.reboot(session, timeout=login_timeout)
        return new_sess

    def install_crash(session):
        """
        Install the "crash" utility in the guest.

        :param session: login shell session.
        :raise: exceptions from session.cmd().
        """
        output = session.cmd("yum -y install crash")
        logging.debug("%s", output)

    def check_disk_space(session):
        """
        Check free disk space in the guest before uploading,
        uncompressing and analyzing the vmcore.

        :param session: login shell session.
        :raise: exceptions from session.cmd(); error.TestError if free
                space is insufficient.
        """
        output = session.cmd("rm -f -v %s %s.gz" % (VMCORE_BASE, VMCORE_BASE))
        logging.debug("%s", output)
        output = session.cmd("yum clean all")
        logging.debug("%s", output)
        output = session.cmd("LC_ALL=C df --portability --block-size=1M .")
        logging.debug("%s", output)
X
Xu Han 已提交
140
        df_megs = int(output.split()[10])
141
        if (df_megs < REQ_GUEST_DF):
X
Xu Han 已提交
142 143
            test.error("insufficient free disk space: %d < %d" %
                       (df_megs, REQ_GUEST_DF))
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

    def dump_and_compress(qmp_monitor, vmcore_host):
        """
        Dump the guest vmcore on the host side and compress it.

        Use the "dump-guest-memory" QMP command with paging=false. Start
        a new Python thread that compresses data from a file descriptor
        to a host file. Create a pipe and pass its writeable end to qemu
        for vmcore dumping. Pass the pipe's readable end (with full
        ownership) to the compressor thread. Track references to the
        file descriptions underlying the pipe end fds carefully.

        Compressing the vmcore on the fly, then copying it to the guest,
        then decompressing it inside the guest should be much faster
        than dumping and copying a huge plaintext vmcore, especially on
        rotational media.

        :param qmp_monitor: QMP monitor for the guest.
        :param vmcore_host: absolute pathname of gzipped destination
                file.
        :raise: all sorts of exceptions. No resources should be leaked.
        """
        def compress_from_fd(input_fd, gzfile):
            # Run in a separate thread, take ownership of input_fd.
            try:
                buf = os.read(input_fd, 4096)
                while (buf):
                    gzfile.write(buf)
                    buf = os.read(input_fd, 4096)
            finally:
                # If we've run into a problem, this causes an EPIPE in
                # the qemu process, preventing it from blocking in
                # write() forever.
                os.close(input_fd)

        def dump_vmcore(qmp_monitor, vmcore_fd):
            # Temporarily create another reference to vmcore_fd, in the
            # qemu process. We own the duplicate.
            qmp_monitor.cmd(cmd="getfd",
                            args={"fdname": "%s" % VMCORE_FD_NAME},
                            fd=vmcore_fd)
            try:
                # Includes ownership transfer on success, no need to
                # call the "closefd" command then.
                qmp_monitor.cmd(cmd="dump-guest-memory",
                                args={"paging": False,
                                      "protocol": "fd:%s" % VMCORE_FD_NAME},
                                timeout=LONG_TIMEOUT)
            except:
                qmp_monitor.cmd(cmd="closefd",
                                args={"fdname": "%s" % VMCORE_FD_NAME})
                raise

        gzfile = gzip.open(vmcore_host, "wb", 1)
        try:
            try:
                (read_by_gzip, written_by_qemu) = os.pipe()
                try:
                    compressor = threading.Thread(target=compress_from_fd,
                                                  name="compressor",
                                                  args=(read_by_gzip, gzfile))
                    compressor.start()
                    # Compressor running, ownership of readable end has
                    # been transferred.
                    read_by_gzip = -1
                    try:
                        dump_vmcore(qmp_monitor, written_by_qemu)
                    finally:
                        # Close Python's own reference to the writeable
                        # end as well, so that the compressor can
                        # experience EOF before we try to join it.
                        os.close(written_by_qemu)
                        written_by_qemu = -1
                        compressor.join()
                finally:
                    if (read_by_gzip != -1):
                        os.close(read_by_gzip)
                    if (written_by_qemu != -1):
                        os.close(written_by_qemu)
            finally:
                # Close the gzipped file first, *then* delete it if
                # there was an error.
                gzfile.close()
        except:
            os.unlink(vmcore_host)
            raise

    def verify_vmcore(vm, session, host_compr, guest_compr, guest_plain):
        """
        Verify the vmcore with the "crash" utility in the guest.

        Standard output needs to be searched for "crash:" and "WARNING:"
        strings; the test is successful iff there are no matches and
        "crash" exits successfully.

        :param vm: virtual machine.
        :param session: login shell session.
        :param host_compr: absolute pathname of gzipped vmcore on host,
                source file.
        :param guest_compr: single-component filename of gzipped vmcore
                on guest, destination file.
        :param guest_plain: single-component filename of gunzipped
                vmcore on guest that guest-side gunzip is expected to
                create.
        :raise: vm.copy_files_to() and session.cmd() exceptions;
                error.TestFail if "crash" meets trouble in the vmcore.
        """
        vm.copy_files_to(host_compr, guest_compr)
        output = session.cmd("gzip -d -v %s" % guest_compr,
                             timeout=LONG_TIMEOUT)
        logging.debug("%s", output)

        session.cmd("{ echo bt; echo quit; } > %s" % CRASH_SCRIPT)
        output = session.cmd("crash -i %s "
                             "/usr/lib/debug/lib/modules/$(uname -r)/vmlinux "
                             "%s" % (CRASH_SCRIPT, guest_plain))
        logging.debug("%s", output)
X
Xu Han 已提交
261 262
        if (output.find("crash:") >= 0 or
                output.find("WARNING:") >= 0):
X
Xu Han 已提交
263
            test.fail("vmcore corrupt")
264 265 266 267 268 269 270 271

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    qmp_monitor = vm.get_monitors_by_type("qmp")
    if qmp_monitor:
        qmp_monitor = qmp_monitor[0]
    else:
X
Xu Han 已提交
272
        test.error('Could not find a QMP monitor, aborting test')
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

    login_timeout = int(params.get("login_timeout", 240))
    session = vm.wait_for_login(timeout=login_timeout)
    try:
        check_requirements(vm, session)

        new_sess = install_kernel_debuginfo(vm, session, login_timeout)
        if (new_sess is not None):
            session = new_sess
            install_kernel_debuginfo(None, session, None)

        install_crash(session)
        check_disk_space(session)

        vmcore_compr = "%s.gz" % VMCORE_BASE
        vmcore_host = os.path.join(test.tmpdir, vmcore_compr)
        dump_and_compress(qmp_monitor, vmcore_host)
        try:
            verify_vmcore(vm, session, vmcore_host, vmcore_compr, VMCORE_BASE)
        finally:
            os.unlink(vmcore_host)
    finally:
        session.close()