From 5653143175394f0fb0e0c377bd94213c18770477 Mon Sep 17 00:00:00 2001 From: Edward Guzman Date: Wed, 17 Oct 2018 11:22:19 -0700 Subject: [PATCH] cli: add guest hook path option Fixes: #720 Co-authored-by: Edward Guzman Co-authored-by: Felix Abecassis Signed-off-by: Felix Abecassis --- Makefile | 4 ++++ cli/config.go | 10 ++++++++++ cli/config/configuration.toml.in | 7 +++++++ cli/config_test.go | 18 ++++++++++++++++++ virtcontainers/hypervisor.go | 3 +++ virtcontainers/kata_agent.go | 9 +++++---- 6 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index ad79f31dd6..9e06b576f8 100644 --- a/Makefile +++ b/Makefile @@ -149,6 +149,7 @@ DEFENABLEDEBUG := false DEFDISABLENESTINGCHECKS := false DEFMSIZE9P := 8192 DEFHOTPLUGVFIOONROOTBUS := false +DEFGUESTHOOKPATH := SED = sed @@ -225,6 +226,7 @@ USER_VARS += DEFDISABLENESTINGCHECKS USER_VARS += DEFMSIZE9P USER_VARS += DEFHOTPLUGVFIOONROOTBUS USER_VARS += DEFENTROPYSOURCE +USER_VARS += DEFGUESTHOOKPATH V = @ @@ -328,6 +330,7 @@ const defaultDisableNestingChecks bool = $(DEFDISABLENESTINGCHECKS) const defaultMsize9p uint32 = $(DEFMSIZE9P) const defaultHotplugVFIOOnRootBus bool = $(DEFHOTPLUGVFIOONROOTBUS) const defaultEntropySource = "$(DEFENTROPYSOURCE)" +const defaultGuestHookPath string = "$(DEFGUESTHOOKPATH)" // Default config file used by stateless systems. var defaultRuntimeConfiguration = "$(CONFIG_PATH)" @@ -419,6 +422,7 @@ $(GENERATED_FILES): %: %.in Makefile VERSION -e "s|@DEFMSIZE9P@|$(DEFMSIZE9P)|g" \ -e "s|@DEFHOTPLUGONROOTBUS@|$(DEFHOTPLUGVFIOONROOTBUS)|g" \ -e "s|@DEFENTROPYSOURCE@|$(DEFENTROPYSOURCE)|g" \ + -e "s|@DEFGUESTHOOKPATH@|$(DEFGUESTHOOKPATH)|g" \ $< > $@ generate-config: $(CONFIG) diff --git a/cli/config.go b/cli/config.go index bc3215909f..bf44f7c94a 100644 --- a/cli/config.go +++ b/cli/config.go @@ -98,6 +98,7 @@ type hypervisor struct { UseVSock bool `toml:"use_vsock"` HotplugVFIOOnRootBus bool `toml:"hotplug_vfio_on_root_bus"` DisableVhostNet bool `toml:"disable_vhost_net"` + GuestHookPath string `toml:"guest_hook_path"` } type proxy struct { @@ -302,6 +303,13 @@ func (h hypervisor) useVSock() bool { return h.UseVSock } +func (h hypervisor) guestHookPath() string { + if h.GuestHookPath == "" { + return defaultGuestHookPath + } + return h.GuestHookPath +} + func (p proxy) path() string { if p.Path == "" { return defaultProxyPath @@ -426,6 +434,7 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { UseVSock: useVSock, HotplugVFIOOnRootBus: h.HotplugVFIOOnRootBus, DisableVhostNet: h.DisableVhostNet, + GuestHookPath: h.guestHookPath(), }, nil } @@ -547,6 +556,7 @@ func loadConfiguration(configPath string, ignoreLogging bool) (resolvedConfigPat EnableIOThreads: defaultEnableIOThreads, Msize9p: defaultMsize9p, HotplugVFIOOnRootBus: defaultHotplugVFIOOnRootBus, + GuestHookPath: defaultGuestHookPath, } err = config.InterNetworkModel.SetModel(defaultInterNetworkingModel) diff --git a/cli/config/configuration.toml.in b/cli/config/configuration.toml.in index 5ee4e474e5..a4ed0d1f42 100644 --- a/cli/config/configuration.toml.in +++ b/cli/config/configuration.toml.in @@ -167,6 +167,13 @@ enable_iothreads = @DEFENABLEIOTHREADS@ # all practical purposes. #entropy_source= "@DEFENTROPYSOURCE@" +# If set to an absolute path within the guest rootfs, the agent will search +# this directory for OCI hooks and add them to the guest container's lifecycle. +# https://github.com/opencontainers/runtime-spec/blob/v1.0.1/config.md#posix-platform-hooks +# Hooks must be stored in a subdirectory of guest_hook_path named after the +# hook type, e.g. prestart hooks must be in "guest_hook_path/prestart/" +#guest_hook_path = "@DEFGUESTHOOKPATH@" + [factory] # VM templating support. Once enabled, new VMs are created from template # using vm cloning. They will share the same initial kernel, initramfs and diff --git a/cli/config_test.go b/cli/config_test.go index d3e7c9fbc4..2a08f01dd1 100644 --- a/cli/config_test.go +++ b/cli/config_test.go @@ -61,6 +61,7 @@ func makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath hotplug_vfio_on_root_bus = ` + strconv.FormatBool(hotplugVFIOOnRootBus) + ` msize_9p = ` + strconv.FormatUint(uint64(defaultMsize9p), 10) + ` enable_debug = ` + strconv.FormatBool(hypervisorDebug) + ` + guest_hook_path = "` + defaultGuestHookPath + `" [proxy.kata] enable_debug = ` + strconv.FormatBool(proxyDebug) + ` @@ -161,6 +162,7 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf Msize9p: defaultMsize9p, MemSlots: defaultMemSlots, EntropySource: defaultEntropySource, + GuestHookPath: defaultGuestHookPath, } agentConfig := vc.KataAgentConfig{} @@ -596,6 +598,7 @@ func TestMinimalRuntimeConfig(t *testing.T) { Mlock: !defaultEnableSwap, BlockDeviceDriver: defaultBlockDeviceDriver, Msize9p: defaultMsize9p, + GuestHookPath: defaultGuestHookPath, } expectedAgentConfig := vc.KataAgentConfig{} @@ -1078,6 +1081,21 @@ func TestHypervisorDefaultsImage(t *testing.T) { assert.Equal(p, "") } +func TestHypervisorDefaultsGuestHookPath(t *testing.T) { + assert := assert.New(t) + + h := hypervisor{} + guestHookPath := h.guestHookPath() + assert.Equal(guestHookPath, defaultGuestHookPath, "default guest hook path wrong") + + testGuestHookPath := "/test/guest/hook/path" + h = hypervisor{ + GuestHookPath: testGuestHookPath, + } + guestHookPath = h.guestHookPath() + assert.Equal(guestHookPath, testGuestHookPath, "custom guest hook path wrong") +} + func TestProxyDefaults(t *testing.T) { p := proxy{} diff --git a/virtcontainers/hypervisor.go b/virtcontainers/hypervisor.go index d94591ff7f..1d69eee121 100644 --- a/virtcontainers/hypervisor.go +++ b/virtcontainers/hypervisor.go @@ -250,6 +250,9 @@ type HypervisorConfig struct { // DisableVhostNet is used to indicate if host supports vhost_net DisableVhostNet bool + + // GuestHookPath is the path within the VM that will be used for 'drop-in' hooks + GuestHookPath string } func (conf *HypervisorConfig) checkTemplateConfig() error { diff --git a/virtcontainers/kata_agent.go b/virtcontainers/kata_agent.go index dd4c939314..ec51fde299 100644 --- a/virtcontainers/kata_agent.go +++ b/virtcontainers/kata_agent.go @@ -631,10 +631,11 @@ func (k *kataAgent) startSandbox(sandbox *Sandbox) error { } req := &grpc.CreateSandboxRequest{ - Hostname: hostname, - Storages: storages, - SandboxPidns: sandbox.sharePidNs, - SandboxId: sandbox.id, + Hostname: hostname, + Storages: storages, + SandboxPidns: sandbox.sharePidNs, + SandboxId: sandbox.id, + GuestHookPath: sandbox.config.HypervisorConfig.GuestHookPath, } _, err = k.sendReq(req)