Initial Commit

- initial default booting image
This commit is contained in:
2026-11-06 20:45:42 +01:00
commit b8783708ae
15201 changed files with 602532 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
import os
import re
import sys
import tempfile
import subprocess
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
ARTIFACTS_URL = "https://autobuild.buildroot.org/artefacts/"
BASE_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "../../.."))
def log_file_path(builddir, stage, logtofile=True):
"""Return path to log file"""
return "{}-{}.log".format(builddir, stage) if logtofile else None
def open_log_file(builddir, stage, logtofile=True):
"""
Open a file for logging and return its handler.
If logtofile is True, returns sys.stdout. Otherwise opens a file
with a suitable name in the build directory.
"""
return open(log_file_path(builddir, stage, logtofile), 'a+') if logtofile else sys.stdout
def basepath(relpath=""):
"""Return the absolute path for a file or directory relative to the Buildroot top directory."""
return os.path.join(BASE_DIR, relpath)
def filepath(relpath):
return os.path.join(BASE_DIR, "support/testing", relpath)
def download(dldir, filename):
finalpath = os.path.join(dldir, filename)
if os.path.exists(finalpath):
return finalpath
if not os.path.exists(dldir):
os.makedirs(dldir)
tmpfile = tempfile.mktemp(dir=dldir)
print("Downloading to {}".format(tmpfile))
try:
url_fh = urlopen(os.path.join(ARTIFACTS_URL, filename))
with open(tmpfile, "w+b") as tmpfile_fh:
tmpfile_fh.write(url_fh.read())
except (HTTPError, URLError) as err:
os.unlink(tmpfile)
raise err
print("Renaming from {} to {}".format(tmpfile, finalpath))
os.rename(tmpfile, finalpath)
return finalpath
def run_cmd_on_host(builddir, cmd):
"""Call subprocess.check_output and return the text output."""
try:
host_bin = os.path.join(builddir, "host", "bin")
br_path = host_bin + os.pathsep + os.environ["PATH"]
out = subprocess.check_output(cmd,
cwd=builddir,
env={
"LANG": "C",
"PATH": br_path
},
stderr=subprocess.STDOUT,
text=True,
universal_newlines=True)
except subprocess.CalledProcessError as e:
print(f"Command failed with return code {e.returncode}")
print("=== STDOUT/STDERR ===")
print(e.output)
print("=====================")
raise
return out
def get_elf_arch_tag(builddir, prefix, fpath, tag):
"""
Runs the cross readelf on 'fpath', then extracts the value of tag 'tag'.
Example:
>>> get_elf_arch_tag('output', 'arm-none-linux-gnueabi-',
'bin/busybox', 'Tag_CPU_arch')
v5TEJ
>>>
"""
cmd = ["host/bin/{}-readelf".format(prefix),
"-A", os.path.join("target", fpath)]
out = run_cmd_on_host(builddir, cmd)
regexp = re.compile(r"^ {}: (.*)$".format(tag))
for line in out.splitlines():
m = regexp.match(line)
if not m:
continue
return m.group(1)
return None
def get_file_arch(builddir, prefix, fpath):
return get_elf_arch_tag(builddir, prefix, fpath, "Tag_CPU_arch")
def get_elf_prog_interpreter(builddir, prefix, fpath):
"""
Runs the cross readelf on 'fpath' to extract the program interpreter
name and returns it.
Example:
>>> get_elf_prog_interpreter('br-tests/TestExternalToolchainLinaroArm',
'arm-linux-gnueabihf',
'bin/busybox')
/lib/ld-linux-armhf.so.3
>>>
"""
cmd = ["host/bin/{}-readelf".format(prefix),
"-l", os.path.join("target", fpath)]
out = run_cmd_on_host(builddir, cmd)
regexp = re.compile(r"^ *\[Requesting program interpreter: (.*)\]$")
for line in out.splitlines():
m = regexp.match(line)
if not m:
continue
return m.group(1)
return None
def img_round_power2(img):
"""
Rounds up the size of an image file to the next power of 2
"""
sz = os.stat(img).st_size
pow2 = 1
while pow2 < sz:
pow2 = pow2 << 1
with open(img, 'ab') as f:
f.truncate(pow2)

View File

@@ -0,0 +1,163 @@
import unittest
import os
import dataclasses
import datetime
import json
from typing import ClassVar
from infra.builder import Builder
from infra.emulator import Emulator
BASIC_TOOLCHAIN_CONFIG = \
"""
BR2_arm=y
BR2_TOOLCHAIN_EXTERNAL=y
BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_ARMV5_EABI_GLIBC_STABLE=y
"""
MINIMAL_CONFIG = \
"""
BR2_INIT_NONE=y
BR2_SYSTEM_BIN_SH_NONE=y
# BR2_PACKAGE_BUSYBOX is not set
# BR2_TARGET_ROOTFS_TAR is not set
"""
@dataclasses.dataclass
class BRTestSettings:
"""support/testing/run-tests stores runtime settings in this
object, and serializes it into the environment variable named in
CONF_ENV. On instantiation, BRConfigTest loads this
configuration. That way configuration can be transferred into
multiprocessing worker processes without requiring the "fork"
start method.
"""
CONF_ENV: ClassVar[str] = 'BR_TEST_CONFIG_JSON'
downloaddir: str
outputdir: str
logtofile: bool = True
keepbuilds: bool = False
jlevel: int = 0
timeout_multiplier: int = 1
def to_env(self) -> None:
os.environ[self.CONF_ENV] = json.dumps(dataclasses.asdict(self))
@classmethod
def from_env(cls) -> 'BRTestSettings':
return cls(**json.loads(os.environ[cls.CONF_ENV]))
class BRConfigTest(unittest.TestCase):
"""Test up to the configure stage."""
config: str
br2_external: list[str] = list()
downloaddir = None
outputdir = None
logtofile = True
keepbuilds = False
jlevel = 0
timeout_multiplier = 1
def __init__(self, names):
super(BRConfigTest, self).__init__(names)
if BRTestSettings.CONF_ENV in os.environ:
self.conf_from_env()
self.testname = self.__class__.__name__
self.builddir = self.outputdir and os.path.join(self.outputdir, self.testname)
self.config += '\nBR2_DL_DIR="{}"\n'.format(self.downloaddir)
self.config += "\nBR2_JLEVEL={}\n".format(self.jlevel)
def show_msg(self, msg):
print("{} {:40s} {}".format(datetime.datetime.now().strftime("%H:%M:%S"),
self.testname, msg))
def conf_from_env(self) -> None:
conf = BRTestSettings.from_env()
for f in dataclasses.fields(conf):
setattr(self, f.name, getattr(conf, f.name))
def setUp(self):
self.show_msg("Starting")
self.b = Builder(self.config, self.builddir, self.logtofile, self.jlevel)
if not self.keepbuilds:
self.b.delete()
if not self.b.is_finished():
self.b.configure(make_extra_opts=["BR2_EXTERNAL={}".format(":".join(self.br2_external))])
def tearDown(self):
self.show_msg("Cleaning up")
if self.b and not self.keepbuilds:
self.b.delete()
class BRHostPkgTest(BRConfigTest):
"""Test up to the build stage of a host package. Define hostpkgs in
the class to the list of host packages that should be built."""
config = \
BASIC_TOOLCHAIN_CONFIG + \
MINIMAL_CONFIG
hostpkgs = None
def __init__(self, names):
super(BRHostPkgTest, self).__init__(names)
def setUp(self):
super(BRHostPkgTest, self).setUp()
if not self.b.is_finished():
self.show_msg("Building")
self.b.build(make_extra_opts=self.hostpkgs)
self.show_msg("Building done")
def tearDown(self):
super(BRHostPkgTest, self).tearDown()
class BRTest(BRConfigTest):
"""Test up to the build stage and instantiate an emulator."""
def __init__(self, names):
super(BRTest, self).__init__(names)
self.emulator = None
def setUp(self):
super(BRTest, self).setUp()
if not self.b.is_finished():
self.show_msg("Building")
self.b.build()
self.show_msg("Building done")
self.emulator = Emulator(self.builddir, self.downloaddir,
self.logtofile, self.timeout_multiplier)
def tearDown(self):
if self.emulator:
self.emulator.stop()
super(BRTest, self).tearDown()
# Run the given 'cmd' with a 'timeout' on the target and
# assert that the command succeeded; on error, print the
# faulty command and its output
def assertRunOk(self, cmd, timeout=-1):
out, exit_code = self.emulator.run(cmd, timeout)
self.assertEqual(
exit_code,
0,
"\nFailed to run: {}\noutput was:\n{}".format(cmd, ' '+'\n '.join(out))
)
# Run the given 'cmd' with a 'timeout' on the target and
# assert that the command fails; on success, print the
# faulty command and its output
def assertRunNotOk(self, cmd, timeout=-1):
out, exit_code = self.emulator.run(cmd, timeout)
self.assertNotEqual(
exit_code,
0,
"\nUnexpected success: {}\noutput was:\n{}".format(cmd, ' '+'\n '.join(out))
)

View File

@@ -0,0 +1,117 @@
import os
import shutil
import subprocess
import infra
class Builder(object):
def __init__(self, config, builddir, logtofile, jlevel=None):
self.config = '\n'.join([line.lstrip() for line in
config.splitlines()]) + '\n'
self.builddir = builddir
self.logfile = infra.open_log_file(builddir, "build", logtofile)
self.jlevel = jlevel
def is_defconfig_valid(self, configfile, defconfig):
"""Check if the .config is contains all lines present in the defconfig."""
with open(configfile) as configf:
configlines = configf.readlines()
defconfiglines = defconfig.split("\n")
# Check that all the defconfig lines are still present
for defconfigline in defconfiglines:
if defconfigline + "\n" not in configlines:
self.logfile.write("WARN: defconfig can't be used\n")
self.logfile.write(" Missing: %s\n" % defconfigline.strip())
self.logfile.flush()
return False
return True
def configure(self, make_extra_opts=[], make_extra_env={}):
"""Configure the build.
make_extra_opts: a list of arguments to be passed to the make
command.
e.g. make_extra_opts=["BR2_EXTERNAL=/path"]
make_extra_env: a dict of variables to be appended (or replaced)
in the environment that calls make.
e.g. make_extra_env={"BR2_DL_DIR": "/path"}
"""
if not os.path.isdir(self.builddir):
os.makedirs(self.builddir)
config_file = os.path.join(self.builddir, ".config")
with open(config_file, "w+") as cf:
cf.write(self.config)
# dump the defconfig to the logfile for easy debugging
self.logfile.write("> start defconfig\n" + self.config +
"> end defconfig\n")
self.logfile.flush()
env = {
"PATH": os.environ["PATH"],
"HOME": os.environ["HOME"]
}
env.update(make_extra_env)
cmd = ["make",
"O={}".format(self.builddir)]
cmd += make_extra_opts
cmd += ["olddefconfig"]
ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
cwd=infra.basepath(), env=env)
if ret != 0:
raise SystemError("Cannot olddefconfig")
if not self.is_defconfig_valid(config_file, self.config):
raise SystemError("The defconfig is not valid")
def build(self, make_extra_opts=[], make_extra_env={}):
"""Perform the build.
make_extra_opts: a list of arguments to be passed to the make
command. It can include a make target.
e.g. make_extra_opts=["foo-source"]
make_extra_env: a dict of variables to be appended (or replaced)
in the environment that calls make.
e.g. make_extra_env={"BR2_DL_DIR": "/path"}
"""
env = {
"PATH": os.environ["PATH"],
"HOME": os.environ["HOME"]
}
if "http_proxy" in os.environ:
self.logfile.write("Using system proxy: " +
os.environ["http_proxy"] + "\n")
env['http_proxy'] = os.environ["http_proxy"]
env['https_proxy'] = os.environ["http_proxy"]
env.update(make_extra_env)
cmd = ["make", "-C", self.builddir]
if "BR2_PER_PACKAGE_DIRECTORIES=y" in self.config.splitlines() and self.jlevel:
cmd.append(f"-j{self.jlevel}")
cmd += make_extra_opts
ret = subprocess.call(cmd, stdout=self.logfile, stderr=self.logfile,
env=env)
if ret != 0:
raise SystemError("Build failed")
open(self.stamp_path(), 'a').close()
def stamp_path(self):
return os.path.join(self.builddir, "build-done")
def is_finished(self):
return os.path.exists(self.stamp_path())
def delete(self):
if os.path.exists(self.builddir):
shutil.rmtree(self.builddir)

View File

@@ -0,0 +1,192 @@
# SPDX-License-Identifier: GPL-2.0
# SPDX-License-Identifier: ISC
import os
import pexpect
import pexpect.replwrap
import time
import infra
BR_PROMPT = '[BRTEST# '
BR_CONTINUATION_PROMPT = '[BRTEST+ '
def _repl_sh_child(child, orig_prompt, extra_init_cmd):
"""Wrap the shell prompt to handle command output
Based on pexpect.replwrap._repl_sh() (ISC licensed)
https://github.com/pexpect/pexpect/blob/aa989594e1e413f45c18b26ded1783f7d5990fe5/pexpect/replwrap.py#L115
"""
# If the user runs 'env', the value of PS1 will be in the output. To avoid
# replwrap seeing that as the next prompt, we'll embed the marker characters
# for invisible characters in the prompt; these show up when inspecting the
# environment variable, but not when bash displays the prompt.
non_printable_insert = '\\[\\]'
ps1 = BR_PROMPT[:5] + non_printable_insert + BR_PROMPT[5:]
ps2 = (BR_CONTINUATION_PROMPT[:5] + non_printable_insert +
BR_CONTINUATION_PROMPT[5:])
prompt_change = "PS1='{0}' PS2='{1}' PROMPT_COMMAND=''".format(ps1, ps2)
# Note: this will run various commands, each with the default timeout defined
# when qemu was spawned.
return pexpect.replwrap.REPLWrapper(
child,
orig_prompt,
prompt_change,
new_prompt=BR_PROMPT,
continuation_prompt=BR_CONTINUATION_PROMPT,
extra_init_cmd=extra_init_cmd
)
class Emulator(object):
def __init__(self, builddir, downloaddir, logtofile, timeout_multiplier):
self.qemu = None
self.repl = None
self.builddir = builddir
self.downloaddir = downloaddir
self.logfile = infra.open_log_file(builddir, "run", logtofile)
# We use elastic runners on the cloud to runs our tests. Those runners
# can take a long time to run the emulator. Use a timeout multiplier
# when running the tests to avoid sporadic failures.
self.timeout_multiplier = timeout_multiplier
# Start Qemu to boot the system
#
# arch: Qemu architecture to use
#
# kernel: path to the kernel image, or the special string
# 'builtin'. 'builtin' means a pre-built kernel image will be
# downloaded from ARTIFACTS_URL and suitable options are
# automatically passed to qemu and added to the kernel cmdline. So
# far only armv5, armv7 and i386 builtin kernels are available.
# If None, then no kernel is used, and we assume a bootable device
# will be specified.
#
# kernel_cmdline: array of kernel arguments to pass to Qemu -append option
#
# options: array of command line options to pass to Qemu
#
def boot(self, arch, kernel=None, kernel_cmdline=None, options=None):
if arch in ["armv7", "armv5"]:
qemu_arch = "arm"
else:
qemu_arch = arch
qemu_cmd = ["qemu-system-{}".format(qemu_arch),
"-serial", "stdio",
"-display", "none",
"-m", "256"]
if options:
qemu_cmd += options
if kernel_cmdline is None:
kernel_cmdline = []
if kernel:
if kernel == "builtin":
if arch in ["armv7", "armv5"]:
kernel_cmdline.append("console=ttyAMA0")
if arch == "armv7":
kernel = infra.download(self.downloaddir,
"kernel-vexpress-5.10.202")
dtb = infra.download(self.downloaddir,
"vexpress-v2p-ca9-5.10.202.dtb")
qemu_cmd += ["-dtb", dtb]
qemu_cmd += ["-M", "vexpress-a9"]
elif arch == "armv5":
kernel = infra.download(self.downloaddir,
"kernel-versatile-5.10.202")
dtb = infra.download(self.downloaddir,
"versatile-pb-5.10.202.dtb")
qemu_cmd += ["-dtb", dtb]
qemu_cmd += ["-M", "versatilepb"]
qemu_cmd += ["-device", "virtio-rng-pci"]
qemu_cmd += ["-kernel", kernel]
if kernel_cmdline:
qemu_cmd += ["-append", " ".join(kernel_cmdline)]
self.logfile.write(f"> host cpu count: {os.cpu_count()}\n")
ldavg = os.getloadavg()
ldavg_str = f"{ldavg[0]:.2f}, {ldavg[1]:.2f}, {ldavg[2]:.2f}"
self.logfile.write(f"> host loadavg: {ldavg_str}\n")
self.logfile.write(f"> timeout multiplier: {self.timeout_multiplier}\n")
self.logfile.write(f"> emulator using {qemu_cmd[0]} version:\n")
host_bin = os.path.join(self.builddir, "host", "bin")
br_path = host_bin + os.pathsep + os.environ["PATH"]
qemu_env = {"QEMU_AUDIO_DRV": "none",
"PATH": br_path}
pexpect.run(f"{qemu_cmd[0]} --version",
encoding='utf-8',
logfile=self.logfile,
env=qemu_env)
self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
self.qemu = pexpect.spawn(qemu_cmd[0], qemu_cmd[1:],
timeout=5 * self.timeout_multiplier,
encoding='utf-8',
codec_errors='replace',
env=qemu_env)
# We want only stdout into the log to avoid double echo
self.qemu.logfile_read = self.logfile
# Wait for the login prompt to appear, and then login as root with
# the provided password, or no password if not specified.
def login(self, password=None, timeout=60):
# The login prompt can take some time to appear when running multiple
# instances in parallel, so set the timeout to a large value
index = self.qemu.expect(["buildroot login:", pexpect.TIMEOUT],
timeout=timeout * self.timeout_multiplier)
if index != 0:
self.logfile.write("==> System does not boot")
raise SystemError("System does not boot")
self.qemu.sendline("root")
if password:
self.qemu.expect("Password:")
self.qemu.sendline(password)
self.connect_shell()
output, exit_code = self.run(f"date -s @{int(time.time())}")
if exit_code:
raise SystemError("Cannot set date in virtual machine")
def connect_shell(self):
extra_init_cmd = " && ".join([
'export PAGER=cat',
'dmesg -n 1',
# Prevent the shell from wrapping the commands at 80 columns.
'stty columns 29999',
# Fix the prompt of any subshells that get run
'printf "%s\n" "PS1=\'$PS1\'" "PS2=\'$PS2\'" "PROMPT_COMMAND=\'\'" >>/etc/profile'
])
self.repl = _repl_sh_child(self.qemu, '# ', extra_init_cmd)
if not self.repl:
raise SystemError("Cannot initialize REPL prompt")
# Run the given 'cmd' with a 'timeout' on the target
# return a tuple (output, exit_code)
def run(self, cmd, timeout=-1):
if timeout != -1:
timeout *= self.timeout_multiplier
output = self.repl.run_command(cmd, timeout=timeout)
# Remove double carriage return from qemu stdout so str.splitlines()
# works as expected.
output = output.replace("\r\r", "\r").splitlines()[1:]
exit_code = self.repl.run_command("echo $?")
exit_code = self.qemu.before.splitlines()[2]
exit_code = int(exit_code)
return output, exit_code
def stop(self):
if self.qemu is None:
return
self.qemu.terminate(force=True)