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