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/qemucommand.py | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 scripts/qemucommand.py (limited to 'scripts/qemucommand.py') diff --git a/scripts/qemucommand.py b/scripts/qemucommand.py new file mode 100644 index 0000000..ed14d9b --- /dev/null +++ b/scripts/qemucommand.py @@ -0,0 +1,118 @@ +from os.path import exists, join, realpath +from os import listdir +import random +import socket + +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 + + -- cgit v1.2.3-54-g00ecf From 9d5ad230a7558ae9adea42ea69d633d489c6dec0 Mon Sep 17 00:00:00 2001 From: Patrick Vacek Date: Tue, 7 Nov 2017 17:34:13 +0100 Subject: Rough draft of a run-qemu-ota test. Not very useful yet. Could be made into a function for the purpose of running arbitrary commands via SSH, for example. However, I had plenty of trouble even getting this far. Note that I created a softlink to qemucommand to get around the Python path issues in oe-selftest. I'm not sure if there's a better way to handle that, since manipulating the path is seemingly impossible. --- .gitignore | 1 + lib/oeqa/selftest/qemucommand.py | 1 + lib/oeqa/selftest/updater.py | 33 +++++++++++++++++++++++++++++++++ scripts/qemucommand.py | 4 ++-- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 120000 lib/oeqa/selftest/qemucommand.py (limited to 'scripts/qemucommand.py') diff --git a/.gitignore b/.gitignore index bee8a64..8d35cb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +*.pyc diff --git a/lib/oeqa/selftest/qemucommand.py b/lib/oeqa/selftest/qemucommand.py new file mode 120000 index 0000000..bc06dde --- /dev/null +++ b/lib/oeqa/selftest/qemucommand.py @@ -0,0 +1 @@ +../../../scripts/qemucommand.py \ No newline at end of file diff --git a/lib/oeqa/selftest/updater.py b/lib/oeqa/selftest/updater.py index e9048fd..4cdd1a2 100644 --- a/lib/oeqa/selftest/updater.py +++ b/lib/oeqa/selftest/updater.py @@ -4,6 +4,9 @@ import logging from oeqa.selftest.base import oeSelfTest from oeqa.utils.commands import runCmd, bitbake, get_bb_var +import subprocess +from oeqa.selftest.qemucommand import QemuCommand +import time class UpdaterTests(oeSelfTest): @@ -39,3 +42,33 @@ class UpdaterTests(oeSelfTest): def test_hsm(self): self.write_config('SOTA_CLIENT_FEATURES="hsm hsm-test"') bitbake('core-image-minimal') + + def test_qemu(self): + print('') + # Create empty object. + args = type('', (), {})() + args.imagename = 'core-image-minimal' + args.mac = None + args.dir = 'tmp/deploy/images' + args.efi = False + args.machine = None + args.no_kvm = False + args.no_gui = True + args.gdb = False + args.pcap = None + args.overlay = None + args.dry_run = False + + qemu_command = QemuCommand(args) + cmdline = qemu_command.command_line() + print('Booting image with run-qemu-ota...') + s = subprocess.Popen(cmdline) + time.sleep(10) + print('Machine name (hostname) of device is:') + ssh_cmd = ['ssh', '-q', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', 'root@localhost', '-p', str(qemu_command.ssh_port), 'hostname'] + s2 = subprocess.Popen(ssh_cmd) + time.sleep(5) + try: + s.terminate() + except KeyboardInterrupt: + pass diff --git a/scripts/qemucommand.py b/scripts/qemucommand.py index ed14d9b..a75ffb6 100644 --- a/scripts/qemucommand.py +++ b/scripts/qemucommand.py @@ -1,4 +1,4 @@ -from os.path import exists, join, realpath +from os.path import exists, join, realpath, abspath from os import listdir import random import socket @@ -49,7 +49,7 @@ class QemuCommand(object): if args.efi: self.bios = 'OVMF.fd' else: - uboot = join(args.dir, self.machine, 'u-boot-qemux86-64.rom') + uboot = abspath(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 -- 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/qemucommand.py') 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 From fba014fd897e938a130d1229929839c7930bc671 Mon Sep 17 00:00:00 2001 From: Phil Wise Date: Fri, 1 Dec 2017 11:04:56 +0100 Subject: Add a hint when machine autodetection fails Also fix a pylint warning about indentation --- scripts/qemucommand.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'scripts/qemucommand.py') diff --git a/scripts/qemucommand.py b/scripts/qemucommand.py index 82a9540..9a893d8 100644 --- a/scripts/qemucommand.py +++ b/scripts/qemucommand.py @@ -46,7 +46,7 @@ class QemuCommand(object): if len(machines) == 1: self.machine = machines[0] else: - raise ValueError("Could not autodetect machine type from %s" % args.dir) + raise ValueError("Could not autodetect machine type. More than one entry in %s. Maybe --machine qemux86-64?" % args.dir) if args.efi: self.bios = 'OVMF.fd' else: @@ -118,10 +118,9 @@ class QemuCommand(object): def img_command_line(self): cmdline = [ - "qemu-img", "create", - "-o", "backing_file=%s" % self.image, - "-f", "qcow2", - self.overlay] + "qemu-img", "create", + "-o", "backing_file=%s" % self.image, + "-f", "qcow2", + self.overlay] return cmdline - -- cgit v1.2.3-54-g00ecf From 84baa1c3d8f996f7daf2d8aa3d26197378218f21 Mon Sep 17 00:00:00 2001 From: Patrick Vacek Date: Thu, 18 Jan 2018 18:36:33 +0100 Subject: Fix some basic oe-selftest errors. Grub, HSM, and qemu hostname tests still fail for reasons I haven't figured out yet. --- lib/oeqa/selftest/cases/updater.py | 4 +++- scripts/qemucommand.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'scripts/qemucommand.py') diff --git a/lib/oeqa/selftest/cases/updater.py b/lib/oeqa/selftest/cases/updater.py index 7d7bde7..253320d 100644 --- a/lib/oeqa/selftest/cases/updater.py +++ b/lib/oeqa/selftest/cases/updater.py @@ -13,6 +13,7 @@ class SotaToolsTests(OESelftestTestCase): @classmethod def setUpClass(cls): + super(SotaToolsTests, cls).setUpClass() logger = logging.getLogger("selftest") logger.info('Running bitbake to build aktualizr-native tools') bitbake('aktualizr-native') @@ -109,6 +110,7 @@ class QemuTests(OESelftestTestCase): @classmethod def setUpClass(cls): + super(QemuTests, cls).setUpClass() cls.qemu, cls.s = qemu_launch(machine='qemux86-64') @classmethod @@ -140,7 +142,7 @@ class GrubTests(OESelftestTestCase): def setUpLocal(self): # This is a bit of a hack but I can't see a better option. path = os.path.abspath(os.path.dirname(__file__)) - metadir = path + "/../../../../" + metadir = path + "/../../../../../" grub_config = 'OSTREE_BOOTLOADER = "grub"\nMACHINE = "intel-corei7-64"' self.append_config(grub_config) self.meta_intel = metadir + "meta-intel" diff --git a/scripts/qemucommand.py b/scripts/qemucommand.py index 9a893d8..7ae9381 100644 --- a/scripts/qemucommand.py +++ b/scripts/qemucommand.py @@ -96,7 +96,7 @@ class QemuCommand(object): "-serial", "tcp:127.0.0.1:%d,server,nowait" % self.serial_port, "-m", "1G", "-usb", - "-usbdevice", "tablet", + "-device", "usb-tablet", "-show-cursor", "-vga", "std", "-net", netuser, -- cgit v1.2.3-54-g00ecf From 44a4dd8e039ea16319f337c3881964759ada73a5 Mon Sep 17 00:00:00 2001 From: Laurent Bonnans Date: Fri, 19 Jan 2018 09:16:49 +0100 Subject: More general exception handler for `kvm-ok` If the program is not in PATH, `FileNotFoundError` is raised --- scripts/qemucommand.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scripts/qemucommand.py') diff --git a/scripts/qemucommand.py b/scripts/qemucommand.py index 7ae9381..6b1106d 100644 --- a/scripts/qemucommand.py +++ b/scripts/qemucommand.py @@ -73,7 +73,7 @@ class QemuCommand(object): try: check_output(['kvm-ok']) self.kvm = True - except CalledProcessError: + except Exception: self.kvm = False else: self.kvm = args.kvm -- cgit v1.2.3-54-g00ecf