|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from pydantic import BaseModel, PositiveInt |
| 4 | + |
| 5 | +ROOTFS_PATH = "./runtimes/test.squashfs" |
| 6 | +KERNEL_PATH = "./bin/vmlinux.bin" |
| 7 | +VSOCK_PATH = "/tmp/v.sock" |
| 8 | + |
| 9 | + |
| 10 | +class BootSource(BaseModel): |
| 11 | + kernel_image_path: Path = Path(KERNEL_PATH) |
| 12 | + boot_args: str = "console=ttyS0 reboot=k panic=1 pci=off ro noapic nomodules random.trust_cpu=on" |
| 13 | + |
| 14 | + @staticmethod |
| 15 | + def args(enable_console: bool = True, writable: bool = False): |
| 16 | + default = "reboot=k panic=1 pci=off noapic nomodules random.trust_cpu=on" |
| 17 | + if writable: |
| 18 | + default = default + " rw" |
| 19 | + else: |
| 20 | + default = default + " ro" |
| 21 | + if enable_console: |
| 22 | + return "console=ttyS0 " + default |
| 23 | + else: |
| 24 | + return default |
| 25 | + |
| 26 | + |
| 27 | +class Drive(BaseModel): |
| 28 | + drive_id: str = "rootfs" |
| 29 | + path_on_host: Path = Path(ROOTFS_PATH) |
| 30 | + is_root_device: bool = True |
| 31 | + is_read_only: bool = True |
| 32 | + |
| 33 | + |
| 34 | +class MachineConfig(BaseModel): |
| 35 | + vcpu_count: PositiveInt = 1 |
| 36 | + mem_size_mib: PositiveInt = 512 |
| 37 | + smt: bool = False |
| 38 | + |
| 39 | + |
| 40 | +class Vsock(BaseModel): |
| 41 | + vsock_id: str = "1" |
| 42 | + guest_cid: PositiveInt = 3 |
| 43 | + uds_path: str = VSOCK_PATH |
| 44 | + |
| 45 | + |
| 46 | +class NetworkInterface(BaseModel): |
| 47 | + iface_id: str = "eth0" |
| 48 | + guest_mac: str = "AA:FC:00:00:00:01" |
| 49 | + host_dev_name: str |
| 50 | + |
| 51 | + |
| 52 | +class FirecrackerConfig(BaseModel): |
| 53 | + boot_source: BootSource |
| 54 | + drives: list[Drive] |
| 55 | + machine_config: MachineConfig |
| 56 | + vsock: Vsock | None |
| 57 | + network_interfaces: list[NetworkInterface] | None |
| 58 | + |
| 59 | + class Config: |
| 60 | + allow_population_by_field_name = True |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def alias_generator(x: str): |
| 64 | + return x.replace("_", "-") |
0 commit comments