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 <bergwolf@gmail.com>
This commit is contained in:
Peng Tao 2018-03-22 10:59:49 +08:00
parent 4f57b65147
commit 19c3555412
4 changed files with 67 additions and 3 deletions

View File

@ -37,6 +37,7 @@ PROJECT_BUG_URL = $(PROJECT_URL)/kata-containers/issues/new
BIN_PREFIX = $(PROJECT_TYPE) BIN_PREFIX = $(PROJECT_TYPE)
PROJECT_DIR = $(PROJECT_TAG) PROJECT_DIR = $(PROJECT_TAG)
IMAGENAME = $(PROJECT_TAG).img IMAGENAME = $(PROJECT_TAG).img
INITRDNAME = $(PROJECT_TAG)-initrd.img
TARGET = $(BIN_PREFIX)-runtime TARGET = $(BIN_PREFIX)-runtime
TARGET_OUTPUT = $(CURDIR)/$(TARGET) TARGET_OUTPUT = $(CURDIR)/$(TARGET)
@ -77,6 +78,7 @@ PKGRUNDIR := $(LOCALSTATEDIR)/run/$(PROJECT_DIR)
PKGLIBEXECDIR := $(LIBEXECDIR)/$(PROJECT_DIR) PKGLIBEXECDIR := $(LIBEXECDIR)/$(PROJECT_DIR)
KERNELPATH := $(PKGDATADIR)/vmlinuz.container KERNELPATH := $(PKGDATADIR)/vmlinuz.container
INITRDPATH := $(PKGDATADIR)/$(INITRDNAME)
IMAGEPATH := $(PKGDATADIR)/$(IMAGENAME) IMAGEPATH := $(PKGDATADIR)/$(IMAGENAME)
FIRMWAREPATH := FIRMWAREPATH :=
@ -140,6 +142,8 @@ USER_VARS += DESTSYSCONFIG
USER_VARS += DESTTARGET USER_VARS += DESTTARGET
USER_VARS += IMAGENAME USER_VARS += IMAGENAME
USER_VARS += IMAGEPATH USER_VARS += IMAGEPATH
USER_VARS += INITRDNAME
USER_VARS += INITRDPATH
USER_VARS += MACHINETYPE USER_VARS += MACHINETYPE
USER_VARS += KERNELPATH USER_VARS += KERNELPATH
USER_VARS += FIRMWAREPATH USER_VARS += FIRMWAREPATH
@ -244,6 +248,7 @@ var showConfigPathsOption = fmt.Sprintf("%s-show-default-config-paths", projectP
var defaultHypervisorPath = "$(QEMUPATH)" var defaultHypervisorPath = "$(QEMUPATH)"
var defaultImagePath = "$(IMAGEPATH)" var defaultImagePath = "$(IMAGEPATH)"
var defaultKernelPath = "$(KERNELPATH)" var defaultKernelPath = "$(KERNELPATH)"
var defaultInitrdPath = "$(INITRDPATH)"
var defaultFirmwarePath = "$(FIRMWAREPATH)" var defaultFirmwarePath = "$(FIRMWAREPATH)"
var defaultMachineAccelerators = "$(MACHINEACCELERATORS)" var defaultMachineAccelerators = "$(MACHINEACCELERATORS)"
var defaultShimPath = "$(SHIMPATH)" var defaultShimPath = "$(SHIMPATH)"
@ -319,6 +324,7 @@ $(GENERATED_FILES): %: %.in Makefile VERSION
-e "s|@DESTSYSCONFIG@|$(DESTSYSCONFIG)|g" \ -e "s|@DESTSYSCONFIG@|$(DESTSYSCONFIG)|g" \
-e "s|@IMAGEPATH@|$(IMAGEPATH)|g" \ -e "s|@IMAGEPATH@|$(IMAGEPATH)|g" \
-e "s|@KERNELPATH@|$(KERNELPATH)|g" \ -e "s|@KERNELPATH@|$(KERNELPATH)|g" \
-e "s|@INITRDPATH@|$(INITRDPATH)|g" \
-e "s|@FIRMWAREPATH@|$(FIRMWAREPATH)|g" \ -e "s|@FIRMWAREPATH@|$(FIRMWAREPATH)|g" \
-e "s|@MACHINEACCELERATORS@|$(MACHINEACCELERATORS)|g" \ -e "s|@MACHINEACCELERATORS@|$(MACHINEACCELERATORS)|g" \
-e "s|@KERNELPARAMS@|$(KERNELPARAMS)|g" \ -e "s|@KERNELPARAMS@|$(KERNELPARAMS)|g" \

View File

@ -76,6 +76,7 @@ type tomlConfig struct {
type hypervisor struct { type hypervisor struct {
Path string `toml:"path"` Path string `toml:"path"`
Kernel string `toml:"kernel"` Kernel string `toml:"kernel"`
Initrd string `toml:"initrd"`
Image string `toml:"image"` Image string `toml:"image"`
Firmware string `toml:"firmware"` Firmware string `toml:"firmware"`
MachineAccelerators string `toml:"machine_accelerators"` MachineAccelerators string `toml:"machine_accelerators"`
@ -131,11 +132,21 @@ func (h hypervisor) kernel() (string, error) {
return resolvePath(p) 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) { func (h hypervisor) image() (string, error) {
p := h.Image p := h.Image
if p == "" { if p == "" {
p = defaultImagePath return "", nil
} }
return resolvePath(p) return resolvePath(p)
@ -267,6 +278,11 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
return vc.HypervisorConfig{}, err return vc.HypervisorConfig{}, err
} }
initrd, err := h.initrd()
if err != nil {
return vc.HypervisorConfig{}, err
}
image, err := h.image() image, err := h.image()
if err != nil { if err != nil {
return vc.HypervisorConfig{}, err return vc.HypervisorConfig{}, err
@ -289,6 +305,7 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) {
return vc.HypervisorConfig{ return vc.HypervisorConfig{
HypervisorPath: hypervisor, HypervisorPath: hypervisor,
KernelPath: kernel, KernelPath: kernel,
InitrdPath: initrd,
ImagePath: image, ImagePath: image,
FirmwarePath: firmware, FirmwarePath: firmware,
MachineAccelerators: machineAccelerators, MachineAccelerators: machineAccelerators,
@ -393,6 +410,7 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat
HypervisorPath: defaultHypervisorPath, HypervisorPath: defaultHypervisorPath,
KernelPath: defaultKernelPath, KernelPath: defaultKernelPath,
ImagePath: defaultImagePath, ImagePath: defaultImagePath,
InitrdPath: defaultInitrdPath,
FirmwarePath: defaultFirmwarePath, FirmwarePath: defaultFirmwarePath,
MachineAccelerators: defaultMachineAccelerators, MachineAccelerators: defaultMachineAccelerators,
HypervisorMachineType: defaultMachineType, HypervisorMachineType: defaultMachineType,

View File

@ -8,6 +8,7 @@
[hypervisor.qemu] [hypervisor.qemu]
path = "@QEMUPATH@" path = "@QEMUPATH@"
kernel = "@KERNELPATH@" kernel = "@KERNELPATH@"
initrd = "@INITRDPATH@"
image = "@IMAGEPATH@" image = "@IMAGEPATH@"
machine_type = "@MACHINETYPE@" machine_type = "@MACHINETYPE@"

View File

@ -514,6 +514,7 @@ func TestMinimalRuntimeConfig(t *testing.T) {
HypervisorPath: defaultHypervisorPath, HypervisorPath: defaultHypervisorPath,
KernelPath: defaultKernelPath, KernelPath: defaultKernelPath,
ImagePath: defaultImagePath, ImagePath: defaultImagePath,
InitrdPath: defaultInitrdPath,
HypervisorMachineType: defaultMachineType, HypervisorMachineType: defaultMachineType,
DefaultVCPUs: defaultVCPUCount, DefaultVCPUs: defaultVCPUCount,
DefaultMemSz: defaultMemSize, DefaultMemSz: defaultMemSize,
@ -757,6 +758,44 @@ func TestHypervisorDefaultsKernel(t *testing.T) {
assert.Equal(h.kernelParams(), kernelParams, "custom hypervisor kernel parameterms wrong") 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) { func TestHypervisorDefaultsImage(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
@ -783,14 +822,14 @@ func TestHypervisorDefaultsImage(t *testing.T) {
h := hypervisor{} h := hypervisor{}
p, err := h.image() p, err := h.image()
assert.NoError(err) assert.NoError(err)
assert.Equal(p, defaultImagePath, "default Image path wrong") assert.Equal(p, "", "default Image path wrong")
// test path resolution // test path resolution
defaultImagePath = testImageLinkPath defaultImagePath = testImageLinkPath
h = hypervisor{} h = hypervisor{}
p, err = h.image() p, err = h.image()
assert.NoError(err) assert.NoError(err)
assert.Equal(p, testImagePath) assert.Equal(p, "")
} }
func TestProxyDefaults(t *testing.T) { func TestProxyDefaults(t *testing.T) {