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