Skip to content

Commit 3b68c4d

Browse files
committed
create unified for CH and FC
1 parent a7b02f1 commit 3b68c4d

6 files changed

Lines changed: 33 additions & 37 deletions

File tree

cmd/vm/debug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ func printFCDebug(configs []*types.StorageConfig, boot *types.BootConfig, vmCfg
124124
len(configs), cowPath)
125125
fmt.Println()
126126

127-
balloonMiB := memMiB / 4 //nolint:mnd
128-
if vmCfg.Memory >= 256<<20 { //nolint:mnd
127+
balloonMiB := memMiB / hypervisor.DefaultBalloonDiv
128+
if vmCfg.Memory >= hypervisor.MinBalloonMemory {
129129
fmt.Printf("# 4. Balloon\n")
130130
fmt.Printf("curl --unix-socket %s -X PUT http://localhost/balloon \\\n", sock)
131131
fmt.Printf(" -d '{\"amount_mib\": %d, \"deflate_on_oom\": true, \"free_page_reporting\": true}'\n", balloonMiB)

hypervisor/backend.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,26 @@ func (b *Backend) PrepareStart(ctx context.Context, id string, runtimeFiles []st
442442
return &rec, nil
443443
}
444444

445+
// FinalizeCreate writes the fully populated VM record to the DB after
446+
// disk preparation, replacing the placeholder written by ReserveVM.
447+
// RunDir and LogDir are carried over from the existing placeholder.
448+
func (b *Backend) FinalizeCreate(ctx context.Context, id string, info *types.VM, bootCfg *types.BootConfig, blobIDs map[string]struct{}) error {
449+
return b.DB.Update(ctx, func(idx *VMIndex) error {
450+
existing := idx.VMs[id]
451+
if existing == nil {
452+
return fmt.Errorf("vm %s disappeared from index", id)
453+
}
454+
idx.VMs[id] = &VMRecord{
455+
VM: *info,
456+
BootConfig: bootCfg,
457+
ImageBlobIDs: blobIDs,
458+
RunDir: existing.RunDir,
459+
LogDir: existing.LogDir,
460+
}
461+
return nil
462+
})
463+
}
464+
445465
// FinalizeClone marks a just-cloned VM as running in the DB.
446466
// If blobIDs is non-nil, it overwrites the record's image blob pin set.
447467
func (b *Backend) FinalizeClone(ctx context.Context, vmID string, info *types.VM, bootCfg *types.BootConfig, blobIDs map[string]struct{}) error {

hypervisor/cloudhypervisor/create.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,12 @@ func (ch *CloudHypervisor) Create(ctx context.Context, id string, vmCfg *types.V
6666

6767
info := &types.VM{
6868
ID: id, Hypervisor: typ, State: types.VMStateCreated,
69-
Config: *vmCfg,
70-
StorageConfigs: preparedStorage,
71-
NetworkConfigs: networkConfigs,
72-
CreatedAt: now, UpdatedAt: now,
73-
}
74-
rec := hypervisor.VMRecord{
75-
VM: *info,
76-
BootConfig: bootCopy,
77-
ImageBlobIDs: blobIDs,
78-
RunDir: runDir,
79-
LogDir: logDir,
80-
}
81-
if err := ch.DB.Update(ctx, func(idx *hypervisor.VMIndex) error {
82-
idx.VMs[id] = &rec
83-
return nil
84-
}); err != nil {
69+
Config: *vmCfg, StorageConfigs: preparedStorage, NetworkConfigs: networkConfigs,
70+
CreatedAt: now, UpdatedAt: now,
71+
}
72+
if err := ch.FinalizeCreate(ctx, id, info, bootCopy, blobIDs); err != nil {
8573
return nil, fmt.Errorf("finalize VM record: %w", err)
8674
}
87-
8875
return info, nil
8976
}
9077

hypervisor/firecracker/create.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,10 @@ func (fc *Firecracker) Create(ctx context.Context, id string, vmCfg *types.VMCon
5656

5757
info := &types.VM{
5858
ID: id, Hypervisor: typ, State: types.VMStateCreated,
59-
Config: *vmCfg,
60-
StorageConfigs: preparedStorage,
61-
NetworkConfigs: networkConfigs,
62-
CreatedAt: now, UpdatedAt: now,
63-
}
64-
rec := hypervisor.VMRecord{
65-
VM: *info,
66-
BootConfig: bootCopy,
67-
ImageBlobIDs: blobIDs,
68-
RunDir: runDir,
69-
LogDir: logDir,
70-
}
71-
if err := fc.DB.Update(ctx, func(idx *hypervisor.VMIndex) error {
72-
idx.VMs[id] = &rec
73-
return nil
74-
}); err != nil {
59+
Config: *vmCfg, StorageConfigs: preparedStorage, NetworkConfigs: networkConfigs,
60+
CreatedAt: now, UpdatedAt: now,
61+
}
62+
if err := fc.FinalizeCreate(ctx, id, info, bootCopy, blobIDs); err != nil {
7563
return nil, fmt.Errorf("finalize VM record: %w", err)
7664
}
7765
return info, nil

images/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func GCStaleTemp(ctx context.Context, dir string, dirOnly bool) []error {
167167
}
168168

169169
// GCCollectBlobs removes temp files and blob artifacts by hex ID.
170-
// removers are called for each hex; os.IsNotExist errors are ignored.
170+
// removers are called for each hex; fs.ErrNotExist errors are ignored.
171171
func GCCollectBlobs(ctx context.Context, tempDir string, dirOnly bool, ids []string, removers ...func(string) error) error {
172172
var errs []error
173173
errs = append(errs, GCStaleTemp(ctx, tempDir, dirOnly)...)

snapshot/localfile/localfile_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"errors"
99
"io"
10+
"io/fs"
1011
"os"
1112
"path/filepath"
1213
"strings"
@@ -312,7 +313,7 @@ func TestDelete(t *testing.T) {
312313
}
313314

314315
// Data dir should be gone.
315-
if _, err := os.Stat(lf.conf.SnapshotDataDir(id)); !os.IsNotExist(err) {
316+
if _, err := os.Stat(lf.conf.SnapshotDataDir(id)); !errors.Is(err, fs.ErrNotExist) {
316317
t.Error("expected data dir to be removed")
317318
}
318319

0 commit comments

Comments
 (0)