Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions lib/hypervisor/cloudhypervisor/cloudhypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/kernel/hypeman/lib/hypervisor"
"github.com/kernel/hypeman/lib/logger"
"github.com/kernel/hypeman/lib/vmm"
)

Expand Down Expand Up @@ -70,6 +71,7 @@ func CapabilitiesForVersion(v vmm.CHVersion) hypervisor.Capabilities {
switch v {
case vmm.V51_1:
caps.SupportsDiskResize = true
caps.SupportsConcurrentForkPrepare = true
}
return caps
}
Expand Down Expand Up @@ -170,6 +172,13 @@ func (c *CloudHypervisor) Snapshot(ctx context.Context, destPath string) error {
if resp.StatusCode() != 204 {
return fmt.Errorf("snapshot failed with status %d", resp.StatusCode())
}
optimized, err := prepareSnapshotForKernelPaging(destPath)
if err != nil {
return fmt.Errorf("prepare kernel-paged snapshot: %w", err)
}
if optimized {
logger.FromContext(ctx).DebugContext(ctx, "prepared Cloud Hypervisor snapshot for kernel paging", "snapshot_dir", destPath)
}
return nil
}

Expand Down
19 changes: 17 additions & 2 deletions lib/hypervisor/cloudhypervisor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func serialSocketPath(logPath string) string {
return filepath.Join(filepath.Dir(filepath.Dir(logPath)), "serial.sock")
}

const kernelPagingMemoryZoneID = "hypeman-kernel-paging"

// ToVMConfig converts hypervisor.VMConfig to Cloud Hypervisor's vmm.VmConfig.
func ToVMConfig(cfg hypervisor.VMConfig) vmm.VmConfig {
// Payload configuration (kernel + initramfs)
Expand All @@ -51,8 +53,21 @@ func ToVMConfig(cfg hypervisor.VMConfig) vmm.VmConfig {
}

// Memory configuration
memory := vmm.MemoryConfig{
Size: cfg.MemoryBytes,
memory := vmm.MemoryConfig{Size: cfg.MemoryBytes}
if cfg.MemoryBackingFile != "" {
zones := []vmm.MemoryZoneConfig{{
Id: kernelPagingMemoryZoneID,
Size: cfg.MemoryBytes,
File: ptr(cfg.MemoryBackingFile),
Shared: ptr(false),
Prefault: ptr(false),
Hugepages: ptr(false),
}}
memory.Size = 0
memory.Shared = ptr(false)
memory.Prefault = ptr(false)
memory.Hugepages = ptr(false)
memory.Zones = &zones
}
if cfg.HotplugBytes > 0 {
memory.HotplugSize = &cfg.HotplugBytes
Expand Down
26 changes: 26 additions & 0 deletions lib/hypervisor/cloudhypervisor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ import (
"github.com/stretchr/testify/require"
)

func TestToVMConfigFileBackedMemory(t *testing.T) {
t.Parallel()

vmCfg := ToVMConfig(hypervisor.VMConfig{
VCPUs: 2,
MemoryBytes: 8 * 1024 * 1024 * 1024,
MemoryBackingFile: "/var/lib/hypeman/memory.raw",
})

require.NotNil(t, vmCfg.Memory)
assert.Zero(t, vmCfg.Memory.Size)
require.NotNil(t, vmCfg.Memory.Zones)
require.Len(t, *vmCfg.Memory.Zones, 1)
zone := (*vmCfg.Memory.Zones)[0]
assert.Equal(t, kernelPagingMemoryZoneID, zone.Id)
assert.Equal(t, int64(8*1024*1024*1024), zone.Size)
require.NotNil(t, zone.File)
assert.Equal(t, "/var/lib/hypeman/memory.raw", *zone.File)
require.NotNil(t, zone.Shared)
assert.False(t, *zone.Shared)
require.NotNil(t, zone.Prefault)
assert.False(t, *zone.Prefault)
require.NotNil(t, zone.Hugepages)
assert.False(t, *zone.Hugepages)
}

func TestToVMConfig_GuestMemoryBalloon(t *testing.T) {
cfg := hypervisor.VMConfig{
VCPUs: 1,
Expand Down
Loading
Loading