Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"console": "integratedTerminal",
"args": [
"--src-tree", "../linux",
"--output-tree", "../linux/kernel-build",
"--output-tree", "../linux/kernel_build",
"--root-output-in-tree", "vmlinux",
"--debug"
]
Expand Down
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"python.testing.unittestEnabled": true,
"ruff.lineLength": 120,
"editor.formatOnSave": true,
"search.useIgnoreFiles": false,
"files.exclude": {
"**/__pycache__": true
},
Expand Down
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ A script to generate an SPDX-format Software Bill of Materials (SBOM) for the `v
The eventual goal is to integrate the `sbom/` directory into the `linux/scripts/` directory in the official [linux](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/) kernel source tree.

## Getting Started
1. Clone the repository
2. Activate the venv and install build dependencies
```bash
python3 -m venv .venv
source .venv/bin/activate
pip install pre-commit reuse ruff
pre-commit install
```
3. provide a linux src and output tree, e.g., by downloading precompiled testdata from [KernelSbom-TestData](https://fileshare.tngtech.com/library/98e7e6f8-bffe-4a55-a8d2-817d4f3e51e8/KernelSbom-TestData/)
1. Provide a linux src and output tree, e.g., by downloading precompiled testdata from [KernelSbom-TestData](https://fileshare.tngtech.com/library/98e7e6f8-bffe-4a55-a8d2-817d4f3e51e8/KernelSbom-TestData/)
```bash
test_archive="linux-defconfig.tar.gz"
curl -L -o "$test_archive" "https://fileshare.tngtech.com/d/e69946da808b41f88047/files/?p=%2F$test_archive&dl=1"
Expand All @@ -31,16 +23,21 @@ The eventual goal is to integrate the `sbom/` directory into the `linux/scripts/
make <config> O=kernel_build
make -j$(nproc) O=kernel_build
```
4. Run the [sbom.py](sbom/sbom.py) script
2. Clone the repository
```
git clone git@github.com:TNG/KernelSbom.git
cd KernelSbom
```
3. Run the [sbom.py](sbom/sbom.py) script
```bash
python3 sbom/sbom.py \
--src-tree linux \
--output-tree linux/kernel_build \
--src-tree ../linux \
--output-tree ../linux/kernel_build \
--root-output-in-tree vmlinux \
--spdx sbom.spdx.json \
--used-files sbom.used_files.txt
```
Starting from `vmlinux` the script builds the **cmd graph**, a directed acyclic graph (DAG) where nodes are filenames and edges represent build dependencies extracted from `.<filename>.cmd` files. Based on the cmd graph, the final `sbom.spdx.json`, `sbom.used_files.txt` files are created and saved in this repository’s root directory.
Starting from `vmlinux` the script builds the **cmd graph**, a directed acyclic graph (DAG) where nodes are filenames and edges represent build dependencies extracted from `.<filename>.cmd` files. Based on the cmd graph, the final `sbom.spdx.json`, and `sbom.used_files.txt` files are created and saved in this repository’s root directory.

## Directory Structure

Expand All @@ -55,7 +52,15 @@ The eventual goal is to integrate the `sbom/` directory into the `linux/scripts/

The main contribution is the content of the `sbom` directory which eventually should be moved into the `linux/scripts/` directory in the official [linux](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/) kernel source tree.

## Reuse
## Development

Activate the venv and install build dependencies
```bash
python3 -m venv .venv
source .venv/bin/activate
pip install pre-commit reuse ruff
pre-commit install
```

when commiting `reuse lint` is executed as a pre-commit hook to check if all files have compliant License headers. If any file is missing a license header add it via
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,26 @@ def _remove_files(base_path: Path, patterns_to_remove: list[re.Pattern[str]], ig


if __name__ == "__main__":
"""
cmd_graph_based_kernel_build.py <src_tree> <output_tree>
"""
script_path = Path(__file__).parent
# Paths to the original source and build directories
cmd_graph_path = script_path / "../cmd_graph.pickle"
src_tree = (script_path / "../../linux").resolve()
output_tree = (script_path / "../../linux/kernel_build").resolve()
src_tree = (
Path(sys.argv[1]).resolve()
if len(sys.argv) >= 2 and sys.argv[1]
else (script_path / "../../../linux").resolve()
)
output_tree = (
Path(sys.argv[1]).resolve() if len(sys.argv) >= 3 and sys.argv[2] else (src_tree / "kernel_build").resolve()
)
root_output_in_tree = Path("vmlinux")
cmd_graph_path = script_path / "../cmd_graph.pickle"
cmd_src_tree = src_tree.parent / f"{src_tree.name}_cmd"

# Configure logging
logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s")

# Copy the original source tree
cmd_src_tree = (script_path / "../../linux_cmd").resolve()
if cmd_src_tree.exists():
shutil.rmtree(cmd_src_tree)
logging.info(f"Copy {src_tree} into {cmd_src_tree}")
Expand Down
15 changes: 12 additions & 3 deletions sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,20 @@ def traverse(node: CmdGraphNode, depth: int = 0):


if __name__ == "__main__":
"""
cmd_graph_visualization.py <src_tree> <output_tree>
"""
script_path = Path(__file__).parent
cmd_graph_path = script_path / "../cmd_graph.pickle"
src_tree = (script_path / "../../linux").resolve()
output_tree = (script_path / "../../linux/kernel_build").resolve()
src_tree = (
Path(sys.argv[1]).resolve()
if len(sys.argv) >= 2 and sys.argv[1]
else (script_path / "../../../linux").resolve()
)
output_tree = (
Path(sys.argv[1]).resolve() if len(sys.argv) >= 3 and sys.argv[2] else (src_tree / "kernel_build").resolve()
)
root_output_in_tree = Path("vmlinux")
cmd_graph_path = script_path / "../cmd_graph.pickle"
cmd_graph_json_gz_path = script_path / "web/cmd_graph.json.gz"
max_visualization_depth: int | None = None

Expand Down