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