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