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