-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.py
More file actions
429 lines (340 loc) · 12.9 KB
/
config.py
File metadata and controls
429 lines (340 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# config.py
"""
HyperBEAM OS Configuration Management
Provides type-safe, well-structured configuration for the HyperBEAM build system.
Uses dataclasses for better IDE support, type checking, and validation.
"""
import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Dict, Any, Optional
@dataclass
class DirectoryConfig:
"""Directory paths used throughout the build system."""
# Core directories
base: str = field(init=False)
build: str = field(init=False)
bin: str = field(init=False)
content: str = field(init=False)
guest: str = field(init=False)
kernel: str = field(init=False)
verity: str = field(init=False)
snp: str = field(init=False)
snp_package: str = field(init=False)
resources: str = field(init=False)
scripts: str = field(init=False)
config: str = field(init=False)
def __post_init__(self):
"""Initialize computed directory paths."""
self.base = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
self.build = os.path.realpath("build")
self.bin = os.path.join(self.build, "bin")
self.content = os.path.join(self.build, "content")
self.guest = os.path.join(self.build, "guest")
self.kernel = os.path.join(self.build, "kernel")
self.verity = os.path.join(self.build, "verity")
self.snp = os.path.join(self.build, "snp-release")
self.snp_package = os.path.join(self.build, "SNP_PACKAGE")
self.resources = os.path.realpath("resources")
self.scripts = os.path.realpath("scripts")
self.config = os.path.realpath("config")
@dataclass
class BuildConfig:
"""Build-related configuration settings."""
# Branch configuration
hb_branch: str = "edge"
ao_branch: str = "tillathehun0/cu-experimental"
# Debug and virtualization
debug: bool = False
enable_kvm: bool = True
enable_tpm: bool = True
enable_gpu: bool = False # Enable GPU support
# Image names
base_image: str = "base.qcow2"
guest_image: str = "guest.qcow2"
# GPU admin tools repository
gpu_admin_tools_repo: str = "https://github.com/permaweb/gpu-admin-tools"
@dataclass
class VMConfig:
"""Virtual machine configuration settings."""
# VM Hardware Configuration
host_cpu_family: str = "Genoa"
vcpu_count: int = 12
memory_mb: int = 204800
# SEV-SNP Guest Policy Settings
guest_features: str = "0x1"
platform_info: str = "0x3"
guest_policy: str = "0x30000"
family_id: str = "00000000000000000000000000000000"
image_id: str = "00000000000000000000000000000000"
# Kernel command line
cmdline: str = "console=ttyS0 earlyprintk=serial root=/dev/sda"
@dataclass
class TCBConfig:
"""Trusted Computing Base (TCB) configuration."""
bootloader: int = 9
tee: int = 0
snp: int = 22
microcode: int = 72
reserved: List[int] = field(default_factory=lambda: [0, 0, 0, 0])
@dataclass
class NetworkConfig:
"""Network and SSH configuration."""
vm_host: str = "localhost"
vm_port: int = 2222
vm_user: str = "ubuntu"
hb_port: int = 80
qemu_port: int = 4444
@dataclass
class QEMUConfig:
"""QEMU-specific configuration."""
launch_script: str = "./launch.sh"
snp_params: str = "-sev-snp"
@dataclass
class SNPConfig:
"""SNP package building configuration."""
release_url: str = "https://github.com/permaweb/hb-os/releases/download/v1.0.0/snp-release.tar.gz"
dependencies: List[str] = field(default_factory=lambda: [
"build-essential", "git", "python3", "python3-venv", "ninja-build",
"libglib2.0-dev", "uuid-dev", "iasl", "nasm", "python-is-python3",
"flex", "bison", "openssl", "libssl-dev", "libelf-dev", "bc",
"libncurses-dev", "gawk", "dkms", "libudev-dev", "libpci-dev",
"libiberty-dev", "autoconf", "llvm", "cpio", "zstd", "debhelper",
"rsync", "wget", "python3-tomli"
])
class HyperBeamConfig:
"""
Main configuration class that aggregates all configuration sections.
Provides type-safe access to all configuration settings with logical grouping
and computed properties for commonly used derived values.
"""
def __init__(self):
# Configuration sections
self.dirs = DirectoryConfig()
self.build = BuildConfig()
self.vm = VMConfig()
self.tcb = TCBConfig()
self.network = NetworkConfig()
self.qemu = QEMUConfig()
self.snp = SNPConfig()
# Allow runtime modification of branch settings
self._runtime_hb_branch: Optional[str] = None
self._runtime_ao_branch: Optional[str] = None
# ===================== Dynamic Properties =====================
@property
def hb_branch(self) -> str:
"""HyperBEAM branch (can be overridden at runtime)."""
return self._runtime_hb_branch or self.build.hb_branch
@hb_branch.setter
def hb_branch(self, value: str):
"""Set HyperBEAM branch at runtime."""
self._runtime_hb_branch = value
@property
def ao_branch(self) -> str:
"""AO branch (can be overridden at runtime)."""
return self._runtime_ao_branch or self.build.ao_branch
@ao_branch.setter
def ao_branch(self, value: str):
"""Set AO branch at runtime."""
self._runtime_ao_branch = value
# ===================== Computed File Paths =====================
@property
def vm_image_base_name(self) -> str:
"""Base VM image filename."""
return self.build.base_image
@property
def vm_image_base_path(self) -> str:
"""Full path to base VM image."""
return os.path.join(self.dirs.guest, self.build.base_image)
@property
def vm_cloud_config(self) -> str:
"""Path to VM cloud config blob."""
return os.path.join(self.dirs.guest, "config-blob.img")
@property
def vm_template_user_data(self) -> str:
"""Path to VM template user data."""
return os.path.join(self.dirs.resources, "template-user-data")
@property
def kernel_deb(self) -> str:
"""Path pattern for kernel .deb package."""
return os.path.join(self.dirs.snp, "linux", "guest", "linux-image-*.deb")
@property
def kernel_vmlinuz(self) -> str:
"""Path pattern for kernel vmlinuz."""
return os.path.join(self.dirs.kernel, "boot", "vmlinuz-*")
@property
def ovmf(self) -> str:
"""Path to OVMF firmware."""
return os.path.join(self.dirs.snp, "usr", "local", "share", "qemu", "DIRECT_BOOT_OVMF.fd")
@property
def initrd(self) -> str:
"""Path to initramfs image."""
return os.path.join(self.dirs.build, "initramfs.cpio.gz")
@property
def initramfs_script(self) -> str:
"""Path to initramfs init script."""
return os.path.join(self.dirs.scripts, "init.sh")
@property
def initramfs_dockerfile(self) -> str:
"""Path to initramfs Dockerfile."""
return os.path.join(self.dirs.resources, "initramfs.Dockerfile")
@property
def content_dockerfile(self) -> str:
"""Path to content Dockerfile."""
return os.path.join(self.dirs.resources, "content.Dockerfile")
@property
def vm_config_file(self) -> str:
"""Path to VM configuration file."""
return os.path.join(self.dirs.guest, "vm-config.toml")
@property
def verity_image(self) -> str:
"""Path to verity image."""
return os.path.join(self.dirs.verity, self.build.guest_image)
@property
def verity_hash_tree(self) -> str:
"""Path to verity hash tree."""
return os.path.join(self.dirs.verity, "hash_tree.bin")
@property
def verity_root_hash(self) -> str:
"""Path to verity root hash file."""
return os.path.join(self.dirs.verity, "roothash.txt")
@property
def ssh_hosts_file(self) -> str:
"""Path to SSH known hosts file."""
return os.path.join(self.dirs.build, "known_hosts")
@property
def snp_amdsev_path(self) -> str:
"""Path to AMDSEV repository."""
return os.path.join(self.dirs.build, "AMDSEV")
# ===================== Computed Parameters =====================
@property
def debug(self) -> str:
"""Debug flag as string (for backward compatibility)."""
return "1" if self.build.debug else "0"
@property
def enable_kvm(self) -> str:
"""KVM enable flag as string (for backward compatibility)."""
return "1" if self.build.enable_kvm else "0"
@property
def enable_tpm(self) -> str:
"""TPM enable flag as string (for backward compatibility)."""
return "1" if self.build.enable_tpm else "0"
@property
def enable_gpu(self) -> str:
"""GPU enable flag as string (for backward compatibility)."""
return "1" if self.build.enable_gpu else "0"
@property
def vcpu_count(self) -> int:
"""Number of virtual CPUs."""
return self.vm.vcpu_count
@property
def cmdline(self) -> str:
"""Kernel command line."""
return self.vm.cmdline
@property
def guest_policy(self) -> str:
"""Guest policy setting."""
return self.vm.guest_policy
@property
def host_cpu_family(self) -> str:
"""Host CPU family."""
return self.vm.host_cpu_family
@property
def guest_features(self) -> str:
"""Guest features setting."""
return self.vm.guest_features
@property
def platform_info(self) -> str:
"""Platform info setting."""
return self.vm.platform_info
@property
def family_id(self) -> str:
"""Family ID."""
return self.vm.family_id
@property
def image_id(self) -> str:
"""Image ID."""
return self.vm.image_id
@property
def min_committed_tcb(self) -> Dict[str, Any]:
"""Minimum committed TCB configuration."""
return {
"bootloader": self.tcb.bootloader,
"tee": self.tcb.tee,
"snp": self.tcb.snp,
"microcode": self.tcb.microcode,
"_reserved": self.tcb.reserved,
}
@property
def network_vm_host(self) -> str:
"""VM network host."""
return self.network.vm_host
@property
def network_vm_port(self) -> str:
"""VM network port as string (for backward compatibility)."""
return str(self.network.vm_port)
@property
def network_vm_user(self) -> str:
"""VM network user."""
return self.network.vm_user
@property
def qemu_launch_script(self) -> str:
"""QEMU launch script path."""
return self.qemu.launch_script
@property
def qemu_snp_params(self) -> str:
"""QEMU SNP parameters."""
return self.qemu.snp_params
@property
def qemu_memory(self) -> str:
"""QEMU memory as string (for backward compatibility)."""
return str(self.vm.memory_mb)
@property
def qemu_hb_port(self) -> str:
"""QEMU HyperBEAM port as string (for backward compatibility)."""
return str(self.network.hb_port)
@property
def qemu_port(self) -> str:
"""QEMU port as string (for backward compatibility)."""
return str(self.network.qemu_port)
@property
def qemu_ovmf(self) -> str:
"""QEMU OVMF path."""
return self.ovmf
@property
def qemu_build_dir(self) -> str:
"""QEMU build directory."""
return self.dirs.build
@property
def qemu_default_params(self) -> str:
"""Default QEMU parameters."""
log_file = os.path.join(self.dirs.build, 'stdout.log')
return (f"-default-network -log {log_file} "
f"-mem {self.qemu_memory} -smp {self.vcpu_count} ")
@property
def qemu_extra_params(self) -> str:
"""Extra QEMU parameters."""
return f"-bios {self.qemu_ovmf} -policy {self.guest_policy}"
@property
def snp_dependencies(self) -> List[str]:
"""SNP build dependencies."""
return self.snp.dependencies
@property
def verity_params(self) -> str:
"""
Computes the verity parameters by reading the content of the root hash file.
If the file does not exist or an error occurs, a placeholder value is used.
"""
try:
with open(self.verity_root_hash, "r") as f:
roothash = f.read().strip()
except Exception:
roothash = "unknown"
return f"boot=verity verity_disk=/dev/sdb verity_roothash={roothash}"
# ===================== Backward Compatibility =====================
@property
def dir(self) -> DirectoryConfig:
"""Backward compatibility alias for directories."""
return self.dirs
# Create the global configuration instance
config = HyperBeamConfig()