Пример #1
0
 def teardown(self):
     try:
         self.shutdown()
         self.qmp.close()
     except:
         pass
     self.qmp = QEMUMonitorProtocol(('127.0.0.1', 1235))
     self._pid = None
     self._reset_host_configuration()
     sleep(20)
     self.delete_tun()
     sleep(2)
     self.unload_kvm()
     super().teardown()
Пример #2
0
    def _pre_launch(self) -> None:
        if self._console_set:
            self._remove_files.append(self._console_address)

        if self._qmp_set:
            if isinstance(self._monitor_address, str):
                self._remove_files.append(self._monitor_address)
            self._qmp_connection = QEMUMonitorProtocol(
                self._monitor_address,
                server=True,
                nickname=self._name
            )

        # NOTE: Make sure any opened resources are *definitely* freed in
        # _post_shutdown()!
        # pylint: disable=consider-using-with
        self._qemu_log_path = os.path.join(self.log_dir, self._name + ".log")
        self._qemu_log_file = open(self._qemu_log_path, 'wb')

        self._iolog = None
        self._qemu_full_args = tuple(chain(
            self._wrapper,
            [self._binary],
            self._base_args,
            self._args
        ))
Пример #3
0
def main() -> None:
    """
    Driver of the whole script, parses arguments, initialize the TUI and
    the logger.
    """
    parser = argparse.ArgumentParser(description='AQMP TUI')
    parser.add_argument('qmp_server',
                        help='Address of the QMP server. '
                        'Format <UNIX socket path | TCP addr:port>')
    parser.add_argument('--num-retries',
                        type=int,
                        default=10,
                        help='Number of times to reconnect before giving up.')
    parser.add_argument('--retry-delay',
                        type=int,
                        help='Time(s) to wait before next retry. '
                        'Default action is to wait 2s between each retry.')
    parser.add_argument('--log-file', help='The Log file name')
    parser.add_argument('--log-level',
                        default='WARNING',
                        help='Log level <CRITICAL|ERROR|WARNING|INFO|DEBUG|>')
    parser.add_argument('--asyncio-debug',
                        action='store_true',
                        help='Enable debug mode for asyncio loop. '
                        'Generates lot of output, makes TUI unusable when '
                        'logs are logged in the TUI. '
                        'Use only when logging to a file.')
    args = parser.parse_args()

    try:
        address = QEMUMonitorProtocol.parse_address(args.qmp_server)
    except QMPBadPortError as err:
        parser.error(str(err))

    app = App(address, args.num_retries, args.retry_delay)

    root_logger = logging.getLogger()
    root_logger.setLevel(logging.getLevelName(args.log_level))

    if args.log_file:
        root_logger.addHandler(logging.FileHandler(args.log_file))
    else:
        root_logger.addHandler(TUILogHandler(app))

    app.run(args.asyncio_debug)
Пример #4
0
    def _pre_launch(self) -> None:
        self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log")

        if self._console_set:
            self._remove_files.append(self._console_address)

        if self._qmp_set:
            if self._remove_monitor_sockfile:
                assert isinstance(self._monitor_address, str)
                self._remove_files.append(self._monitor_address)
            self._qmp_connection = QEMUMonitorProtocol(self._monitor_address,
                                                       server=True,
                                                       nickname=self._name)

        # NOTE: Make sure any opened resources are *definitely* freed in
        # _post_shutdown()!
        # pylint: disable=consider-using-with
        self._qemu_log_file = open(self._qemu_log_path, 'wb')
Пример #5
0
class Qemu(VM):
    QEMU_EXE = ""

    QEMU_E1000_DEBUG_PARAMETERS_FILE = "/tmp/e1000_debug_parameters"

    QEMU_E1000 = "e1000"
    QEMU_VIRTIO = "virtio-net-pci"

    BOOTUP_WAIT = 30

    def __init__(self, disk_path, guest_ip, host_ip, cpu_to_pin="2"):
        super(Qemu, self).__init__(disk_path, guest_ip, host_ip)
        self._pid = None

        self.cpu_to_pin = cpu_to_pin
        # self.cpu_num = cpu_num
        self.mac_address = "52:54:00:a0:e5:1c"
        self.vnc_number = "10"

        self.ethernet_dev = self.QEMU_E1000  # can be "virtio-net-pci" or "e1000"
        self.vhost = False
        self.sidecore = False
        self.mem = "8192"

        self.io_thread_cpu = ""

        # auto config
        self.tap_device = ''
        self.pidfile = None

        self.qemu_config = dict()
        self.bridge = None
        self.exe = self.QEMU_EXE

        self.is_io_thread_nice = False
        self.io_nice = 1  # nice value to set

        self.root = Machine(self._remote_ip, "root")

        # self.kernel = r"/home/bdaviv/repos/e1000-improv/linux-3.13.0/arch/x86/boot/bzImage"
        # self.kernel = r"/homes/bdaviv/repos/msc-ng/linux-4.13.9/arch/x86/boot/bzImage"
        self.kernel = r"../vms/vmlinuz"  #r"../linux/arch/x86/boot/bzImage"
        # self.initrd = r"../vms/initrd.img"
        # self.initrd = r"/homes/bdaviv/repos/msc-ng/vm-files/kernels/initrd.img-4.13.9-ng+"
        self.initrd = r"../vms/initrd.img"  # r"../vms/initrd.img"
        self.kernel_cmdline = r"BOOT_IMAGE=/vmlinuz-5.4.0-73-generic root=/dev/mapper/ubuntu--vg-ubuntu--lv ro maybe-ubiquity"
        self.kernel_cmdline_additional = ""

        self.nic_additionals = ""
        self.qemu_additionals = ""

        self.disable_kvm_poll = False

        self.guest_e1000_ng_flag = 0

        self.qmp = None

    def get_info(self, old_info=None):
        KERNEL_FILE = "kernel_file"
        KERNEL_GIT = "kernel_git"
        KERNEL_CMD = "kernel_cmd"

        QEMU_FILE = "qemu_file"
        QEMU_GIT = "qemu_git"

        NIC_TYPE = "nic_type"
        CPU_PIN = "cpu_pin"
        MEM = "mem"
        NICE = "nice"

        keys = (KERNEL_FILE, KERNEL_GIT, KERNEL_CMD, QEMU_FILE, QEMU_GIT,
                NIC_TYPE, CPU_PIN, MEM, NICE)

        info = super().get_info(old_info)

        if not self.enabled:
            info.update({k: old_info[k] for k in keys})
        else:
            #info[KERNEL_FILE] = self.kernel
            #info[KERNEL_GIT] = run_command_output("git -C {directory} rev-parse --short HEAD".format(
            #    directory=os.path.dirname(self.kernel)
            #)).strip()
            info[KERNEL_CMD] = self.kernel_cmdline

            info[QEMU_FILE] = self.exe
            #info[QEMU_GIT] = run_command_output("git -C {directory} rev-parse --short HEAD".format(
            #    directory=os.path.dirname(self.exe)
            #)).strip()

            info[NIC_TYPE] = self.ethernet_dev
            info[CPU_PIN] = self.cpu_to_pin
            info[MEM] = self.mem
            info[NICE] = (self.is_io_thread_nice, self.io_nice)
        return info

    def run(self, configure_guest=True):
        super().run(configure_guest)

    def create_tun(self):
        """
        create tun device and assign it an IP
        """
        current_user = os.environ["USER"]

        output = run_command_output(
            "sudo tunctl -u {user}".format(user=current_user))
        self.tap_device = output.split("'")[1]
        assert self.tap_device == 'tap0'

        run_command_check(
            "sudo  ip link set {tap} up".format(tap=self.tap_device))
        if self.ip_host and not self.bridge:
            run_command_check("sudo ip a a {host_ip}/24 dev {tap}".format(
                host_ip=self.ip_host, tap=self.tap_device))
        if self.bridge:
            run_command_check("sudo brctl addif {br} {iff}".format(
                br=self.bridge, iff=self.tap_device))

    def delete_tun(self):
        if self.bridge:
            run_command("sudo brctl delif {br} {iff}".format(
                br=self.bridge, iff=self.tap_device))
        while True:
            try:
                run_command_check(
                    "sudo tunctl -d {tap}".format(tap=self.tap_device))
                break
            except:
                sleep(1)

    def load_kvm(self):
        run_command_check("sudo modprobe kvm-intel")
        if self.disable_kvm_poll:
            run_command_check(
                "echo 0 | sudo tee /sys/module/kvm/parameters/halt_poll_ns")

    def unload_kvm(self):
        sleep(1)
        run_command("sudo modprobe -r kvm-intel")

    def _clean_cpu(self):
        return
        run_command(
            "echo 0 |sudo tee /sys/devices/system/cpu/cpu{cpu}/online".format(
                cpu=self.cpu_to_pin),
            shell=True)
        run_command(
            "echo 1 |sudo tee /sys/devices/system/cpu/cpu{cpu}/online".format(
                cpu=self.cpu_to_pin),
            shell=True)

    def setup(self):
        super().setup()
        self.load_kvm()
        self.create_tun()
        self._configure_host()
        self._clean_cpu()

    def teardown(self):
        try:
            self.shutdown()
            self.qmp.close()
        except:
            pass
        self.qmp = QEMUMonitorProtocol(('127.0.0.1', 1235))
        self._pid = None
        self._reset_host_configuration()
        sleep(20)
        self.delete_tun()
        sleep(2)
        self.unload_kvm()
        super().teardown()

    def _get_temp_nic_additional(self):
        return ""

    def _run(self):
        assert self.exe

        self.qmp = QEMUMonitorProtocol(('127.0.0.1', 1235))
        self.pidfile = NamedTemporaryFile()

        if self.vhost:
            vhost_param = ",vhost=on"
            # HACK HACK HACK
            localRoot.remote_command("chown :kvm /dev/vhost-net")
            localRoot.remote_command("chmod 660 /dev/vhost-net")
        else:
            vhost_param = ""

        if self.sidecore:
            sidecore_param = "-enable-e1000-sidecore"
        else:
            sidecore_param = ""

        self.pidfile.close()
        self.pidfile = NamedTemporaryFile()

        kernel_spicific_boot = ""
        kernel_command_line = self.kernel_cmdline_additional
        if "e1000.NG_flags" not in self.kernel_cmdline_additional and self.guest_e1000_ng_flag != 0:
            kernel_command_line += " e1000.NG_flags={}".format(
                self.guest_e1000_ng_flag)
        if self.kernel:
            kernel_spicific_boot = "-kernel {kernel} -initrd {initrd} -append '{cmdline} {cmdline_more}'".format(
                kernel=self.kernel,
                initrd=self.initrd,
                cmdline=self.kernel_cmdline,
                cmdline_more=kernel_command_line)

        qemu_command = "numactl -C {cpu} -m 0 {qemu_exe} -enable-kvm {sidecore} -k en-us -m {mem} " \
                       "{kernel_additions} " \
                       "{qemu_additionals} " \
                       "-drive file='{disk}',if=none,id=drive-virtio-disk0,format=qcow2 " \
                       "-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 " \
                       "-netdev tap,ifname={tap},id=net0,script=no{vhost} " \
                       "-object iothread,id=iothread0 " \
                       "-device {dev_type},netdev=net0,mac={mac}{nic_additionals} " \
                       "-vnc :{vnc} " \
                       "-pidfile {pidfile} " \
                       "-monitor tcp:127.0.0.1:1234,server,nowait,nodelay " \
                       "-qmp tcp:127.0.0.1:1235,server,nowait,nodelay " \
                       "".format(  # -monitor tcp:1234,server,nowait,nodelay
            cpu=self.cpu_to_pin,
            qemu_exe=self.exe,
            sidecore=sidecore_param,
            kernel_additions=kernel_spicific_boot,
            qemu_additionals=self.qemu_additionals,
            disk=self.path,
            tap=self.tap_device,
            vhost=vhost_param,
            dev_type=self.ethernet_dev,
            mac=self.mac_address,
            nic_additionals=self.nic_additionals + self._get_temp_nic_additional(),
            pidfile=self.pidfile.name,
            vnc=self.vnc_number,
            mem=self.mem,
            #                       "-pidfile {pidfile} " \


        )
        run_command_async(qemu_command)
        sleep(1)
        if self.qemu_config:
            self.change_qemu_parameters()
        sleep(1)
        if self.io_thread_cpu:
            command = "sudo taskset -p -c {} {}".format(
                self.io_thread_cpu, self.get_pid())
            run_command_check(command)
        if self.is_io_thread_nice:
            self.set_iothread_nice()
        sleep(1)
        self.qmp.connect()

    def set_iothread_nice(self, nice=None):
        if nice is None:
            nice = self.io_nice
        run_command_remote("127.0.0.1", "root",
                           "renice -n {} -p {}".format(nice, self.get_pid()))

    def change_qemu_parameters(self, config=None):
        if config:
            self.qemu_config.update(config)

        with open(self.QEMU_E1000_DEBUG_PARAMETERS_FILE, "w") as f:
            for name, value in self.qemu_config.items():
                f.write("{} {}\n".format(name, value))
                logger.debug("set qemu option: %s=%s", name, value)
        self._signal_qemu()

    def get_pid(self):
        if self._pid:
            return self._pid
        with open(self.pidfile.name, "r") as f:
            pid = int(f.read().strip())
        self._pid = pid
        return pid

    def _signal_qemu(self):
        pid = self.get_pid()
        os.kill(pid, signal.SIGUSR1)

    def configure_guest(self):
        super().configure_guest()

    def _configure_host(self):
        pass

    def _reset_host_configuration(self):
        pass
Пример #6
0
    def _run(self):
        assert self.exe

        self.qmp = QEMUMonitorProtocol(('127.0.0.1', 1235))
        self.pidfile = NamedTemporaryFile()

        if self.vhost:
            vhost_param = ",vhost=on"
            # HACK HACK HACK
            localRoot.remote_command("chown :kvm /dev/vhost-net")
            localRoot.remote_command("chmod 660 /dev/vhost-net")
        else:
            vhost_param = ""

        if self.sidecore:
            sidecore_param = "-enable-e1000-sidecore"
        else:
            sidecore_param = ""

        self.pidfile.close()
        self.pidfile = NamedTemporaryFile()

        kernel_spicific_boot = ""
        kernel_command_line = self.kernel_cmdline_additional
        if "e1000.NG_flags" not in self.kernel_cmdline_additional and self.guest_e1000_ng_flag != 0:
            kernel_command_line += " e1000.NG_flags={}".format(
                self.guest_e1000_ng_flag)
        if self.kernel:
            kernel_spicific_boot = "-kernel {kernel} -initrd {initrd} -append '{cmdline} {cmdline_more}'".format(
                kernel=self.kernel,
                initrd=self.initrd,
                cmdline=self.kernel_cmdline,
                cmdline_more=kernel_command_line)

        qemu_command = "numactl -C {cpu} -m 0 {qemu_exe} -enable-kvm {sidecore} -k en-us -m {mem} " \
                       "{kernel_additions} " \
                       "{qemu_additionals} " \
                       "-drive file='{disk}',if=none,id=drive-virtio-disk0,format=qcow2 " \
                       "-device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 " \
                       "-netdev tap,ifname={tap},id=net0,script=no{vhost} " \
                       "-object iothread,id=iothread0 " \
                       "-device {dev_type},netdev=net0,mac={mac}{nic_additionals} " \
                       "-vnc :{vnc} " \
                       "-pidfile {pidfile} " \
                       "-monitor tcp:127.0.0.1:1234,server,nowait,nodelay " \
                       "-qmp tcp:127.0.0.1:1235,server,nowait,nodelay " \
                       "".format(  # -monitor tcp:1234,server,nowait,nodelay
            cpu=self.cpu_to_pin,
            qemu_exe=self.exe,
            sidecore=sidecore_param,
            kernel_additions=kernel_spicific_boot,
            qemu_additionals=self.qemu_additionals,
            disk=self.path,
            tap=self.tap_device,
            vhost=vhost_param,
            dev_type=self.ethernet_dev,
            mac=self.mac_address,
            nic_additionals=self.nic_additionals + self._get_temp_nic_additional(),
            pidfile=self.pidfile.name,
            vnc=self.vnc_number,
            mem=self.mem,
            #                       "-pidfile {pidfile} " \


        )
        run_command_async(qemu_command)
        sleep(1)
        if self.qemu_config:
            self.change_qemu_parameters()
        sleep(1)
        if self.io_thread_cpu:
            command = "sudo taskset -p -c {} {}".format(
                self.io_thread_cpu, self.get_pid())
            run_command_check(command)
        if self.is_io_thread_nice:
            self.set_iothread_nice()
        sleep(1)
        self.qmp.connect()
Пример #7
0
def hmp_cmd(srv, command_line):
    print(">>> HMP: {}".format(command_line))
    rsp = srv.cmd("human-monitor-command", {"command-line": command_line})
    print(rsp['return'])
    return rsp['return']


def shell_cmd(command_line):
    print(">>> Shell: {}".format(command_line))
    os.system(command_line)


target_socket = sys.argv[1]

print("--- Connecing to {}".format(target_socket))
srv = QEMUMonitorProtocol(target_socket)
srv.connect()

print("--- stopping")
rsp = srv.cmd("stop")
print(rsp)

info_registers = hmp_cmd(srv, "info registers")
hmp_cmd(srv, "info lapic")
hmp_cmd(srv, "info pic")
hmp_cmd(srv, "gva2gpa 0xfffffe0000000000")
hmp_cmd(srv, "gva2gpa 0xfffffe0000001000")
hmp_cmd(srv, "gva2gpa 0xfffffe0000003000")
hmp_cmd(srv, "x /32xg 0xfffffe0000000000")
hmp_cmd(srv, "x /16xg 0xfffffe0000001000")
hmp_cmd(srv, "x /32xg 0xfffffe0000003000")
Пример #8
0
class LibvirtGuest():
    def __init__(self, name):
        self.name = name

    def command(self, cmd):
        # only supports qmp commands without parameters
        m = {'execute': cmd}
        ar = ['virsh', 'qemu-monitor-command', self.name, json.dumps(m)]

        reply = json.loads(subprocess.check_output(ar))

        if 'error' in reply:
            raise QMPResponseError(reply)

        return reply['return']


if __name__ == '__main__':
    obj = sys.argv[1]
    out = sys.argv[2]

    if os.path.exists(obj):
        # assume unix socket
        qmp = QEMUMonitorProtocol(obj)
        qmp.connect()
    else:
        # assume libvirt guest name
        qmp = LibvirtGuest(obj)

    render_block_graph(qmp, out)
Пример #9
0
#!/usr/bin/env python3

import sys
import os
import time

sys.path.append('/home/dmurik/qemu/python')
from qemu.qmp import QEMUMonitorProtocol

target_socket = sys.argv[1]
user_cmd = sys.argv[2]

print("--- Connecing to {}".format(target_socket))
srv = QEMUMonitorProtocol(target_socket)
srv.connect()

print("--- {}".format(user_cmd))
rsp = srv.cmd(user_cmd)
print(rsp)
srv.close()
import sys
import os
import time

sys.path.append('/home/dmurik/qemu/python')
from qemu.qmp import QEMUMonitorProtocol

KB = 1024
MB = 1024 * KB
GB = 1024 * MB

target_socket = sys.argv[1]

print("--- Connecing to {}".format(target_socket))
srv = QEMUMonitorProtocol(target_socket)
srv.connect()

print("--- stop")
rsp = srv.cmd("stop")
print(rsp)

print("--- load part 1")
rsp = srv.cmd("pmemload", {
    "val": 0x0,
    "size": 0x80b000,
    "filename": "mem-dump-1.bin"
})
print(rsp)
print("--- load part 2")
rsp = srv.cmd("pmemload", {