Debian Codegraph @

vmtest/kmod.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import logging
5 import os
6 from pathlib import Path
7 import shutil
8 import subprocess
9 import tempfile
10 from typing import Optional, TextIO
11
12 from util import nproc, out_of_date
13 from vmtest.config import Kernel
14 from vmtest.download import downloaded_compiler
15
16 logger = logging.getLogger(__name__)
17
18
19 def build_kmod(
20 download_dir: Path, kernel: Kernel, outfile: Optional[TextIO] = None
21 ) -> Path:
22 kmod = kernel.path.parent / f"drgn_test-{kernel.release}.ko"
23 # External modules can't do out-of-tree builds for some reason, so copy the
24 # source files to a temporary directory and build the module there, then
25 # move it to the final location.
26 kmod_source_dir = Path("tests/linux_kernel/kmod")
27 source_files = ("drgn_test.c", "Makefile")
28 if out_of_date(kmod, *[kmod_source_dir / filename for filename in source_files]):
29 logger.info("building %s", kmod)
30
31 compiler = downloaded_compiler(download_dir, kernel.arch)
32 kernel_build_dir = kernel.path / "build"
33
34 with tempfile.TemporaryDirectory(dir=kmod.parent) as tmp_name:
35 tmp_dir = Path(tmp_name)
36 # Make sure that header files have the same paths as in the
37 # original kernel build.
38 debug_prefix_map = [
39 f"{kernel_build_dir.resolve()}=.",
40 f"{tmp_dir.resolve()}=./drgn_test",
41 ]
42 cflags = " ".join(["-fdebug-prefix-map=" + map for map in debug_prefix_map])
43 for filename in source_files:
44 shutil.copy(kmod_source_dir / filename, tmp_dir / filename)
45 subprocess.check_call(
46 [
47 "make",
48 "ARCH=" + kernel.arch.kernel_arch,
49 "-C",
50 kernel_build_dir,
51 f"M={tmp_dir.resolve()}",
52 "KAFLAGS=" + cflags,
53 "KCFLAGS=" + cflags,
54 "-j",
55 str(nproc()),
56 ],
57 env={**os.environ, **compiler.env()},
58 stdout=outfile,
59 stderr=outfile,
60 )
61 (tmp_dir / "drgn_test.ko").rename(kmod)
62 else:
63 logger.info("%s is up to date", kmod)
64 return kmod
65
66
67 def _main() -> None:
68 import argparse
69
70 from vmtest.download import (
71 DOWNLOAD_KERNEL_ARGPARSE_METAVAR,
72 DownloadCompiler,
73 download_in_thread,
74 download_kernel_argparse_type,
75 )
76
77 logging.basicConfig(
78 format="%(asctime)s:%(levelname)s:%(name)s:%(message)s", level=logging.INFO
79 )
80
81 parser = argparse.ArgumentParser(
82 description="build drgn test kernel module",
83 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
84 )
85 parser.add_argument(
86 "-k",
87 "--kernel",
88 metavar=DOWNLOAD_KERNEL_ARGPARSE_METAVAR,
89 dest="kernels",
90 action="append",
91 type=download_kernel_argparse_type,
92 default=argparse.SUPPRESS,
93 help="kernel to build for; may be given multiple times",
94 )
95 parser.add_argument(
96 "-d",
97 "--directory",
98 metavar="DIR",
99 type=Path,
100 default="build/vmtest",
101 help="directory to download assets to",
102 )
103 args = parser.parse_args()
104 if not hasattr(args, "kernels"):
105 args.kernels = []
106
107 compilers_needed = {
108 kernel.arch.name: DownloadCompiler(kernel.arch) for kernel in args.kernels
109 }
110
111 to_download = list(compilers_needed.values())
112 for kernel in args.kernels:
113 to_download.append(kernel)
114 with download_in_thread(args.directory, to_download) as downloads:
115 download_it = iter(downloads)
116
117 for i in range(len(compilers_needed)):
118 next(download_it)
119
120 for kernel in args.kernels:
121 downloaded: Kernel = next(download_it) # type: ignore[assignment]
122 print(build_kmod(args.directory, downloaded))
123
124
125 if __name__ == "__main__":
126 _main()