qemuboot.bbclass 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Help runqemu boot target board, "QB" means Qemu Boot, the following
  2. # vars can be set in conf files, such as <bsp.conf> to make it can be
  3. # boot by runqemu:
  4. #
  5. # QB_SYSTEM_NAME: qemu name, e.g., "qemu-system-i386"
  6. #
  7. # QB_OPT_APPEND: options to append to qemu, e.g., "-device usb-mouse"
  8. #
  9. # QB_DEFAULT_KERNEL: default kernel to boot, e.g., "bzImage"
  10. #
  11. # QB_DEFAULT_FSTYPE: default FSTYPE to boot, e.g., "ext4"
  12. #
  13. # QB_MEM: memory, e.g., "-m 512"
  14. #
  15. # QB_MACHINE: qemu machine, e.g., "-machine virt"
  16. #
  17. # QB_CPU: qemu cpu, e.g., "-cpu qemu32"
  18. #
  19. # QB_CPU_KVM: the similar to QB_CPU, but used when kvm, e.g., '-cpu kvm64',
  20. # set it when support kvm.
  21. #
  22. # QB_KERNEL_CMDLINE_APPEND: options to append to kernel's -append
  23. # option, e.g., "console=ttyS0 console=tty"
  24. #
  25. # QB_DTB: qemu dtb name
  26. #
  27. # QB_AUDIO_DRV: qemu audio driver, e.g., "alsa", set it when support audio
  28. #
  29. # QB_AUDIO_OPT: qemu audio option, e.g., "-soundhw ac97,es1370", used
  30. # when QB_AUDIO_DRV is set.
  31. #
  32. # QB_RNG: Pass-through for host random number generator, it can speedup boot
  33. # in system mode, where system is experiencing entropy starvation
  34. #
  35. # QB_KERNEL_ROOT: kernel's root, e.g., /dev/vda
  36. #
  37. # QB_NETWORK_DEVICE: network device, e.g., "-device virtio-net-pci,netdev=net0,mac=@MAC@",
  38. # it needs work with QB_TAP_OPT and QB_SLIRP_OPT.
  39. # Note, runqemu will replace @MAC@ with a predefined mac, you can set
  40. # a custom one, but that may cause conflicts when multiple qemus are
  41. # running on the same host.
  42. # Note: If more than one interface of type -device virtio-net-device gets added,
  43. # QB_NETWORK_DEVICE_prepend might be used, since Qemu enumerates the eth*
  44. # devices in reverse order to -device arguments.
  45. #
  46. # QB_TAP_OPT: netowrk option for 'tap' mode, e.g.,
  47. # "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no"
  48. # Note, runqemu will replace "@TAP@" with the one which is used, such as tap0, tap1 ...
  49. #
  50. # QB_SLIRP_OPT: network option for SLIRP mode, e.g., -netdev user,id=net0"
  51. #
  52. # QB_CMDLINE_IP_SLIRP: If QB_NETWORK_DEVICE adds more than one network interface to qemu, usually the
  53. # ip= kernel comand line argument needs to be changed accordingly. Details are documented
  54. # in the kernel docuemntation https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
  55. # Example to configure only the first interface: "ip=eth0:dhcp"
  56. # QB_CMDLINE_IP_TAP: This parameter is similar to the QB_CMDLINE_IP_SLIRP parameter. Since the tap interface requires
  57. # static IP configuration @CLIENT@ and @GATEWAY@ place holders are replaced by the IP and the gateway
  58. # address of the qemu guest by runqemu.
  59. # Example: "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0::eth0"
  60. #
  61. # QB_ROOTFS_OPT: used as rootfs, e.g.,
  62. # "-drive id=disk0,file=@ROOTFS@,if=none,format=raw -device virtio-blk-device,drive=disk0"
  63. # Note, runqemu will replace "@ROOTFS@" with the one which is used, such as core-image-minimal-qemuarm64.ext4.
  64. #
  65. # QB_SERIAL_OPT: serial port, e.g., "-serial mon:stdio"
  66. #
  67. # QB_TCPSERIAL_OPT: tcp serial port option, e.g.,
  68. # " -device virtio-serial-device -chardev socket,id=virtcon,port=@PORT@,host=127.0.0.1 -device virtconsole,chardev=virtcon"
  69. # Note, runqemu will replace "@PORT@" with the port number which is used.
  70. #
  71. # QB_ROOTFS_EXTRA_OPT: extra options to be appended to the rootfs device in case there is none specified by QB_ROOTFS_OPT.
  72. # Can be used to automatically determine the image from the other variables
  73. # but define things link 'bootindex' when booting from EFI or 'readonly' when using squashfs
  74. # without the need to specify a dedicated qemu configuration
  75. # Usage:
  76. # IMAGE_CLASSES += "qemuboot"
  77. # See "runqemu help" for more info
  78. QB_MEM ?= "-m 256"
  79. QB_SERIAL_OPT ?= "-serial mon:stdio -serial null"
  80. QB_DEFAULT_KERNEL ?= "${KERNEL_IMAGETYPE}"
  81. QB_DEFAULT_FSTYPE ?= "ext4"
  82. QB_RNG ?= "-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0"
  83. QB_OPT_APPEND ?= ""
  84. QB_NETWORK_DEVICE ?= "-device virtio-net-pci,netdev=net0,mac=@MAC@"
  85. QB_CMDLINE_IP_SLIRP ?= "ip=dhcp"
  86. QB_CMDLINE_IP_TAP ?= "ip=192.168.7.@CLIENT@::192.168.7.@GATEWAY@:255.255.255.0"
  87. QB_ROOTFS_EXTRA_OPT ?= ""
  88. # This should be kept align with ROOT_VM
  89. QB_DRIVE_TYPE ?= "/dev/sd"
  90. inherit image-artifact-names
  91. # Create qemuboot.conf
  92. addtask do_write_qemuboot_conf after do_rootfs before do_image
  93. def qemuboot_vars(d):
  94. build_vars = ['MACHINE', 'TUNE_ARCH', 'DEPLOY_DIR_IMAGE',
  95. 'KERNEL_IMAGETYPE', 'IMAGE_NAME', 'IMAGE_LINK_NAME',
  96. 'STAGING_DIR_NATIVE', 'STAGING_BINDIR_NATIVE',
  97. 'STAGING_DIR_HOST']
  98. return build_vars + [k for k in d.keys() if k.startswith('QB_')]
  99. do_write_qemuboot_conf[vardeps] += "${@' '.join(qemuboot_vars(d))}"
  100. do_write_qemuboot_conf[vardepsexclude] += "TOPDIR"
  101. python do_write_qemuboot_conf() {
  102. import configparser
  103. qemuboot = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_NAME'))
  104. qemuboot_link = "%s/%s.qemuboot.conf" % (d.getVar('IMGDEPLOYDIR'), d.getVar('IMAGE_LINK_NAME'))
  105. finalpath = d.getVar("DEPLOY_DIR_IMAGE")
  106. topdir = d.getVar('TOPDIR')
  107. cf = configparser.ConfigParser()
  108. cf.add_section('config_bsp')
  109. for k in sorted(qemuboot_vars(d)):
  110. # qemu-helper-native sysroot is not removed by rm_work and
  111. # contains all tools required by runqemu
  112. if k == 'STAGING_BINDIR_NATIVE':
  113. val = os.path.join(d.getVar('BASE_WORKDIR'), d.getVar('BUILD_SYS'),
  114. 'qemu-helper-native/1.0-r1/recipe-sysroot-native/usr/bin/')
  115. else:
  116. val = d.getVar(k)
  117. # we only want to write out relative paths so that we can relocate images
  118. # and still run them
  119. if val.startswith(topdir):
  120. val = os.path.relpath(val, finalpath)
  121. cf.set('config_bsp', k, '%s' % val)
  122. # QB_DEFAULT_KERNEL's value of KERNEL_IMAGETYPE is the name of a symlink
  123. # to the kernel file, which hinders relocatability of the qb conf.
  124. # Read the link and replace it with the full filename of the target.
  125. kernel_link = os.path.join(d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('QB_DEFAULT_KERNEL'))
  126. kernel = os.path.realpath(kernel_link)
  127. # we only want to write out relative paths so that we can relocate images
  128. # and still run them
  129. kernel = os.path.relpath(kernel, finalpath)
  130. cf.set('config_bsp', 'QB_DEFAULT_KERNEL', kernel)
  131. bb.utils.mkdirhier(os.path.dirname(qemuboot))
  132. with open(qemuboot, 'w') as f:
  133. cf.write(f)
  134. if qemuboot_link != qemuboot:
  135. if os.path.lexists(qemuboot_link):
  136. os.remove(qemuboot_link)
  137. os.symlink(os.path.basename(qemuboot), qemuboot_link)
  138. }