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