feat: firecracker mmds identity

This commit is contained in:
Harivansh Rathi 2026-04-10 00:53:47 +00:00
parent 500354cd9b
commit 3eb610b703
23 changed files with 1813 additions and 263 deletions

View file

@ -16,16 +16,50 @@ type MachineSpec struct {
MemoryMiB int64
KernelImagePath string
RootFSPath string
RootDrive DriveSpec
KernelArgs string
Drives []DriveSpec
MMDS *MMDSSpec
Vsock *VsockSpec
}
// DriveSpec describes an additional guest block device.
type DriveSpec struct {
ID string
Path string
ReadOnly bool
ID string
Path string
ReadOnly bool
CacheType DriveCacheType
IOEngine DriveIOEngine
}
type DriveCacheType string
const (
DriveCacheTypeUnsafe DriveCacheType = "Unsafe"
DriveCacheTypeWriteback DriveCacheType = "Writeback"
)
type DriveIOEngine string
const (
DriveIOEngineSync DriveIOEngine = "Sync"
DriveIOEngineAsync DriveIOEngine = "Async"
)
type MMDSVersion string
const (
MMDSVersionV1 MMDSVersion = "V1"
MMDSVersionV2 MMDSVersion = "V2"
)
// MMDSSpec describes the MMDS network configuration and initial payload.
type MMDSSpec struct {
NetworkInterfaces []string
Version MMDSVersion
IPv4Address string
IMDSCompat bool
Data any
}
// VsockSpec describes a single host-guest vsock device.
@ -49,17 +83,22 @@ func (s MachineSpec) Validate() error {
if strings.TrimSpace(s.KernelImagePath) == "" {
return fmt.Errorf("machine kernel image path is required")
}
if strings.TrimSpace(s.RootFSPath) == "" {
return fmt.Errorf("machine rootfs path is required")
}
if filepath.Base(strings.TrimSpace(string(s.ID))) != strings.TrimSpace(string(s.ID)) {
return fmt.Errorf("machine id %q must not contain path separators", s.ID)
}
if err := s.rootDrive().Validate(); err != nil {
return fmt.Errorf("root drive: %w", err)
}
for i, drive := range s.Drives {
if err := drive.Validate(); err != nil {
return fmt.Errorf("drive %d: %w", i, err)
}
}
if s.MMDS != nil {
if err := s.MMDS.Validate(); err != nil {
return fmt.Errorf("mmds: %w", err)
}
}
if s.Vsock != nil {
if err := s.Vsock.Validate(); err != nil {
return fmt.Errorf("vsock: %w", err)
@ -70,11 +109,39 @@ func (s MachineSpec) Validate() error {
// Validate reports whether the drive specification is usable.
func (d DriveSpec) Validate() error {
if strings.TrimSpace(d.Path) == "" {
return fmt.Errorf("drive path is required")
}
if strings.TrimSpace(d.ID) == "" {
return fmt.Errorf("drive id is required")
}
if strings.TrimSpace(d.Path) == "" {
return fmt.Errorf("drive path is required")
switch d.CacheType {
case "", DriveCacheTypeUnsafe, DriveCacheTypeWriteback:
default:
return fmt.Errorf("unsupported drive cache type %q", d.CacheType)
}
switch d.IOEngine {
case "", DriveIOEngineSync, DriveIOEngineAsync:
default:
return fmt.Errorf("unsupported drive io engine %q", d.IOEngine)
}
return nil
}
// Validate reports whether the MMDS configuration is usable.
func (m MMDSSpec) Validate() error {
if len(m.NetworkInterfaces) == 0 {
return fmt.Errorf("mmds network interfaces are required")
}
switch m.Version {
case "", MMDSVersionV1, MMDSVersionV2:
default:
return fmt.Errorf("unsupported mmds version %q", m.Version)
}
for i, iface := range m.NetworkInterfaces {
if strings.TrimSpace(iface) == "" {
return fmt.Errorf("mmds network_interfaces[%d] is required", i)
}
}
return nil
}
@ -92,3 +159,14 @@ func (v VsockSpec) Validate() error {
}
return nil
}
func (s MachineSpec) rootDrive() DriveSpec {
root := s.RootDrive
if strings.TrimSpace(root.ID) == "" {
root.ID = defaultRootDriveID
}
if strings.TrimSpace(root.Path) == "" {
root.Path = s.RootFSPath
}
return root
}