Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | # Help runqemu boot target board, "QB" means Qemu Boot, the following |
| 8 | # vars can be set in conf files, such as <bsp.conf> to make it can be |
| 9 | # boot by runqemu: |
| 10 | # |
| 11 | # QB_SYSTEM_NAME: qemu name, e.g., "qemu-system-i386" |
| 12 | # |
| 13 | # QB_OPT_APPEND: options to append to qemu, e.g., "-device usb-mouse" |
| 14 | # |
| 15 | # QB_DEFAULT_KERNEL: default kernel to boot, e.g., "bzImage" |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 16 | # e.g., "bzImage-initramfs-qemux86-64.bin" if INITRAMFS_IMAGE_BUNDLE is set to 1. |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 17 | # |
| 18 | # QB_DEFAULT_FSTYPE: default FSTYPE to boot, e.g., "ext4" |
| 19 | # |
| 20 | # QB_MEM: memory, e.g., "-m 512" |
| 21 | # |
| 22 | # QB_MACHINE: qemu machine, e.g., "-machine virt" |
| 23 | # |
| 24 | # QB_CPU: qemu cpu, e.g., "-cpu qemu32" |
| 25 | # |
| 26 | # QB_CPU_KVM: the similar to QB_CPU, but used when kvm, e.g., '-cpu kvm64', |
| 27 | # set it when support kvm. |
| 28 | # |
| 29 | # QB_SMP: amount of CPU cores inside qemu guest, each mapped to a thread on the host, |
| 30 | # e.g. "-smp 8". |
| 31 | # |
| 32 | # QB_KERNEL_CMDLINE_APPEND: options to append to kernel's -append |
| 33 | # option, e.g., "console=ttyS0 console=tty" |
| 34 | # |
| 35 | # QB_DTB: qemu dtb name |
| 36 | # |
| 37 | # QB_AUDIO_DRV: qemu audio driver, e.g., "alsa", set it when support audio |
| 38 | # |
| 39 | # QB_AUDIO_OPT: qemu audio option, e.g., "-device AC97", used |
| 40 | # when QB_AUDIO_DRV is set. |
| 41 | # |
| 42 | # QB_RNG: Pass-through for host random number generator, it can speedup boot |
| 43 | # in system mode, where system is experiencing entropy starvation |
| 44 | # |
| 45 | # QB_KERNEL_ROOT: kernel's root, e.g., /dev/vda |
| 46 | # By default "/dev/vda rw" gets passed to the kernel. |
| 47 | # To mount the rootfs read-only QB_KERNEL_ROOT can be set to e.g. "/dev/vda ro". |
| 48 | # |
| 49 | # QB_NETWORK_DEVICE: network device, e.g., "-device virtio-net-pci,netdev=net0,mac=@MAC@", |
| 50 | # it needs work with QB_TAP_OPT and QB_SLIRP_OPT. |
| 51 | # Note, runqemu will replace @MAC@ with a predefined mac, you can set |
| 52 | # a custom one, but that may cause conflicts when multiple qemus are |
| 53 | # running on the same host. |
| 54 | # Note: If more than one interface of type -device virtio-net-device gets added, |
| 55 | # QB_NETWORK_DEVICE:prepend might be used, since Qemu enumerates the eth* |
| 56 | # devices in reverse order to -device arguments. |
| 57 | # |
| 58 | # QB_TAP_OPT: network option for 'tap' mode, e.g., |
| 59 | # "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no" |
| 60 | # Note, runqemu will replace "@TAP@" with the one which is used, such as tap0, tap1 ... |
| 61 | # |
| 62 | # QB_SLIRP_OPT: network option for SLIRP mode, e.g., -netdev user,id=net0" |
| 63 | # |
| 64 | # QB_CMDLINE_IP_SLIRP: If QB_NETWORK_DEVICE adds more than one network interface to qemu, usually the |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 65 | # ip= kernel command line argument needs to be changed accordingly. Details are documented |
| 66 | # in the kernel documentation https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 67 | # Example to configure only the first interface: "ip=eth0:dhcp" |
| 68 | # QB_CMDLINE_IP_TAP: This parameter is similar to the QB_CMDLINE_IP_SLIRP parameter. Since the tap interface requires |
| 69 | # static IP configuration @CLIENT@ and @GATEWAY@ place holders are replaced by the IP and the gateway |
| 70 | # address of the qemu guest by runqemu. |
| 71 | # Example: "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0" |
| 72 | # |
| 73 | # QB_ROOTFS_OPT: used as rootfs, e.g., |
| 74 | # "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0" |
| 75 | # Note, runqemu will replace "@ROOTFS@" with the one which is used, such as core-image-minimal-qemuarm64.ext4. |
| 76 | # |
| 77 | # QB_SERIAL_OPT: serial port, e.g., "-serial mon:stdio" |
| 78 | # |
| 79 | # QB_TCPSERIAL_OPT: tcp serial port option, e.g., |
| 80 | # " -device virtio-serial-device -chardev socket,id=virtcon,port=@PORT@,host=127.0.0.1 -device virtconsole,chardev=virtcon" |
| 81 | # Note, runqemu will replace "@PORT@" with the port number which is used. |
| 82 | # |
| 83 | # QB_ROOTFS_EXTRA_OPT: extra options to be appended to the rootfs device in case there is none specified by QB_ROOTFS_OPT. |
| 84 | # Can be used to automatically determine the image from the other variables |
| 85 | # but define things link 'bootindex' when booting from EFI or 'readonly' when using squashfs |
| 86 | # without the need to specify a dedicated qemu configuration |
| 87 | # |
| 88 | # QB_GRAPHICS: QEMU video card type (e.g. "-vga std") |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 89 | # QB_NFSROOTFS_EXTRA_OPT: extra options to be appended to the nfs rootfs options in kernel boot arg, e.g., |
| 90 | # "wsize=4096,rsize=4096" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 91 | # |
| 92 | # Usage: |
| 93 | # IMAGE_CLASSES += "qemuboot" |
| 94 | # See "runqemu help" for more info |
| 95 | |
| 96 | QB_MEM ?= "-m 256" |
| 97 | QB_SMP ?= "" |
| 98 | QB_SERIAL_OPT ?= "-serial mon:stdio -serial null" |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 99 | QB_DEFAULT_KERNEL ?= "${@bb.utils.contains("INITRAMFS_IMAGE_BUNDLE", "1", "${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin", "${KERNEL_IMAGETYPE}", d)}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 100 | QB_DEFAULT_FSTYPE ?= "ext4" |
| 101 | QB_RNG ?= "-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0" |
| 102 | QB_OPT_APPEND ?= "" |
| 103 | QB_NETWORK_DEVICE ?= "-device virtio-net-pci,netdev=net0,mac=@MAC@" |
Patrick Williams | 3965356 | 2024-03-01 08:54:02 -0600 | [diff] [blame] | 104 | |
| 105 | # qemurunner needs ip information first, so append QB_NO_PNI |
| 106 | # |
| 107 | QB_NO_PNI ?= "${@bb.utils.contains('DISTRO_FEATURES', 'pni-names', '', 'net.ifnames=0', d)}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 108 | QB_CMDLINE_IP_SLIRP ?= "ip=dhcp" |
Patrick Williams | 3965356 | 2024-03-01 08:54:02 -0600 | [diff] [blame] | 109 | QB_CMDLINE_IP_TAP ?= "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0:off:8.8.8.8 ${QB_NO_PNI}" |
| 110 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 111 | QB_ROOTFS_EXTRA_OPT ?= "" |
| 112 | QB_GRAPHICS ?= "" |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 113 | QB_NFSROOTFS_EXTRA_OPT ?= "" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 114 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 115 | # With 6.5+ (specifically, if DMA_BOUNCE_UNALIGNED_KMALLOC is set) the SW IO TLB |
| 116 | # is used, and it defaults to 64MB. This is too much when there's only 256MB of |
| 117 | # RAM, so request 0 slabs and lets the kernel round up to the appropriate minimum |
| 118 | # (1MB, typically). In virtual hardware there's very little need for these bounce |
| 119 | # buffers, so the 64MB would be mostly wasted. |
| 120 | QB_KERNEL_CMDLINE_APPEND:append = " swiotlb=0" |
| 121 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 122 | # This should be kept align with ROOT_VM |
| 123 | QB_DRIVE_TYPE ?= "/dev/sd" |
| 124 | |
| 125 | inherit image-artifact-names |
| 126 | |
| 127 | # Create qemuboot.conf |
| 128 | addtask do_write_qemuboot_conf after do_rootfs before do_image |
| 129 | |
| 130 | def qemuboot_vars(d): |
| 131 | build_vars = ['MACHINE', 'TUNE_ARCH', 'DEPLOY_DIR_IMAGE', |
| 132 | 'KERNEL_IMAGETYPE', 'IMAGE_NAME', 'IMAGE_LINK_NAME', |
| 133 | 'STAGING_DIR_NATIVE', 'STAGING_BINDIR_NATIVE', |
| 134 | 'STAGING_DIR_HOST', 'SERIAL_CONSOLES', 'UNINATIVE_LOADER'] |
| 135 | return build_vars + [k for k in d.keys() if k.startswith('QB_')] |
| 136 | |
| 137 | do_write_qemuboot_conf[vardeps] += "${@' '.join(qemuboot_vars(d))}" |
| 138 | do_write_qemuboot_conf[vardepsexclude] += "TOPDIR" |
| 139 | python do_write_qemuboot_conf() { |
| 140 | import configparser |
| 141 | |
| 142 | qemuboot = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_NAME')) |
| 143 | if d.getVar('IMAGE_LINK_NAME'): |
| 144 | qemuboot_link = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_LINK_NAME')) |
| 145 | else: |
| 146 | qemuboot_link = "" |
| 147 | finalpath = d.getVar("DEPLOY_DIR_IMAGE") |
| 148 | topdir = d.getVar('TOPDIR') |
| 149 | cf = configparser.ConfigParser() |
| 150 | cf.add_section('config_bsp') |
| 151 | for k in sorted(qemuboot_vars(d)): |
| 152 | if ":" in k: |
| 153 | continue |
| 154 | # qemu-helper-native sysroot is not removed by rm_work and |
| 155 | # contains all tools required by runqemu |
| 156 | if k == 'STAGING_BINDIR_NATIVE': |
| 157 | val = os.path.join(d.getVar('BASE_WORKDIR'), d.getVar('BUILD_SYS'), |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 158 | 'qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/') |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 159 | else: |
| 160 | val = d.getVar(k) |
| 161 | if val is None: |
| 162 | continue |
| 163 | # we only want to write out relative paths so that we can relocate images |
| 164 | # and still run them |
| 165 | if val.startswith(topdir): |
| 166 | val = os.path.relpath(val, finalpath) |
| 167 | cf.set('config_bsp', k, '%s' % val) |
| 168 | |
| 169 | # QB_DEFAULT_KERNEL's value of KERNEL_IMAGETYPE is the name of a symlink |
| 170 | # to the kernel file, which hinders relocatability of the qb conf. |
| 171 | # Read the link and replace it with the full filename of the target. |
| 172 | kernel_link = os.path.join(d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('QB_DEFAULT_KERNEL')) |
| 173 | kernel = os.path.realpath(kernel_link) |
| 174 | # we only want to write out relative paths so that we can relocate images |
| 175 | # and still run them |
| 176 | kernel = os.path.relpath(kernel, finalpath) |
| 177 | cf.set('config_bsp', 'QB_DEFAULT_KERNEL', kernel) |
| 178 | |
| 179 | bb.utils.mkdirhier(os.path.dirname(qemuboot)) |
| 180 | with open(qemuboot, 'w') as f: |
| 181 | cf.write(f) |
| 182 | |
| 183 | if qemuboot_link and qemuboot_link != qemuboot: |
| 184 | if os.path.lexists(qemuboot_link): |
| 185 | os.remove(qemuboot_link) |
| 186 | os.symlink(os.path.basename(qemuboot), qemuboot_link) |
| 187 | } |