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