From 19c35554123ccd964d4cb70a536c878a966a7894 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Thu, 22 Mar 2018 10:59:49 +0800 Subject: [PATCH] cli: add hypervisor initrd config option Add `initrd=[path]` option to configuration.toml and use it to set the HypervisorConfig.InitrdPath option. The default value of hypervisor image option is removed since we want to allow it to be unset. For the same reason, there is no default value for hypervisor initrd option either. Signed-off-by: Peng Tao --- Makefile | 6 +++++ cli/config.go | 20 ++++++++++++++- cli/config/configuration.toml.in | 1 + cli/config_test.go | 43 ++++++++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 08048d2102..91f9421549 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ PROJECT_BUG_URL = $(PROJECT_URL)/kata-containers/issues/new BIN_PREFIX = $(PROJECT_TYPE) PROJECT_DIR = $(PROJECT_TAG) IMAGENAME = $(PROJECT_TAG).img +INITRDNAME = $(PROJECT_TAG)-initrd.img TARGET = $(BIN_PREFIX)-runtime TARGET_OUTPUT = $(CURDIR)/$(TARGET) @@ -77,6 +78,7 @@ PKGRUNDIR := $(LOCALSTATEDIR)/run/$(PROJECT_DIR) PKGLIBEXECDIR := $(LIBEXECDIR)/$(PROJECT_DIR) KERNELPATH := $(PKGDATADIR)/vmlinuz.container +INITRDPATH := $(PKGDATADIR)/$(INITRDNAME) IMAGEPATH := $(PKGDATADIR)/$(IMAGENAME) FIRMWAREPATH := @@ -140,6 +142,8 @@ USER_VARS += DESTSYSCONFIG USER_VARS += DESTTARGET USER_VARS += IMAGENAME USER_VARS += IMAGEPATH +USER_VARS += INITRDNAME +USER_VARS += INITRDPATH USER_VARS += MACHINETYPE USER_VARS += KERNELPATH USER_VARS += FIRMWAREPATH @@ -244,6 +248,7 @@ var showConfigPathsOption = fmt.Sprintf("%s-show-default-config-paths", projectP var defaultHypervisorPath = "$(QEMUPATH)" var defaultImagePath = "$(IMAGEPATH)" var defaultKernelPath = "$(KERNELPATH)" +var defaultInitrdPath = "$(INITRDPATH)" var defaultFirmwarePath = "$(FIRMWAREPATH)" var defaultMachineAccelerators = "$(MACHINEACCELERATORS)" var defaultShimPath = "$(SHIMPATH)" @@ -319,6 +324,7 @@ $(GENERATED_FILES): %: %.in Makefile VERSION -e "s|@DESTSYSCONFIG@|$(DESTSYSCONFIG)|g" \ -e "s|@IMAGEPATH@|$(IMAGEPATH)|g" \ -e "s|@KERNELPATH@|$(KERNELPATH)|g" \ + -e "s|@INITRDPATH@|$(INITRDPATH)|g" \ -e "s|@FIRMWAREPATH@|$(FIRMWAREPATH)|g" \ -e "s|@MACHINEACCELERATORS@|$(MACHINEACCELERATORS)|g" \ -e "s|@KERNELPARAMS@|$(KERNELPARAMS)|g" \ diff --git a/cli/config.go b/cli/config.go index 3efa509056..e22f857e8c 100644 --- a/cli/config.go +++ b/cli/config.go @@ -76,6 +76,7 @@ type tomlConfig struct { type hypervisor struct { Path string `toml:"path"` Kernel string `toml:"kernel"` + Initrd string `toml:"initrd"` Image string `toml:"image"` Firmware string `toml:"firmware"` MachineAccelerators string `toml:"machine_accelerators"` @@ -131,11 +132,21 @@ func (h hypervisor) kernel() (string, error) { return resolvePath(p) } +func (h hypervisor) initrd() (string, error) { + p := h.Initrd + + if p == "" { + return "", nil + } + + return resolvePath(p) +} + func (h hypervisor) image() (string, error) { p := h.Image if p == "" { - p = defaultImagePath + return "", nil } return resolvePath(p) @@ -267,6 +278,11 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { return vc.HypervisorConfig{}, err } + initrd, err := h.initrd() + if err != nil { + return vc.HypervisorConfig{}, err + } + image, err := h.image() if err != nil { return vc.HypervisorConfig{}, err @@ -289,6 +305,7 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { return vc.HypervisorConfig{ HypervisorPath: hypervisor, KernelPath: kernel, + InitrdPath: initrd, ImagePath: image, FirmwarePath: firmware, MachineAccelerators: machineAccelerators, @@ -393,6 +410,7 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat HypervisorPath: defaultHypervisorPath, KernelPath: defaultKernelPath, ImagePath: defaultImagePath, + InitrdPath: defaultInitrdPath, FirmwarePath: defaultFirmwarePath, MachineAccelerators: defaultMachineAccelerators, HypervisorMachineType: defaultMachineType, diff --git a/cli/config/configuration.toml.in b/cli/config/configuration.toml.in index fc6532c8c8..e3416adaa9 100644 --- a/cli/config/configuration.toml.in +++ b/cli/config/configuration.toml.in @@ -8,6 +8,7 @@ [hypervisor.qemu] path = "@QEMUPATH@" kernel = "@KERNELPATH@" +initrd = "@INITRDPATH@" image = "@IMAGEPATH@" machine_type = "@MACHINETYPE@" diff --git a/cli/config_test.go b/cli/config_test.go index 3ba31d17d5..54f358179b 100644 --- a/cli/config_test.go +++ b/cli/config_test.go @@ -514,6 +514,7 @@ func TestMinimalRuntimeConfig(t *testing.T) { HypervisorPath: defaultHypervisorPath, KernelPath: defaultKernelPath, ImagePath: defaultImagePath, + InitrdPath: defaultInitrdPath, HypervisorMachineType: defaultMachineType, DefaultVCPUs: defaultVCPUCount, DefaultMemSz: defaultMemSize, @@ -757,6 +758,44 @@ func TestHypervisorDefaultsKernel(t *testing.T) { assert.Equal(h.kernelParams(), kernelParams, "custom hypervisor kernel parameterms wrong") } +// The default initrd path is not returned by h.initrd() +func TestHypervisorDefaultsInitrd(t *testing.T) { + assert := assert.New(t) + + tmpdir, err := ioutil.TempDir(testDir, "") + assert.NoError(err) + defer os.RemoveAll(tmpdir) + + testInitrdPath := filepath.Join(tmpdir, "initrd") + testInitrdLinkPath := filepath.Join(tmpdir, "initrd-link") + + err = createEmptyFile(testInitrdPath) + assert.NoError(err) + + err = syscall.Symlink(testInitrdPath, testInitrdLinkPath) + assert.NoError(err) + + savedInitrdPath := defaultInitrdPath + + defer func() { + defaultInitrdPath = savedInitrdPath + }() + + defaultInitrdPath = testInitrdPath + h := hypervisor{} + p, err := h.initrd() + assert.NoError(err) + assert.Equal(p, "", "default Image path wrong") + + // test path resolution + defaultInitrdPath = testInitrdLinkPath + h = hypervisor{} + p, err = h.initrd() + assert.NoError(err) + assert.Equal(p, "") +} + +// The default image path is not returned by h.image() func TestHypervisorDefaultsImage(t *testing.T) { assert := assert.New(t) @@ -783,14 +822,14 @@ func TestHypervisorDefaultsImage(t *testing.T) { h := hypervisor{} p, err := h.image() assert.NoError(err) - assert.Equal(p, defaultImagePath, "default Image path wrong") + assert.Equal(p, "", "default Image path wrong") // test path resolution defaultImagePath = testImageLinkPath h = hypervisor{} p, err = h.image() assert.NoError(err) - assert.Equal(p, testImagePath) + assert.Equal(p, "") } func TestProxyDefaults(t *testing.T) {