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 return vc.HypervisorConfig{}, err
} }
if initrd != "" { if image == "" && initrd == "" {
return vc.HypervisorConfig{}, return vc.HypervisorConfig{},
errors.New("having an initrd defined in the configuration file is not supported") errors.New("image or initrd must be defined in the configuration file")
}
if image == "" {
return vc.HypervisorConfig{},
errors.New("image must be defined in the configuration file")
} }
firmware, err := h.firmware() firmware, err := h.firmware()

View File

@ -284,17 +284,27 @@ func (clh *cloudHypervisor) CreateVM(ctx context.Context, id string, networkNS N
return err return err
} }
if imagePath == "" { initrdPath, err := clh.config.InitrdAssetPath()
return errors.New("image path is empty") if err != nil {
return err
} }
if imagePath != "" {
pmem := chclient.NewPmemConfig(imagePath) pmem := chclient.NewPmemConfig(imagePath)
*pmem.DiscardWrites = true *pmem.DiscardWrites = true
if clh.vmconfig.Pmem != nil { if clh.vmconfig.Pmem != nil {
*clh.vmconfig.Pmem = append(*clh.vmconfig.Pmem, *pmem) *clh.vmconfig.Pmem = append(*clh.vmconfig.Pmem, *pmem)
} else { } else {
clh.vmconfig.Pmem = &[]chclient.PmemConfig{*pmem} clh.vmconfig.Pmem = &[]chclient.PmemConfig{*pmem}
} }
} else if initrdPath != "" {
initrd := chclient.NewInitramfsConfig(initrdPath)
clh.vmconfig.SetInitramfs(*initrd)
} else {
return errors.New("no image or initrd specified")
}
// Use serial port as the guest console only in debug mode, // Use serial port as the guest console only in debug mode,
// so that we can gather early OS booting log // so that we can gather early OS booting log

View File

@ -229,11 +229,43 @@ func TestCloudHypervisorCleanupVM(t *testing.T) {
assert.True(os.IsNotExist(err), "persist.GetDriver() unexpected error") 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) { func TestClhCreateVM(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
clhConfig, err := newClhConfig() clhConfig, err := newClhConfig()
assert.NoError(err) assert.NoError(err)
assert.NotEmpty(clhConfig.ImagePath)
store, err := persist.GetDriver() store, err := persist.GetDriver()
assert.NoError(err) assert.NoError(err)

View File

@ -44,6 +44,7 @@ var testQemuImagePath = ""
var testQemuPath = "" var testQemuPath = ""
var testClhKernelPath = "" var testClhKernelPath = ""
var testClhImagePath = "" var testClhImagePath = ""
var testClhInitrdPath = ""
var testClhPath = "" var testClhPath = ""
var testAcrnKernelPath = "" var testAcrnKernelPath = ""
var testAcrnImagePath = "" var testAcrnImagePath = ""
@ -157,6 +158,7 @@ func TestMain(m *testing.M) {
testVirtiofsdPath = filepath.Join(testDir, testBundle, testVirtiofsd) testVirtiofsdPath = filepath.Join(testDir, testBundle, testVirtiofsd)
testClhKernelPath = filepath.Join(testDir, testBundle, testKernel) testClhKernelPath = filepath.Join(testDir, testBundle, testKernel)
testClhImagePath = filepath.Join(testDir, testBundle, testImage) testClhImagePath = filepath.Join(testDir, testBundle, testImage)
testClhInitrdPath = filepath.Join(testDir, testBundle, testInitrd)
testClhPath = filepath.Join(testDir, testBundle, testHypervisor) testClhPath = filepath.Join(testDir, testBundle, testHypervisor)
setupClh() setupClh()