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 |
| 65 | # ip= kernel comand line argument needs to be changed accordingly. Details are documented |
| 66 | # in the kernel docuemntation https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt |
| 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") |
| 89 | # |
| 90 | # Usage: |
| 91 | # IMAGE_CLASSES += "qemuboot" |
| 92 | # See "runqemu help" for more info |
| 93 | |
| 94 | QB_MEM ?= "-m 256" |
| 95 | QB_SMP ?= "" |
| 96 | QB_SERIAL_OPT ?= "-serial mon:stdio -serial null" |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 97 | 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] | 98 | QB_DEFAULT_FSTYPE ?= "ext4" |
| 99 | QB_RNG ?= "-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0" |
| 100 | QB_OPT_APPEND ?= "" |
| 101 | QB_NETWORK_DEVICE ?= "-device virtio-net-pci,netdev=net0,mac=@MAC@" |
| 102 | QB_CMDLINE_IP_SLIRP ?= "ip=dhcp" |
| 103 | QB_CMDLINE_IP_TAP ?= "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0:off:8.8.8.8" |
| 104 | QB_ROOTFS_EXTRA_OPT ?= "" |
| 105 | QB_GRAPHICS ?= "" |
| 106 | |
| 107 | # This should be kept align with ROOT_VM |
| 108 | QB_DRIVE_TYPE ?= "/dev/sd" |
| 109 | |
| 110 | inherit image-artifact-names |
| 111 | |
| 112 | # Create qemuboot.conf |
| 113 | addtask do_write_qemuboot_conf after do_rootfs before do_image |
| 114 | |
| 115 | def qemuboot_vars(d): |
| 116 | build_vars = ['MACHINE', 'TUNE_ARCH', 'DEPLOY_DIR_IMAGE', |
| 117 | 'KERNEL_IMAGETYPE', 'IMAGE_NAME', 'IMAGE_LINK_NAME', |
| 118 | 'STAGING_DIR_NATIVE', 'STAGING_BINDIR_NATIVE', |
| 119 | 'STAGING_DIR_HOST', 'SERIAL_CONSOLES', 'UNINATIVE_LOADER'] |
| 120 | return build_vars + [k for k in d.keys() if k.startswith('QB_')] |
| 121 | |
| 122 | do_write_qemuboot_conf[vardeps] += "${@' '.join(qemuboot_vars(d))}" |
| 123 | do_write_qemuboot_conf[vardepsexclude] += "TOPDIR" |
| 124 | python do_write_qemuboot_conf() { |
| 125 | import configparser |
| 126 | |
| 127 | qemuboot = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_NAME')) |
| 128 | if d.getVar('IMAGE_LINK_NAME'): |
| 129 | qemuboot_link = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_LINK_NAME')) |
| 130 | else: |
| 131 | qemuboot_link = "" |
| 132 | finalpath = d.getVar("DEPLOY_DIR_IMAGE") |
| 133 | topdir = d.getVar('TOPDIR') |
| 134 | cf = configparser.ConfigParser() |
| 135 | cf.add_section('config_bsp') |
| 136 | for k in sorted(qemuboot_vars(d)): |
| 137 | if ":" in k: |
| 138 | continue |
| 139 | # qemu-helper-native sysroot is not removed by rm_work and |
| 140 | # contains all tools required by runqemu |
| 141 | if k == 'STAGING_BINDIR_NATIVE': |
| 142 | val = os.path.join(d.getVar('BASE_WORKDIR'), d.getVar('BUILD_SYS'), |
| 143 | 'qemu-helper-native/1.0-r1/recipe-sysroot-native/usr/bin/') |
| 144 | else: |
| 145 | val = d.getVar(k) |
| 146 | if val is None: |
| 147 | continue |
| 148 | # we only want to write out relative paths so that we can relocate images |
| 149 | # and still run them |
| 150 | if val.startswith(topdir): |
| 151 | val = os.path.relpath(val, finalpath) |
| 152 | cf.set('config_bsp', k, '%s' % val) |
| 153 | |
| 154 | # QB_DEFAULT_KERNEL's value of KERNEL_IMAGETYPE is the name of a symlink |
| 155 | # to the kernel file, which hinders relocatability of the qb conf. |
| 156 | # Read the link and replace it with the full filename of the target. |
| 157 | kernel_link = os.path.join(d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('QB_DEFAULT_KERNEL')) |
| 158 | kernel = os.path.realpath(kernel_link) |
| 159 | # we only want to write out relative paths so that we can relocate images |
| 160 | # and still run them |
| 161 | kernel = os.path.relpath(kernel, finalpath) |
| 162 | cf.set('config_bsp', 'QB_DEFAULT_KERNEL', kernel) |
| 163 | |
| 164 | bb.utils.mkdirhier(os.path.dirname(qemuboot)) |
| 165 | with open(qemuboot, 'w') as f: |
| 166 | cf.write(f) |
| 167 | |
| 168 | if qemuboot_link and qemuboot_link != qemuboot: |
| 169 | if os.path.lexists(qemuboot_link): |
| 170 | os.remove(qemuboot_link) |
| 171 | os.symlink(os.path.basename(qemuboot), qemuboot_link) |
| 172 | } |