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/data/__init__.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Copyright 2026 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
|
+
"""Package data for kerf, including the pre-built init binary."""
|
|
16
|
+
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
__all__ = ["get_init_binary_path"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_init_binary_path() -> Path:
|
|
23
|
+
"""Return the path to the pre-built kerf-init binary."""
|
|
24
|
+
return Path(__file__).parent / "kerf-init"
|
kerf/data/kerf-init
ADDED
|
Binary file
|
kerf/daxfs/__init__.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
"""DAXFS filesystem image creation for multikernel."""
|
|
16
|
+
|
|
17
|
+
from .mkdaxfs import (
|
|
18
|
+
create_daxfs_image,
|
|
19
|
+
unmount_daxfs,
|
|
20
|
+
DaxfsError,
|
|
21
|
+
DaxfsImage,
|
|
22
|
+
inject_kerf_init,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"create_daxfs_image",
|
|
27
|
+
"unmount_daxfs",
|
|
28
|
+
"DaxfsError",
|
|
29
|
+
"DaxfsImage",
|
|
30
|
+
"inject_kerf_init",
|
|
31
|
+
]
|
kerf/daxfs/mkdaxfs.py
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
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
|
+
DAXFS filesystem image creation (on-disk format v8).
|
|
17
|
+
|
|
18
|
+
Creates a daxfs image from a directory and writes it to DMA heap memory.
|
|
19
|
+
The image layout mirrors daxfs tools/mkdaxfs.c:
|
|
20
|
+
|
|
21
|
+
[ Superblock (1 block) | Base image | Overlay region ]
|
|
22
|
+
|
|
23
|
+
The base image holds the read-only snapshot (inode table, flat dirent
|
|
24
|
+
arrays, file data); the overlay region makes the mount writable. The
|
|
25
|
+
daxfs filesystem holds a reference to the dma-buf, so the memory
|
|
26
|
+
persists even after the dmabuf fd is closed.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
import ctypes
|
|
30
|
+
import fcntl
|
|
31
|
+
import mmap
|
|
32
|
+
import os
|
|
33
|
+
import shutil
|
|
34
|
+
import stat as stat_mod
|
|
35
|
+
import struct
|
|
36
|
+
from dataclasses import dataclass
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
from typing import Dict, List, Optional, Tuple
|
|
39
|
+
|
|
40
|
+
from kerf.data import get_init_binary_path
|
|
41
|
+
|
|
42
|
+
DAXFS_SUPER_MAGIC = 0x64617835 # "dax5"
|
|
43
|
+
DAXFS_VERSION = 8
|
|
44
|
+
DAXFS_MIN_BLOCK_SIZE = 4096
|
|
45
|
+
DAXFS_INODE_SIZE = 64
|
|
46
|
+
DAXFS_NAME_MAX = 255
|
|
47
|
+
# The inode size field counts DAXFS_DIRENT_SIZE per entry, but the kernel
|
|
48
|
+
# indexes the dirent array as struct daxfs_dirent[], which the compiler
|
|
49
|
+
# pads to 4-byte alignment. Entries must be laid out at the padded stride.
|
|
50
|
+
DAXFS_DIRENT_SIZE = 16 + DAXFS_NAME_MAX
|
|
51
|
+
DAXFS_DIRENT_STRIDE = (DAXFS_DIRENT_SIZE + 3) & ~3
|
|
52
|
+
DAXFS_ROOT_INO = 1
|
|
53
|
+
|
|
54
|
+
DAXFS_OVERLAY_MAGIC = 0x6F766C79 # "ovly"
|
|
55
|
+
DAXFS_OVERLAY_VERSION = 2
|
|
56
|
+
DAXFS_OVL_FREE_END = 0xFFFFFFFFFFFFFFFF
|
|
57
|
+
DAXFS_OVERLAY_BUCKET_SIZE = 16
|
|
58
|
+
DAXFS_DEFAULT_OVERLAY_POOL = 64 * 1024 * 1024
|
|
59
|
+
DAXFS_DEFAULT_BUCKET_COUNT = 65536
|
|
60
|
+
|
|
61
|
+
# Block size = native page size, validated by the kernel at mount time
|
|
62
|
+
DAXFS_BLOCK_SIZE = max(mmap.PAGESIZE, DAXFS_MIN_BLOCK_SIZE)
|
|
63
|
+
|
|
64
|
+
SUPERBLOCK_FORMAT = '<IIIIQQQQIIQQQIIQQII'
|
|
65
|
+
INODE_FORMAT = '<IIIIQQI28x'
|
|
66
|
+
DIRENT_HEADER_FORMAT = '<IIH6x'
|
|
67
|
+
OVERLAY_HEADER_FORMAT = '<IIQQQQQQQQ'
|
|
68
|
+
|
|
69
|
+
DMA_HEAP_IOC_MAGIC = ord('H')
|
|
70
|
+
DMA_HEAP_IOCTL_ALLOC = 0xC0184800
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class DaxfsError(Exception):
|
|
74
|
+
"""Exception raised for daxfs errors."""
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass
|
|
78
|
+
class DaxfsImage:
|
|
79
|
+
"""Represents a created daxfs image."""
|
|
80
|
+
phys_addr: int
|
|
81
|
+
size: int
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class FileEntry:
|
|
86
|
+
"""Represents a file entry in the daxfs image."""
|
|
87
|
+
path: str
|
|
88
|
+
name: str
|
|
89
|
+
stat: os.stat_result
|
|
90
|
+
ino: int = 0
|
|
91
|
+
parent_ino: int = 0
|
|
92
|
+
data_offset: int = 0
|
|
93
|
+
child_count: int = 0
|
|
94
|
+
is_hardlink: bool = False
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class DaxfsBuilder:
|
|
98
|
+
"""Builds a daxfs v8 filesystem image: base snapshot plus write overlay."""
|
|
99
|
+
|
|
100
|
+
def __init__(self, src_dir: str,
|
|
101
|
+
overlay_pool_size: int = DAXFS_DEFAULT_OVERLAY_POOL):
|
|
102
|
+
self.src_dir = Path(src_dir)
|
|
103
|
+
self.files: List[FileEntry] = []
|
|
104
|
+
self.files_by_path: Dict[str, FileEntry] = {}
|
|
105
|
+
# First entry seen for each hardlinked (st_dev, st_ino)
|
|
106
|
+
self.hardlinks: Dict[Tuple[int, int], FileEntry] = {}
|
|
107
|
+
self.next_ino = 1
|
|
108
|
+
self.base_size = 0
|
|
109
|
+
self.overlay_pool_size = overlay_pool_size
|
|
110
|
+
self.overlay_buckets = self._auto_bucket_count(overlay_pool_size)
|
|
111
|
+
|
|
112
|
+
@staticmethod
|
|
113
|
+
def _align(value: int, alignment: int) -> int:
|
|
114
|
+
return (value + alignment - 1) & ~(alignment - 1)
|
|
115
|
+
|
|
116
|
+
@staticmethod
|
|
117
|
+
def _auto_bucket_count(pool_size: int) -> int:
|
|
118
|
+
"""Size the overlay bucket count from the pool.
|
|
119
|
+
|
|
120
|
+
Target ~2x pool pages so the open-addressed table stays near 0.5
|
|
121
|
+
load, floored at the mkdaxfs default (matches tools/mkdaxfs.c).
|
|
122
|
+
"""
|
|
123
|
+
pool_pages = (pool_size + DAXFS_BLOCK_SIZE - 1) // DAXFS_BLOCK_SIZE
|
|
124
|
+
buckets = 1
|
|
125
|
+
while buckets < pool_pages * 2 and buckets < (1 << 31):
|
|
126
|
+
buckets <<= 1
|
|
127
|
+
return max(buckets, DAXFS_DEFAULT_BUCKET_COUNT)
|
|
128
|
+
|
|
129
|
+
def _add_file(self, relpath: str, stat_result: os.stat_result) -> FileEntry:
|
|
130
|
+
entry = FileEntry(
|
|
131
|
+
path=relpath,
|
|
132
|
+
name=os.path.basename(relpath)[:DAXFS_NAME_MAX] if relpath else "",
|
|
133
|
+
stat=stat_result,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# Hardlinked regular files share one inode and one data extent
|
|
137
|
+
if stat_mod.S_ISREG(stat_result.st_mode) and stat_result.st_nlink > 1:
|
|
138
|
+
key = (stat_result.st_dev, stat_result.st_ino)
|
|
139
|
+
owner = self.hardlinks.get(key)
|
|
140
|
+
if owner is not None:
|
|
141
|
+
entry.ino = owner.ino
|
|
142
|
+
entry.is_hardlink = True
|
|
143
|
+
else:
|
|
144
|
+
entry.ino = self.next_ino
|
|
145
|
+
self.next_ino += 1
|
|
146
|
+
self.hardlinks[key] = entry
|
|
147
|
+
else:
|
|
148
|
+
entry.ino = self.next_ino
|
|
149
|
+
self.next_ino += 1
|
|
150
|
+
|
|
151
|
+
self.files.append(entry)
|
|
152
|
+
self.files_by_path[relpath] = entry
|
|
153
|
+
return entry
|
|
154
|
+
|
|
155
|
+
def _scan_directory_recursive(self, relpath: str) -> None:
|
|
156
|
+
fullpath = self.src_dir / relpath if relpath else self.src_dir
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
entries = list(fullpath.iterdir())
|
|
160
|
+
except PermissionError:
|
|
161
|
+
return
|
|
162
|
+
|
|
163
|
+
for entry in sorted(entries, key=lambda e: e.name):
|
|
164
|
+
newrel = f"{relpath}/{entry.name}" if relpath else entry.name
|
|
165
|
+
|
|
166
|
+
try:
|
|
167
|
+
file_stat = entry.lstat()
|
|
168
|
+
except (PermissionError, FileNotFoundError):
|
|
169
|
+
continue
|
|
170
|
+
|
|
171
|
+
self._add_file(newrel, file_stat)
|
|
172
|
+
|
|
173
|
+
if entry.is_dir() and not entry.is_symlink():
|
|
174
|
+
self._scan_directory_recursive(newrel)
|
|
175
|
+
|
|
176
|
+
def scan(self) -> None:
|
|
177
|
+
"""Scan the source directory."""
|
|
178
|
+
root_stat = self.src_dir.lstat()
|
|
179
|
+
self._add_file("", root_stat)
|
|
180
|
+
self._scan_directory_recursive("")
|
|
181
|
+
|
|
182
|
+
def build_tree(self) -> None:
|
|
183
|
+
"""Assign parent inodes and count directory children."""
|
|
184
|
+
for e in self.files:
|
|
185
|
+
if not e.path:
|
|
186
|
+
e.parent_ino = 0
|
|
187
|
+
continue
|
|
188
|
+
|
|
189
|
+
parent = self.files_by_path.get(os.path.dirname(e.path))
|
|
190
|
+
if parent:
|
|
191
|
+
e.parent_ino = parent.ino
|
|
192
|
+
parent.child_count += 1
|
|
193
|
+
|
|
194
|
+
def calculate_offsets(self) -> None:
|
|
195
|
+
"""Assign base-relative data offsets and compute the base size."""
|
|
196
|
+
data_offset = self._align(len(self.files) * DAXFS_INODE_SIZE,
|
|
197
|
+
DAXFS_BLOCK_SIZE)
|
|
198
|
+
|
|
199
|
+
for e in self.files:
|
|
200
|
+
mode = e.stat.st_mode
|
|
201
|
+
if stat_mod.S_ISREG(mode):
|
|
202
|
+
if e.is_hardlink:
|
|
203
|
+
owner = self.hardlinks[(e.stat.st_dev, e.stat.st_ino)]
|
|
204
|
+
e.data_offset = owner.data_offset
|
|
205
|
+
else:
|
|
206
|
+
e.data_offset = data_offset
|
|
207
|
+
data_offset += self._align(e.stat.st_size, DAXFS_BLOCK_SIZE)
|
|
208
|
+
elif stat_mod.S_ISLNK(mode):
|
|
209
|
+
e.data_offset = data_offset
|
|
210
|
+
data_offset += self._align(e.stat.st_size + 1, DAXFS_BLOCK_SIZE)
|
|
211
|
+
elif stat_mod.S_ISDIR(mode) and e.child_count > 0:
|
|
212
|
+
e.data_offset = data_offset
|
|
213
|
+
data_offset += self._align(e.child_count * DAXFS_DIRENT_STRIDE,
|
|
214
|
+
DAXFS_BLOCK_SIZE)
|
|
215
|
+
|
|
216
|
+
self.base_size = data_offset
|
|
217
|
+
|
|
218
|
+
def _overlay_region_size(self) -> int:
|
|
219
|
+
bucket_array = self._align(
|
|
220
|
+
self.overlay_buckets * DAXFS_OVERLAY_BUCKET_SIZE, DAXFS_BLOCK_SIZE)
|
|
221
|
+
return DAXFS_BLOCK_SIZE + bucket_array + self.overlay_pool_size
|
|
222
|
+
|
|
223
|
+
def _overlay_offset(self) -> int:
|
|
224
|
+
return self._align(DAXFS_BLOCK_SIZE + self.base_size, DAXFS_BLOCK_SIZE)
|
|
225
|
+
|
|
226
|
+
def calculate_total_size(self) -> int:
|
|
227
|
+
"""Calculate total image size: superblock + base + overlay."""
|
|
228
|
+
return self._overlay_offset() + self._overlay_region_size()
|
|
229
|
+
|
|
230
|
+
def write_image(self, mem: mmap.mmap, mem_size: int) -> None:
|
|
231
|
+
"""Write the daxfs image to memory."""
|
|
232
|
+
total_size = self.calculate_total_size()
|
|
233
|
+
if total_size > mem_size:
|
|
234
|
+
raise DaxfsError(
|
|
235
|
+
f"Image size {total_size} exceeds allocated size {mem_size}"
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
zero_chunk = b'\x00' * (4 * 1024 * 1024)
|
|
239
|
+
mem.seek(0)
|
|
240
|
+
remaining = mem_size
|
|
241
|
+
while remaining > 0:
|
|
242
|
+
mem.write(zero_chunk[:min(remaining, len(zero_chunk))])
|
|
243
|
+
remaining -= min(remaining, len(zero_chunk))
|
|
244
|
+
|
|
245
|
+
base_offset = DAXFS_BLOCK_SIZE
|
|
246
|
+
base_data_offset = self._align(len(self.files) * DAXFS_INODE_SIZE,
|
|
247
|
+
DAXFS_BLOCK_SIZE)
|
|
248
|
+
overlay_offset = self._overlay_offset()
|
|
249
|
+
bucket_array = self._align(
|
|
250
|
+
self.overlay_buckets * DAXFS_OVERLAY_BUCKET_SIZE, DAXFS_BLOCK_SIZE)
|
|
251
|
+
|
|
252
|
+
mem.seek(0)
|
|
253
|
+
mem.write(struct.pack(
|
|
254
|
+
SUPERBLOCK_FORMAT,
|
|
255
|
+
DAXFS_SUPER_MAGIC,
|
|
256
|
+
DAXFS_VERSION,
|
|
257
|
+
DAXFS_BLOCK_SIZE,
|
|
258
|
+
0, # reserved0
|
|
259
|
+
total_size,
|
|
260
|
+
base_offset,
|
|
261
|
+
self.base_size,
|
|
262
|
+
0, # inode_offset (relative to base)
|
|
263
|
+
len(self.files),
|
|
264
|
+
DAXFS_ROOT_INO,
|
|
265
|
+
base_data_offset, # data_offset (relative to base)
|
|
266
|
+
overlay_offset,
|
|
267
|
+
self._overlay_region_size(),
|
|
268
|
+
self.overlay_buckets,
|
|
269
|
+
self.overlay_buckets.bit_length() - 1,
|
|
270
|
+
0, 0, 0, 0, # no pcache
|
|
271
|
+
))
|
|
272
|
+
|
|
273
|
+
self._write_base(mem, base_offset)
|
|
274
|
+
self._write_overlay(mem, overlay_offset, bucket_array)
|
|
275
|
+
|
|
276
|
+
def _write_base(self, mem: mmap.mmap, base_offset: int) -> None:
|
|
277
|
+
children: Dict[int, List[FileEntry]] = {}
|
|
278
|
+
for e in self.files:
|
|
279
|
+
if e.path:
|
|
280
|
+
children.setdefault(e.parent_ino, []).append(e)
|
|
281
|
+
|
|
282
|
+
for e in self.files:
|
|
283
|
+
mode = e.stat.st_mode
|
|
284
|
+
|
|
285
|
+
if stat_mod.S_ISDIR(mode):
|
|
286
|
+
size = e.child_count * DAXFS_DIRENT_SIZE
|
|
287
|
+
elif stat_mod.S_ISREG(mode) or stat_mod.S_ISLNK(mode):
|
|
288
|
+
size = e.stat.st_size
|
|
289
|
+
else:
|
|
290
|
+
size = 0
|
|
291
|
+
|
|
292
|
+
mem.seek(base_offset + (e.ino - 1) * DAXFS_INODE_SIZE)
|
|
293
|
+
mem.write(struct.pack(
|
|
294
|
+
INODE_FORMAT,
|
|
295
|
+
e.ino,
|
|
296
|
+
mode,
|
|
297
|
+
e.stat.st_uid,
|
|
298
|
+
e.stat.st_gid,
|
|
299
|
+
size,
|
|
300
|
+
e.data_offset,
|
|
301
|
+
e.stat.st_nlink,
|
|
302
|
+
))
|
|
303
|
+
|
|
304
|
+
if stat_mod.S_ISREG(mode) and not e.is_hardlink:
|
|
305
|
+
self._write_file_data(mem, base_offset + e.data_offset, e)
|
|
306
|
+
elif stat_mod.S_ISLNK(mode):
|
|
307
|
+
try:
|
|
308
|
+
target = os.readlink(self.src_dir / e.path)
|
|
309
|
+
mem.seek(base_offset + e.data_offset)
|
|
310
|
+
mem.write(target.encode('utf-8') + b'\x00')
|
|
311
|
+
except (PermissionError, FileNotFoundError):
|
|
312
|
+
pass
|
|
313
|
+
elif stat_mod.S_ISDIR(mode) and e.child_count > 0:
|
|
314
|
+
for i, child in enumerate(children.get(e.ino, [])):
|
|
315
|
+
name = child.name.encode('utf-8')[:DAXFS_NAME_MAX]
|
|
316
|
+
mem.seek(base_offset + e.data_offset
|
|
317
|
+
+ i * DAXFS_DIRENT_STRIDE)
|
|
318
|
+
mem.write(struct.pack(DIRENT_HEADER_FORMAT,
|
|
319
|
+
child.ino,
|
|
320
|
+
child.stat.st_mode,
|
|
321
|
+
len(name)))
|
|
322
|
+
mem.write(name)
|
|
323
|
+
|
|
324
|
+
def _write_file_data(self, mem: mmap.mmap, offset: int,
|
|
325
|
+
entry: FileEntry) -> None:
|
|
326
|
+
try:
|
|
327
|
+
with open(self.src_dir / entry.path, 'rb') as f:
|
|
328
|
+
mem.seek(offset)
|
|
329
|
+
shutil.copyfileobj(f, mem)
|
|
330
|
+
except (PermissionError, FileNotFoundError, IsADirectoryError):
|
|
331
|
+
pass
|
|
332
|
+
|
|
333
|
+
def _write_overlay(self, mem: mmap.mmap, overlay_offset: int,
|
|
334
|
+
bucket_array: int) -> None:
|
|
335
|
+
mem.seek(overlay_offset)
|
|
336
|
+
mem.write(struct.pack(
|
|
337
|
+
OVERLAY_HEADER_FORMAT,
|
|
338
|
+
DAXFS_OVERLAY_MAGIC,
|
|
339
|
+
DAXFS_OVERLAY_VERSION,
|
|
340
|
+
DAXFS_BLOCK_SIZE, # bucket_offset
|
|
341
|
+
DAXFS_BLOCK_SIZE + bucket_array, # pool_offset
|
|
342
|
+
self.overlay_pool_size,
|
|
343
|
+
0, # pool_alloc
|
|
344
|
+
self.next_ino,
|
|
345
|
+
DAXFS_OVL_FREE_END, # free_inode
|
|
346
|
+
DAXFS_OVL_FREE_END, # free_data
|
|
347
|
+
DAXFS_OVL_FREE_END, # free_dirent
|
|
348
|
+
))
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _get_libc():
|
|
352
|
+
"""Get libc with errno support."""
|
|
353
|
+
return ctypes.CDLL(None, use_errno=True)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _syscall(libc, nr, *args):
|
|
357
|
+
"""Invoke a syscall and raise OSError on failure."""
|
|
358
|
+
fn = libc.syscall
|
|
359
|
+
fn.restype = ctypes.c_long
|
|
360
|
+
ret = fn(ctypes.c_long(nr), *[ctypes.c_long(a) if isinstance(a, int) else a for a in args])
|
|
361
|
+
if ret < 0:
|
|
362
|
+
errno_val = ctypes.get_errno()
|
|
363
|
+
raise OSError(errno_val, os.strerror(errno_val))
|
|
364
|
+
return ret
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _daxfs_iomem_regions() -> set:
|
|
368
|
+
"""Return the set of (phys_addr, size) daxfs regions in /proc/iomem."""
|
|
369
|
+
regions = set()
|
|
370
|
+
try:
|
|
371
|
+
with open('/proc/iomem', 'r', encoding='utf-8') as f:
|
|
372
|
+
for line in f:
|
|
373
|
+
parts = line.strip().split(' : ')
|
|
374
|
+
if len(parts) == 2 and parts[1] == 'daxfs':
|
|
375
|
+
start_str, end_str = parts[0].strip().split('-')
|
|
376
|
+
start = int(start_str, 16)
|
|
377
|
+
end = int(end_str, 16)
|
|
378
|
+
regions.add((start, end - start + 1))
|
|
379
|
+
except (FileNotFoundError, PermissionError, ValueError):
|
|
380
|
+
pass
|
|
381
|
+
return regions
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _umount_all(path) -> None:
|
|
385
|
+
"""Unmount any mounts stacked on path (no-op if not mounted)."""
|
|
386
|
+
libc = _get_libc()
|
|
387
|
+
path_bytes = str(path).encode('utf-8')
|
|
388
|
+
while os.path.ismount(path):
|
|
389
|
+
if libc.umount2(ctypes.c_char_p(path_bytes), 0) < 0:
|
|
390
|
+
errno_val = ctypes.get_errno()
|
|
391
|
+
raise DaxfsError(
|
|
392
|
+
f"Failed to unmount {path}: {os.strerror(errno_val)}"
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def unmount_daxfs(instance_name: str) -> None:
|
|
397
|
+
"""Unmount an instance's daxfs image and remove its mountpoint.
|
|
398
|
+
|
|
399
|
+
Unmounting drops the filesystem's dma-buf reference, freeing the
|
|
400
|
+
image memory back to the multikernel pool.
|
|
401
|
+
"""
|
|
402
|
+
mnt_dir = Path(KERF_DAXFS_MNT_DIR) / instance_name
|
|
403
|
+
_umount_all(mnt_dir)
|
|
404
|
+
try:
|
|
405
|
+
mnt_dir.rmdir()
|
|
406
|
+
except OSError:
|
|
407
|
+
pass
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def _allocate_dma_heap(heap_path: str, size: int) -> tuple[int, mmap.mmap]:
|
|
411
|
+
"""
|
|
412
|
+
Allocate memory from DMA heap.
|
|
413
|
+
Returns (dmabuf_fd, mmap) tuple.
|
|
414
|
+
"""
|
|
415
|
+
heap_fd = os.open(heap_path, os.O_RDWR)
|
|
416
|
+
try:
|
|
417
|
+
alloc_data = struct.pack('<QIIQ', size, 0, os.O_RDWR | os.O_CLOEXEC, 0)
|
|
418
|
+
result = fcntl.ioctl(heap_fd, DMA_HEAP_IOCTL_ALLOC, alloc_data)
|
|
419
|
+
_, dmabuf_fd, _, _ = struct.unpack('<QIIQ', result)
|
|
420
|
+
finally:
|
|
421
|
+
os.close(heap_fd)
|
|
422
|
+
|
|
423
|
+
mem = mmap.mmap(dmabuf_fd, size, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE)
|
|
424
|
+
return dmabuf_fd, mem
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
KERF_DAXFS_MNT_DIR = "/var/lib/kerf/daxfs"
|
|
428
|
+
|
|
429
|
+
# Syscall numbers (same on all architectures, added in kernel 5.2+)
|
|
430
|
+
SYS_MOVE_MOUNT = 429
|
|
431
|
+
SYS_FSOPEN = 430
|
|
432
|
+
SYS_FSCONFIG = 431
|
|
433
|
+
SYS_FSMOUNT = 432
|
|
434
|
+
|
|
435
|
+
# fsconfig command constants
|
|
436
|
+
FSCONFIG_SET_STRING = 1
|
|
437
|
+
FSCONFIG_SET_FD = 5
|
|
438
|
+
FSCONFIG_CMD_CREATE = 6
|
|
439
|
+
|
|
440
|
+
# move_mount flags
|
|
441
|
+
MOVE_MOUNT_F_EMPTY_PATH = 0x00000004
|
|
442
|
+
|
|
443
|
+
AT_FDCWD = -100
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def inject_kerf_init(rootfs_path: str) -> None:
|
|
447
|
+
"""Inject /init binary into rootfs.
|
|
448
|
+
|
|
449
|
+
The entrypoint is passed via kernel cmdline as kerf.entrypoint=<path>.
|
|
450
|
+
"""
|
|
451
|
+
rootfs = Path(rootfs_path)
|
|
452
|
+
|
|
453
|
+
# Copy the pre-built init binary
|
|
454
|
+
init_binary = get_init_binary_path()
|
|
455
|
+
if not init_binary.exists():
|
|
456
|
+
raise DaxfsError(
|
|
457
|
+
f"Init binary not found at {init_binary}. "
|
|
458
|
+
"Run 'make' to build it first."
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
init_path = rootfs / "init"
|
|
462
|
+
shutil.copy2(init_binary, init_path)
|
|
463
|
+
os.chmod(init_path, 0o755)
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def _mount_daxfs(instance_name: str, dmabuf_fd: int) -> None:
|
|
467
|
+
"""
|
|
468
|
+
Mount daxfs on the host kernel, passing the dmabuf fd directly.
|
|
469
|
+
|
|
470
|
+
Uses fsopen/fsconfig/fsmount/move_mount syscalls. The daxfs filesystem
|
|
471
|
+
takes a reference on the dma-buf, so the fd can be closed after mounting.
|
|
472
|
+
|
|
473
|
+
Args:
|
|
474
|
+
instance_name: Name of the multikernel instance
|
|
475
|
+
dmabuf_fd: File descriptor for the dma-buf holding the filesystem image
|
|
476
|
+
"""
|
|
477
|
+
mnt_dir = Path(KERF_DAXFS_MNT_DIR) / instance_name
|
|
478
|
+
mnt_dir.mkdir(parents=True, exist_ok=True)
|
|
479
|
+
|
|
480
|
+
libc = _get_libc()
|
|
481
|
+
fs_fd = -1
|
|
482
|
+
mnt_fd = -1
|
|
483
|
+
|
|
484
|
+
try:
|
|
485
|
+
# fsopen("daxfs", 0)
|
|
486
|
+
fstype = b"daxfs"
|
|
487
|
+
fs_fd = _syscall(libc, SYS_FSOPEN, ctypes.c_char_p(fstype), 0)
|
|
488
|
+
|
|
489
|
+
# fsconfig(fs_fd, FSCONFIG_SET_FD, "dmabuf", NULL, dmabuf_fd)
|
|
490
|
+
_syscall(libc, SYS_FSCONFIG, fs_fd, FSCONFIG_SET_FD,
|
|
491
|
+
ctypes.c_char_p(b"dmabuf"), 0, dmabuf_fd)
|
|
492
|
+
|
|
493
|
+
# fsconfig(fs_fd, FSCONFIG_SET_STRING, "name", instance_name, 0)
|
|
494
|
+
name_bytes = instance_name.encode('utf-8')
|
|
495
|
+
_syscall(libc, SYS_FSCONFIG, fs_fd, FSCONFIG_SET_STRING,
|
|
496
|
+
ctypes.c_char_p(b"name"), ctypes.c_char_p(name_bytes), 0)
|
|
497
|
+
|
|
498
|
+
# fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0)
|
|
499
|
+
_syscall(libc, SYS_FSCONFIG, fs_fd, FSCONFIG_CMD_CREATE, 0, 0, 0)
|
|
500
|
+
|
|
501
|
+
# fsmount(fs_fd, 0, 0)
|
|
502
|
+
mnt_fd = _syscall(libc, SYS_FSMOUNT, fs_fd, 0, 0)
|
|
503
|
+
|
|
504
|
+
# move_mount(mnt_fd, "", AT_FDCWD, mountpoint, MOVE_MOUNT_F_EMPTY_PATH)
|
|
505
|
+
mountpoint = str(mnt_dir).encode('utf-8')
|
|
506
|
+
_syscall(libc, SYS_MOVE_MOUNT, mnt_fd, ctypes.c_char_p(b""),
|
|
507
|
+
AT_FDCWD, ctypes.c_char_p(mountpoint), MOVE_MOUNT_F_EMPTY_PATH)
|
|
508
|
+
except OSError as e:
|
|
509
|
+
raise DaxfsError(f"Failed to mount daxfs: {e}") from e
|
|
510
|
+
finally:
|
|
511
|
+
if mnt_fd >= 0:
|
|
512
|
+
os.close(mnt_fd)
|
|
513
|
+
if fs_fd >= 0:
|
|
514
|
+
os.close(fs_fd)
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
def create_daxfs_image(
|
|
518
|
+
rootfs_path: str,
|
|
519
|
+
instance_name: str,
|
|
520
|
+
heap_path: str = "/dev/dma_heap/multikernel",
|
|
521
|
+
size: Optional[int] = None,
|
|
522
|
+
) -> DaxfsImage:
|
|
523
|
+
"""
|
|
524
|
+
Create a daxfs filesystem image from a directory.
|
|
525
|
+
|
|
526
|
+
Args:
|
|
527
|
+
rootfs_path: Path to the root filesystem directory
|
|
528
|
+
instance_name: Name of the multikernel instance
|
|
529
|
+
heap_path: Path to the DMA heap device
|
|
530
|
+
size: Size to allocate (if None, calculated automatically)
|
|
531
|
+
|
|
532
|
+
Returns:
|
|
533
|
+
DaxfsImage with physical address and size
|
|
534
|
+
|
|
535
|
+
Raises:
|
|
536
|
+
DaxfsError: If image creation fails
|
|
537
|
+
"""
|
|
538
|
+
if not os.path.isdir(rootfs_path):
|
|
539
|
+
raise DaxfsError(f"Rootfs directory '{rootfs_path}' does not exist")
|
|
540
|
+
|
|
541
|
+
builder = DaxfsBuilder(rootfs_path)
|
|
542
|
+
builder.scan()
|
|
543
|
+
builder.build_tree()
|
|
544
|
+
builder.calculate_offsets()
|
|
545
|
+
|
|
546
|
+
required_size = builder.calculate_total_size()
|
|
547
|
+
|
|
548
|
+
if size is None:
|
|
549
|
+
size = required_size
|
|
550
|
+
|
|
551
|
+
if required_size > size:
|
|
552
|
+
raise DaxfsError(
|
|
553
|
+
f"Required size {required_size} exceeds allocated size {size}"
|
|
554
|
+
)
|
|
555
|
+
|
|
556
|
+
# Unmount any previous image for this instance first, releasing its
|
|
557
|
+
# memory back to the pool before the new allocation.
|
|
558
|
+
_umount_all(Path(KERF_DAXFS_MNT_DIR) / instance_name)
|
|
559
|
+
|
|
560
|
+
# The heap allocation registers a 'daxfs' iomem region; snapshot
|
|
561
|
+
# existing regions (other instances) so ours can be identified after.
|
|
562
|
+
regions_before = _daxfs_iomem_regions()
|
|
563
|
+
|
|
564
|
+
try:
|
|
565
|
+
dmabuf_fd, mem = _allocate_dma_heap(heap_path, size)
|
|
566
|
+
except OSError as e:
|
|
567
|
+
raise DaxfsError(f"Failed to allocate from DMA heap: {e}") from e
|
|
568
|
+
|
|
569
|
+
try:
|
|
570
|
+
builder.write_image(mem, size)
|
|
571
|
+
mem.close()
|
|
572
|
+
except Exception as e:
|
|
573
|
+
os.close(dmabuf_fd)
|
|
574
|
+
raise DaxfsError(f"Failed to write daxfs image: {e}") from e
|
|
575
|
+
|
|
576
|
+
try:
|
|
577
|
+
_mount_daxfs(instance_name, dmabuf_fd)
|
|
578
|
+
except DaxfsError:
|
|
579
|
+
os.close(dmabuf_fd)
|
|
580
|
+
raise
|
|
581
|
+
|
|
582
|
+
# daxfs now holds a reference to the dma-buf, safe to close
|
|
583
|
+
os.close(dmabuf_fd)
|
|
584
|
+
|
|
585
|
+
new_regions = _daxfs_iomem_regions() - regions_before
|
|
586
|
+
if len(new_regions) != 1:
|
|
587
|
+
_umount_all(Path(KERF_DAXFS_MNT_DIR) / instance_name)
|
|
588
|
+
raise DaxfsError(
|
|
589
|
+
"Could not identify this image's 'daxfs' region in /proc/iomem "
|
|
590
|
+
f"(found {len(new_regions)} new entries)"
|
|
591
|
+
)
|
|
592
|
+
phys_addr, actual_size = new_regions.pop()
|
|
593
|
+
|
|
594
|
+
return DaxfsImage(
|
|
595
|
+
phys_addr=phys_addr,
|
|
596
|
+
size=actual_size,
|
|
597
|
+
)
|
kerf/delete/__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
|
+
Delete kernel instance subcommand implementation.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from .main import delete
|
|
20
|
+
|
|
21
|
+
__all__ = ["delete"]
|