Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | # Handle running OE images standalone with QEMU |
| 4 | # |
| 5 | # Copyright (C) 2006-2011 Linux Foundation |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 6 | # Copyright (c) 2016 Wind River Systems, Inc. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 8 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 11 | import os |
| 12 | import sys |
| 13 | import logging |
| 14 | import subprocess |
| 15 | import re |
| 16 | import fcntl |
| 17 | import shutil |
| 18 | import glob |
| 19 | import configparser |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 20 | import signal |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame^] | 21 | import time |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 22 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | class RunQemuError(Exception): |
| 24 | """Custom exception to raise on known errors.""" |
| 25 | pass |
| 26 | |
| 27 | class OEPathError(RunQemuError): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | """Custom Exception to give better guidance on missing binaries""" |
| 29 | def __init__(self, message): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 30 | super().__init__("In order for this script to dynamically infer paths\n \ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 31 | kernels or filesystem images, you either need bitbake in your PATH\n \ |
| 32 | or to source oe-init-build-env before running this script.\n\n \ |
| 33 | Dynamic path inference can be avoided by passing a *.qemuboot.conf to\n \ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 34 | runqemu, i.e. `runqemu /path/to/my-image-name.qemuboot.conf`\n\n %s" % message) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def create_logger(): |
| 38 | logger = logging.getLogger('runqemu') |
| 39 | logger.setLevel(logging.INFO) |
| 40 | |
| 41 | # create console handler and set level to debug |
| 42 | ch = logging.StreamHandler() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 43 | ch.setLevel(logging.DEBUG) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 44 | |
| 45 | # create formatter |
| 46 | formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s') |
| 47 | |
| 48 | # add formatter to ch |
| 49 | ch.setFormatter(formatter) |
| 50 | |
| 51 | # add ch to logger |
| 52 | logger.addHandler(ch) |
| 53 | |
| 54 | return logger |
| 55 | |
| 56 | logger = create_logger() |
| 57 | |
| 58 | def print_usage(): |
| 59 | print(""" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 60 | Usage: you can run this script with any valid combination |
| 61 | of the following environment variables (in any order): |
| 62 | KERNEL - the kernel image file to use |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 63 | BIOS - the bios image file to use |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 64 | ROOTFS - the rootfs image file or nfsroot directory to use |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 65 | DEVICE_TREE - the device tree blob to use |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 66 | MACHINE - the machine name (optional, autodetected from KERNEL filename if unspecified) |
| 67 | Simplified QEMU command-line options can be passed with: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 68 | nographic - disable video console |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 69 | novga - Disable VGA emulation completely |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 70 | sdl - choose the SDL UI frontend |
| 71 | gtk - choose the Gtk UI frontend |
Brad Bishop | 6dbb316 | 2019-11-25 09:41:34 -0500 | [diff] [blame] | 72 | gl - enable virgl-based GL acceleration (also needs gtk or sdl options) |
| 73 | gl-es - enable virgl-based GL acceleration, using OpenGL ES (also needs gtk or sdl options) |
| 74 | egl-headless - enable headless EGL output; use vnc (via publicvnc option) or spice to see it |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | serial - enable a serial console on /dev/ttyS0 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 76 | serialstdio - enable a serial console on the console (regardless of graphics mode) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 77 | slirp - enable user networking, no root privileges is required |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 78 | snapshot - don't write changes to back to images |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 79 | kvm - enable KVM when running x86/x86_64 (VT-capable CPU required) |
| 80 | kvm-vhost - enable KVM with vhost when running x86/x86_64 (VT-capable CPU required) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 81 | publicvnc - enable a VNC server open to all hosts |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 82 | audio - enable audio |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 83 | [*/]ovmf* - OVMF firmware file or base name for booting with UEFI |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 84 | tcpserial=<port> - specify tcp serial port number |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 85 | qemuparams=<xyz> - specify custom parameters to QEMU |
| 86 | bootparams=<xyz> - specify custom kernel parameters during boot |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | help, -h, --help: print this text |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 88 | -d, --debug: Enable debug output |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 89 | -q, --quiet: Hide most output except error messages |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 90 | |
| 91 | Examples: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 92 | runqemu |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 93 | runqemu qemuarm |
| 94 | runqemu tmp/deploy/images/qemuarm |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 95 | runqemu tmp/deploy/images/qemux86/<qemuboot.conf> |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 96 | runqemu qemux86-64 core-image-sato ext4 |
| 97 | runqemu qemux86-64 wic-image-minimal wic |
| 98 | runqemu path/to/bzImage-qemux86.bin path/to/nfsrootdir/ serial |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 99 | runqemu qemux86 iso/hddimg/wic.vmdk/wic.vhd/wic.vhdx/wic.qcow2/wic.vdi/ramfs/cpio.gz... |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | runqemu qemux86 qemuparams="-m 256" |
| 101 | runqemu qemux86 bootparams="psplash=false" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 102 | runqemu path/to/<image>-<machine>.wic |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 103 | runqemu path/to/<image>-<machine>.wic.vmdk |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 104 | runqemu path/to/<image>-<machine>.wic.vhdx |
| 105 | runqemu path/to/<image>-<machine>.wic.vhd |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | """) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | def check_tun(): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 109 | """Check /dev/net/tun""" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 110 | dev_tun = '/dev/net/tun' |
| 111 | if not os.path.exists(dev_tun): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 112 | raise RunQemuError("TUN control device %s is unavailable; you may need to enable TUN (e.g. sudo modprobe tun)" % dev_tun) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 114 | if not os.access(dev_tun, os.W_OK): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 115 | raise RunQemuError("TUN control device %s is not writable, please fix (e.g. sudo chmod 666 %s)" % (dev_tun, dev_tun)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 117 | def get_first_file(cmds): |
| 118 | """Return first file found in wildcard cmds""" |
| 119 | for cmd in cmds: |
| 120 | all_files = glob.glob(cmd) |
| 121 | if all_files: |
| 122 | for f in all_files: |
| 123 | if not os.path.isdir(f): |
| 124 | return f |
| 125 | return '' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | class BaseConfig(object): |
| 128 | def __init__(self): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 129 | # The self.d saved vars from self.set(), part of them are from qemuboot.conf |
| 130 | self.d = {'QB_KERNEL_ROOT': '/dev/vda'} |
| 131 | |
| 132 | # Supported env vars, add it here if a var can be got from env, |
| 133 | # and don't use os.getenv in the code. |
| 134 | self.env_vars = ('MACHINE', |
| 135 | 'ROOTFS', |
| 136 | 'KERNEL', |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 137 | 'BIOS', |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 138 | 'DEVICE_TREE', |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 139 | 'DEPLOY_DIR_IMAGE', |
| 140 | 'OE_TMPDIR', |
| 141 | 'OECORE_NATIVE_SYSROOT', |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 142 | 'MULTICONFIG', |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 143 | 'SERIAL_CONSOLES', |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 144 | ) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 146 | self.qemu_opt = '' |
| 147 | self.qemu_opt_script = '' |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 148 | self.qemuparams = '' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 149 | self.nfs_server = '' |
| 150 | self.rootfs = '' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 151 | # File name(s) of a OVMF firmware file or variable store, |
| 152 | # to be added with -drive if=pflash. |
| 153 | # Found in the same places as the rootfs, with or without one of |
| 154 | # these suffices: qcow2, bin. |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 155 | self.ovmf_bios = [] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 156 | # When enrolling default Secure Boot keys, the hypervisor |
| 157 | # must provide the Platform Key and the first Key Exchange Key |
| 158 | # certificate in the Type 11 SMBIOS table. |
| 159 | self.ovmf_secboot_pkkek1 = '' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 160 | self.qemuboot = '' |
| 161 | self.qbconfload = False |
| 162 | self.kernel = '' |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 163 | self.bios = '' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 164 | self.kernel_cmdline = '' |
| 165 | self.kernel_cmdline_script = '' |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 166 | self.bootparams = '' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 167 | self.dtb = '' |
| 168 | self.fstype = '' |
| 169 | self.kvm_enabled = False |
| 170 | self.vhost_enabled = False |
| 171 | self.slirp_enabled = False |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 172 | self.net_bridge = None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 173 | self.nfs_instance = 0 |
| 174 | self.nfs_running = False |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 175 | self.serialconsole = False |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 176 | self.serialstdio = False |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 177 | self.nographic = False |
| 178 | self.sdl = False |
| 179 | self.gtk = False |
| 180 | self.gl = False |
| 181 | self.gl_es = False |
| 182 | self.egl_headless = False |
| 183 | self.novga = False |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 184 | self.cleantap = False |
| 185 | self.saved_stty = '' |
| 186 | self.audio_enabled = False |
| 187 | self.tcpserial_portnum = '' |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 188 | self.taplock = '' |
| 189 | self.taplock_descriptor = None |
| 190 | self.portlocks = {} |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 191 | self.bitbake_e = '' |
| 192 | self.snapshot = False |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 193 | self.wictypes = ('wic', 'wic.vmdk', 'wic.qcow2', 'wic.vdi', "wic.vhd", "wic.vhdx") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 194 | self.fstypes = ('ext2', 'ext3', 'ext4', 'jffs2', 'nfs', 'btrfs', |
| 195 | 'cpio.gz', 'cpio', 'ramfs', 'tar.bz2', 'tar.gz') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 196 | self.vmtypes = ('hddimg', 'iso') |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 197 | self.fsinfo = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 198 | self.network_device = "-device e1000,netdev=net0,mac=@MAC@" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 199 | self.cmdline_ip_slirp = "ip=dhcp" |
| 200 | self.cmdline_ip_tap = "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 201 | # Use different mac section for tap and slirp to avoid |
| 202 | # conflicts, e.g., when one is running with tap, the other is |
| 203 | # running with slirp. |
| 204 | # The last section is dynamic, which is for avoiding conflicts, |
| 205 | # when multiple qemus are running, e.g., when multiple tap or |
| 206 | # slirp qemus are running. |
| 207 | self.mac_tap = "52:54:00:12:34:" |
| 208 | self.mac_slirp = "52:54:00:12:35:" |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 209 | # pid of the actual qemu process |
| 210 | self.qemupid = None |
| 211 | # avoid cleanup twice |
| 212 | self.cleaned = False |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 213 | # Files to cleanup after run |
| 214 | self.cleanup_files = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 215 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 216 | def acquire_taplock(self, error=True): |
| 217 | logger.debug("Acquiring lockfile %s..." % self.taplock) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 218 | try: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 219 | self.taplock_descriptor = open(self.taplock, 'w') |
| 220 | fcntl.flock(self.taplock_descriptor, fcntl.LOCK_EX|fcntl.LOCK_NB) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 221 | except Exception as e: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 222 | msg = "Acquiring lockfile %s failed: %s" % (self.taplock, e) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 223 | if error: |
| 224 | logger.error(msg) |
| 225 | else: |
| 226 | logger.info(msg) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 227 | if self.taplock_descriptor: |
| 228 | self.taplock_descriptor.close() |
| 229 | self.taplock_descriptor = None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 230 | return False |
| 231 | return True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 232 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 233 | def release_taplock(self): |
| 234 | if self.taplock_descriptor: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 235 | logger.debug("Releasing lockfile for tap device '%s'" % self.tap) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 236 | fcntl.flock(self.taplock_descriptor, fcntl.LOCK_UN) |
| 237 | self.taplock_descriptor.close() |
| 238 | os.remove(self.taplock) |
| 239 | self.taplock_descriptor = None |
| 240 | |
| 241 | def check_free_port(self, host, port, lockdir): |
| 242 | """ Check whether the port is free or not """ |
| 243 | import socket |
| 244 | from contextlib import closing |
| 245 | |
| 246 | lockfile = os.path.join(lockdir, str(port) + '.lock') |
| 247 | if self.acquire_portlock(lockfile): |
| 248 | with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: |
| 249 | if sock.connect_ex((host, port)) == 0: |
| 250 | # Port is open, so not free |
| 251 | self.release_portlock(lockfile) |
| 252 | return False |
| 253 | else: |
| 254 | # Port is not open, so free |
| 255 | return True |
| 256 | else: |
| 257 | return False |
| 258 | |
| 259 | def acquire_portlock(self, lockfile): |
| 260 | logger.debug("Acquiring lockfile %s..." % lockfile) |
| 261 | try: |
| 262 | portlock_descriptor = open(lockfile, 'w') |
| 263 | self.portlocks.update({lockfile: portlock_descriptor}) |
| 264 | fcntl.flock(self.portlocks[lockfile], fcntl.LOCK_EX|fcntl.LOCK_NB) |
| 265 | except Exception as e: |
| 266 | msg = "Acquiring lockfile %s failed: %s" % (lockfile, e) |
| 267 | logger.info(msg) |
| 268 | if lockfile in self.portlocks.keys() and self.portlocks[lockfile]: |
| 269 | self.portlocks[lockfile].close() |
| 270 | del self.portlocks[lockfile] |
| 271 | return False |
| 272 | return True |
| 273 | |
| 274 | def release_portlock(self, lockfile=None): |
| 275 | if lockfile != None: |
| 276 | logger.debug("Releasing lockfile '%s'" % lockfile) |
| 277 | fcntl.flock(self.portlocks[lockfile], fcntl.LOCK_UN) |
| 278 | self.portlocks[lockfile].close() |
| 279 | os.remove(lockfile) |
| 280 | del self.portlocks[lockfile] |
| 281 | elif len(self.portlocks): |
| 282 | for lockfile, descriptor in self.portlocks.items(): |
| 283 | logger.debug("Releasing lockfile '%s'" % lockfile) |
| 284 | fcntl.flock(descriptor, fcntl.LOCK_UN) |
| 285 | descriptor.close() |
| 286 | os.remove(lockfile) |
| 287 | self.portlocks = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 289 | def get(self, key): |
| 290 | if key in self.d: |
| 291 | return self.d.get(key) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 292 | elif os.getenv(key): |
| 293 | return os.getenv(key) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 294 | else: |
| 295 | return '' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 296 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 297 | def set(self, key, value): |
| 298 | self.d[key] = value |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 299 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 300 | def is_deploy_dir_image(self, p): |
| 301 | if os.path.isdir(p): |
| 302 | if not re.search('.qemuboot.conf$', '\n'.join(os.listdir(p)), re.M): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 303 | logger.debug("Can't find required *.qemuboot.conf in %s" % p) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 304 | return False |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 305 | if not any(map(lambda name: '-image-' in name, os.listdir(p))): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 306 | logger.debug("Can't find *-image-* in %s" % p) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 307 | return False |
| 308 | return True |
| 309 | else: |
| 310 | return False |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 311 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 312 | def check_arg_fstype(self, fst): |
| 313 | """Check and set FSTYPE""" |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 314 | if fst not in self.fstypes + self.vmtypes + self.wictypes: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 315 | logger.warning("Maybe unsupported FSTYPE: %s" % fst) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 316 | if not self.fstype or self.fstype == fst: |
| 317 | if fst == 'ramfs': |
| 318 | fst = 'cpio.gz' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 319 | if fst in ('tar.bz2', 'tar.gz'): |
| 320 | fst = 'nfs' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 321 | self.fstype = fst |
| 322 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 323 | raise RunQemuError("Conflicting: FSTYPE %s and %s" % (self.fstype, fst)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 324 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 325 | def set_machine_deploy_dir(self, machine, deploy_dir_image): |
| 326 | """Set MACHINE and DEPLOY_DIR_IMAGE""" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 327 | logger.debug('MACHINE: %s' % machine) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 328 | self.set("MACHINE", machine) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 329 | logger.debug('DEPLOY_DIR_IMAGE: %s' % deploy_dir_image) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 330 | self.set("DEPLOY_DIR_IMAGE", deploy_dir_image) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 331 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 332 | def check_arg_nfs(self, p): |
| 333 | if os.path.isdir(p): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 334 | self.rootfs = p |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 335 | else: |
| 336 | m = re.match('(.*):(.*)', p) |
| 337 | self.nfs_server = m.group(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 338 | self.rootfs = m.group(2) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 339 | self.check_arg_fstype('nfs') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 340 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 341 | def check_arg_path(self, p): |
| 342 | """ |
| 343 | - Check whether it is <image>.qemuboot.conf or contains <image>.qemuboot.conf |
| 344 | - Check whether is a kernel file |
| 345 | - Check whether is a image file |
| 346 | - Check whether it is a nfs dir |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 347 | - Check whether it is a OVMF flash file |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 348 | """ |
| 349 | if p.endswith('.qemuboot.conf'): |
| 350 | self.qemuboot = p |
| 351 | self.qbconfload = True |
| 352 | elif re.search('\.bin$', p) or re.search('bzImage', p) or \ |
| 353 | re.search('zImage', p) or re.search('vmlinux', p) or \ |
| 354 | re.search('fitImage', p) or re.search('uImage', p): |
| 355 | self.kernel = p |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 356 | elif os.path.exists(p) and (not os.path.isdir(p)) and '-image-' in os.path.basename(p): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 357 | self.rootfs = p |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 358 | # Check filename against self.fstypes can hanlde <file>.cpio.gz, |
| 359 | # otherwise, its type would be "gz", which is incorrect. |
| 360 | fst = "" |
| 361 | for t in self.fstypes: |
| 362 | if p.endswith(t): |
| 363 | fst = t |
| 364 | break |
| 365 | if not fst: |
| 366 | m = re.search('.*\.(.*)$', self.rootfs) |
| 367 | if m: |
| 368 | fst = m.group(1) |
| 369 | if fst: |
| 370 | self.check_arg_fstype(fst) |
| 371 | qb = re.sub('\.' + fst + "$", '', self.rootfs) |
| 372 | qb = '%s%s' % (re.sub('\.rootfs$', '', qb), '.qemuboot.conf') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 373 | if os.path.exists(qb): |
| 374 | self.qemuboot = qb |
| 375 | self.qbconfload = True |
| 376 | else: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 377 | logger.warning("%s doesn't exist" % qb) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 378 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 379 | raise RunQemuError("Can't find FSTYPE from: %s" % p) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 380 | |
| 381 | elif os.path.isdir(p) or re.search(':', p) and re.search('/', p): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 382 | if self.is_deploy_dir_image(p): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 383 | logger.debug('DEPLOY_DIR_IMAGE: %s' % p) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 384 | self.set("DEPLOY_DIR_IMAGE", p) |
| 385 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 386 | logger.debug("Assuming %s is an nfs rootfs" % p) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 387 | self.check_arg_nfs(p) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 388 | elif os.path.basename(p).startswith('ovmf'): |
| 389 | self.ovmf_bios.append(p) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 390 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 391 | raise RunQemuError("Unknown path arg %s" % p) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 392 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 393 | def check_arg_machine(self, arg): |
| 394 | """Check whether it is a machine""" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 395 | if self.get('MACHINE') == arg: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 396 | return |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 397 | elif self.get('MACHINE') and self.get('MACHINE') != arg: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 398 | raise RunQemuError("Maybe conflicted MACHINE: %s vs %s" % (self.get('MACHINE'), arg)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 399 | elif re.search('/', arg): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 400 | raise RunQemuError("Unknown arg: %s" % arg) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 401 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 402 | logger.debug('Assuming MACHINE = %s' % arg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 403 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 404 | # if we're running under testimage, or similarly as a child |
| 405 | # of an existing bitbake invocation, we can't invoke bitbake |
| 406 | # to validate the MACHINE setting and must assume it's correct... |
| 407 | # FIXME: testimage.bbclass exports these two variables into env, |
| 408 | # are there other scenarios in which we need to support being |
| 409 | # invoked by bitbake? |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 410 | deploy = self.get('DEPLOY_DIR_IMAGE') |
| 411 | bbchild = deploy and self.get('OE_TMPDIR') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 412 | if bbchild: |
| 413 | self.set_machine_deploy_dir(arg, deploy) |
| 414 | return |
| 415 | # also check whether we're running under a sourced toolchain |
| 416 | # environment file |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 417 | if self.get('OECORE_NATIVE_SYSROOT'): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 418 | self.set("MACHINE", arg) |
| 419 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 420 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 421 | self.bitbake_e = self.run_bitbake_env(arg) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 422 | # bitbake -e doesn't report invalid MACHINE as an error, so |
| 423 | # let's check DEPLOY_DIR_IMAGE to make sure that it is a valid |
| 424 | # MACHINE. |
| 425 | s = re.search('^DEPLOY_DIR_IMAGE="(.*)"', self.bitbake_e, re.M) |
| 426 | if s: |
| 427 | deploy_dir_image = s.group(1) |
| 428 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 429 | raise RunQemuError("bitbake -e %s" % self.bitbake_e) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 430 | if self.is_deploy_dir_image(deploy_dir_image): |
| 431 | self.set_machine_deploy_dir(arg, deploy_dir_image) |
| 432 | else: |
| 433 | logger.error("%s not a directory valid DEPLOY_DIR_IMAGE" % deploy_dir_image) |
| 434 | self.set("MACHINE", arg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 435 | |
Andrew Geissler | c182c62 | 2020-05-15 14:13:32 -0500 | [diff] [blame] | 436 | def set_dri_path(self): |
| 437 | # As runqemu can be run within bitbake (when using testimage, for example), |
| 438 | # we need to ensure that we run host pkg-config, and that it does not |
| 439 | # get mis-directed to native build paths set by bitbake. |
| 440 | try: |
| 441 | del os.environ['PKG_CONFIG_PATH'] |
| 442 | del os.environ['PKG_CONFIG_DIR'] |
| 443 | del os.environ['PKG_CONFIG_LIBDIR'] |
| 444 | del os.environ['PKG_CONFIG_SYSROOT_DIR'] |
| 445 | except KeyError: |
| 446 | pass |
| 447 | try: |
| 448 | dripath = subprocess.check_output("PATH=/bin:/usr/bin:$PATH pkg-config --variable=dridriverdir dri", shell=True) |
| 449 | except subprocess.CalledProcessError as e: |
| 450 | raise RunQemuError("Could not determine the path to dri drivers on the host via pkg-config.\nPlease install Mesa development files (particularly, dri.pc) on the host machine.") |
| 451 | os.environ['LIBGL_DRIVERS_PATH'] = dripath.decode('utf-8').strip() |
| 452 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 453 | def check_args(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 454 | for debug in ("-d", "--debug"): |
| 455 | if debug in sys.argv: |
| 456 | logger.setLevel(logging.DEBUG) |
| 457 | sys.argv.remove(debug) |
| 458 | |
| 459 | for quiet in ("-q", "--quiet"): |
| 460 | if quiet in sys.argv: |
| 461 | logger.setLevel(logging.ERROR) |
| 462 | sys.argv.remove(quiet) |
| 463 | |
Andrew Geissler | c182c62 | 2020-05-15 14:13:32 -0500 | [diff] [blame] | 464 | if 'gl' not in sys.argv[1:] and 'gl-es' not in sys.argv[1:]: |
| 465 | os.environ['SDL_RENDER_DRIVER'] = 'software' |
| 466 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 467 | unknown_arg = "" |
| 468 | for arg in sys.argv[1:]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 469 | if arg in self.fstypes + self.vmtypes + self.wictypes: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 470 | self.check_arg_fstype(arg) |
| 471 | elif arg == 'nographic': |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 472 | self.nographic = True |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 473 | elif arg == 'sdl': |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 474 | self.sdl = True |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 475 | elif arg == 'gtk': |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 476 | self.gtk = True |
| 477 | elif arg == 'gl': |
| 478 | self.gl = True |
| 479 | elif 'gl-es' in sys.argv[1:]: |
| 480 | self.gl_es = True |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 481 | elif arg == 'egl-headless': |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 482 | self.egl_headless = True |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 483 | elif arg == 'novga': |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 484 | self.novga = True |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 485 | elif arg == 'serial': |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 486 | self.serialconsole = True |
| 487 | elif arg == "serialstdio": |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 488 | self.serialstdio = True |
| 489 | elif arg == 'audio': |
| 490 | logger.info("Enabling audio in qemu") |
| 491 | logger.info("Please install sound drivers in linux host") |
| 492 | self.audio_enabled = True |
| 493 | elif arg == 'kvm': |
| 494 | self.kvm_enabled = True |
| 495 | elif arg == 'kvm-vhost': |
| 496 | self.vhost_enabled = True |
| 497 | elif arg == 'slirp': |
| 498 | self.slirp_enabled = True |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 499 | elif arg.startswith('bridge='): |
| 500 | self.net_bridge = '%s' % arg[len('bridge='):] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 501 | elif arg == 'snapshot': |
| 502 | self.snapshot = True |
| 503 | elif arg == 'publicvnc': |
| 504 | self.qemu_opt_script += ' -vnc :0' |
| 505 | elif arg.startswith('tcpserial='): |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 506 | self.tcpserial_portnum = '%s' % arg[len('tcpserial='):] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 507 | elif arg.startswith('qemuparams='): |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 508 | self.qemuparams = ' %s' % arg[len('qemuparams='):] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 509 | elif arg.startswith('bootparams='): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 510 | self.bootparams = arg[len('bootparams='):] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 511 | elif os.path.exists(arg) or (re.search(':', arg) and re.search('/', arg)): |
| 512 | self.check_arg_path(os.path.abspath(arg)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 513 | elif re.search(r'-image-|-image$', arg): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 514 | # Lazy rootfs |
| 515 | self.rootfs = arg |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 516 | elif arg.startswith('ovmf'): |
| 517 | self.ovmf_bios.append(arg) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 518 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 519 | # At last, assume it is the MACHINE |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 520 | if (not unknown_arg) or unknown_arg == arg: |
| 521 | unknown_arg = arg |
| 522 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 523 | raise RunQemuError("Can't handle two unknown args: %s %s\n" |
| 524 | "Try 'runqemu help' on how to use it" % \ |
| 525 | (unknown_arg, arg)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 526 | # Check to make sure it is a valid machine |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 527 | if unknown_arg and self.get('MACHINE') != unknown_arg: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 528 | if self.get('DEPLOY_DIR_IMAGE'): |
| 529 | machine = os.path.basename(self.get('DEPLOY_DIR_IMAGE')) |
| 530 | if unknown_arg == machine: |
| 531 | self.set("MACHINE", machine) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 532 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 533 | self.check_arg_machine(unknown_arg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 534 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 535 | if not (self.get('DEPLOY_DIR_IMAGE') or self.qbconfload): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 536 | self.load_bitbake_env() |
| 537 | s = re.search('^DEPLOY_DIR_IMAGE="(.*)"', self.bitbake_e, re.M) |
| 538 | if s: |
| 539 | self.set("DEPLOY_DIR_IMAGE", s.group(1)) |
| 540 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 541 | def check_kvm(self): |
| 542 | """Check kvm and kvm-host""" |
| 543 | if not (self.kvm_enabled or self.vhost_enabled): |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 544 | self.qemu_opt_script += ' %s %s %s' % (self.get('QB_MACHINE'), self.get('QB_CPU'), self.get('QB_SMP')) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 545 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 546 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 547 | if not self.get('QB_CPU_KVM'): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 548 | raise RunQemuError("QB_CPU_KVM is NULL, this board doesn't support kvm") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 549 | |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 550 | self.qemu_opt_script += ' %s %s %s' % (self.get('QB_MACHINE'), self.get('QB_CPU_KVM'), self.get('QB_SMP')) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 551 | yocto_kvm_wiki = "https://wiki.yoctoproject.org/wiki/How_to_enable_KVM_for_Poky_qemu" |
| 552 | yocto_paravirt_kvm_wiki = "https://wiki.yoctoproject.org/wiki/Running_an_x86_Yocto_Linux_image_under_QEMU_KVM" |
| 553 | dev_kvm = '/dev/kvm' |
| 554 | dev_vhost = '/dev/vhost-net' |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 555 | if self.qemu_system.endswith(('i386', 'x86_64')): |
| 556 | with open('/proc/cpuinfo', 'r') as f: |
| 557 | kvm_cap = re.search('vmx|svm', "".join(f.readlines())) |
| 558 | if not kvm_cap: |
| 559 | logger.error("You are trying to enable KVM on a cpu without VT support.") |
| 560 | logger.error("Remove kvm from the command-line, or refer:") |
| 561 | raise RunQemuError(yocto_kvm_wiki) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 562 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 563 | if not os.path.exists(dev_kvm): |
| 564 | logger.error("Missing KVM device. Have you inserted kvm modules?") |
| 565 | logger.error("For further help see:") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 566 | raise RunQemuError(yocto_kvm_wiki) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 567 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 568 | if os.access(dev_kvm, os.W_OK|os.R_OK): |
| 569 | self.qemu_opt_script += ' -enable-kvm' |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 570 | if self.get('MACHINE') == "qemux86": |
| 571 | # Workaround for broken APIC window on pre 4.15 host kernels which causes boot hangs |
| 572 | # See YOCTO #12301 |
| 573 | # On 64 bit we use x2apic |
| 574 | self.kernel_cmdline_script += " clocksource=kvm-clock hpet=disable noapic nolapic" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 575 | else: |
| 576 | logger.error("You have no read or write permission on /dev/kvm.") |
| 577 | logger.error("Please change the ownership of this file as described at:") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 578 | raise RunQemuError(yocto_kvm_wiki) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 579 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 580 | if self.vhost_enabled: |
| 581 | if not os.path.exists(dev_vhost): |
| 582 | logger.error("Missing virtio net device. Have you inserted vhost-net module?") |
| 583 | logger.error("For further help see:") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 584 | raise RunQemuError(yocto_paravirt_kvm_wiki) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 585 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 586 | if not os.access(dev_vhost, os.W_OK|os.R_OK): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 587 | logger.error("You have no read or write permission on /dev/vhost-net.") |
| 588 | logger.error("Please change the ownership of this file as described at:") |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 589 | raise RunQemuError(yocto_paravirt_kvm_wiki) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 590 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 591 | def check_fstype(self): |
| 592 | """Check and setup FSTYPE""" |
| 593 | if not self.fstype: |
| 594 | fstype = self.get('QB_DEFAULT_FSTYPE') |
| 595 | if fstype: |
| 596 | self.fstype = fstype |
| 597 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 598 | raise RunQemuError("FSTYPE is NULL!") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 599 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 600 | # parse QB_FSINFO into dict, e.g. { 'wic': ['no-kernel-in-fs', 'a-flag'], 'ext4': ['another-flag']} |
| 601 | wic_fs = False |
| 602 | qb_fsinfo = self.get('QB_FSINFO') |
| 603 | if qb_fsinfo: |
| 604 | qb_fsinfo = qb_fsinfo.split() |
| 605 | for fsinfo in qb_fsinfo: |
| 606 | try: |
| 607 | fstype, fsflag = fsinfo.split(':') |
| 608 | |
| 609 | if fstype == 'wic': |
| 610 | if fsflag == 'no-kernel-in-fs': |
| 611 | wic_fs = True |
| 612 | elif fsflag == 'kernel-in-fs': |
| 613 | wic_fs = False |
| 614 | else: |
| 615 | logger.warn('Unknown flag "%s:%s" in QB_FSINFO', fstype, fsflag) |
| 616 | continue |
| 617 | else: |
| 618 | logger.warn('QB_FSINFO is not supported for image type "%s"', fstype) |
| 619 | continue |
| 620 | |
| 621 | if fstype in self.fsinfo: |
| 622 | self.fsinfo[fstype].append(fsflag) |
| 623 | else: |
| 624 | self.fsinfo[fstype] = [fsflag] |
| 625 | except Exception: |
| 626 | logger.error('Invalid parameter "%s" in QB_FSINFO', fsinfo) |
| 627 | |
| 628 | # treat wic images as vmimages (with kernel) or as fsimages (rootfs only) |
| 629 | if wic_fs: |
| 630 | self.fstypes = self.fstypes + self.wictypes |
| 631 | else: |
| 632 | self.vmtypes = self.vmtypes + self.wictypes |
| 633 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 634 | def check_rootfs(self): |
| 635 | """Check and set rootfs""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 636 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 637 | if self.fstype == "none": |
| 638 | return |
| 639 | |
| 640 | if self.get('ROOTFS'): |
| 641 | if not self.rootfs: |
| 642 | self.rootfs = self.get('ROOTFS') |
| 643 | elif self.get('ROOTFS') != self.rootfs: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 644 | raise RunQemuError("Maybe conflicted ROOTFS: %s vs %s" % (self.get('ROOTFS'), self.rootfs)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 645 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 646 | if self.fstype == 'nfs': |
| 647 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 648 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 649 | if self.rootfs and not os.path.exists(self.rootfs): |
| 650 | # Lazy rootfs |
| 651 | self.rootfs = "%s/%s-%s.%s" % (self.get('DEPLOY_DIR_IMAGE'), |
| 652 | self.rootfs, self.get('MACHINE'), |
| 653 | self.fstype) |
| 654 | elif not self.rootfs: |
| 655 | cmd_name = '%s/%s*.%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_NAME'), self.fstype) |
| 656 | cmd_link = '%s/%s*.%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_LINK_NAME'), self.fstype) |
| 657 | cmds = (cmd_name, cmd_link) |
| 658 | self.rootfs = get_first_file(cmds) |
| 659 | if not self.rootfs: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 660 | raise RunQemuError("Failed to find rootfs: %s or %s" % cmds) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 661 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 662 | if not os.path.exists(self.rootfs): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 663 | raise RunQemuError("Can't find rootfs: %s" % self.rootfs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 664 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 665 | def setup_pkkek1(self): |
| 666 | """ |
| 667 | Extract from PEM certificate the Platform Key and first Key |
| 668 | Exchange Key certificate string. The hypervisor needs to provide |
| 669 | it in the Type 11 SMBIOS table |
| 670 | """ |
| 671 | pemcert = '%s/%s' % (self.get('DEPLOY_DIR_IMAGE'), 'OvmfPkKek1.pem') |
| 672 | try: |
| 673 | with open(pemcert, 'r') as pemfile: |
| 674 | key = pemfile.read().replace('\n', ''). \ |
| 675 | replace('-----BEGIN CERTIFICATE-----', ''). \ |
| 676 | replace('-----END CERTIFICATE-----', '') |
| 677 | self.ovmf_secboot_pkkek1 = key |
| 678 | |
| 679 | except FileNotFoundError: |
| 680 | raise RunQemuError("Can't open PEM certificate %s " % pemcert) |
| 681 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 682 | def check_ovmf(self): |
| 683 | """Check and set full path for OVMF firmware and variable file(s).""" |
| 684 | |
| 685 | for index, ovmf in enumerate(self.ovmf_bios): |
| 686 | if os.path.exists(ovmf): |
| 687 | continue |
| 688 | for suffix in ('qcow2', 'bin'): |
| 689 | path = '%s/%s.%s' % (self.get('DEPLOY_DIR_IMAGE'), ovmf, suffix) |
| 690 | if os.path.exists(path): |
| 691 | self.ovmf_bios[index] = path |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 692 | if ovmf.endswith('secboot'): |
| 693 | self.setup_pkkek1() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 694 | break |
| 695 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 696 | raise RunQemuError("Can't find OVMF firmware: %s" % ovmf) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 697 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 698 | def check_kernel(self): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 699 | """Check and set kernel""" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 700 | # The vm image doesn't need a kernel |
| 701 | if self.fstype in self.vmtypes: |
| 702 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 703 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 704 | # See if the user supplied a KERNEL option |
| 705 | if self.get('KERNEL'): |
| 706 | self.kernel = self.get('KERNEL') |
| 707 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 708 | # QB_DEFAULT_KERNEL is always a full file path |
| 709 | kernel_name = os.path.basename(self.get('QB_DEFAULT_KERNEL')) |
| 710 | |
| 711 | # The user didn't want a kernel to be loaded |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 712 | if kernel_name == "none" and not self.kernel: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 713 | return |
| 714 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 715 | deploy_dir_image = self.get('DEPLOY_DIR_IMAGE') |
| 716 | if not self.kernel: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 717 | kernel_match_name = "%s/%s" % (deploy_dir_image, kernel_name) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 718 | kernel_match_link = "%s/%s" % (deploy_dir_image, self.get('KERNEL_IMAGETYPE')) |
| 719 | kernel_startswith = "%s/%s*" % (deploy_dir_image, self.get('KERNEL_IMAGETYPE')) |
| 720 | cmds = (kernel_match_name, kernel_match_link, kernel_startswith) |
| 721 | self.kernel = get_first_file(cmds) |
| 722 | if not self.kernel: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 723 | raise RunQemuError('KERNEL not found: %s, %s or %s' % cmds) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 724 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 725 | if not os.path.exists(self.kernel): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 726 | raise RunQemuError("KERNEL %s not found" % self.kernel) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 727 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 728 | def check_dtb(self): |
| 729 | """Check and set dtb""" |
| 730 | # Did the user specify a device tree? |
| 731 | if self.get('DEVICE_TREE'): |
| 732 | self.dtb = self.get('DEVICE_TREE') |
| 733 | if not os.path.exists(self.dtb): |
| 734 | raise RunQemuError('Specified DTB not found: %s' % self.dtb) |
| 735 | return |
| 736 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 737 | dtb = self.get('QB_DTB') |
| 738 | if dtb: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 739 | deploy_dir_image = self.get('DEPLOY_DIR_IMAGE') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 740 | cmd_match = "%s/%s" % (deploy_dir_image, dtb) |
| 741 | cmd_startswith = "%s/%s*" % (deploy_dir_image, dtb) |
| 742 | cmd_wild = "%s/*.dtb" % deploy_dir_image |
| 743 | cmds = (cmd_match, cmd_startswith, cmd_wild) |
| 744 | self.dtb = get_first_file(cmds) |
| 745 | if not os.path.exists(self.dtb): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 746 | raise RunQemuError('DTB not found: %s, %s or %s' % cmds) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 747 | |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 748 | def check_bios(self): |
| 749 | """Check and set bios""" |
| 750 | |
| 751 | # See if the user supplied a BIOS option |
| 752 | if self.get('BIOS'): |
| 753 | self.bios = self.get('BIOS') |
| 754 | |
| 755 | # QB_DEFAULT_BIOS is always a full file path |
| 756 | bios_name = os.path.basename(self.get('QB_DEFAULT_BIOS')) |
| 757 | |
| 758 | # The user didn't want a bios to be loaded |
| 759 | if (bios_name == "" or bios_name == "none") and not self.bios: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 760 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 761 | |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 762 | if not self.bios: |
| 763 | deploy_dir_image = self.get('DEPLOY_DIR_IMAGE') |
| 764 | self.bios = "%s/%s" % (deploy_dir_image, bios_name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 765 | |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 766 | if not self.bios: |
| 767 | raise RunQemuError('BIOS not found: %s' % bios_match_name) |
| 768 | |
| 769 | if not os.path.exists(self.bios): |
| 770 | raise RunQemuError("KERNEL %s not found" % self.bios) |
| 771 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 772 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 773 | def check_mem(self): |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 774 | """ |
| 775 | Both qemu and kernel needs memory settings, so check QB_MEM and set it |
| 776 | for both. |
| 777 | """ |
| 778 | s = re.search('-m +([0-9]+)', self.qemuparams) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 779 | if s: |
| 780 | self.set('QB_MEM', '-m %s' % s.group(1)) |
| 781 | elif not self.get('QB_MEM'): |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 782 | logger.info('QB_MEM is not set, use 256M by default') |
| 783 | self.set('QB_MEM', '-m 256') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 784 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 785 | # Check and remove M or m suffix |
| 786 | qb_mem = self.get('QB_MEM') |
| 787 | if qb_mem.endswith('M') or qb_mem.endswith('m'): |
| 788 | qb_mem = qb_mem[:-1] |
| 789 | |
| 790 | # Add -m prefix it not present |
| 791 | if not qb_mem.startswith('-m'): |
| 792 | qb_mem = '-m %s' % qb_mem |
| 793 | |
| 794 | self.set('QB_MEM', qb_mem) |
| 795 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 796 | mach = self.get('MACHINE') |
| 797 | if not mach.startswith('qemumips'): |
| 798 | self.kernel_cmdline_script += ' mem=%s' % self.get('QB_MEM').replace('-m','').strip() + 'M' |
| 799 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 800 | self.qemu_opt_script += ' %s' % self.get('QB_MEM') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 801 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 802 | def check_tcpserial(self): |
| 803 | if self.tcpserial_portnum: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 804 | ports = self.tcpserial_portnum.split(':') |
| 805 | port = ports[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 806 | if self.get('QB_TCPSERIAL_OPT'): |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 807 | self.qemu_opt_script += ' ' + self.get('QB_TCPSERIAL_OPT').replace('@PORT@', port) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 808 | else: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 809 | self.qemu_opt_script += ' -serial tcp:127.0.0.1:%s' % port |
| 810 | |
| 811 | if len(ports) > 1: |
| 812 | for port in ports[1:]: |
| 813 | self.qemu_opt_script += ' -serial tcp:127.0.0.1:%s' % port |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 814 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 815 | def check_and_set(self): |
| 816 | """Check configs sanity and set when needed""" |
| 817 | self.validate_paths() |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 818 | if not self.slirp_enabled and not self.net_bridge: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 819 | check_tun() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 820 | # Check audio |
| 821 | if self.audio_enabled: |
| 822 | if not self.get('QB_AUDIO_DRV'): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 823 | raise RunQemuError("QB_AUDIO_DRV is NULL, this board doesn't support audio") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 824 | if not self.get('QB_AUDIO_OPT'): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 825 | logger.warning('QB_AUDIO_OPT is NULL, you may need define it to make audio work') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 826 | else: |
| 827 | self.qemu_opt_script += ' %s' % self.get('QB_AUDIO_OPT') |
| 828 | os.putenv('QEMU_AUDIO_DRV', self.get('QB_AUDIO_DRV')) |
| 829 | else: |
| 830 | os.putenv('QEMU_AUDIO_DRV', 'none') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 831 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 832 | self.check_qemu_system() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 833 | self.check_kvm() |
| 834 | self.check_fstype() |
| 835 | self.check_rootfs() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 836 | self.check_ovmf() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 837 | self.check_kernel() |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 838 | self.check_dtb() |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 839 | self.check_bios() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 840 | self.check_mem() |
| 841 | self.check_tcpserial() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 842 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 843 | def read_qemuboot(self): |
| 844 | if not self.qemuboot: |
| 845 | if self.get('DEPLOY_DIR_IMAGE'): |
| 846 | deploy_dir_image = self.get('DEPLOY_DIR_IMAGE') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 847 | else: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 848 | logger.warning("Can't find qemuboot conf file, DEPLOY_DIR_IMAGE is NULL!") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 849 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 850 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 851 | if self.rootfs and not os.path.exists(self.rootfs): |
| 852 | # Lazy rootfs |
| 853 | machine = self.get('MACHINE') |
| 854 | if not machine: |
| 855 | machine = os.path.basename(deploy_dir_image) |
| 856 | self.qemuboot = "%s/%s-%s.qemuboot.conf" % (deploy_dir_image, |
| 857 | self.rootfs, machine) |
| 858 | else: |
| 859 | cmd = 'ls -t %s/*.qemuboot.conf' % deploy_dir_image |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 860 | logger.debug('Running %s...' % cmd) |
| 861 | try: |
| 862 | qbs = subprocess.check_output(cmd, shell=True).decode('utf-8') |
| 863 | except subprocess.CalledProcessError as err: |
| 864 | raise RunQemuError(err) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 865 | if qbs: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 866 | for qb in qbs.split(): |
| 867 | # Don't use initramfs when other choices unless fstype is ramfs |
| 868 | if '-initramfs-' in os.path.basename(qb) and self.fstype != 'cpio.gz': |
| 869 | continue |
| 870 | self.qemuboot = qb |
| 871 | break |
| 872 | if not self.qemuboot: |
| 873 | # Use the first one when no choice |
| 874 | self.qemuboot = qbs.split()[0] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 875 | self.qbconfload = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 876 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 877 | if not self.qemuboot: |
| 878 | # If we haven't found a .qemuboot.conf at this point it probably |
| 879 | # doesn't exist, continue without |
| 880 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 881 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 882 | if not os.path.exists(self.qemuboot): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 883 | raise RunQemuError("Failed to find %s (wrong image name or BSP does not support running under qemu?)." % self.qemuboot) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 884 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 885 | logger.debug('CONFFILE: %s' % self.qemuboot) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 886 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 887 | cf = configparser.ConfigParser() |
| 888 | cf.read(self.qemuboot) |
| 889 | for k, v in cf.items('config_bsp'): |
| 890 | k_upper = k.upper() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 891 | if v.startswith("../"): |
| 892 | v = os.path.abspath(os.path.dirname(self.qemuboot) + "/" + v) |
| 893 | elif v == ".": |
| 894 | v = os.path.dirname(self.qemuboot) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 895 | self.set(k_upper, v) |
| 896 | |
| 897 | def validate_paths(self): |
| 898 | """Ensure all relevant path variables are set""" |
| 899 | # When we're started with a *.qemuboot.conf arg assume that image |
| 900 | # artefacts are relative to that file, rather than in whatever |
| 901 | # directory DEPLOY_DIR_IMAGE in the conf file points to. |
| 902 | if self.qbconfload: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 903 | imgdir = os.path.realpath(os.path.dirname(self.qemuboot)) |
| 904 | if imgdir != os.path.realpath(self.get('DEPLOY_DIR_IMAGE')): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 905 | logger.info('Setting DEPLOY_DIR_IMAGE to folder containing %s (%s)' % (self.qemuboot, imgdir)) |
| 906 | self.set('DEPLOY_DIR_IMAGE', imgdir) |
| 907 | |
| 908 | # If the STAGING_*_NATIVE directories from the config file don't exist |
| 909 | # and we're in a sourced OE build directory try to extract the paths |
| 910 | # from `bitbake -e` |
| 911 | havenative = os.path.exists(self.get('STAGING_DIR_NATIVE')) and \ |
| 912 | os.path.exists(self.get('STAGING_BINDIR_NATIVE')) |
| 913 | |
| 914 | if not havenative: |
| 915 | if not self.bitbake_e: |
| 916 | self.load_bitbake_env() |
| 917 | |
| 918 | if self.bitbake_e: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 919 | native_vars = ['STAGING_DIR_NATIVE'] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 920 | for nv in native_vars: |
| 921 | s = re.search('^%s="(.*)"' % nv, self.bitbake_e, re.M) |
| 922 | if s and s.group(1) != self.get(nv): |
| 923 | logger.info('Overriding conf file setting of %s to %s from Bitbake environment' % (nv, s.group(1))) |
| 924 | self.set(nv, s.group(1)) |
| 925 | else: |
| 926 | # when we're invoked from a running bitbake instance we won't |
| 927 | # be able to call `bitbake -e`, then try: |
| 928 | # - get OE_TMPDIR from environment and guess paths based on it |
| 929 | # - get OECORE_NATIVE_SYSROOT from environment (for sdk) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 930 | tmpdir = self.get('OE_TMPDIR') |
| 931 | oecore_native_sysroot = self.get('OECORE_NATIVE_SYSROOT') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 932 | if tmpdir: |
| 933 | logger.info('Setting STAGING_DIR_NATIVE and STAGING_BINDIR_NATIVE relative to OE_TMPDIR (%s)' % tmpdir) |
| 934 | hostos, _, _, _, machine = os.uname() |
| 935 | buildsys = '%s-%s' % (machine, hostos.lower()) |
| 936 | staging_dir_native = '%s/sysroots/%s' % (tmpdir, buildsys) |
| 937 | self.set('STAGING_DIR_NATIVE', staging_dir_native) |
| 938 | elif oecore_native_sysroot: |
| 939 | logger.info('Setting STAGING_DIR_NATIVE to OECORE_NATIVE_SYSROOT (%s)' % oecore_native_sysroot) |
| 940 | self.set('STAGING_DIR_NATIVE', oecore_native_sysroot) |
| 941 | if self.get('STAGING_DIR_NATIVE'): |
| 942 | # we have to assume that STAGING_BINDIR_NATIVE is at usr/bin |
| 943 | staging_bindir_native = '%s/usr/bin' % self.get('STAGING_DIR_NATIVE') |
| 944 | logger.info('Setting STAGING_BINDIR_NATIVE to %s' % staging_bindir_native) |
| 945 | self.set('STAGING_BINDIR_NATIVE', '%s/usr/bin' % self.get('STAGING_DIR_NATIVE')) |
| 946 | |
| 947 | def print_config(self): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 948 | logoutput = ['Continuing with the following parameters:'] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 949 | if not self.fstype in self.vmtypes: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 950 | logoutput.append('KERNEL: [%s]' % self.kernel) |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 951 | if self.bios: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 952 | logoutput.append('BIOS: [%s]' % self.bios) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 953 | if self.dtb: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 954 | logoutput.append('DTB: [%s]' % self.dtb) |
| 955 | logoutput.append('MACHINE: [%s]' % self.get('MACHINE')) |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 956 | try: |
| 957 | fstype_flags = ' (' + ', '.join(self.fsinfo[self.fstype]) + ')' |
| 958 | except KeyError: |
| 959 | fstype_flags = '' |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 960 | logoutput.append('FSTYPE: [%s%s]' % (self.fstype, fstype_flags)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 961 | if self.fstype == 'nfs': |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 962 | logoutput.append('NFS_DIR: [%s]' % self.rootfs) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 963 | else: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 964 | logoutput.append('ROOTFS: [%s]' % self.rootfs) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 965 | if self.ovmf_bios: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 966 | logoutput.append('OVMF: %s' % self.ovmf_bios) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 967 | if (self.ovmf_secboot_pkkek1): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 968 | logoutput.append('SECBOOT PKKEK1: [%s...]' % self.ovmf_secboot_pkkek1[0:100]) |
| 969 | logoutput.append('CONFFILE: [%s]' % self.qemuboot) |
| 970 | logoutput.append('') |
| 971 | logger.info('\n'.join(logoutput)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 972 | |
| 973 | def setup_nfs(self): |
| 974 | if not self.nfs_server: |
| 975 | if self.slirp_enabled: |
| 976 | self.nfs_server = '10.0.2.2' |
| 977 | else: |
| 978 | self.nfs_server = '192.168.7.1' |
| 979 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 980 | # Figure out a new nfs_instance to allow multiple qemus running. |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 981 | ps = subprocess.check_output(("ps", "auxww")).decode('utf-8') |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 982 | pattern = '/bin/unfsd .* -i .*\.pid -e .*/exports([0-9]+) ' |
| 983 | all_instances = re.findall(pattern, ps, re.M) |
| 984 | if all_instances: |
| 985 | all_instances.sort(key=int) |
| 986 | self.nfs_instance = int(all_instances.pop()) + 1 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 987 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 988 | nfsd_port = 3049 + 2 * self.nfs_instance |
| 989 | mountd_port = 3048 + 2 * self.nfs_instance |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 990 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 991 | # Export vars for runqemu-export-rootfs |
| 992 | export_dict = { |
| 993 | 'NFS_INSTANCE': self.nfs_instance, |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 994 | 'NFSD_PORT': nfsd_port, |
| 995 | 'MOUNTD_PORT': mountd_port, |
| 996 | } |
| 997 | for k, v in export_dict.items(): |
| 998 | # Use '%s' since they are integers |
| 999 | os.putenv(k, '%s' % v) |
| 1000 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1001 | self.unfs_opts="nfsvers=3,port=%s,tcp,mountport=%s" % (nfsd_port, mountd_port) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1002 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1003 | # Extract .tar.bz2 or .tar.bz if no nfs dir |
| 1004 | if not (self.rootfs and os.path.isdir(self.rootfs)): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1005 | src_prefix = '%s/%s' % (self.get('DEPLOY_DIR_IMAGE'), self.get('IMAGE_LINK_NAME')) |
| 1006 | dest = "%s-nfsroot" % src_prefix |
| 1007 | if os.path.exists('%s.pseudo_state' % dest): |
| 1008 | logger.info('Use %s as NFS_DIR' % dest) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1009 | self.rootfs = dest |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1010 | else: |
| 1011 | src = "" |
| 1012 | src1 = '%s.tar.bz2' % src_prefix |
| 1013 | src2 = '%s.tar.gz' % src_prefix |
| 1014 | if os.path.exists(src1): |
| 1015 | src = src1 |
| 1016 | elif os.path.exists(src2): |
| 1017 | src = src2 |
| 1018 | if not src: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1019 | raise RunQemuError("No NFS_DIR is set, and can't find %s or %s to extract" % (src1, src2)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1020 | logger.info('NFS_DIR not found, extracting %s to %s' % (src, dest)) |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1021 | cmd = ('runqemu-extract-sdk', src, dest) |
| 1022 | logger.info('Running %s...' % str(cmd)) |
| 1023 | if subprocess.call(cmd) != 0: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1024 | raise RunQemuError('Failed to run %s' % cmd) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1025 | self.rootfs = dest |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 1026 | self.cleanup_files.append(self.rootfs) |
| 1027 | self.cleanup_files.append('%s.pseudo_state' % self.rootfs) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1028 | |
| 1029 | # Start the userspace NFS server |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1030 | cmd = ('runqemu-export-rootfs', 'start', self.rootfs) |
| 1031 | logger.info('Running %s...' % str(cmd)) |
| 1032 | if subprocess.call(cmd) != 0: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1033 | raise RunQemuError('Failed to run %s' % cmd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1034 | |
| 1035 | self.nfs_running = True |
| 1036 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1037 | def setup_net_bridge(self): |
| 1038 | self.set('NETWORK_CMD', '-netdev bridge,br=%s,id=net0,helper=%s -device virtio-net-pci,netdev=net0 ' % ( |
| 1039 | self.net_bridge, os.path.join(self.bindir_native, 'qemu-oe-bridge-helper'))) |
| 1040 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1041 | def setup_slirp(self): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 1042 | """Setup user networking""" |
| 1043 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1044 | if self.fstype == 'nfs': |
| 1045 | self.setup_nfs() |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1046 | netconf = " " + self.cmdline_ip_slirp |
| 1047 | logger.info("Network configuration:%s", netconf) |
| 1048 | self.kernel_cmdline_script += netconf |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1049 | # Port mapping |
| 1050 | hostfwd = ",hostfwd=tcp::2222-:22,hostfwd=tcp::2323-:23" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1051 | qb_slirp_opt_default = "-netdev user,id=net0%s,tftp=%s" % (hostfwd, self.get('DEPLOY_DIR_IMAGE')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1052 | qb_slirp_opt = self.get('QB_SLIRP_OPT') or qb_slirp_opt_default |
| 1053 | # Figure out the port |
| 1054 | ports = re.findall('hostfwd=[^-]*:([0-9]+)-[^,-]*', qb_slirp_opt) |
| 1055 | ports = [int(i) for i in ports] |
| 1056 | mac = 2 |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1057 | |
| 1058 | lockdir = "/tmp/qemu-port-locks" |
| 1059 | if not os.path.exists(lockdir): |
| 1060 | # There might be a race issue when multi runqemu processess are |
| 1061 | # running at the same time. |
| 1062 | try: |
| 1063 | os.mkdir(lockdir) |
| 1064 | os.chmod(lockdir, 0o777) |
| 1065 | except FileExistsError: |
| 1066 | pass |
| 1067 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1068 | # Find a free port to avoid conflicts |
| 1069 | for p in ports[:]: |
| 1070 | p_new = p |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1071 | while not self.check_free_port('localhost', p_new, lockdir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1072 | p_new += 1 |
| 1073 | mac += 1 |
| 1074 | while p_new in ports: |
| 1075 | p_new += 1 |
| 1076 | mac += 1 |
| 1077 | if p != p_new: |
| 1078 | ports.append(p_new) |
| 1079 | qb_slirp_opt = re.sub(':%s-' % p, ':%s-' % p_new, qb_slirp_opt) |
| 1080 | logger.info("Port forward changed: %s -> %s" % (p, p_new)) |
| 1081 | mac = "%s%02x" % (self.mac_slirp, mac) |
| 1082 | self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qb_slirp_opt)) |
| 1083 | # Print out port foward |
| 1084 | hostfwd = re.findall('(hostfwd=[^,]*)', qb_slirp_opt) |
| 1085 | if hostfwd: |
| 1086 | logger.info('Port forward: %s' % ' '.join(hostfwd)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1087 | |
| 1088 | def setup_tap(self): |
| 1089 | """Setup tap""" |
| 1090 | |
| 1091 | # This file is created when runqemu-gen-tapdevs creates a bank of tap |
| 1092 | # devices, indicating that the user should not bring up new ones using |
| 1093 | # sudo. |
| 1094 | nosudo_flag = '/etc/runqemu-nosudo' |
| 1095 | self.qemuifup = shutil.which('runqemu-ifup') |
| 1096 | self.qemuifdown = shutil.which('runqemu-ifdown') |
| 1097 | ip = shutil.which('ip') |
| 1098 | lockdir = "/tmp/qemu-tap-locks" |
| 1099 | |
| 1100 | if not (self.qemuifup and self.qemuifdown and ip): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1101 | logger.error("runqemu-ifup: %s" % self.qemuifup) |
| 1102 | logger.error("runqemu-ifdown: %s" % self.qemuifdown) |
| 1103 | logger.error("ip: %s" % ip) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1104 | raise OEPathError("runqemu-ifup, runqemu-ifdown or ip not found") |
| 1105 | |
| 1106 | if not os.path.exists(lockdir): |
| 1107 | # There might be a race issue when multi runqemu processess are |
| 1108 | # running at the same time. |
| 1109 | try: |
| 1110 | os.mkdir(lockdir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1111 | os.chmod(lockdir, 0o777) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1112 | except FileExistsError: |
| 1113 | pass |
| 1114 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1115 | cmd = (ip, 'link') |
| 1116 | logger.debug('Running %s...' % str(cmd)) |
| 1117 | ip_link = subprocess.check_output(cmd).decode('utf-8') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1118 | # Matches line like: 6: tap0: <foo> |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1119 | possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1120 | tap = "" |
| 1121 | for p in possibles: |
| 1122 | lockfile = os.path.join(lockdir, p) |
| 1123 | if os.path.exists('%s.skip' % lockfile): |
| 1124 | logger.info('Found %s.skip, skipping %s' % (lockfile, p)) |
| 1125 | continue |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1126 | self.taplock = lockfile + '.lock' |
| 1127 | if self.acquire_taplock(error=False): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1128 | tap = p |
| 1129 | logger.info("Using preconfigured tap device %s" % tap) |
| 1130 | logger.info("If this is not intended, touch %s.skip to make runqemu skip %s." %(lockfile, tap)) |
| 1131 | break |
| 1132 | |
| 1133 | if not tap: |
| 1134 | if os.path.exists(nosudo_flag): |
| 1135 | logger.error("Error: There are no available tap devices to use for networking,") |
| 1136 | logger.error("and I see %s exists, so I am not going to try creating" % nosudo_flag) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1137 | raise RunQemuError("a new one with sudo.") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1138 | |
| 1139 | gid = os.getgid() |
| 1140 | uid = os.getuid() |
| 1141 | logger.info("Setting up tap interface under sudo") |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1142 | cmd = ('sudo', self.qemuifup, str(uid), str(gid), self.bindir_native) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1143 | try: |
| 1144 | tap = subprocess.check_output(cmd).decode('utf-8').strip() |
| 1145 | except subprocess.CalledProcessError as e: |
| 1146 | logger.error('Setting up tap device failed:\n%s\nRun runqemu-gen-tapdevs to manually create one.' % str(e)) |
| 1147 | sys.exit(1) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1148 | lockfile = os.path.join(lockdir, tap) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1149 | self.taplock = lockfile + '.lock' |
| 1150 | self.acquire_taplock() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1151 | self.cleantap = True |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1152 | logger.debug('Created tap: %s' % tap) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1153 | |
| 1154 | if not tap: |
| 1155 | logger.error("Failed to setup tap device. Run runqemu-gen-tapdevs to manually create.") |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1156 | sys.exit(1) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1157 | self.tap = tap |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 1158 | tapnum = int(tap[3:]) |
| 1159 | gateway = tapnum * 2 + 1 |
| 1160 | client = gateway + 1 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1161 | if self.fstype == 'nfs': |
| 1162 | self.setup_nfs() |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1163 | netconf = " " + self.cmdline_ip_tap |
| 1164 | netconf = netconf.replace('@CLIENT@', str(client)) |
| 1165 | netconf = netconf.replace('@GATEWAY@', str(gateway)) |
| 1166 | logger.info("Network configuration:%s", netconf) |
| 1167 | self.kernel_cmdline_script += netconf |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1168 | mac = "%s%02x" % (self.mac_tap, client) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1169 | qb_tap_opt = self.get('QB_TAP_OPT') |
| 1170 | if qb_tap_opt: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1171 | qemu_tap_opt = qb_tap_opt.replace('@TAP@', tap) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1172 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1173 | qemu_tap_opt = "-netdev tap,id=net0,ifname=%s,script=no,downscript=no" % (self.tap) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1174 | |
| 1175 | if self.vhost_enabled: |
| 1176 | qemu_tap_opt += ',vhost=on' |
| 1177 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1178 | self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qemu_tap_opt)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1179 | |
| 1180 | def setup_network(self): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1181 | if self.get('QB_NET') == 'none': |
| 1182 | return |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1183 | if sys.stdin.isatty(): |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1184 | self.saved_stty = subprocess.check_output(("stty", "-g")).decode('utf-8').strip() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1185 | self.network_device = self.get('QB_NETWORK_DEVICE') or self.network_device |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1186 | if self.net_bridge: |
| 1187 | self.setup_net_bridge() |
| 1188 | elif self.slirp_enabled: |
| 1189 | self.cmdline_ip_slirp = self.get('QB_CMDLINE_IP_SLIRP') or self.cmdline_ip_slirp |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1190 | self.setup_slirp() |
| 1191 | else: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1192 | self.cmdline_ip_tap = self.get('QB_CMDLINE_IP_TAP') or self.cmdline_ip_tap |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1193 | self.setup_tap() |
| 1194 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1195 | def setup_rootfs(self): |
| 1196 | if self.get('QB_ROOTFS') == 'none': |
| 1197 | return |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1198 | if 'wic.' in self.fstype: |
| 1199 | self.fstype = self.fstype[4:] |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1200 | rootfs_format = self.fstype if self.fstype in ('vmdk', 'vhd', 'vhdx', 'qcow2', 'vdi') else 'raw' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1201 | |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 1202 | tmpfsdir = os.environ.get("RUNQEMU_TMPFS_DIR", None) |
| 1203 | if self.snapshot and tmpfsdir: |
| 1204 | newrootfs = os.path.join(tmpfsdir, os.path.basename(self.rootfs)) + "." + str(os.getpid()) |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame^] | 1205 | logger.info("Copying rootfs to %s" % newrootfs) |
| 1206 | copy_start = time.time() |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 1207 | shutil.copyfile(self.rootfs, newrootfs) |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame^] | 1208 | logger.info("Copy done in %s seconds" % (time.time() - copy_start)) |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 1209 | self.rootfs = newrootfs |
| 1210 | # Don't need a second copy now! |
| 1211 | self.snapshot = False |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 1212 | self.cleanup_files.append(newrootfs) |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 1213 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1214 | qb_rootfs_opt = self.get('QB_ROOTFS_OPT') |
| 1215 | if qb_rootfs_opt: |
| 1216 | self.rootfs_options = qb_rootfs_opt.replace('@ROOTFS@', self.rootfs) |
| 1217 | else: |
| 1218 | self.rootfs_options = '-drive file=%s,if=virtio,format=%s' % (self.rootfs, rootfs_format) |
| 1219 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1220 | qb_rootfs_extra_opt = self.get("QB_ROOTFS_EXTRA_OPT") |
| 1221 | if qb_rootfs_extra_opt and not qb_rootfs_extra_opt.startswith(","): |
| 1222 | qb_rootfs_extra_opt = "," + qb_rootfs_extra_opt |
| 1223 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1224 | if self.fstype in ('cpio.gz', 'cpio'): |
| 1225 | self.kernel_cmdline = 'root=/dev/ram0 rw debugshell' |
| 1226 | self.rootfs_options = '-initrd %s' % self.rootfs |
| 1227 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1228 | vm_drive = '' |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1229 | if self.fstype in self.vmtypes: |
| 1230 | if self.fstype == 'iso': |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1231 | vm_drive = '-drive file=%s,if=virtio,media=cdrom' % self.rootfs |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1232 | elif self.get('QB_DRIVE_TYPE'): |
| 1233 | drive_type = self.get('QB_DRIVE_TYPE') |
| 1234 | if drive_type.startswith("/dev/sd"): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1235 | logger.info('Using scsi drive') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1236 | vm_drive = '-drive if=none,id=hd,file=%s,format=%s -device virtio-scsi-pci,id=scsi -device scsi-hd,drive=hd%s' \ |
| 1237 | % (self.rootfs, rootfs_format, qb_rootfs_extra_opt) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1238 | elif drive_type.startswith("/dev/hd"): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 1239 | logger.info('Using ide drive') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1240 | vm_drive = "-drive file=%s,format=%s" % (self.rootfs, rootfs_format) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1241 | elif drive_type.startswith("/dev/vdb"): |
| 1242 | logger.info('Using block virtio drive'); |
| 1243 | vm_drive = '-drive id=disk0,file=%s,if=none,format=%s -device virtio-blk-device,drive=disk0%s' \ |
| 1244 | % (self.rootfs, rootfs_format,qb_rootfs_extra_opt) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1245 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1246 | # virtio might have been selected explicitly (just use it), or |
| 1247 | # is used as fallback (then warn about that). |
| 1248 | if not drive_type.startswith("/dev/vd"): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1249 | logger.warning("Unknown QB_DRIVE_TYPE: %s" % drive_type) |
| 1250 | logger.warning("Failed to figure out drive type, consider define or fix QB_DRIVE_TYPE") |
| 1251 | logger.warning('Trying to use virtio block drive') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1252 | vm_drive = '-drive if=virtio,file=%s,format=%s' % (self.rootfs, rootfs_format) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1253 | |
| 1254 | # All branches above set vm_drive. |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 1255 | self.rootfs_options = vm_drive |
| 1256 | if not self.fstype in self.vmtypes: |
| 1257 | self.rootfs_options += ' -no-reboot' |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 1258 | self.kernel_cmdline = 'root=%s rw' % (self.get('QB_KERNEL_ROOT')) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1259 | |
| 1260 | if self.fstype == 'nfs': |
| 1261 | self.rootfs_options = '' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1262 | k_root = '/dev/nfs nfsroot=%s:%s,%s' % (self.nfs_server, os.path.abspath(self.rootfs), self.unfs_opts) |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 1263 | self.kernel_cmdline = 'root=%s rw' % k_root |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1264 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1265 | if self.fstype == 'none': |
| 1266 | self.rootfs_options = '' |
| 1267 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1268 | self.set('ROOTFS_OPTIONS', self.rootfs_options) |
| 1269 | |
| 1270 | def guess_qb_system(self): |
| 1271 | """attempt to determine the appropriate qemu-system binary""" |
| 1272 | mach = self.get('MACHINE') |
| 1273 | if not mach: |
| 1274 | search = '.*(qemux86-64|qemux86|qemuarm64|qemuarm|qemumips64|qemumips64el|qemumipsel|qemumips|qemuppc).*' |
| 1275 | if self.rootfs: |
| 1276 | match = re.match(search, self.rootfs) |
| 1277 | if match: |
| 1278 | mach = match.group(1) |
| 1279 | elif self.kernel: |
| 1280 | match = re.match(search, self.kernel) |
| 1281 | if match: |
| 1282 | mach = match.group(1) |
| 1283 | |
| 1284 | if not mach: |
| 1285 | return None |
| 1286 | |
| 1287 | if mach == 'qemuarm': |
| 1288 | qbsys = 'arm' |
| 1289 | elif mach == 'qemuarm64': |
| 1290 | qbsys = 'aarch64' |
| 1291 | elif mach == 'qemux86': |
| 1292 | qbsys = 'i386' |
| 1293 | elif mach == 'qemux86-64': |
| 1294 | qbsys = 'x86_64' |
| 1295 | elif mach == 'qemuppc': |
| 1296 | qbsys = 'ppc' |
| 1297 | elif mach == 'qemumips': |
| 1298 | qbsys = 'mips' |
| 1299 | elif mach == 'qemumips64': |
| 1300 | qbsys = 'mips64' |
| 1301 | elif mach == 'qemumipsel': |
| 1302 | qbsys = 'mipsel' |
| 1303 | elif mach == 'qemumips64el': |
| 1304 | qbsys = 'mips64el' |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1305 | elif mach == 'qemuriscv64': |
| 1306 | qbsys = 'riscv64' |
| 1307 | elif mach == 'qemuriscv32': |
| 1308 | qbsys = 'riscv32' |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1309 | else: |
| 1310 | logger.error("Unable to determine QEMU PC System emulator for %s machine." % mach) |
| 1311 | logger.error("As %s is not among valid QEMU machines such as," % mach) |
| 1312 | logger.error("qemux86-64, qemux86, qemuarm64, qemuarm, qemumips64, qemumips64el, qemumipsel, qemumips, qemuppc") |
| 1313 | raise RunQemuError("Set qb_system_name with suitable QEMU PC System emulator in .*qemuboot.conf.") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1314 | |
| 1315 | return 'qemu-system-%s' % qbsys |
| 1316 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 1317 | def check_qemu_system(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1318 | qemu_system = self.get('QB_SYSTEM_NAME') |
| 1319 | if not qemu_system: |
| 1320 | qemu_system = self.guess_qb_system() |
| 1321 | if not qemu_system: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1322 | raise RunQemuError("Failed to boot, QB_SYSTEM_NAME is NULL!") |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 1323 | self.qemu_system = qemu_system |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1324 | |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1325 | def setup_vga(self): |
| 1326 | if self.nographic == True: |
| 1327 | if self.sdl == True: |
| 1328 | raise RunQemuError('Option nographic makes no sense alongside the sdl option.') |
| 1329 | if self.gtk == True: |
| 1330 | raise RunQemuError('Option nographic makes no sense alongside the gtk option.') |
| 1331 | self.qemu_opt += ' -nographic' |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1332 | |
| 1333 | if self.novga == True: |
| 1334 | self.qemu_opt += ' -vga none' |
| 1335 | return |
| 1336 | |
| 1337 | if (self.gl_es == True or self.gl == True) and (self.sdl == False and self.gtk == False): |
| 1338 | raise RunQemuError('Option gl/gl-es needs gtk or sdl option.') |
| 1339 | |
| 1340 | if self.sdl == True or self.gtk == True or self.egl_headless == True: |
| 1341 | self.set_dri_path() |
| 1342 | self.qemu_opt += ' -vga virtio -display ' |
| 1343 | if self.egl_headless == True: |
| 1344 | self.qemu_opt += 'egl-headless,' |
| 1345 | else: |
| 1346 | if self.sdl == True: |
| 1347 | self.qemu_opt += 'sdl,' |
| 1348 | elif self.gtk == True: |
| 1349 | self.qemu_opt += 'gtk,' |
| 1350 | |
| 1351 | if self.gl == True: |
| 1352 | self.qemu_opt += 'gl=on,' |
| 1353 | elif self.gl_es == True: |
| 1354 | self.qemu_opt += 'gl=es,' |
| 1355 | self.qemu_opt += 'show-cursor=on' |
| 1356 | |
| 1357 | self.qemu_opt += ' %s' %self.get('QB_GRAPHICS') |
| 1358 | |
| 1359 | def setup_serial(self): |
| 1360 | # Setup correct kernel command line for serial |
| 1361 | if self.serialstdio == True or self.serialconsole == True or self.nographic == True or self.tcpserial_portnum: |
| 1362 | for entry in self.get('SERIAL_CONSOLES').split(' '): |
| 1363 | self.kernel_cmdline_script += ' console=%s' %entry.split(';')[1] |
| 1364 | |
| 1365 | if self.serialstdio == True or self.nographic == True: |
| 1366 | self.qemu_opt += " -serial mon:stdio" |
| 1367 | else: |
| 1368 | self.qemu_opt += " -serial mon:vc" |
| 1369 | if self.serialconsole: |
| 1370 | if sys.stdin.isatty(): |
| 1371 | subprocess.check_call(("stty", "intr", "^]")) |
| 1372 | logger.info("Interrupt character is '^]'") |
| 1373 | |
| 1374 | self.qemu_opt += " %s" % self.get("QB_SERIAL_OPT") |
| 1375 | |
| 1376 | # We always wants ttyS0 and ttyS1 in qemu machines (see SERIAL_CONSOLES). |
| 1377 | # If no serial or serialtcp options were specified, only ttyS0 is created |
| 1378 | # and sysvinit shows an error trying to enable ttyS1: |
| 1379 | # INIT: Id "S1" respawning too fast: disabled for 5 minutes |
| 1380 | serial_num = len(re.findall("-serial", self.qemu_opt)) |
| 1381 | if serial_num < 2: |
| 1382 | self.qemu_opt += " -serial null" |
| 1383 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 1384 | def setup_final(self): |
| 1385 | qemu_bin = os.path.join(self.bindir_native, self.qemu_system) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1386 | |
| 1387 | # It is possible to have qemu-native in ASSUME_PROVIDED, and it won't |
| 1388 | # find QEMU in sysroot, it needs to use host's qemu. |
| 1389 | if not os.path.exists(qemu_bin): |
| 1390 | logger.info("QEMU binary not found in %s, trying host's QEMU" % qemu_bin) |
| 1391 | for path in (os.environ['PATH'] or '').split(':'): |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 1392 | qemu_bin_tmp = os.path.join(path, self.qemu_system) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1393 | logger.info("Trying: %s" % qemu_bin_tmp) |
| 1394 | if os.path.exists(qemu_bin_tmp): |
| 1395 | qemu_bin = qemu_bin_tmp |
| 1396 | if not os.path.isabs(qemu_bin): |
| 1397 | qemu_bin = os.path.abspath(qemu_bin) |
| 1398 | logger.info("Using host's QEMU: %s" % qemu_bin) |
| 1399 | break |
| 1400 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1401 | if not os.access(qemu_bin, os.X_OK): |
| 1402 | raise OEPathError("No QEMU binary '%s' could be found" % qemu_bin) |
| 1403 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 1404 | self.qemu_opt = "%s %s %s %s %s" % (qemu_bin, self.get('NETWORK_CMD'), self.get('QB_RNG'), self.get('ROOTFS_OPTIONS'), self.get('QB_OPT_APPEND')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1405 | |
| 1406 | for ovmf in self.ovmf_bios: |
| 1407 | format = ovmf.rsplit('.', 1)[-1] |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1408 | if format == "bin": |
| 1409 | format = "raw" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1410 | self.qemu_opt += ' -drive if=pflash,format=%s,file=%s' % (format, ovmf) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1411 | |
| 1412 | self.qemu_opt += ' ' + self.qemu_opt_script |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1413 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1414 | if self.ovmf_secboot_pkkek1: |
| 1415 | # Provide the Platform Key and first Key Exchange Key certificate as an |
| 1416 | # OEM string in the SMBIOS Type 11 table. Prepend the certificate string |
| 1417 | # with "application prefix" of the EnrollDefaultKeys.efi application |
| 1418 | self.qemu_opt += ' -smbios type=11,value=4e32566d-8e9e-4f52-81d3-5bb9715f9727:' \ |
| 1419 | + self.ovmf_secboot_pkkek1 |
| 1420 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 1421 | # Append qemuparams to override previous settings |
| 1422 | if self.qemuparams: |
| 1423 | self.qemu_opt += ' ' + self.qemuparams |
| 1424 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1425 | if self.snapshot: |
| 1426 | self.qemu_opt += " -snapshot" |
| 1427 | |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1428 | self.setup_serial() |
| 1429 | self.setup_vga() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1430 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1431 | def start_qemu(self): |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1432 | import shlex |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1433 | if self.kernel: |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 1434 | kernel_opts = "-kernel %s -append '%s %s %s %s'" % (self.kernel, self.kernel_cmdline, |
| 1435 | self.kernel_cmdline_script, self.get('QB_KERNEL_CMDLINE_APPEND'), |
| 1436 | self.bootparams) |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 1437 | if self.bios: |
| 1438 | kernel_opts += " -bios %s" % self.bios |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1439 | if self.dtb: |
| 1440 | kernel_opts += " -dtb %s" % self.dtb |
| 1441 | else: |
| 1442 | kernel_opts = "" |
| 1443 | cmd = "%s %s" % (self.qemu_opt, kernel_opts) |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1444 | cmds = shlex.split(cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1445 | logger.info('Running %s\n' % cmd) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 1446 | pass_fds = [] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1447 | if self.taplock_descriptor: |
| 1448 | pass_fds = [self.taplock_descriptor.fileno()] |
| 1449 | if len(self.portlocks): |
| 1450 | for descriptor in self.portlocks.values(): |
| 1451 | pass_fds.append(descriptor.fileno()) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 1452 | process = subprocess.Popen(cmds, stderr=subprocess.PIPE, pass_fds=pass_fds) |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1453 | self.qemupid = process.pid |
| 1454 | retcode = process.wait() |
| 1455 | if retcode: |
| 1456 | if retcode == -signal.SIGTERM: |
| 1457 | logger.info("Qemu terminated by SIGTERM") |
| 1458 | else: |
| 1459 | logger.error("Failed to run qemu: %s", process.stderr.read().decode()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1460 | |
| 1461 | def cleanup(self): |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1462 | if self.cleaned: |
| 1463 | return |
| 1464 | |
| 1465 | # avoid dealing with SIGTERM when cleanup function is running |
| 1466 | signal.signal(signal.SIGTERM, signal.SIG_IGN) |
| 1467 | |
| 1468 | logger.info("Cleaning up") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1469 | if self.cleantap: |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1470 | cmd = ('sudo', self.qemuifdown, self.tap, self.bindir_native) |
| 1471 | logger.debug('Running %s' % str(cmd)) |
| 1472 | subprocess.check_call(cmd) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1473 | self.release_taplock() |
| 1474 | self.release_portlock() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1475 | |
| 1476 | if self.nfs_running: |
| 1477 | logger.info("Shutting down the userspace NFS server...") |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1478 | cmd = ("runqemu-export-rootfs", "stop", self.rootfs) |
| 1479 | logger.debug('Running %s' % str(cmd)) |
| 1480 | subprocess.check_call(cmd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1481 | |
| 1482 | if self.saved_stty: |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1483 | subprocess.check_call(("stty", self.saved_stty)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1484 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 1485 | if self.cleanup_files: |
| 1486 | for ent in self.cleanup_files: |
| 1487 | logger.info('Removing %s' % ent) |
| 1488 | if os.path.isfile(ent): |
| 1489 | os.remove(ent) |
| 1490 | else: |
| 1491 | shutil.rmtree(ent) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1492 | |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1493 | self.cleaned = True |
| 1494 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1495 | def run_bitbake_env(self, mach=None): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1496 | bitbake = shutil.which('bitbake') |
| 1497 | if not bitbake: |
| 1498 | return |
| 1499 | |
| 1500 | if not mach: |
| 1501 | mach = self.get('MACHINE') |
| 1502 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1503 | multiconfig = self.get('MULTICONFIG') |
| 1504 | if multiconfig: |
| 1505 | multiconfig = "mc:%s" % multiconfig |
| 1506 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1507 | if mach: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1508 | cmd = 'MACHINE=%s bitbake -e %s' % (mach, multiconfig) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1509 | else: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1510 | cmd = 'bitbake -e %s' % multiconfig |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1511 | |
| 1512 | logger.info('Running %s...' % cmd) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1513 | return subprocess.check_output(cmd, shell=True).decode('utf-8') |
| 1514 | |
| 1515 | def load_bitbake_env(self, mach=None): |
| 1516 | if self.bitbake_e: |
| 1517 | return |
| 1518 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1519 | try: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1520 | self.bitbake_e = self.run_bitbake_env(mach=mach) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1521 | except subprocess.CalledProcessError as err: |
| 1522 | self.bitbake_e = '' |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1523 | logger.warning("Couldn't run 'bitbake -e' to gather environment information:\n%s" % err.output.decode('utf-8')) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1524 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1525 | def validate_combos(self): |
| 1526 | if (self.fstype in self.vmtypes) and self.kernel: |
| 1527 | raise RunQemuError("%s doesn't need kernel %s!" % (self.fstype, self.kernel)) |
| 1528 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1529 | @property |
| 1530 | def bindir_native(self): |
| 1531 | result = self.get('STAGING_BINDIR_NATIVE') |
| 1532 | if result and os.path.exists(result): |
| 1533 | return result |
| 1534 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1535 | cmd = ['bitbake', '-e'] |
| 1536 | multiconfig = self.get('MULTICONFIG') |
| 1537 | if multiconfig: |
| 1538 | cmd.append('mc:%s:qemu-helper-native' % multiconfig) |
| 1539 | else: |
| 1540 | cmd.append('qemu-helper-native') |
| 1541 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 1542 | logger.info('Running %s...' % str(cmd)) |
| 1543 | out = subprocess.check_output(cmd).decode('utf-8') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1544 | |
| 1545 | match = re.search('^STAGING_BINDIR_NATIVE="(.*)"', out, re.M) |
| 1546 | if match: |
| 1547 | result = match.group(1) |
| 1548 | if os.path.exists(result): |
| 1549 | self.set('STAGING_BINDIR_NATIVE', result) |
| 1550 | return result |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1551 | raise RunQemuError("Native sysroot directory %s doesn't exist" % result) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1552 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1553 | raise RunQemuError("Can't find STAGING_BINDIR_NATIVE in '%s' output" % cmd) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1554 | |
| 1555 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1556 | def main(): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1557 | if "help" in sys.argv or '-h' in sys.argv or '--help' in sys.argv: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1558 | print_usage() |
| 1559 | return 0 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1560 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1561 | config = BaseConfig() |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1562 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 1563 | renice = os.path.expanduser("~/bin/runqemu-renice") |
| 1564 | if os.path.exists(renice): |
| 1565 | logger.info('Using %s to renice' % renice) |
| 1566 | subprocess.check_call([renice, str(os.getpid())]) |
| 1567 | |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1568 | def sigterm_handler(signum, frame): |
| 1569 | logger.info("SIGTERM received") |
| 1570 | os.kill(config.qemupid, signal.SIGTERM) |
| 1571 | config.cleanup() |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1572 | # Deliberately ignore the return code of 'tput smam'. |
| 1573 | subprocess.call(["tput", "smam"]) |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 1574 | signal.signal(signal.SIGTERM, sigterm_handler) |
| 1575 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1576 | config.check_args() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1577 | config.read_qemuboot() |
| 1578 | config.check_and_set() |
| 1579 | # Check whether the combos is valid or not |
| 1580 | config.validate_combos() |
| 1581 | config.print_config() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1582 | config.setup_network() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1583 | config.setup_rootfs() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1584 | config.setup_final() |
| 1585 | config.start_qemu() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1586 | except RunQemuError as err: |
| 1587 | logger.error(err) |
| 1588 | return 1 |
| 1589 | except Exception as err: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1590 | import traceback |
| 1591 | traceback.print_exc() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1592 | return 1 |
| 1593 | finally: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1594 | config.cleanup() |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1595 | # Deliberately ignore the return code of 'tput smam'. |
| 1596 | subprocess.call(["tput", "smam"]) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1597 | |
| 1598 | if __name__ == "__main__": |
| 1599 | sys.exit(main()) |