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