kerf-multikernel 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kerf/init/main.py ADDED
@@ -0,0 +1,696 @@
1
+ # Copyright 2025 Multikernel Technologies, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ Initialize baseline device tree configuration.
17
+
18
+ This command sets up the baseline device tree which describes the hardware
19
+ resources available for allocation to kernel instances. The baseline must
20
+ contain only resources (no instances).
21
+ """
22
+
23
+ import ctypes
24
+ import os
25
+ import re
26
+ import sys
27
+ from pathlib import Path
28
+ from typing import Optional
29
+
30
+ import click
31
+ import libfdt
32
+
33
+ try:
34
+ import pyudev
35
+ except ImportError:
36
+ pyudev = None
37
+
38
+ from ..baseline import BaselineManager
39
+ from ..create.main import parse_cpu_spec, parse_device_list, parse_memory_spec
40
+ from ..dtc.parser import DeviceTreeParser
41
+ from ..lazy_cma import LAZY_CMA_DEVICE, allocate_multikernel_pool
42
+ from ..resources import get_memory_pool_from_iomem
43
+ from ..dtc.reporter import ValidationReporter
44
+ from ..dtc.validator import MultikernelValidator
45
+ from ..exceptions import KernelInterfaceError, ParseError, ValidationError
46
+ from ..models import (
47
+ CPUAllocation,
48
+ DeviceInfo,
49
+ GlobalDeviceTree,
50
+ HardwareInventory,
51
+ MemoryAllocation,
52
+ )
53
+
54
+
55
+ MULTIKERNEL_MOUNT_POINT = "/sys/fs/multikernel"
56
+
57
+ def is_multikernel_mounted() -> bool:
58
+ mount_point = Path(MULTIKERNEL_MOUNT_POINT)
59
+ if not mount_point.exists():
60
+ return False
61
+
62
+ try:
63
+ with open('/proc/mounts', 'r', encoding='utf-8') as f:
64
+ for line in f:
65
+ parts = line.split()
66
+ if len(parts) >= 2 and parts[1] == MULTIKERNEL_MOUNT_POINT:
67
+ return True
68
+ except (OSError, IOError):
69
+ pass
70
+
71
+ return False
72
+
73
+
74
+ def mount_multikernel_fs(verbose: bool = False) -> None:
75
+ if is_multikernel_mounted():
76
+ if verbose:
77
+ click.echo(f"✓ Multikernel filesystem already mounted at {MULTIKERNEL_MOUNT_POINT}")
78
+ return
79
+
80
+ mount_point = Path(MULTIKERNEL_MOUNT_POINT)
81
+ try:
82
+ mount_point.mkdir(parents=True, exist_ok=True)
83
+ except OSError as e:
84
+ raise KernelInterfaceError(
85
+ f"Failed to create mount point {MULTIKERNEL_MOUNT_POINT}: {e}"
86
+ ) from e
87
+
88
+ libc = ctypes.CDLL(None, use_errno=True)
89
+
90
+ # mount() signature: int mount(const char *source, const char *target,
91
+ # const char *filesystemtype, unsigned long mountflags,
92
+ # const void *data);
93
+ libc.mount.argtypes = [
94
+ ctypes.c_char_p, # source
95
+ ctypes.c_char_p, # target
96
+ ctypes.c_char_p, # filesystemtype
97
+ ctypes.c_ulong, # mountflags
98
+ ctypes.c_void_p # data
99
+ ]
100
+ libc.mount.restype = ctypes.c_int
101
+
102
+ source = b"none"
103
+ target = MULTIKERNEL_MOUNT_POINT.encode('utf-8')
104
+ fstype = b"multikernel"
105
+ mountflags = 0
106
+ data = None
107
+
108
+ if verbose:
109
+ click.echo(f"Mounting multikernel filesystem at {MULTIKERNEL_MOUNT_POINT}...")
110
+
111
+ result = libc.mount(source, target, fstype, mountflags, data)
112
+
113
+ if result != 0:
114
+ errno = ctypes.get_errno()
115
+ error_msg = os.strerror(errno)
116
+ raise KernelInterfaceError(
117
+ f"Failed to mount multikernel filesystem: {error_msg} (errno: {errno})\n"
118
+ f"Make sure the multikernel kernel module is loaded and you have root privileges."
119
+ )
120
+
121
+ if verbose:
122
+ click.echo("✓ Successfully mounted multikernel filesystem")
123
+
124
+
125
+ def get_total_memory_from_system() -> Optional[int]:
126
+ """
127
+ Get total system memory from /proc/meminfo.
128
+ Returns total memory in bytes or None if not available.
129
+ """
130
+ try:
131
+ meminfo_path = Path('/proc/meminfo')
132
+ if not meminfo_path.exists():
133
+ return None
134
+ with open(meminfo_path, 'r', encoding='utf-8') as f:
135
+ for line in f:
136
+ if line.startswith('MemTotal:'):
137
+ match = re.search(r'(\d+)', line)
138
+ if match:
139
+ # Convert from KB to bytes
140
+ return int(match.group(1)) * 1024
141
+ except (OSError, IOError, ValueError):
142
+ pass
143
+
144
+ return None
145
+
146
+
147
+ def detect_pci_device(device_name: str) -> Optional[DeviceInfo]:
148
+ """
149
+ Detect PCI device information from the system.
150
+
151
+ Args:
152
+ device_name: Device name (e.g., "enp9s0" or PCI BDF "0000:09:00.0")
153
+
154
+ Returns:
155
+ DeviceInfo with detected PCI information, or None if not found
156
+ """
157
+ if pyudev is None:
158
+ raise KernelInterfaceError(
159
+ "pyudev is required for device detection. Please install it: pip install pyudev"
160
+ )
161
+
162
+ try:
163
+ context = pyudev.Context()
164
+ pci_device = None
165
+ pci_slot = None
166
+
167
+ if re.match(r'^[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\.[0-9a-f]$', device_name):
168
+ pci_slot = device_name
169
+ try:
170
+ pci_device = pyudev.Devices.from_path(context, f'/sys/bus/pci/devices/{pci_slot}')
171
+ except (ValueError, pyudev.DeviceNotFoundError):
172
+ return None
173
+ else:
174
+ try:
175
+ net_device = pyudev.Devices.from_name(context, 'net', device_name)
176
+ pci_device = net_device.find_parent('pci')
177
+ if pci_device:
178
+ pci_slot = pci_device.sys_name
179
+ except (ValueError, pyudev.DeviceNotFoundError):
180
+ pass
181
+
182
+ if not pci_device:
183
+ for device in context.list_devices(subsystem='pci'):
184
+ try:
185
+ for child in device.children:
186
+ if child.sys_name == device_name:
187
+ pci_device = device
188
+ pci_slot = device.sys_name
189
+ break
190
+ if pci_device:
191
+ break
192
+ except (OSError, AttributeError):
193
+ continue
194
+
195
+ if not pci_device or not pci_slot:
196
+ return None
197
+
198
+ vendor_id = None
199
+ device_id = None
200
+ pci_device_path = Path(pci_device.sys_path)
201
+
202
+ vendor_file = pci_device_path / 'vendor'
203
+ if vendor_file.exists():
204
+ try:
205
+ with open(vendor_file, 'r', encoding='utf-8') as f:
206
+ vendor_id = int(f.read().strip(), 16)
207
+ except (ValueError, IOError):
208
+ pass
209
+
210
+ device_file = pci_device_path / 'device'
211
+ if device_file.exists():
212
+ try:
213
+ with open(device_file, 'r', encoding='utf-8') as f:
214
+ device_id = int(f.read().strip(), 16)
215
+ except (ValueError, IOError):
216
+ pass
217
+
218
+ compatible = "pci-device" # Default
219
+ class_file = pci_device_path / 'class'
220
+ if class_file.exists():
221
+ try:
222
+ with open(class_file, 'r', encoding='utf-8') as f:
223
+ pci_class = int(f.read().strip(), 16)
224
+ class_code = (pci_class >> 16) & 0xFF
225
+ if class_code == 0x02: # Network controller
226
+ compatible = "pci-network"
227
+ elif class_code == 0x01: # Mass storage controller
228
+ compatible = "pci-storage"
229
+ elif class_code == 0x03: # Display controller
230
+ compatible = "pci-display"
231
+ elif class_code == 0x0c: # Serial bus controller
232
+ compatible = "pci-serial"
233
+ except (ValueError, IOError):
234
+ pass
235
+
236
+ return DeviceInfo(
237
+ name=device_name,
238
+ compatible=compatible,
239
+ device_type="pci",
240
+ pci_id=pci_slot,
241
+ vendor_id=vendor_id,
242
+ device_id=device_id
243
+ )
244
+ except (OSError, IOError, ValueError, AttributeError, pyudev.DeviceNotFoundError):
245
+ return None
246
+
247
+
248
+ def detect_platform_device(device_name: str) -> Optional[DeviceInfo]:
249
+ """
250
+ Detect platform device information from the system.
251
+
252
+ Args:
253
+ device_name: Device name (e.g., "serial_console")
254
+
255
+ Returns:
256
+ DeviceInfo with detected platform information, or None if not found
257
+ """
258
+ try:
259
+ platform_devices = Path('/sys/devices/platform')
260
+ if not platform_devices.exists():
261
+ return None
262
+
263
+ if 'serial' in device_name.lower() or 'console' in device_name.lower():
264
+ serial_path = platform_devices / 'serial8250'
265
+ if serial_path.exists():
266
+ return DeviceInfo(
267
+ name=device_name,
268
+ compatible="ns16550",
269
+ device_type="platform",
270
+ device_name="serial8250"
271
+ )
272
+
273
+ for platform_dev in platform_devices.iterdir():
274
+ if platform_dev.name in device_name or device_name in platform_dev.name:
275
+ return DeviceInfo(
276
+ name=device_name,
277
+ compatible="platform-device",
278
+ device_type="platform",
279
+ device_name=platform_dev.name
280
+ )
281
+
282
+ return None
283
+ except (OSError, IOError, ValueError):
284
+ return None
285
+
286
+
287
+ def detect_device_from_system(device_name: str) -> Optional[DeviceInfo]:
288
+ """
289
+ Detect device information from the system.
290
+ Tries PCI first, then platform devices.
291
+
292
+ Args:
293
+ device_name: Device name to detect
294
+
295
+ Returns:
296
+ DeviceInfo with detected information, or None if not found
297
+ """
298
+ pci_device = detect_pci_device(device_name)
299
+ if pci_device:
300
+ return pci_device
301
+
302
+ platform_device = detect_platform_device(device_name)
303
+ if platform_device:
304
+ return platform_device
305
+
306
+ return None
307
+
308
+
309
+ def get_total_cpus_from_system() -> Optional[int]:
310
+ """
311
+ Get total number of logical CPUs from the system via sysfs.
312
+ Returns total CPU count or None if not available.
313
+ """
314
+ try:
315
+ cpu_dir = Path('/sys/devices/system/cpu')
316
+ if cpu_dir.exists():
317
+ cpu_files = [f for f in cpu_dir.iterdir() if f.name.startswith('cpu') and f.name[3:].isdigit()]
318
+ if cpu_files:
319
+ cpu_numbers = [int(f.name[3:]) for f in cpu_files]
320
+ return max(cpu_numbers) + 1
321
+ except (OSError, ValueError):
322
+ pass
323
+
324
+ return None
325
+
326
+
327
+ def get_valid_apic_ids_from_system() -> Optional[set]:
328
+ """
329
+ Get set of valid APIC IDs from the system via /proc/cpuinfo.
330
+ Returns set of valid APIC IDs or None if not available.
331
+ """
332
+ try:
333
+ cpuinfo_path = Path('/proc/cpuinfo')
334
+ if not cpuinfo_path.exists():
335
+ return None
336
+
337
+ apic_ids = set()
338
+ with open(cpuinfo_path, 'r', encoding='utf-8') as f:
339
+ for line in f:
340
+ if line.startswith('apicid'):
341
+ parts = line.split(':')
342
+ if len(parts) == 2:
343
+ try:
344
+ apic_id = int(parts[1].strip())
345
+ apic_ids.add(apic_id)
346
+ except ValueError:
347
+ pass
348
+
349
+ return apic_ids if apic_ids else None
350
+ except (OSError, IOError):
351
+ pass
352
+
353
+ return None
354
+
355
+
356
+ def build_baseline_from_cmdline(
357
+ cpus: str,
358
+ memory: Optional[str] = None,
359
+ devices: Optional[str] = None,
360
+ verbose: bool = False
361
+ ) -> GlobalDeviceTree:
362
+ """
363
+ Build a GlobalDeviceTree from command line arguments.
364
+
365
+ Args:
366
+ cpus: CPU specification string (e.g., "4-7" or "4,5,6,7")
367
+ memory: Optional pool size to allocate at runtime via /dev/lazy_cma
368
+ (e.g., "1GB"); when omitted an existing pool must already
369
+ be registered in /proc/iomem
370
+ devices: Optional device names (comma-separated, e.g., "enp9s0_dev,nvme0")
371
+ verbose: Whether to print verbose output
372
+
373
+ Returns:
374
+ GlobalDeviceTree with resources only (no instances)
375
+
376
+ Raises:
377
+ ValueError: If CPU or memory specification is invalid
378
+ KernelInterfaceError: If the memory pool cannot be found or allocated
379
+ """
380
+ try:
381
+ cpu_list = parse_cpu_spec(cpus)
382
+ except ValueError as e:
383
+ raise ValueError(f"Invalid CPU specification '{cpus}': {e}") from e
384
+
385
+ # Validate against valid APIC IDs on the system
386
+ valid_apic_ids = get_valid_apic_ids_from_system()
387
+ if valid_apic_ids is None:
388
+ raise KernelInterfaceError(
389
+ "Could not read APIC IDs from /proc/cpuinfo. "
390
+ "Ensure the system exposes CPU topology information."
391
+ )
392
+
393
+ invalid_cpus = set(cpu_list) - valid_apic_ids
394
+ if invalid_cpus:
395
+ raise ValueError(
396
+ f"Invalid APIC ID(s) specified: {sorted(invalid_cpus)}. "
397
+ f"Valid APIC IDs on this system: {sorted(valid_apic_ids)}"
398
+ )
399
+
400
+ # Total CPUs is based on the max APIC ID + 1 for sizing purposes
401
+ total_cpus = max(valid_apic_ids) + 1
402
+ # Host reserved are all valid APIC IDs not in the available list
403
+ available_cpus = set(cpu_list)
404
+ host_reserved_cpus = sorted(list(valid_apic_ids - available_cpus))
405
+
406
+ if 0 in available_cpus and len(host_reserved_cpus) == 0:
407
+ if verbose:
408
+ click.echo("Warning: APIC ID 0 is in available list but no host-reserved CPUs. Moving APIC ID 0 to host-reserved.", err=True)
409
+ available_cpus.discard(0)
410
+ host_reserved_cpus = [0]
411
+ cpu_list = sorted(list(available_cpus))
412
+
413
+ memory_pool = get_memory_pool_from_iomem()
414
+ if memory_pool is None:
415
+ if not memory:
416
+ raise KernelInterfaceError(
417
+ "Could not find multikernel memory pool in /proc/iomem. "
418
+ "Pass --memory=SIZE (e.g. --memory=1GB) to allocate the pool "
419
+ f"at runtime via {LAZY_CMA_DEVICE}."
420
+ )
421
+
422
+ pool_bytes = parse_memory_spec(memory)
423
+ pool_base = allocate_multikernel_pool(pool_bytes)
424
+ if verbose:
425
+ click.echo(f"Allocated multikernel memory pool via {LAZY_CMA_DEVICE}:")
426
+ click.echo(f" Base: {hex(pool_base)}")
427
+ click.echo(f" Size: {pool_bytes} bytes ({pool_bytes / (1024**3):.2f} GB)")
428
+
429
+ # Re-read /proc/iomem so the baseline reflects exactly what the
430
+ # kernel registered for the allocation.
431
+ memory_pool = get_memory_pool_from_iomem() or (pool_base, pool_bytes)
432
+ elif memory:
433
+ click.echo(
434
+ "Note: multikernel memory pool already exists in /proc/iomem; "
435
+ "ignoring --memory",
436
+ err=True,
437
+ )
438
+ memory_pool_base, memory_pool_bytes = memory_pool
439
+
440
+ total_bytes = memory_pool_base + memory_pool_bytes
441
+ host_reserved_bytes = memory_pool_base
442
+ if verbose:
443
+ click.echo(f"Parsed APIC ID specification: {cpus}")
444
+ click.echo(f" Valid APIC IDs on system: {sorted(valid_apic_ids)}")
445
+ click.echo(f" Host-reserved APIC IDs: {host_reserved_cpus}")
446
+ click.echo(f" Available APIC IDs: {cpu_list}")
447
+ click.echo("Memory pool from /proc/iomem:")
448
+ click.echo(f" Base: {hex(memory_pool_base)}")
449
+ click.echo(f" Size: {memory_pool_bytes} bytes ({memory_pool_bytes / (1024**3):.2f} GB)")
450
+ click.echo(f" Total bytes: {total_bytes} bytes ({total_bytes / (1024**3):.2f} GB)")
451
+ click.echo(f" Host-reserved: {host_reserved_bytes} bytes ({host_reserved_bytes / (1024**3):.2f} GB)")
452
+
453
+ cpu_allocation = CPUAllocation(
454
+ total=total_cpus,
455
+ host_reserved=host_reserved_cpus,
456
+ available=cpu_list
457
+ )
458
+
459
+ memory_allocation = MemoryAllocation(
460
+ total_bytes=total_bytes,
461
+ host_reserved_bytes=host_reserved_bytes,
462
+ memory_pool_base=memory_pool_base,
463
+ memory_pool_bytes=memory_pool_bytes
464
+ )
465
+
466
+ device_dict = {}
467
+ if devices:
468
+ device_names = parse_device_list(devices)
469
+ for device_name in device_names:
470
+ device_info = detect_device_from_system(device_name)
471
+ if device_info:
472
+ device_dict[device_name] = device_info
473
+ if verbose:
474
+ click.echo(f"Detected device '{device_name}':")
475
+ click.echo(f" Type: {device_info.device_type}")
476
+ click.echo(f" Compatible: {device_info.compatible}")
477
+ if device_info.pci_id:
478
+ click.echo(f" PCI ID: {device_info.pci_id}")
479
+ if device_info.vendor_id is not None:
480
+ click.echo(f" Vendor ID: 0x{device_info.vendor_id:04x}")
481
+ if device_info.device_id is not None:
482
+ click.echo(f" Device ID: 0x{device_info.device_id:04x}")
483
+ if device_info.device_name:
484
+ click.echo(f" Device Name: {device_info.device_name}")
485
+ else:
486
+ raise KernelInterfaceError(
487
+ f"Could not detect device '{device_name}' from system. "
488
+ f"Please ensure the device exists and is accessible, or use --input with a DTS file to specify device details."
489
+ )
490
+
491
+ hardware = HardwareInventory(
492
+ cpus=cpu_allocation,
493
+ memory=memory_allocation,
494
+ devices=device_dict
495
+ )
496
+
497
+ tree = GlobalDeviceTree(
498
+ hardware=hardware,
499
+ instances={},
500
+ device_references={}
501
+ )
502
+
503
+ return tree
504
+
505
+
506
+ @click.command()
507
+ @click.pass_context
508
+ @click.option('--input', '-i', help='Input DTS or DTB file containing all resources. Mutually exclusive with --cpus, --memory and --devices. When used, all resources must come from the file.')
509
+ @click.option('--cpus', '-c', help='APIC ID specification for baseline (e.g., "128-134" or "128,130,132"). Use physical APIC IDs, not logical CPU numbers. Mutually exclusive with --input. Memory comes from --memory or an existing pool in /proc/iomem.')
510
+ @click.option('--memory', '-m', help='Memory pool size to allocate at runtime via /dev/lazy_cma (e.g., "1GB", "512MB"). If omitted, an existing pool is discovered from /proc/iomem. Mutually exclusive with --input.')
511
+ @click.option('--devices', '-d', help='Device names (comma-separated, e.g., "enp9s0_dev,nvme0"). Mutually exclusive with --input. Creates minimal device entries in baseline.')
512
+ @click.option('--dry-run', is_flag=True, help='Validate without applying')
513
+ @click.option('--report', is_flag=True, help='Generate detailed validation report')
514
+ @click.option('--format', type=click.Choice(['text', 'json', 'yaml']),
515
+ default='text', help='Report format (default: text)')
516
+ @click.option('--verbose', '-v', is_flag=True, help='Verbose output')
517
+ def init(ctx: click.Context, input: Optional[str], cpus: Optional[str], memory: Optional[str], devices: Optional[str], dry_run: bool, report: bool, format: str, verbose: bool):
518
+ """
519
+ Initialize baseline device tree configuration.
520
+
521
+ Sets up the baseline device tree which describes hardware resources
522
+ available for allocation. The baseline must contain ONLY resources
523
+ (no instances). Instances are created via 'kerf create' using overlays.
524
+
525
+ By default, the baseline is applied to the kernel after validation.
526
+ Use --dry-run to validate without applying.
527
+
528
+ You can either provide a DTS/DTB file via --input, or construct the
529
+ baseline from command line arguments using --cpus. These options are
530
+ mutually exclusive - when using --input, all resources must come from
531
+ the DTS file. With --cpus, the memory pool is allocated at runtime
532
+ via /dev/lazy_cma when --memory is given, or discovered from
533
+ /proc/iomem otherwise.
534
+
535
+ Examples:
536
+ # Initialize from DTS file (all resources from file)
537
+ kerf init --input=hardware.dts
538
+
539
+ # Initialize from command line, allocating a 1GB pool at runtime
540
+ kerf init --cpus=128-134 --memory=1GB
541
+
542
+ # Initialize reusing a pool already registered in /proc/iomem
543
+ kerf init --cpus=128-134
544
+
545
+ # Initialize with APIC IDs and devices
546
+ kerf init --cpus=128,130,132 --memory=1GB --devices=enp9s0_dev,nvme0
547
+
548
+ # Validate baseline without applying
549
+ kerf init --input=hardware.dts --dry-run
550
+ """
551
+ try:
552
+ # Validate that --input and resource specification options are mutually exclusive
553
+ # When using --input, all resources must come from the DTS file
554
+ if input and (cpus or memory or devices):
555
+ conflicting = []
556
+ if cpus:
557
+ conflicting.append("--cpus")
558
+ if memory:
559
+ conflicting.append("--memory")
560
+ if devices:
561
+ conflicting.append("--devices")
562
+ click.echo(f"Error: --input is mutually exclusive with {', '.join(conflicting)}.", err=True)
563
+ click.echo("When using --input, all resources must come from the DTS/DTB file.", err=True)
564
+ click.echo("Use either --input for a complete DTS/DTB file, or command-line options to construct baseline.", err=True)
565
+ sys.exit(2)
566
+
567
+ if not input and not cpus:
568
+ click.echo("Error: Either --input or --cpus must be specified", err=True)
569
+ click.echo("\nUsage:", err=True)
570
+ click.echo(" kerf init --input=hardware.dts", err=True)
571
+ click.echo(" kerf init --cpus=4-7 --memory=1GB", err=True)
572
+ click.echo(" kerf init --cpus=4-7 --memory=1GB --devices=enp9s0_dev", err=True)
573
+ sys.exit(2)
574
+
575
+ parser = DeviceTreeParser()
576
+ dts_content = None
577
+
578
+ if input:
579
+ # Parse from input file
580
+ input_path = Path(input)
581
+
582
+ if not input_path.exists():
583
+ click.echo(f"Error: Input file '{input}' does not exist", err=True)
584
+ sys.exit(3)
585
+
586
+ if input_path.suffix == '.dts':
587
+ with open(input_path, 'r', encoding='utf-8') as f:
588
+ dts_content = f.read()
589
+ tree = parser.parse_dts(dts_content)
590
+ elif input_path.suffix == '.dtb':
591
+ tree = parser.parse_dtb(str(input_path))
592
+ else:
593
+ click.echo(f"Error: Unsupported input format: {input_path.suffix}", err=True)
594
+ click.echo("Supported formats: .dts, .dtb", err=True)
595
+ sys.exit(2)
596
+ else:
597
+ # Build from command line arguments
598
+ try:
599
+ tree = build_baseline_from_cmdline(cpus, memory=memory, devices=devices, verbose=verbose)
600
+ except ValueError as e:
601
+ click.echo(f"Error: {e}", err=True)
602
+ sys.exit(2)
603
+ except KernelInterfaceError as e:
604
+ click.echo(f"Error: {e}", err=True)
605
+ sys.exit(1)
606
+
607
+ baseline_mgr = BaselineManager()
608
+
609
+ try:
610
+ baseline_mgr.validate_baseline(tree)
611
+ except ValidationError as e:
612
+ click.echo(f"Error: Invalid baseline configuration: {e}", err=True)
613
+ click.echo("\nBaseline must contain:", err=True)
614
+ click.echo(" /resources (hardware inventory)", err=True)
615
+ click.echo(" /instances (must be empty or absent)", err=True)
616
+ click.echo("\nInstances should be created via 'kerf create'", err=True)
617
+ sys.exit(1)
618
+
619
+ validator = MultikernelValidator()
620
+ if dts_content is not None:
621
+ input_path_str = str(input) if input else "command-line"
622
+ validator.set_dts_context(dts_content, input_path_str)
623
+
624
+ validation_result = validator.validate(tree)
625
+
626
+ if report:
627
+ reporter = ValidationReporter()
628
+ report_text = reporter.generate_report(validation_result, tree, verbose, format)
629
+ click.echo(report_text)
630
+ if not validation_result.is_valid:
631
+ sys.exit(1)
632
+ return
633
+
634
+ if not validation_result.is_valid:
635
+ click.echo("Validation failed:", err=True)
636
+ for error in validation_result.errors:
637
+ click.echo(f" ✗ {error}", err=True)
638
+ if validation_result.warnings:
639
+ click.echo("\nWarnings:", err=True)
640
+ for warning in validation_result.warnings:
641
+ click.echo(f" ⚠ {warning}", err=True)
642
+ sys.exit(1)
643
+
644
+ if verbose:
645
+ click.echo("✓ Baseline validation passed")
646
+ if validation_result.warnings:
647
+ click.echo("\nWarnings:")
648
+ for warning in validation_result.warnings:
649
+ click.echo(f" ⚠ {warning}")
650
+
651
+ debug = ctx.obj.get('debug', False) if ctx and ctx.obj else False
652
+
653
+ if dry_run:
654
+ click.echo(" Baseline validation passed")
655
+ click.echo(" Baseline would be applied (dry-run mode)")
656
+ else:
657
+ try:
658
+ mount_multikernel_fs(verbose=verbose)
659
+
660
+ if debug:
661
+ try:
662
+ dtb_data = baseline_mgr.extractor.generate_global_dtb(tree)
663
+ fdt = libfdt.Fdt(dtb_data)
664
+ dts_parser = DeviceTreeParser()
665
+ dts_parser.fdt = fdt
666
+ dts_lines = dts_parser._fdt_to_dts_recursive(0, 0) # pylint: disable=protected-access
667
+ dts_content = '\n'.join(dts_lines)
668
+
669
+ click.echo("Debug: Baseline DTS source being written to kernel:")
670
+ click.echo("─" * 70)
671
+ click.echo(dts_content)
672
+ click.echo("─" * 70)
673
+ except Exception as e:
674
+ click.echo(f"Debug: Failed to convert baseline DTB to DTS: {e}", err=True)
675
+
676
+ if verbose:
677
+ click.echo("Writing baseline to kernel...")
678
+ baseline_mgr.write_baseline(tree)
679
+ click.echo("✓ Baseline applied to kernel successfully")
680
+ click.echo(" Baseline: /sys/fs/multikernel/device_tree")
681
+ except KernelInterfaceError as e:
682
+ click.echo(f"Error: Failed to apply baseline: {e}", err=True)
683
+ if verbose:
684
+ import traceback
685
+ traceback.print_exc()
686
+ sys.exit(1)
687
+
688
+ except ParseError as e:
689
+ click.echo(f"Error: Failed to parse input file: {e}", err=True)
690
+ sys.exit(1)
691
+ except Exception as e:
692
+ click.echo(f"Error: {e}", err=True)
693
+ if verbose:
694
+ import traceback
695
+ traceback.print_exc()
696
+ sys.exit(1)