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