Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
qemu: fix memory prealloc option handling
Browse files Browse the repository at this point in the history
Memory preallocation is just a property that hugepage, file backed
memory and memory-backend-ram can each choose to configure.

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Aug 16, 2019
1 parent 6c77d76 commit aebc496
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
9 changes: 6 additions & 3 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func (q *qemu) setupTemplate(knobs *govmmQemu.Knobs, memory *govmmQemu.Memory) g
memory.Path = q.config.MemoryPath

if q.config.BootToBeTemplate {
knobs.FileBackedMemShared = true
knobs.MemShared = true
}

if q.config.BootFromTemplate {
Expand All @@ -454,7 +454,7 @@ func (q *qemu) setupFileBackedMem(knobs *govmmQemu.Knobs, memory *govmmQemu.Memo
}

knobs.FileBackedMem = true
knobs.FileBackedMemShared = true
knobs.MemShared = true
memory.Path = target
}

Expand Down Expand Up @@ -522,6 +522,9 @@ func (q *qemu) createSandbox(ctx context.Context, id string, networkNS NetworkNa
} else {
return errors.New("VM templating has been enabled with either virtio-fs or file backed memory and this configuration will not work")
}
if q.config.HugePages {
knobs.MemPrealloc = true
}
}

rtc := govmmQemu.RTC{
Expand Down Expand Up @@ -1484,7 +1487,7 @@ func (q *qemu) hotplugAddMemory(memDev *memoryDevice) (int, error) {
target = q.qemuConfig.Memory.Path
memoryBack = "memory-backend-file"
}
if q.qemuConfig.Knobs.FileBackedMemShared {
if q.qemuConfig.Knobs.MemShared {
share = true
}
err = q.qmpMonitorCh.qmp.ExecHotplugMemory(q.qmpMonitorCh.ctx, memoryBack, "mem"+strconv.Itoa(memDev.slot), target, memDev.sizeMB, share)
Expand Down
12 changes: 6 additions & 6 deletions virtcontainers/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package virtcontainers

import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/store"
"github.com/kata-containers/runtime/virtcontainers/types"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -445,7 +445,7 @@ func TestQemuAddDeviceToBridge(t *testing.T) {
// fail to add device to bridge cause no more available bridge slot
_, _, err := q.addDeviceToBridge("qemu-bridge-31")
exceptErr := errors.New("no more bridge slots available")
assert.Equal(exceptErr, err)
assert.Equal(exceptErr.Error(), err.Error())

// addDeviceToBridge fails cause q.state.Bridges == 0
config.HypervisorMachineType = QemuPCLite
Expand All @@ -456,7 +456,7 @@ func TestQemuAddDeviceToBridge(t *testing.T) {
q.state.Bridges = q.arch.bridges(q.config.DefaultBridges)
_, _, err = q.addDeviceToBridge("qemu-bridge")
exceptErr = errors.New("failed to get available address from bridges")
assert.Equal(exceptErr, err)
assert.Equal(exceptErr.Error(), err.Error())
}

func TestQemuFileBackedMem(t *testing.T) {
Expand All @@ -472,7 +472,7 @@ func TestQemuFileBackedMem(t *testing.T) {
assert.NoError(err)

assert.Equal(q.qemuConfig.Knobs.FileBackedMem, true)
assert.Equal(q.qemuConfig.Knobs.FileBackedMemShared, true)
assert.Equal(q.qemuConfig.Knobs.MemShared, true)
assert.Equal(q.qemuConfig.Memory.Path, fallbackFileBackedMemDir)

// Check failure for VM templating
Expand All @@ -487,7 +487,7 @@ func TestQemuFileBackedMem(t *testing.T) {
err = q.createSandbox(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig, sandbox.store)

expectErr := errors.New("VM templating has been enabled with either virtio-fs or file backed memory and this configuration will not work")
assert.Equal(expectErr, err)
assert.Equal(expectErr.Error(), err.Error())

// Check Setting of non-existent shared-mem path
sandbox, err = createQemuSandboxConfig()
Expand All @@ -498,7 +498,7 @@ func TestQemuFileBackedMem(t *testing.T) {
err = q.createSandbox(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig, sandbox.store)
assert.NoError(err)
assert.Equal(q.qemuConfig.Knobs.FileBackedMem, false)
assert.Equal(q.qemuConfig.Knobs.FileBackedMemShared, false)
assert.Equal(q.qemuConfig.Knobs.MemShared, false)
assert.Equal(q.qemuConfig.Memory.Path, "")
}

Expand Down

0 comments on commit aebc496

Please sign in to comment.