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