Skip to content

Commit

Permalink
virtcontainers: Enable initrd for Cloud Hypervisor
Browse files Browse the repository at this point in the history
Since CH has supported booting with an initramfs since version 0.7.0
[1], allow an `initrd=` to be specified.

Fixes: #3566.

[1] - https://github.com/cloud-hypervisor/cloud-hypervisor/releases/tag/v0.7.0

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
  • Loading branch information
jodh-intel committed Jan 28, 2022
1 parent a5ebeb9 commit 7c956e0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
9 changes: 2 additions & 7 deletions src/runtime/pkg/katautils/config.go
Expand Up @@ -778,14 +778,9 @@ func newClhHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
return vc.HypervisorConfig{}, err
}

if initrd != "" {
return vc.HypervisorConfig{},
errors.New("having an initrd defined in the configuration file is not supported")
}

if image == "" {
if image == "" && initrd == "" {
return vc.HypervisorConfig{},
errors.New("image must be defined in the configuration file")
errors.New("image or initrd must be defined in the configuration file")
}

firmware, err := h.firmware()
Expand Down
24 changes: 17 additions & 7 deletions src/runtime/virtcontainers/clh.go
Expand Up @@ -284,16 +284,26 @@ func (clh *cloudHypervisor) CreateVM(ctx context.Context, id string, networkNS N
return err
}

if imagePath == "" {
return errors.New("image path is empty")
initrdPath, err := clh.config.InitrdAssetPath()
if err != nil {
return err
}

pmem := chclient.NewPmemConfig(imagePath)
*pmem.DiscardWrites = true
if clh.vmconfig.Pmem != nil {
*clh.vmconfig.Pmem = append(*clh.vmconfig.Pmem, *pmem)
if imagePath != "" {
pmem := chclient.NewPmemConfig(imagePath)
*pmem.DiscardWrites = true

if clh.vmconfig.Pmem != nil {
*clh.vmconfig.Pmem = append(*clh.vmconfig.Pmem, *pmem)
} else {
clh.vmconfig.Pmem = &[]chclient.PmemConfig{*pmem}
}
} else if initrdPath != "" {
initrd := chclient.NewInitramfsConfig(initrdPath)

clh.vmconfig.SetInitramfs(*initrd)
} else {
clh.vmconfig.Pmem = &[]chclient.PmemConfig{*pmem}
return errors.New("no image or initrd specified")
}

// Use serial port as the guest console only in debug mode,
Expand Down
32 changes: 32 additions & 0 deletions src/runtime/virtcontainers/clh_test.go
Expand Up @@ -229,11 +229,43 @@ func TestCloudHypervisorCleanupVM(t *testing.T) {
assert.True(os.IsNotExist(err), "persist.GetDriver() unexpected error")
}

func TestClhCreateVMWithInitrd(t *testing.T) {
assert := assert.New(t)

clhConfig, err := newClhConfig()
assert.NoError(err)
clhConfig.ImagePath = ""
clhConfig.InitrdPath = testClhInitrdPath

store, err := persist.GetDriver()
assert.NoError(err)

clhConfig.VMStorePath = store.RunVMStoragePath()
clhConfig.RunStorePath = store.RunStoragePath()

clh := &cloudHypervisor{
config: clhConfig,
}

sandbox := &Sandbox{
ctx: context.Background(),
id: "testSandbox",
config: &SandboxConfig{
HypervisorConfig: clhConfig,
},
}

err = clh.CreateVM(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig)
assert.NoError(err)
assert.Exactly(clhConfig, clh.config)
}

func TestClhCreateVM(t *testing.T) {
assert := assert.New(t)

clhConfig, err := newClhConfig()
assert.NoError(err)
assert.NotEmpty(clhConfig.ImagePath)

store, err := persist.GetDriver()
assert.NoError(err)
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/virtcontainers/virtcontainers_test.go
Expand Up @@ -44,6 +44,7 @@ var testQemuImagePath = ""
var testQemuPath = ""
var testClhKernelPath = ""
var testClhImagePath = ""
var testClhInitrdPath = ""
var testClhPath = ""
var testAcrnKernelPath = ""
var testAcrnImagePath = ""
Expand Down Expand Up @@ -157,6 +158,7 @@ func TestMain(m *testing.M) {
testVirtiofsdPath = filepath.Join(testDir, testBundle, testVirtiofsd)
testClhKernelPath = filepath.Join(testDir, testBundle, testKernel)
testClhImagePath = filepath.Join(testDir, testBundle, testImage)
testClhInitrdPath = filepath.Join(testDir, testBundle, testInitrd)
testClhPath = filepath.Join(testDir, testBundle, testHypervisor)

setupClh()
Expand Down

0 comments on commit 7c956e0

Please sign in to comment.