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