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