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") |
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@" |
| 104 | QB_CMDLINE_IP_SLIRP ?= "ip=dhcp" |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 105 | QB_CMDLINE_IP_TAP ?= "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0:off:8.8.8.8 net.ifnames=0" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 106 | QB_ROOTFS_EXTRA_OPT ?= "" |
| 107 | QB_GRAPHICS ?= "" |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 108 | QB_NFSROOTFS_EXTRA_OPT ?= "" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 109 | |
| 110 | # This should be kept align with ROOT_VM |
| 111 | QB_DRIVE_TYPE ?= "/dev/sd" |
| 112 | |
| 113 | inherit image-artifact-names |
| 114 | |
| 115 | # Create qemuboot.conf |
| 116 | addtask do_write_qemuboot_conf after do_rootfs before do_image |
| 117 | |
| 118 | def qemuboot_vars(d): |
| 119 | build_vars = ['MACHINE', 'TUNE_ARCH', 'DEPLOY_DIR_IMAGE', |
| 120 | 'KERNEL_IMAGETYPE', 'IMAGE_NAME', 'IMAGE_LINK_NAME', |
| 121 | 'STAGING_DIR_NATIVE', 'STAGING_BINDIR_NATIVE', |
| 122 | 'STAGING_DIR_HOST', 'SERIAL_CONSOLES', 'UNINATIVE_LOADER'] |
| 123 | return build_vars + [k for k in d.keys() if k.startswith('QB_')] |
| 124 | |
| 125 | do_write_qemuboot_conf[vardeps] += "${@' '.join(qemuboot_vars(d))}" |
| 126 | do_write_qemuboot_conf[vardepsexclude] += "TOPDIR" |
| 127 | python do_write_qemuboot_conf() { |
| 128 | import configparser |
| 129 | |
| 130 | qemuboot = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_NAME')) |
| 131 | if d.getVar('IMAGE_LINK_NAME'): |
| 132 | qemuboot_link = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_LINK_NAME')) |
| 133 | else: |
| 134 | qemuboot_link = "" |
| 135 | finalpath = d.getVar("DEPLOY_DIR_IMAGE") |
| 136 | topdir = d.getVar('TOPDIR') |
| 137 | cf = configparser.ConfigParser() |
| 138 | cf.add_section('config_bsp') |
| 139 | for k in sorted(qemuboot_vars(d)): |
| 140 | if ":" in k: |
| 141 | continue |
| 142 | # qemu-helper-native sysroot is not removed by rm_work and |
| 143 | # contains all tools required by runqemu |
| 144 | if k == 'STAGING_BINDIR_NATIVE': |
| 145 | val = os.path.join(d.getVar('BASE_WORKDIR'), d.getVar('BUILD_SYS'), |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 146 | 'qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/') |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 147 | else: |
| 148 | val = d.getVar(k) |
| 149 | if val is None: |
| 150 | continue |
| 151 | # we only want to write out relative paths so that we can relocate images |
| 152 | # and still run them |
| 153 | if val.startswith(topdir): |
| 154 | val = os.path.relpath(val, finalpath) |
| 155 | cf.set('config_bsp', k, '%s' % val) |
| 156 | |
| 157 | # QB_DEFAULT_KERNEL's value of KERNEL_IMAGETYPE is the name of a symlink |
| 158 | # to the kernel file, which hinders relocatability of the qb conf. |
| 159 | # Read the link and replace it with the full filename of the target. |
| 160 | kernel_link = os.path.join(d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('QB_DEFAULT_KERNEL')) |
| 161 | kernel = os.path.realpath(kernel_link) |
| 162 | # we only want to write out relative paths so that we can relocate images |
| 163 | # and still run them |
| 164 | kernel = os.path.relpath(kernel, finalpath) |
| 165 | cf.set('config_bsp', 'QB_DEFAULT_KERNEL', kernel) |
| 166 | |
| 167 | bb.utils.mkdirhier(os.path.dirname(qemuboot)) |
| 168 | with open(qemuboot, 'w') as f: |
| 169 | cf.write(f) |
| 170 | |
| 171 | if qemuboot_link and qemuboot_link != qemuboot: |
| 172 | if os.path.lexists(qemuboot_link): |
| 173 | os.remove(qemuboot_link) |
| 174 | os.symlink(os.path.basename(qemuboot), qemuboot_link) |
| 175 | } |