[PATCH v2 2/4] bootlinux: support existing kernel trees

Daniel Gomez <[email protected]> Mon, 01 Dec 2025 20:14:39 +0100
Newsgroups dev.linux.lists.kdevops
Message-ID <20251201-custom-linux-9p-tree-and-mount-point-v2-2-c720a83cab91@samsung.com>
From: Daniel Gomez <[email protected]>

Add intelligent detection and validation for existing kernel source trees
at the configured 9P host path. When a directory already exists at the
configured location, the bootlinux role now validates it is a proper
kernel tree before proceeding instead of failing the git clone operation.

The validation checks for required kernel tree markers using the same
logic as scripts/checkpatch.pl top_of_kernel_tree() function. This
ensures the directory contains essential files and directories that
define a valid Linux kernel source tree.

When bootlinux_9p_host_path exists, the role checks for kernel markers
like COPYING, MAINTAINERS, Makefile, and directories arch, drivers, and
kernel. If validation passes, git clone is skipped. The role then checks
if the tree is a git repository and attempts to checkout the configured
ref. This supports development workflows where users have pre-cloned
kernel trees and want to reuse them instead of cloning repeatedly.

The validation prevents silent failures when pointing to wrong directories.
If validation fails, users get clear guidance to either remove the invalid
directory and let kdevops clone, or point to a valid kernel tree. If ref
checkout fails, it indicates a mismatch between the configured tree
selection and the actual upstream in the existing directory.

The validation tasks include clone and checkout tags so they execute when
running with --tags clone, ensuring the prerequisite kernel_dir_stat
variable is set before the git clone task evaluates its condition.

Generated-by: Claude AI
Signed-off-by: Daniel Gomez <[email protected]>
---
 playbooks/roles/bootlinux/tasks/build/9p.yml | 75 +++++++++++++++++++++++-----
 1 file changed, 63 insertions(+), 12 deletions(-)

diff --git a/playbooks/roles/bootlinux/tasks/build/9p.yml b/playbooks/roles/bootlinux/tasks/build/9p.yml
index f390f028..0f24185f 100644
--- a/playbooks/roles/bootlinux/tasks/build/9p.yml
+++ b/playbooks/roles/bootlinux/tasks/build/9p.yml
@@ -30,40 +30,91 @@
       $ {{ ansible_callback_diy.result.output.cmd | join(' ') }}
       {{ ansible_callback_diy.result.output.stdout | default('') }}
 
-- name: Check if target directory exists when using 9p and Linux CLI was set
+- name: Check if kernel source directory exists
   ansible.builtin.stat:
     path: "{{ bootlinux_9p_host_path }}"
-  register: target_directory_stat
+  register: kernel_dir_stat
   run_once: true
   delegate_to: localhost
+  tags: ["clone", "checkout"]
+
+- name: Check for required kernel tree markers
+  ansible.builtin.stat:
+    path: "{{ bootlinux_9p_host_path }}/{{ item }}"
+  register: kernel_markers
+  loop:
+    - COPYING
+    - MAINTAINERS
+    - Makefile
+    - arch
+    - drivers
+    - kernel
   when:
-    - bootlinux_tree_set_by_cli|bool
+    - kernel_dir_stat.stat.exists
+  run_once: true
+  delegate_to: localhost
+  tags: ["clone", "checkout"]
 
-- name: Fail if target directory does not exist when using 9p and Linux CLI was set
-  ansible.builtin.fail:
-    msg: "The target directory {{ bootlinux_9p_host_path }} does not exist."
+- name: Validate kernel tree
+  ansible.builtin.assert:
+    that:
+      - kernel_markers.results | selectattr('stat.exists') | list | length == 6
+    fail_msg: |
+      ERROR: {{ bootlinux_9p_host_path }} exists but is NOT a valid Linux kernel tree.
+
+      Missing required files/directories. A valid kernel tree must contain:
+      COPYING, MAINTAINERS, Makefile, arch/, drivers/, kernel/
+
+      Please either:
+      1. Remove/rename the directory and let kdevops clone
+      2. Point BOOTLINUX_9P_HOST_PATH to a valid kernel tree
+    success_msg: "Validated {{ bootlinux_9p_host_path }} is a kernel tree"
+  when:
+    - kernel_dir_stat.stat.exists
   run_once: true
   delegate_to: localhost
+  tags: ["clone", "checkout"]
+
+- name: Check if kernel tree is a git repository
+  ansible.builtin.stat:
+    path: "{{ bootlinux_9p_host_path }}/.git"
+  register: kernel_git_stat
   when:
-    - bootlinux_tree_set_by_cli|bool
-    - not target_directory_stat.stat.exists
+    - kernel_dir_stat.stat.exists
+  run_once: true
+  delegate_to: localhost
+  tags: ["clone", "checkout"]
 
 - name: Git clone {{ target_linux_tree }} on the control node
   ansible.builtin.git:
     repo: "{{ target_linux_git }}"
     dest: "{{ bootlinux_9p_host_path }}"
-    update: true
+    update: false
     depth: "{{ target_linux_shallow_depth }}"
     version: "{{ active_linux_ref | default(target_linux_ref) }}"
   retries: 3
   delay: 5
-  register: result
-  until: not result.failed
+  register: git_clone_result
+  until: not git_clone_result.failed
+  when:
+    - not kernel_dir_stat.stat.exists
+  run_once: true
+  delegate_to: localhost
   tags: ["clone"]
+
+- name: Checkout git ref {{ active_linux_ref | default(target_linux_ref) }} in existing tree
+  ansible.builtin.command:
+    cmd: git checkout {{ active_linux_ref | default(target_linux_ref) }}
+    chdir: "{{ bootlinux_9p_host_path }}"
+  register: git_checkout
+  failed_when: git_checkout.rc != 0
+  changed_when: "'Switched to' in git_checkout.stderr or 'Already on' in git_checkout.stdout"
   when:
-    - not bootlinux_tree_set_by_cli|bool
+    - kernel_dir_stat.stat.exists
+    - kernel_git_stat.stat.exists
   run_once: true
   delegate_to: localhost
+  tags: ["clone", "checkout"]
 
 - name: Copy kernel delta if requested on the control node
   ansible.builtin.template:

-- 
2.52.0