Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cmd/api/api/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ func (s *ApiService) CreateInstance(ctx context.Context, request oapi.CreateInst
networkEnabled = *request.Body.Network.Enabled
}

// Parse volumes
var volumes []instances.VolumeAttachment
if request.Body.Volumes != nil {
volumes = make([]instances.VolumeAttachment, len(*request.Body.Volumes))
for i, vol := range *request.Body.Volumes {
readonly := false
if vol.Readonly != nil {
readonly = *vol.Readonly
}
volumes[i] = instances.VolumeAttachment{
VolumeID: vol.VolumeId,
MountPath: vol.MountPath,
Readonly: readonly,
}
}
}

domainReq := instances.CreateInstanceRequest{
Name: request.Body.Name,
Image: request.Body.Image,
Expand All @@ -104,6 +121,7 @@ func (s *ApiService) CreateInstance(ctx context.Context, request oapi.CreateInst
Vcpus: vcpus,
Env: env,
NetworkEnabled: networkEnabled,
Volumes: volumes,
}

inst, err := s.InstanceManager.CreateInstance(ctx, domainReq)
Expand Down Expand Up @@ -359,5 +377,18 @@ func instanceToOAPI(inst instances.Instance) oapi.Instance {
oapiInst.Env = &inst.Env
}

// Convert volume attachments
if len(inst.Volumes) > 0 {
oapiVolumes := make([]oapi.VolumeAttachment, len(inst.Volumes))
for i, vol := range inst.Volumes {
oapiVolumes[i] = oapi.VolumeAttachment{
VolumeId: vol.VolumeID,
MountPath: vol.MountPath,
Readonly: lo.ToPtr(vol.Readonly),
}
}
oapiInst.Volumes = &oapiVolumes
}

return oapiInst
}
9 changes: 8 additions & 1 deletion cmd/api/api/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,18 @@ func (s *ApiService) DeleteVolume(ctx context.Context, request oapi.DeleteVolume
}

func volumeToOAPI(vol volumes.Volume) oapi.Volume {
return oapi.Volume{
oapiVol := oapi.Volume{
Id: vol.Id,
Name: vol.Name,
SizeGb: vol.SizeGb,
CreatedAt: vol.CreatedAt,
}
if vol.AttachedTo != nil {
oapiVol.AttachedTo = vol.AttachedTo
}
if vol.MountPath != nil {
oapiVol.MountPath = vol.MountPath
}
return oapiVol
}

4 changes: 2 additions & 2 deletions cmd/api/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/go-containerregistry v0.20.6 h1:cvWX87UxxLgaH76b4hIvya6Dzz9qHB31qAwjAohdSTU=
github.com/google/go-containerregistry v0.20.6/go.mod h1:T0x8MuoAoKX/873bkeSfLD2FAkwCDf9/HZgsFJ02E2Y=
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -203,6 +205,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U=
golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -227,6 +231,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
Expand Down
30 changes: 30 additions & 0 deletions lib/images/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,33 @@ func dirSize(path string) (int64, error) {
return size, err
}

// CreateEmptyExt4Disk creates a sparse disk file and formats it as ext4.
// Used for volumes and instance overlays that need empty writable filesystems.
func CreateEmptyExt4Disk(diskPath string, sizeBytes int64) error {
// Ensure parent directory exists
if err := os.MkdirAll(filepath.Dir(diskPath), 0755); err != nil {
return fmt.Errorf("create disk parent dir: %w", err)
}

// Create sparse file
file, err := os.Create(diskPath)
if err != nil {
return fmt.Errorf("create disk file: %w", err)
}
file.Close()

// Truncate to specified size to create sparse file
if err := os.Truncate(diskPath, sizeBytes); err != nil {
return fmt.Errorf("truncate disk file: %w", err)
}

// Format as ext4
cmd := exec.Command("mkfs.ext4", "-F", diskPath)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("mkfs.ext4 failed: %w, output: %s", err, output)
}

return nil
}

26 changes: 25 additions & 1 deletion lib/instances/configdisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ GUEST_GW="%s"
GUEST_DNS="%s"
`, netConfig.IP, cidr, netConfig.Gateway, netConfig.DNS)
}

// Build volume mounts section
// Volumes are attached as /dev/vdd, /dev/vde, etc. (after vda=rootfs, vdb=overlay, vdc=config)
volumeSection := ""
if len(inst.Volumes) > 0 {
var volumeLines strings.Builder
volumeLines.WriteString("\n# Volume mounts (device:path:readonly)\n")
volumeLines.WriteString("VOLUME_MOUNTS=\"")
for i, vol := range inst.Volumes {
// Device naming: vdd, vde, vdf, ...
device := fmt.Sprintf("/dev/vd%c", 'd'+i)
readonly := "rw"
if vol.Readonly {
readonly = "ro"
}
if i > 0 {
volumeLines.WriteString(" ")
}
volumeLines.WriteString(fmt.Sprintf("%s:%s:%s", device, vol.MountPath, readonly))
}
volumeLines.WriteString("\"\n")
volumeSection = volumeLines.String()
}

// Generate script as a readable template block
// ENTRYPOINT and CMD contain shell-quoted arrays that will be eval'd in init
Expand All @@ -116,13 +139,14 @@ CMD="%s"
WORKDIR=%s

# Environment variables
%s%s`,
%s%s%s`,
inst.Id,
entrypoint,
cmd,
workdir,
envLines.String(),
networkSection,
volumeSection,
)

return script
Expand Down
45 changes: 45 additions & 0 deletions lib/instances/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/onkernel/hypeman/lib/network"
"github.com/onkernel/hypeman/lib/system"
"github.com/onkernel/hypeman/lib/vmm"
"github.com/onkernel/hypeman/lib/volumes"
"gvisor.dev/gvisor/pkg/cleanup"
)

Expand Down Expand Up @@ -179,6 +180,41 @@ func (m *manager) createInstance(
})
}

// 10.5. Validate and attach volumes
if len(req.Volumes) > 0 {
log.DebugContext(ctx, "validating volumes", "id", id, "count", len(req.Volumes))
for _, volAttach := range req.Volumes {
// Check volume exists and is not attached
vol, err := m.volumeManager.GetVolume(ctx, volAttach.VolumeID)
if err != nil {
log.ErrorContext(ctx, "volume not found", "id", id, "volume_id", volAttach.VolumeID, "error", err)
return nil, fmt.Errorf("volume %s: %w", volAttach.VolumeID, err)
}
if vol.AttachedTo != nil {
log.ErrorContext(ctx, "volume already attached", "id", id, "volume_id", volAttach.VolumeID, "attached_to", *vol.AttachedTo)
return nil, fmt.Errorf("volume %s is already attached to instance %s", volAttach.VolumeID, *vol.AttachedTo)
}

// Mark volume as attached
if err := m.volumeManager.AttachVolume(ctx, volAttach.VolumeID, volumes.AttachVolumeRequest{
InstanceID: id,
MountPath: volAttach.MountPath,
Readonly: volAttach.Readonly,
}); err != nil {
log.ErrorContext(ctx, "failed to attach volume", "id", id, "volume_id", volAttach.VolumeID, "error", err)
return nil, fmt.Errorf("attach volume %s: %w", volAttach.VolumeID, err)
}

// Add volume cleanup to stack
volumeID := volAttach.VolumeID // capture for closure
cu.Add(func() {
m.volumeManager.DetachVolume(ctx, volumeID)
})
}
// Store volume attachments in metadata
stored.Volumes = req.Volumes
}

// 11. Create config disk (needs Instance for buildVMConfig)
inst := &Instance{StoredMetadata: *stored}
log.DebugContext(ctx, "creating config disk", "id", id)
Expand Down Expand Up @@ -388,6 +424,15 @@ func (m *manager) buildVMConfig(inst *Instance, imageInfo *images.Image, netConf
},
}

// Add attached volumes as additional disks
for _, volAttach := range inst.Volumes {
volumePath := m.volumeManager.GetVolumePath(volAttach.VolumeID)
disks = append(disks, vmm.DiskConfig{
Path: &volumePath,
Readonly: ptr(volAttach.Readonly),
})
}

// Serial console configuration
serial := vmm.ConsoleConfig{
Mode: vmm.ConsoleConfigMode("File"),
Expand Down
13 changes: 12 additions & 1 deletion lib/instances/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@ func (m *manager) deleteInstance(
}
}

// 5. Delete all instance data
// 5. Detach volumes
if len(inst.Volumes) > 0 {
log.DebugContext(ctx, "detaching volumes", "id", id, "count", len(inst.Volumes))
for _, volAttach := range inst.Volumes {
if err := m.volumeManager.DetachVolume(ctx, volAttach.VolumeID); err != nil {
// Log error but continue with cleanup
log.WarnContext(ctx, "failed to detach volume, continuing with cleanup", "id", id, "volume_id", volAttach.VolumeID, "error", err)
}
}
}

// 6. Delete all instance data
log.DebugContext(ctx, "deleting instance data", "id", id)
if err := m.deleteInstanceData(id); err != nil {
log.ErrorContext(ctx, "failed to delete instance data", "id", id, "error", err)
Expand Down
11 changes: 7 additions & 4 deletions lib/instances/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/onkernel/hypeman/lib/network"
"github.com/onkernel/hypeman/lib/paths"
"github.com/onkernel/hypeman/lib/system"
"github.com/onkernel/hypeman/lib/volumes"
)

type Manager interface {
Expand All @@ -29,18 +30,20 @@ type manager struct {
imageManager images.Manager
systemManager system.Manager
networkManager network.Manager
maxOverlaySize int64 // Maximum overlay disk size in bytes
instanceLocks sync.Map // map[string]*sync.RWMutex - per-instance locks
hostTopology *HostTopology // Cached host CPU topology
volumeManager volumes.Manager
maxOverlaySize int64 // Maximum overlay disk size in bytes
instanceLocks sync.Map // map[string]*sync.RWMutex - per-instance locks
hostTopology *HostTopology // Cached host CPU topology
}

// NewManager creates a new instances manager
func NewManager(p *paths.Paths, imageManager images.Manager, systemManager system.Manager, networkManager network.Manager, maxOverlaySize int64) Manager {
func NewManager(p *paths.Paths, imageManager images.Manager, systemManager system.Manager, networkManager network.Manager, volumeManager volumes.Manager, maxOverlaySize int64) Manager {
return &manager{
paths: p,
imageManager: imageManager,
systemManager: systemManager,
networkManager: networkManager,
volumeManager: volumeManager,
maxOverlaySize: maxOverlaySize,
instanceLocks: sync.Map{},
hostTopology: detectHostTopology(), // Detect and cache host topology
Expand Down
Loading
Loading