diff --git a/.vscode/launch.json b/.vscode/launch.json index d372d052..27c016e4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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" ] diff --git a/.vscode/settings.json b/.vscode/settings.json index 6c3d1a12..aaa102ef 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,7 +13,6 @@ "python.testing.unittestEnabled": true, "ruff.lineLength": 120, "editor.formatOnSave": true, - "search.useIgnoreFiles": false, "files.exclude": { "**/__pycache__": true }, diff --git a/README.md b/README.md index b16eb5f0..27ed3cd2 100644 --- a/README.md +++ b/README.md @@ -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" @@ -31,16 +23,21 @@ The eventual goal is to integrate the `sbom/` directory into the `linux/scripts/ make 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 `..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 `..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 @@ -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 ``` diff --git a/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py b/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py index dbc5255f..51c77e53 100644 --- a/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py +++ b/sbom_analysis/cmd_graph_based_kernel_build/cmd_graph_based_kernel_build.py @@ -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 + """ 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}") diff --git a/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py b/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py index d577feee..bd7c73f8 100644 --- a/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py +++ b/sbom_analysis/cmd_graph_visualization/cmd_graph_visualization.py @@ -69,11 +69,20 @@ def traverse(node: CmdGraphNode, depth: int = 0): if __name__ == "__main__": + """ + cmd_graph_visualization.py + """ 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