From 06711c8543a3af13203b4352b25b1875c29c16f2 Mon Sep 17 00:00:00 2001 From: Patrick Vacek Date: Tue, 7 Nov 2017 15:20:53 +0100 Subject: Refactor QemuCommand class into its own file/module. --- scripts/run-qemu-ota | 118 +-------------------------------------------------- 1 file changed, 2 insertions(+), 116 deletions(-) (limited to 'scripts/run-qemu-ota') diff --git a/scripts/run-qemu-ota b/scripts/run-qemu-ota index 641296c..5f9cebe 100755 --- a/scripts/run-qemu-ota +++ b/scripts/run-qemu-ota @@ -2,126 +2,12 @@ from argparse import ArgumentParser from subprocess import Popen -from os.path import exists, join, realpath -from os import listdir -import random +from os.path import exists, join import sys -import socket +from qemucommand import QemuCommand DEFAULT_DIR = 'tmp/deploy/images' -EXTENSIONS = { - 'intel-corei7-64': 'wic', - 'qemux86-64': 'otaimg' -} - - -def find_local_port(start_port): - """" - Find the next free TCP port after 'start_port'. - """ - - for port in range(start_port, start_port + 10): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.bind(('', port)) - return port - except socket.error: - print("Skipping port %d" % port) - finally: - s.close() - raise Exception("Could not find a free TCP port") - - -def random_mac(): - """Return a random Ethernet MAC address - @link https://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml#ethernet-numbers-2 - """ - head = "ca:fe:" - hex_digits = '0123456789abcdef' - tail = ':'.join([random.choice(hex_digits) + random.choice(hex_digits) for _ in range(4)]) - return head + tail - - -class QemuCommand(object): - def __init__(self, args): - if args.machine: - self.machine = args.machine - else: - machines = listdir(args.dir) - if len(machines) == 1: - self.machine = machines[0] - else: - raise ValueError("Could not autodetect machine type from %s" % args.dir) - if args.efi: - self.bios = 'OVMF.fd' - else: - uboot = join(args.dir, self.machine, 'u-boot-qemux86-64.rom') - if not exists(uboot): - raise ValueError("U-Boot image %s does not exist" % uboot) - self.bios = uboot - if exists(args.imagename): - image = args.imagename - else: - ext = EXTENSIONS.get(self.machine, 'wic') - image = join(args.dir, self.machine, '%s-%s.%s' % (args.imagename, self.machine, ext)) - self.image = realpath(image) - if not exists(self.image): - raise ValueError("OS image %s does not exist" % self.image) - if args.mac: - self.mac_address = args.mac - else: - self.mac_address = random_mac() - self.serial_port = find_local_port(8990) - self.ssh_port = find_local_port(2222) - self.kvm = not args.no_kvm - self.gui = not args.no_gui - self.gdb = args.gdb - self.pcap = args.pcap - self.overlay = args.overlay - - def command_line(self): - netuser = 'user,hostfwd=tcp:0.0.0.0:%d-:22,restrict=off' % self.ssh_port - if self.gdb: - netuser += ',hostfwd=tcp:0.0.0.0:2159-:2159' - cmdline = [ - "qemu-system-x86_64", - "-bios", self.bios - ] - if not self.overlay: - cmdline += ["-drive", "file=%s,if=ide,format=raw,snapshot=on" % self.image] - cmdline += [ - "-serial", "tcp:127.0.0.1:%d,server,nowait" % self.serial_port, - "-m", "1G", - "-usb", - "-usbdevice", "tablet", - "-show-cursor", - "-vga", "std", - "-net", netuser, - "-net", "nic,macaddr=%s" % self.mac_address - ] - if self.pcap: - cmdline += ['-net', 'dump,file=' + self.pcap] - if self.gui: - cmdline += ["-serial", "stdio"] - else: - cmdline.append('-nographic') - if self.kvm: - cmdline.append('-enable-kvm') - else: - cmdline += ['-cpu', 'Haswell'] - if self.overlay: - cmdline.append(self.overlay) - return cmdline - - def img_command_line(self): - cmdline = [ - "qemu-img", "create", - "-o", "backing_file=%s" % self.image, - "-f", "qcow2", - self.overlay] - return cmdline - def main(): parser = ArgumentParser(description='Run meta-updater image in qemu') -- cgit v1.2.3-54-g00ecf From 0b1fd4b3c456a64e40a7ff1125f005c0b72eafd8 Mon Sep 17 00:00:00 2001 From: Phil Wise Date: Thu, 16 Nov 2017 11:00:22 +0100 Subject: Remove unused import, break long lines --- scripts/run-qemu-ota | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'scripts/run-qemu-ota') diff --git a/scripts/run-qemu-ota b/scripts/run-qemu-ota index 5f9cebe..8e25197 100755 --- a/scripts/run-qemu-ota +++ b/scripts/run-qemu-ota @@ -2,7 +2,7 @@ from argparse import ArgumentParser from subprocess import Popen -from os.path import exists, join +from os.path import exists import sys from qemucommand import QemuCommand @@ -25,7 +25,10 @@ def main(): parser.add_argument('--no-gui', help='Disable GUI', action='store_true') parser.add_argument('--gdb', help='Export gdbserver port 2159 from the image', action='store_true') parser.add_argument('--pcap', default=None, help='Dump all network traffic') - parser.add_argument('-o', '--overlay', type=str, metavar='file.cow', help='Use an overlay storage image file. Will be created if it does not exist. This option lets you have a persistent image without modifying the underlying image file, permitting multiple different persistent machines.') + parser.add_argument('-o', '--overlay', type=str, metavar='file.cow', + help='Use an overlay storage image file. Will be created if it does not exist. ' + + 'This option lets you have a persistent image without modifying the underlying image ' + + 'file, permitting multiple different persistent machines.') parser.add_argument('-n', '--dry-run', help='Print qemu command line rather then run it', action='store_true') args = parser.parse_args() try: -- cgit v1.2.3-54-g00ecf From 24e5a6d45886365cecce74c2c9aa1cfd8c0da69a Mon Sep 17 00:00:00 2001 From: Phil Wise Date: Thu, 16 Nov 2017 11:01:25 +0100 Subject: Autodetect KVM Autodetect KVM by using the 'kvm-ok' command line tool. This has two benefits: Firstly, it improves the UX of run-qemu-ota when working on machines without KVM (e.g. AWS). Previously, people had to use the --no-kvm option in these cases. Secondary, it makes oe-selftest usable on machines without KVM. Our tests call run-qemu-ota, and we want to able to run them on machines without KVM. --- lib/oeqa/selftest/updater.py | 4 ++-- scripts/qemucommand.py | 11 ++++++++++- scripts/run-qemu-ota | 6 +++++- 3 files changed, 17 insertions(+), 4 deletions(-) (limited to 'scripts/run-qemu-ota') diff --git a/lib/oeqa/selftest/updater.py b/lib/oeqa/selftest/updater.py index 6339e6e..eb09302 100644 --- a/lib/oeqa/selftest/updater.py +++ b/lib/oeqa/selftest/updater.py @@ -97,12 +97,12 @@ class GeneralTests(oeSelfTest): args = type('', (), {})() args.imagename = 'core-image-minimal' args.mac = None - # Could use DEPLOY_DIR_IMAGE her but it's already in the machine + # Could use DEPLOY_DIR_IMAGE here but it's already in the machine # subdirectory. args.dir = 'tmp/deploy/images' args.efi = False args.machine = None - args.no_kvm = False + args.kvm = None # Autodetect args.no_gui = True args.gdb = False args.pcap = None diff --git a/scripts/qemucommand.py b/scripts/qemucommand.py index a75ffb6..82a9540 100644 --- a/scripts/qemucommand.py +++ b/scripts/qemucommand.py @@ -2,6 +2,7 @@ from os.path import exists, join, realpath, abspath from os import listdir import random import socket +from subprocess import check_output, CalledProcessError EXTENSIONS = { 'intel-corei7-64': 'wic', @@ -67,7 +68,15 @@ class QemuCommand(object): self.mac_address = random_mac() self.serial_port = find_local_port(8990) self.ssh_port = find_local_port(2222) - self.kvm = not args.no_kvm + if args.kvm is None: + # Autodetect KVM using 'kvm-ok' + try: + check_output(['kvm-ok']) + self.kvm = True + except CalledProcessError: + self.kvm = False + else: + self.kvm = args.kvm self.gui = not args.no_gui self.gdb = args.gdb self.pcap = args.pcap diff --git a/scripts/run-qemu-ota b/scripts/run-qemu-ota index 8e25197..56e4fbc 100755 --- a/scripts/run-qemu-ota +++ b/scripts/run-qemu-ota @@ -21,7 +21,11 @@ def main(): 'OSTREE_BOOTLOADER = "grub" and OVMF.fd firmware to be installed (try "apt install ovmf")', action='store_true') parser.add_argument('--machine', default=None, help="Target MACHINE") - parser.add_argument('--no-kvm', help='Disable KVM in QEMU', action='store_true') + kvm_group = parser.add_argument_group() + kvm_group.add_argument('--force-kvm', help='Force use of KVM (default is to autodetect)', + dest='kvm', action='store_true', default=None) + kvm_group.add_argument('--no-kvm', help='Disable KVM in QEMU', + dest='kvm', action='store_false') parser.add_argument('--no-gui', help='Disable GUI', action='store_true') parser.add_argument('--gdb', help='Export gdbserver port 2159 from the image', action='store_true') parser.add_argument('--pcap', default=None, help='Dump all network traffic') -- cgit v1.2.3-54-g00ecf