virtcontainers: Enable initrd for Cloud Hypervisor

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>
This commit is contained in:
James O. D. Hunt 2022-01-28 10:39:51 +00:00
parent a5ebeb96c1
commit 7c956e0d27
4 changed files with 53 additions and 14 deletions

View File

@ -778,14 +778,9 @@ func newClhHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
return vc.HypervisorConfig{}, err
}
if initrd != "" {
if image == "" && initrd == "" {
return vc.HypervisorConfig{},
errors.New("having an initrd defined in the configuration file is not supported")
}
if image == "" {
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()

View File

@ -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,

View File

@ -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)

View File

@ -44,6 +44,7 @@ var testQemuImagePath = ""
var testQemuPath = ""
var testClhKernelPath = ""
var testClhImagePath = ""
var testClhInitrdPath = ""
var testClhPath = ""
var testAcrnKernelPath = ""
var testAcrnImagePath = ""
@ -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()