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