diff --git a/src/runtime/pkg/katautils/config.go b/src/runtime/pkg/katautils/config.go index 67fc685dc..495404667 100644 --- a/src/runtime/pkg/katautils/config.go +++ b/src/runtime/pkg/katautils/config.go @@ -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() diff --git a/src/runtime/virtcontainers/clh.go b/src/runtime/virtcontainers/clh.go index a3cdf02f6..da6e73e3e 100644 --- a/src/runtime/virtcontainers/clh.go +++ b/src/runtime/virtcontainers/clh.go @@ -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, diff --git a/src/runtime/virtcontainers/clh_test.go b/src/runtime/virtcontainers/clh_test.go index b0de2ae9b..f9452cbd3 100644 --- a/src/runtime/virtcontainers/clh_test.go +++ b/src/runtime/virtcontainers/clh_test.go @@ -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) diff --git a/src/runtime/virtcontainers/virtcontainers_test.go b/src/runtime/virtcontainers/virtcontainers_test.go index 84f0387dd..cb03e2351 100644 --- a/src/runtime/virtcontainers/virtcontainers_test.go +++ b/src/runtime/virtcontainers/virtcontainers_test.go @@ -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()