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__.py +74 -0
- kerf/baseline.py +187 -0
- kerf/cli.py +56 -0
- kerf/console/__init__.py +21 -0
- kerf/console/main.py +252 -0
- kerf/create/__init__.py +17 -0
- kerf/create/main.py +780 -0
- kerf/data/__init__.py +24 -0
- kerf/data/kerf-init +0 -0
- kerf/daxfs/__init__.py +31 -0
- kerf/daxfs/mkdaxfs.py +597 -0
- kerf/delete/__init__.py +21 -0
- kerf/delete/main.py +238 -0
- kerf/docker/__init__.py +21 -0
- kerf/docker/image.py +234 -0
- kerf/dtc/__init__.py +27 -0
- kerf/dtc/cells.py +51 -0
- kerf/dtc/extractor.py +362 -0
- kerf/dtc/overlay.py +378 -0
- kerf/dtc/parser.py +1548 -0
- kerf/dtc/reporter.py +308 -0
- kerf/dtc/validator.py +792 -0
- kerf/exceptions.py +49 -0
- kerf/exec/__init__.py +21 -0
- kerf/exec/main.py +260 -0
- kerf/init/__init__.py +13 -0
- kerf/init/main.py +696 -0
- kerf/kill/__init__.py +21 -0
- kerf/kill/main.py +275 -0
- kerf/lazy_cma.py +132 -0
- kerf/load/__init__.py +21 -0
- kerf/load/main.py +613 -0
- kerf/metadata.py +134 -0
- kerf/models.py +241 -0
- kerf/resources.py +369 -0
- kerf/runtime.py +527 -0
- kerf/show/__init__.py +21 -0
- kerf/show/main.py +616 -0
- kerf/unload/__init__.py +21 -0
- kerf/unload/main.py +310 -0
- kerf/update/__init__.py +15 -0
- kerf/update/main.py +450 -0
- kerf/utils.py +95 -0
- kerf/vmlinuz.py +213 -0
- kerf_multikernel-0.1.0.dist-info/LICENSE +201 -0
- kerf_multikernel-0.1.0.dist-info/METADATA +382 -0
- kerf_multikernel-0.1.0.dist-info/RECORD +49 -0
- kerf_multikernel-0.1.0.dist-info/WHEEL +4 -0
- kerf_multikernel-0.1.0.dist-info/entry_points.txt +3 -0
kerf/show/main.py
ADDED
|
@@ -0,0 +1,616 @@
|
|
|
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 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
|
+
Show kernel instance information and baseline hardware resources.
|
|
17
|
+
|
|
18
|
+
This command displays baseline hardware resources from /sys/fs/multikernel/device_tree,
|
|
19
|
+
instance information from /sys/fs/multikernel/instances/*, and kernel image information
|
|
20
|
+
from /proc/kimage in an organized format.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
import re
|
|
24
|
+
import sys
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
from typing import Dict, List, Optional
|
|
27
|
+
|
|
28
|
+
import click
|
|
29
|
+
import libfdt
|
|
30
|
+
|
|
31
|
+
from ..baseline import BaselineManager
|
|
32
|
+
from ..dtc.parser import DeviceTreeParser
|
|
33
|
+
from ..exceptions import KernelInterfaceError, ParseError
|
|
34
|
+
from ..metadata import load_instance_metadata
|
|
35
|
+
from ..models import GlobalDeviceTree
|
|
36
|
+
from ..resources import get_pool_allocated_bytes
|
|
37
|
+
from ..utils import get_instance_id_from_name, get_instance_status
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def read_proc_kimage() -> str:
|
|
41
|
+
"""
|
|
42
|
+
Read and return the contents of /proc/kimage.
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
Contents of /proc/kimage as a string, or empty string if file doesn't exist
|
|
46
|
+
"""
|
|
47
|
+
kimage_path = Path("/proc/kimage")
|
|
48
|
+
|
|
49
|
+
if not kimage_path.exists():
|
|
50
|
+
return ""
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
with open(kimage_path, "r", encoding="utf-8") as f:
|
|
54
|
+
return f.read()
|
|
55
|
+
except (OSError, IOError):
|
|
56
|
+
return ""
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def get_all_instance_names() -> List[str]:
|
|
60
|
+
"""
|
|
61
|
+
Get list of all instance names from /sys/fs/multikernel/instances/.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
List of instance names (directory names)
|
|
65
|
+
"""
|
|
66
|
+
instances_dir = Path("/sys/fs/multikernel/instances")
|
|
67
|
+
|
|
68
|
+
if not instances_dir.exists():
|
|
69
|
+
return []
|
|
70
|
+
|
|
71
|
+
instance_names = []
|
|
72
|
+
try:
|
|
73
|
+
for inst_dir in instances_dir.iterdir():
|
|
74
|
+
if inst_dir.is_dir() and not inst_dir.name.startswith("."):
|
|
75
|
+
instance_names.append(inst_dir.name)
|
|
76
|
+
except (OSError, IOError):
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
return sorted(instance_names)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def read_instance_info(name: str) -> Dict[str, Optional[str]]:
|
|
83
|
+
"""
|
|
84
|
+
Read instance information from /sys/fs/multikernel/instances/{name}/.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
name: Instance name
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
Dictionary with instance information (id, status, etc.)
|
|
91
|
+
"""
|
|
92
|
+
info = {
|
|
93
|
+
"name": name,
|
|
94
|
+
"id": None,
|
|
95
|
+
"status": None,
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
instance_dir = Path(f"/sys/fs/multikernel/instances/{name}")
|
|
99
|
+
|
|
100
|
+
if not instance_dir.exists():
|
|
101
|
+
return info
|
|
102
|
+
|
|
103
|
+
instance_id = get_instance_id_from_name(name)
|
|
104
|
+
if instance_id is not None:
|
|
105
|
+
info["id"] = str(instance_id)
|
|
106
|
+
|
|
107
|
+
status = get_instance_status(name)
|
|
108
|
+
if status is not None:
|
|
109
|
+
info["status"] = status
|
|
110
|
+
|
|
111
|
+
# Try to read any other files in the instance directory
|
|
112
|
+
try:
|
|
113
|
+
for file_path in instance_dir.iterdir():
|
|
114
|
+
if file_path.is_file() and file_path.name not in ["id", "status"]:
|
|
115
|
+
try:
|
|
116
|
+
if file_path.name == "device_tree":
|
|
117
|
+
try:
|
|
118
|
+
with open(file_path, "rb") as f:
|
|
119
|
+
file_data = f.read()
|
|
120
|
+
|
|
121
|
+
if file_data and len(file_data) > 0:
|
|
122
|
+
try:
|
|
123
|
+
parser = DeviceTreeParser()
|
|
124
|
+
fdt = libfdt.Fdt(file_data)
|
|
125
|
+
parser.fdt = fdt
|
|
126
|
+
|
|
127
|
+
dts_lines = parser._fdt_to_dts_recursive(0, 0) # pylint: disable=protected-access
|
|
128
|
+
dts_content = "\n".join(dts_lines)
|
|
129
|
+
|
|
130
|
+
if not dts_content.startswith("/dts-v1/"):
|
|
131
|
+
dts_content = "/dts-v1/;\n\n" + dts_content
|
|
132
|
+
info["device_tree_source"] = dts_content
|
|
133
|
+
except Exception:
|
|
134
|
+
pass
|
|
135
|
+
except (OSError, IOError):
|
|
136
|
+
pass
|
|
137
|
+
except (OSError, IOError):
|
|
138
|
+
pass
|
|
139
|
+
except (OSError, IOError):
|
|
140
|
+
pass
|
|
141
|
+
|
|
142
|
+
return info
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def parse_kimage_table(kimage_content: str) -> Dict[int, Dict[str, str]]:
|
|
146
|
+
"""
|
|
147
|
+
Parse /proc/kimage table format and extract information by MK_ID.
|
|
148
|
+
|
|
149
|
+
Expected format:
|
|
150
|
+
MK_ID Type Start Address Segments Mode Cmdline
|
|
151
|
+
----- ---------- -------------- -------- ---- -------
|
|
152
|
+
1 KEXEC_FILE 0x1000000 ... ... ...
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
kimage_content: Full content of /proc/kimage
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
Dictionary mapping MK_ID to a dict with parsed fields
|
|
159
|
+
"""
|
|
160
|
+
kimage_data = {}
|
|
161
|
+
|
|
162
|
+
if not kimage_content:
|
|
163
|
+
return kimage_data
|
|
164
|
+
|
|
165
|
+
lines = kimage_content.strip().split("\n")
|
|
166
|
+
|
|
167
|
+
header_line = None
|
|
168
|
+
separator_line = None
|
|
169
|
+
data_start_idx = 0
|
|
170
|
+
|
|
171
|
+
for i, line in enumerate(lines):
|
|
172
|
+
if "MK_ID" in line and "Type" in line:
|
|
173
|
+
header_line = line
|
|
174
|
+
if i + 1 < len(lines) and "-----" in lines[i + 1]:
|
|
175
|
+
separator_line = lines[i + 1]
|
|
176
|
+
data_start_idx = i + 2
|
|
177
|
+
else:
|
|
178
|
+
data_start_idx = i + 1
|
|
179
|
+
break
|
|
180
|
+
|
|
181
|
+
if header_line is None:
|
|
182
|
+
return kimage_data
|
|
183
|
+
|
|
184
|
+
boundaries = []
|
|
185
|
+
if separator_line:
|
|
186
|
+
# Each dash block represents a column
|
|
187
|
+
# Find all sequences of dashes - their start positions are column boundaries
|
|
188
|
+
dash_blocks = list(re.finditer(r"-+", separator_line))
|
|
189
|
+
if dash_blocks:
|
|
190
|
+
for match in dash_blocks:
|
|
191
|
+
start_pos = match.start()
|
|
192
|
+
if start_pos not in boundaries:
|
|
193
|
+
boundaries.append(start_pos)
|
|
194
|
+
if dash_blocks:
|
|
195
|
+
last_end = dash_blocks[-1].end()
|
|
196
|
+
if last_end not in boundaries:
|
|
197
|
+
boundaries.append(last_end)
|
|
198
|
+
boundaries.sort()
|
|
199
|
+
else:
|
|
200
|
+
# Fallback: use header line spacing
|
|
201
|
+
boundaries = [0, len(separator_line)]
|
|
202
|
+
else:
|
|
203
|
+
# Fallback: estimate from header spacing (look for multiple spaces)
|
|
204
|
+
boundaries = [0]
|
|
205
|
+
i = 0
|
|
206
|
+
while i < len(header_line):
|
|
207
|
+
if header_line[i] == " ":
|
|
208
|
+
space_count = 0
|
|
209
|
+
j = i
|
|
210
|
+
while j < len(header_line) and header_line[j] == " ":
|
|
211
|
+
space_count += 1
|
|
212
|
+
j += 1
|
|
213
|
+
if space_count >= 2: # Multiple spaces indicate column separator
|
|
214
|
+
if boundaries[-1] != i:
|
|
215
|
+
boundaries.append(i)
|
|
216
|
+
i = j
|
|
217
|
+
else:
|
|
218
|
+
i += 1
|
|
219
|
+
else:
|
|
220
|
+
i += 1
|
|
221
|
+
boundaries.append(len(header_line))
|
|
222
|
+
|
|
223
|
+
columns = []
|
|
224
|
+
for i in range(len(boundaries) - 1):
|
|
225
|
+
start = boundaries[i]
|
|
226
|
+
end = boundaries[i + 1] if i + 1 < len(boundaries) else len(header_line)
|
|
227
|
+
col_name = header_line[start:end].strip().lower().replace(" ", "_")
|
|
228
|
+
if col_name:
|
|
229
|
+
columns.append((col_name, start, end))
|
|
230
|
+
|
|
231
|
+
for line in lines[data_start_idx:]:
|
|
232
|
+
line = line.rstrip()
|
|
233
|
+
if not line.strip():
|
|
234
|
+
continue
|
|
235
|
+
|
|
236
|
+
mk_id = None
|
|
237
|
+
if columns:
|
|
238
|
+
first_col_start = columns[0][1]
|
|
239
|
+
first_col_end = columns[0][2] if len(columns) > 0 else len(line)
|
|
240
|
+
mk_id_str = line[first_col_start:first_col_end].strip()
|
|
241
|
+
try:
|
|
242
|
+
mk_id = int(mk_id_str)
|
|
243
|
+
except (ValueError, IndexError):
|
|
244
|
+
continue
|
|
245
|
+
|
|
246
|
+
if mk_id is None:
|
|
247
|
+
continue
|
|
248
|
+
|
|
249
|
+
# Extract all columns
|
|
250
|
+
row_data = {}
|
|
251
|
+
for i, (col_name, start, end) in enumerate(columns):
|
|
252
|
+
# For the last column, extend to end of line to capture full value
|
|
253
|
+
if i == len(columns) - 1:
|
|
254
|
+
col_value = line[start:].strip()
|
|
255
|
+
else:
|
|
256
|
+
col_value = line[start : min(end, len(line))].strip()
|
|
257
|
+
row_data[col_name] = col_value
|
|
258
|
+
|
|
259
|
+
kimage_data[mk_id] = row_data
|
|
260
|
+
|
|
261
|
+
return kimage_data
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def display_baseline_info(tree: GlobalDeviceTree, verbose: bool = False):
|
|
265
|
+
"""
|
|
266
|
+
Display formatted baseline hardware information.
|
|
267
|
+
|
|
268
|
+
Args:
|
|
269
|
+
tree: GlobalDeviceTree containing hardware resources
|
|
270
|
+
verbose: Whether to show verbose information
|
|
271
|
+
"""
|
|
272
|
+
click.echo(f"\n{'=' * 80}")
|
|
273
|
+
click.echo("Baseline Hardware Resources")
|
|
274
|
+
click.echo(f"{'=' * 80}")
|
|
275
|
+
|
|
276
|
+
hardware = tree.hardware
|
|
277
|
+
|
|
278
|
+
# CPU Information
|
|
279
|
+
click.echo("\n CPUs:")
|
|
280
|
+
click.echo(f" Total: {hardware.cpus.total}")
|
|
281
|
+
click.echo(
|
|
282
|
+
f" Host Reserved: {len(hardware.cpus.host_reserved)} cpus: {hardware.cpus.host_reserved}"
|
|
283
|
+
)
|
|
284
|
+
click.echo(
|
|
285
|
+
f" Available: {len(hardware.cpus.available)} cpus: {hardware.cpus.available}"
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
if verbose and hardware.cpus.topology:
|
|
289
|
+
click.echo("\n Topology:")
|
|
290
|
+
for cpu_id, topo in sorted(hardware.cpus.topology.items()):
|
|
291
|
+
click.echo(
|
|
292
|
+
f" CPU {cpu_id}: NUMA {topo.numa_node}, Socket {topo.socket_id}, Core {topo.core_id}, Thread {topo.thread_id}"
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
# Memory Information
|
|
296
|
+
click.echo("\n Memory:")
|
|
297
|
+
total_gb = hardware.memory.total_bytes / (1024**3)
|
|
298
|
+
reserved_gb = hardware.memory.host_reserved_bytes / (1024**3)
|
|
299
|
+
click.echo(f" Total: {total_gb:.2f} GB ({hardware.memory.total_bytes} bytes)")
|
|
300
|
+
click.echo(
|
|
301
|
+
f" Host Reserved: {reserved_gb:.2f} GB ({hardware.memory.host_reserved_bytes} bytes)"
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
# /proc/iomem is the source of truth for the lazy_cma pool and the
|
|
305
|
+
# instance allocations carved out of it; the baseline tree only
|
|
306
|
+
# snapshots the pool at init time.
|
|
307
|
+
usage = get_pool_allocated_bytes()
|
|
308
|
+
if usage is not None:
|
|
309
|
+
pool_base, pool_bytes, allocated_bytes = usage
|
|
310
|
+
else:
|
|
311
|
+
pool_base = hardware.memory.memory_pool_base
|
|
312
|
+
pool_bytes = hardware.memory.memory_pool_bytes
|
|
313
|
+
allocated_bytes = None
|
|
314
|
+
|
|
315
|
+
pool_gb = pool_bytes / (1024**3)
|
|
316
|
+
click.echo(f" Pool Base: 0x{pool_base:x}")
|
|
317
|
+
click.echo(f" Pool Size: {pool_gb:.2f} GB ({pool_bytes} bytes)")
|
|
318
|
+
click.echo(f" Pool End: 0x{pool_base + pool_bytes:x}")
|
|
319
|
+
if allocated_bytes is not None:
|
|
320
|
+
available_bytes = pool_bytes - allocated_bytes
|
|
321
|
+
allocated_gb = allocated_bytes / (1024**3)
|
|
322
|
+
available_gb = available_bytes / (1024**3)
|
|
323
|
+
click.echo(f" Pool Allocated: {allocated_gb:.2f} GB ({allocated_bytes} bytes)")
|
|
324
|
+
click.echo(f" Pool Available: {available_gb:.2f} GB ({available_bytes} bytes)")
|
|
325
|
+
|
|
326
|
+
# NUMA Topology
|
|
327
|
+
if hardware.topology and hardware.topology.numa_nodes:
|
|
328
|
+
click.echo("\n NUMA Topology:")
|
|
329
|
+
for node_id, numa_node in sorted(hardware.topology.numa_nodes.items()):
|
|
330
|
+
mem_gb = numa_node.memory_size / (1024**3)
|
|
331
|
+
click.echo(f" Node {node_id}:")
|
|
332
|
+
click.echo(f" Memory: {mem_gb:.2f} GB (base: 0x{numa_node.memory_base:x})")
|
|
333
|
+
click.echo(f" CPUs: {numa_node.cpus}")
|
|
334
|
+
click.echo(f" Type: {numa_node.memory_type}")
|
|
335
|
+
if verbose and numa_node.distance_matrix:
|
|
336
|
+
click.echo(" Distance:")
|
|
337
|
+
for target_node, distance in sorted(numa_node.distance_matrix.items()):
|
|
338
|
+
click.echo(f" -> Node {target_node}: {distance}")
|
|
339
|
+
|
|
340
|
+
# Devices
|
|
341
|
+
if hardware.devices:
|
|
342
|
+
click.echo("\n Devices:")
|
|
343
|
+
for device_name, device_info in sorted(hardware.devices.items()):
|
|
344
|
+
click.echo(f" {device_name}:")
|
|
345
|
+
click.echo(f" Compatible: {device_info.compatible}")
|
|
346
|
+
if device_info.device_type:
|
|
347
|
+
click.echo(f" Type: {device_info.device_type}")
|
|
348
|
+
if device_info.pci_id:
|
|
349
|
+
click.echo(f" PCI ID: {device_info.pci_id}")
|
|
350
|
+
if device_info.sriov_vfs:
|
|
351
|
+
click.echo(f" SR-IOV VFs: {device_info.sriov_vfs} total")
|
|
352
|
+
if device_info.available_vfs:
|
|
353
|
+
click.echo(f" Available VFs: {device_info.available_vfs}")
|
|
354
|
+
if device_info.namespaces:
|
|
355
|
+
click.echo(f" Namespaces: {device_info.namespaces} total")
|
|
356
|
+
if device_info.available_ns:
|
|
357
|
+
click.echo(f" Available NS: {device_info.available_ns}")
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def _display_kernel_metadata(metadata: Dict, verbose: bool = False):
|
|
361
|
+
"""Display the load metadata rows of the Kernel Image section."""
|
|
362
|
+
kernel = metadata.get("kernel") or {}
|
|
363
|
+
if kernel.get("path"):
|
|
364
|
+
click.echo(f" {'Image':15} {kernel['path']}")
|
|
365
|
+
image_format = kernel.get("format")
|
|
366
|
+
if image_format == "bzImage":
|
|
367
|
+
compression = kernel.get("compression")
|
|
368
|
+
detail = "vmlinux extracted at load"
|
|
369
|
+
if compression:
|
|
370
|
+
detail = f"{compression} compressed, {detail}"
|
|
371
|
+
click.echo(f" {'Format':15} bzImage ({detail})")
|
|
372
|
+
elif image_format == "vmlinux":
|
|
373
|
+
click.echo(f" {'Format':15} vmlinux (ELF)")
|
|
374
|
+
elif image_format:
|
|
375
|
+
click.echo(f" {'Format':15} {image_format}")
|
|
376
|
+
if kernel.get("version"):
|
|
377
|
+
click.echo(f" {'Version':15} {kernel['version']}")
|
|
378
|
+
initrd = metadata.get("initrd")
|
|
379
|
+
if isinstance(initrd, dict):
|
|
380
|
+
initrd_line = initrd.get("path", "")
|
|
381
|
+
if initrd.get("compression") == "cpio":
|
|
382
|
+
initrd_line += " (uncompressed cpio)"
|
|
383
|
+
elif initrd.get("compression"):
|
|
384
|
+
initrd_line += f" ({initrd['compression']} compressed)"
|
|
385
|
+
click.echo(f" {'Initrd':15} {initrd_line}")
|
|
386
|
+
elif initrd:
|
|
387
|
+
# Records from before initrd inspection hold just the path
|
|
388
|
+
click.echo(f" {'Initrd':15} {initrd}")
|
|
389
|
+
if metadata.get("loaded_at"):
|
|
390
|
+
click.echo(f" {'Loaded At':15} {metadata['loaded_at']}")
|
|
391
|
+
if verbose:
|
|
392
|
+
if kernel.get("sha256"):
|
|
393
|
+
click.echo(f" {'Sha256':15} {kernel['sha256']}")
|
|
394
|
+
if isinstance(initrd, dict) and initrd.get("sha256"):
|
|
395
|
+
click.echo(f" {'Initrd Sha256':15} {initrd['sha256']}")
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def _display_rootfs_metadata(rootfs: Dict, verbose: bool = False):
|
|
399
|
+
"""Display the Rootfs section recorded for --image / --rootfs-dir loads."""
|
|
400
|
+
click.echo("\n Rootfs:")
|
|
401
|
+
if rootfs.get("source") == "docker":
|
|
402
|
+
if rootfs.get("image"):
|
|
403
|
+
click.echo(f" {'Image':15} {rootfs['image']}")
|
|
404
|
+
if verbose and rootfs.get("image_id"):
|
|
405
|
+
click.echo(f" {'Image Id':15} {rootfs['image_id']}")
|
|
406
|
+
if verbose and rootfs.get("path"):
|
|
407
|
+
click.echo(f" {'Extracted To':15} {rootfs['path']}")
|
|
408
|
+
elif rootfs.get("path"):
|
|
409
|
+
click.echo(f" {'Source':15} {rootfs['path']} (directory)")
|
|
410
|
+
if rootfs.get("entrypoint"):
|
|
411
|
+
click.echo(f" {'Entrypoint':15} {rootfs['entrypoint']}")
|
|
412
|
+
daxfs = rootfs.get("daxfs") or {}
|
|
413
|
+
if daxfs.get("phys_addr") is not None:
|
|
414
|
+
click.echo(
|
|
415
|
+
f" {'Daxfs':15} phys=0x{daxfs['phys_addr']:x}, size={daxfs.get('size')}"
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def display_instance_info(
|
|
420
|
+
instance_info: Dict[str, Optional[str]],
|
|
421
|
+
kimage_data: Optional[Dict[str, str]] = None,
|
|
422
|
+
verbose: bool = False,
|
|
423
|
+
metadata: Optional[Dict] = None,
|
|
424
|
+
):
|
|
425
|
+
"""
|
|
426
|
+
Display formatted instance information.
|
|
427
|
+
|
|
428
|
+
Args:
|
|
429
|
+
instance_info: Instance information dictionary
|
|
430
|
+
kimage_data: Optional kimage data for this instance
|
|
431
|
+
verbose: Whether to show verbose information
|
|
432
|
+
metadata: Optional load metadata recorded by kerf load
|
|
433
|
+
"""
|
|
434
|
+
name = instance_info.get("name", "unknown")
|
|
435
|
+
instance_id = instance_info.get("id")
|
|
436
|
+
status = instance_info.get("status")
|
|
437
|
+
|
|
438
|
+
# Header
|
|
439
|
+
click.echo(f"\n{'=' * 80}")
|
|
440
|
+
click.echo(f"Instance: {name}")
|
|
441
|
+
click.echo(f"{'=' * 80}")
|
|
442
|
+
|
|
443
|
+
# Basic info
|
|
444
|
+
if instance_id:
|
|
445
|
+
click.echo(f" ID: {instance_id}")
|
|
446
|
+
if status:
|
|
447
|
+
click.echo(f" Status: {status}")
|
|
448
|
+
|
|
449
|
+
# Kernel image information from /proc/kimage plus load metadata
|
|
450
|
+
if kimage_data or metadata:
|
|
451
|
+
click.echo("\n Kernel Image:")
|
|
452
|
+
if kimage_data:
|
|
453
|
+
for key, value in kimage_data.items():
|
|
454
|
+
# Format key nicely
|
|
455
|
+
key_display = key.replace("_", " ").title()
|
|
456
|
+
click.echo(f" {key_display:15} {value}")
|
|
457
|
+
if metadata:
|
|
458
|
+
_display_kernel_metadata(metadata, verbose)
|
|
459
|
+
elif instance_id and verbose:
|
|
460
|
+
click.echo("\n Kernel Image: (not loaded)")
|
|
461
|
+
|
|
462
|
+
if metadata and metadata.get("rootfs"):
|
|
463
|
+
_display_rootfs_metadata(metadata["rootfs"], verbose)
|
|
464
|
+
|
|
465
|
+
# Device tree source
|
|
466
|
+
if "device_tree_source" in instance_info and instance_info["device_tree_source"]:
|
|
467
|
+
dts = instance_info["device_tree_source"]
|
|
468
|
+
try:
|
|
469
|
+
if isinstance(dts, bytes):
|
|
470
|
+
dts = dts.decode("utf-8", errors="replace")
|
|
471
|
+
elif not isinstance(dts, str):
|
|
472
|
+
dts = str(dts)
|
|
473
|
+
|
|
474
|
+
if dts.strip().startswith("/dts-v1/") or dts.strip().startswith("/"):
|
|
475
|
+
click.echo("\n Device Tree:")
|
|
476
|
+
dts_lines = dts.split("\n")
|
|
477
|
+
for line in dts_lines:
|
|
478
|
+
if line.strip():
|
|
479
|
+
click.echo(f" {line}")
|
|
480
|
+
else:
|
|
481
|
+
pass
|
|
482
|
+
except (UnicodeDecodeError, AttributeError):
|
|
483
|
+
pass
|
|
484
|
+
|
|
485
|
+
other_fields = {
|
|
486
|
+
k: v
|
|
487
|
+
for k, v in instance_info.items()
|
|
488
|
+
if k not in ["name", "id", "status", "device_tree_source"]
|
|
489
|
+
and v
|
|
490
|
+
and isinstance(v, str)
|
|
491
|
+
and len(v) < 1000 # Skip very long or binary data
|
|
492
|
+
and not any(ord(c) < 32 and c not in "\t\n\r" for c in v[:100])
|
|
493
|
+
} # Skip binary
|
|
494
|
+
if other_fields:
|
|
495
|
+
click.echo("\n Additional Info:")
|
|
496
|
+
for key, value in other_fields.items():
|
|
497
|
+
key_display = key.replace("_", " ").title()
|
|
498
|
+
# Truncate long values
|
|
499
|
+
if len(str(value)) > 60:
|
|
500
|
+
value_str = str(value)[:57] + "..."
|
|
501
|
+
else:
|
|
502
|
+
value_str = str(value)
|
|
503
|
+
click.echo(f" {key_display:15} {value_str}")
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
@click.command(name="show")
|
|
507
|
+
@click.argument("name", required=False)
|
|
508
|
+
@click.option("--verbose", "-v", is_flag=True, help="Verbose output")
|
|
509
|
+
def show(name: Optional[str], verbose: bool):
|
|
510
|
+
"""
|
|
511
|
+
Show kernel instance information and baseline hardware resources.
|
|
512
|
+
|
|
513
|
+
This command combines information from /proc/kimage with instance information
|
|
514
|
+
from /sys/fs/multikernel/instances/* and baseline hardware resources from
|
|
515
|
+
/sys/fs/multikernel/device_tree in an organized format.
|
|
516
|
+
|
|
517
|
+
Without an instance name, it shows baseline and all instances.
|
|
518
|
+
With a specific instance name, it shows only that instance (no baseline).
|
|
519
|
+
|
|
520
|
+
Examples:
|
|
521
|
+
|
|
522
|
+
kerf show
|
|
523
|
+
kerf show web-server
|
|
524
|
+
kerf show --verbose
|
|
525
|
+
"""
|
|
526
|
+
try:
|
|
527
|
+
# Read /proc/kimage content
|
|
528
|
+
kimage_content = read_proc_kimage()
|
|
529
|
+
kimage_table = parse_kimage_table(kimage_content)
|
|
530
|
+
|
|
531
|
+
if name:
|
|
532
|
+
# Show specific instance
|
|
533
|
+
instance_id = get_instance_id_from_name(name)
|
|
534
|
+
|
|
535
|
+
if instance_id is None:
|
|
536
|
+
click.echo(f"Error: Instance '{name}' not found", err=True)
|
|
537
|
+
click.echo("Check available instances in /sys/fs/multikernel/instances/", err=True)
|
|
538
|
+
sys.exit(1)
|
|
539
|
+
|
|
540
|
+
# Get instance information
|
|
541
|
+
instance_info = read_instance_info(name)
|
|
542
|
+
|
|
543
|
+
# Get kimage data for this instance
|
|
544
|
+
kimage_data = kimage_table.get(instance_id)
|
|
545
|
+
|
|
546
|
+
# Display information
|
|
547
|
+
display_instance_info(
|
|
548
|
+
instance_info, kimage_data, verbose,
|
|
549
|
+
metadata=load_instance_metadata(name),
|
|
550
|
+
)
|
|
551
|
+
else:
|
|
552
|
+
# Show all instances and baseline
|
|
553
|
+
instance_names = get_all_instance_names()
|
|
554
|
+
|
|
555
|
+
# Try to read and display baseline
|
|
556
|
+
baseline_manager = BaselineManager()
|
|
557
|
+
try:
|
|
558
|
+
tree = baseline_manager.read_baseline()
|
|
559
|
+
display_baseline_info(tree, verbose)
|
|
560
|
+
except (KernelInterfaceError, ParseError) as e:
|
|
561
|
+
if verbose:
|
|
562
|
+
click.echo(f"\nWarning: Could not read baseline: {e}", err=True)
|
|
563
|
+
|
|
564
|
+
if not instance_names:
|
|
565
|
+
click.echo("\n" + "=" * 80)
|
|
566
|
+
click.echo("No instances found in /sys/fs/multikernel/instances/")
|
|
567
|
+
if kimage_content:
|
|
568
|
+
click.echo("\n/proc/kimage:")
|
|
569
|
+
click.echo("=" * 80)
|
|
570
|
+
click.echo(kimage_content)
|
|
571
|
+
sys.exit(0)
|
|
572
|
+
|
|
573
|
+
# Display summary header
|
|
574
|
+
click.echo(f"\n{'=' * 80}")
|
|
575
|
+
click.echo("Multikernel Instances")
|
|
576
|
+
click.echo("=" * 80)
|
|
577
|
+
click.echo(f"Total instances: {len(instance_names)}")
|
|
578
|
+
if kimage_table:
|
|
579
|
+
click.echo(f"Loaded kernels: {len(kimage_table)}")
|
|
580
|
+
click.echo()
|
|
581
|
+
|
|
582
|
+
# Display each instance
|
|
583
|
+
for inst_name in instance_names:
|
|
584
|
+
instance_info = read_instance_info(inst_name)
|
|
585
|
+
instance_id_str = instance_info.get("id")
|
|
586
|
+
|
|
587
|
+
# Get kimage data for this instance
|
|
588
|
+
kimage_data = None
|
|
589
|
+
if instance_id_str:
|
|
590
|
+
try:
|
|
591
|
+
instance_id = int(instance_id_str)
|
|
592
|
+
kimage_data = kimage_table.get(instance_id)
|
|
593
|
+
except (ValueError, TypeError):
|
|
594
|
+
pass
|
|
595
|
+
|
|
596
|
+
display_instance_info(
|
|
597
|
+
instance_info, kimage_data, verbose,
|
|
598
|
+
metadata=load_instance_metadata(inst_name),
|
|
599
|
+
)
|
|
600
|
+
|
|
601
|
+
click.echo()
|
|
602
|
+
|
|
603
|
+
except KeyboardInterrupt:
|
|
604
|
+
click.echo("\nOperation cancelled", err=True)
|
|
605
|
+
sys.exit(130)
|
|
606
|
+
except Exception as e:
|
|
607
|
+
click.echo(f"Unexpected error: {e}", err=True)
|
|
608
|
+
if verbose:
|
|
609
|
+
import traceback
|
|
610
|
+
|
|
611
|
+
traceback.print_exc()
|
|
612
|
+
sys.exit(1)
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
if __name__ == "__main__":
|
|
616
|
+
show()
|
kerf/unload/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
Kernel unloading subcommand implementation.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from .main import unload
|
|
20
|
+
|
|
21
|
+
__all__ = ["unload"]
|