diff --git a/cli/config/configuration-qemu.toml.in b/cli/config/configuration-qemu.toml.in index 8c8989b7a..a70ca9753 100644 --- a/cli/config/configuration-qemu.toml.in +++ b/cli/config/configuration-qemu.toml.in @@ -219,6 +219,11 @@ enable_iothreads = @DEFENABLEIOTHREADS@ # Default false #enable_template = true +# Specifies the path of template. +# +# Default "/run/vc/vm/template" +#template_path = "/run/vc/vm/template" + # The number of caches of VMCache: # unspecified or == 0 --> VMCache is disabled # > 0 --> will be set to the specified number diff --git a/cli/factory.go b/cli/factory.go index 999f2f5cd..36a768f8f 100644 --- a/cli/factory.go +++ b/cli/factory.go @@ -159,9 +159,10 @@ var initFactoryCommand = cli.Command{ } factoryConfig := vf.Config{ - Template: runtimeConfig.FactoryConfig.Template, - Cache: runtimeConfig.FactoryConfig.VMCacheNumber, - VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0, + Template: runtimeConfig.FactoryConfig.Template, + TemplatePath: runtimeConfig.FactoryConfig.TemplatePath, + Cache: runtimeConfig.FactoryConfig.VMCacheNumber, + VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0, VMConfig: vc.VMConfig{ HypervisorType: runtimeConfig.HypervisorType, HypervisorConfig: runtimeConfig.HypervisorConfig, @@ -250,7 +251,8 @@ var destroyFactoryCommand = cli.Command{ time.Sleep(time.Second) } else if runtimeConfig.FactoryConfig.Template { factoryConfig := vf.Config{ - Template: true, + Template: true, + TemplatePath: runtimeConfig.FactoryConfig.TemplatePath, VMConfig: vc.VMConfig{ HypervisorType: runtimeConfig.HypervisorType, HypervisorConfig: runtimeConfig.HypervisorConfig, @@ -305,7 +307,8 @@ var statusFactoryCommand = cli.Command{ } if runtimeConfig.FactoryConfig.Template { factoryConfig := vf.Config{ - Template: true, + Template: true, + TemplatePath: runtimeConfig.FactoryConfig.TemplatePath, VMConfig: vc.VMConfig{ HypervisorType: runtimeConfig.HypervisorType, HypervisorConfig: runtimeConfig.HypervisorConfig, diff --git a/cli/factory_test.go b/cli/factory_test.go index ff7609a8f..1af43bd74 100644 --- a/cli/factory_test.go +++ b/cli/factory_test.go @@ -70,6 +70,7 @@ func TestFactoryCLIFunctionInit(t *testing.T) { } runtimeConfig.FactoryConfig.Template = true + runtimeConfig.FactoryConfig.TemplatePath = "/run/vc/vm/template" runtimeConfig.HypervisorType = vc.MockHypervisor runtimeConfig.AgentType = vc.NoopAgentType runtimeConfig.ProxyType = vc.NoopProxyType diff --git a/pkg/katautils/config-settings.go b/pkg/katautils/config-settings.go index da7ce7ba8..d1fab1b4d 100644 --- a/pkg/katautils/config-settings.go +++ b/pkg/katautils/config-settings.go @@ -43,6 +43,7 @@ const defaultHotplugVFIOOnRootBus bool = false const defaultEntropySource = "/dev/urandom" const defaultGuestHookPath string = "" +const defaultTemplatePath string = "/run/vc/vm/template" const defaultVMCacheEndpoint string = "/var/run/kata-containers/cache.sock" // Default config file used by stateless systems. diff --git a/pkg/katautils/config.go b/pkg/katautils/config.go index 06dff7d89..1a434d7d5 100644 --- a/pkg/katautils/config.go +++ b/pkg/katautils/config.go @@ -76,6 +76,7 @@ type tomlConfig struct { type factory struct { Template bool `toml:"enable_template"` + TemplatePath string `toml:"template_path"` VMCacheNumber uint `toml:"vm_cache_number"` VMCacheEndpoint string `toml:"vm_cache_endpoint"` } @@ -546,11 +547,15 @@ func newQemuHypervisorConfig(h hypervisor) (vc.HypervisorConfig, error) { } func newFactoryConfig(f factory) (oci.FactoryConfig, error) { + if f.TemplatePath == "" { + f.TemplatePath = defaultTemplatePath + } if f.VMCacheEndpoint == "" { f.VMCacheEndpoint = defaultVMCacheEndpoint } return oci.FactoryConfig{ Template: f.Template, + TemplatePath: f.TemplatePath, VMCacheNumber: f.VMCacheNumber, VMCacheEndpoint: f.VMCacheEndpoint, }, nil diff --git a/pkg/katautils/config_test.go b/pkg/katautils/config_test.go index ed89beca3..b91dce21e 100644 --- a/pkg/katautils/config_test.go +++ b/pkg/katautils/config_test.go @@ -185,6 +185,7 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf } factoryConfig := oci.FactoryConfig{ + TemplatePath: defaultTemplatePath, VMCacheEndpoint: defaultVMCacheEndpoint, } @@ -633,6 +634,7 @@ func TestMinimalRuntimeConfig(t *testing.T) { } expectedFactoryConfig := oci.FactoryConfig{ + TemplatePath: defaultTemplatePath, VMCacheEndpoint: defaultVMCacheEndpoint, } @@ -1419,6 +1421,7 @@ func TestUpdateRuntimeConfigurationFactoryConfig(t *testing.T) { config := oci.RuntimeConfig{} expectedFactoryConfig := oci.FactoryConfig{ Template: true, + TemplatePath: defaultTemplatePath, VMCacheEndpoint: defaultVMCacheEndpoint, } diff --git a/pkg/katautils/create.go b/pkg/katautils/create.go index 13880330e..90eacf3c7 100644 --- a/pkg/katautils/create.go +++ b/pkg/katautils/create.go @@ -124,6 +124,7 @@ func HandleFactory(ctx context.Context, vci vc.VC, runtimeConfig *oci.RuntimeCon } factoryConfig := vf.Config{ Template: runtimeConfig.FactoryConfig.Template, + TemplatePath: runtimeConfig.FactoryConfig.TemplatePath, VMCache: runtimeConfig.FactoryConfig.VMCacheNumber > 0, VMCacheEndpoint: runtimeConfig.FactoryConfig.VMCacheEndpoint, VMConfig: vc.VMConfig{ diff --git a/virtcontainers/factory/factory.go b/virtcontainers/factory/factory.go index 8c897d9dd..d78215558 100644 --- a/virtcontainers/factory/factory.go +++ b/virtcontainers/factory/factory.go @@ -25,10 +25,10 @@ var factoryLogger = logrus.FieldLogger(logrus.New()) // Config is a collection of VM factory configurations. type Config struct { - Template bool - + Template bool VMCache bool Cache uint + TemplatePath string VMCacheEndpoint string VMConfig vc.VMConfig @@ -70,12 +70,12 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory, } else { if config.Template { if fetchOnly { - b, err = template.Fetch(config.VMConfig) + b, err = template.Fetch(config.VMConfig, config.TemplatePath) if err != nil { return nil, err } } else { - b, err = template.New(ctx, config.VMConfig) + b, err = template.New(ctx, config.VMConfig, config.TemplatePath) if err != nil { return nil, err } diff --git a/virtcontainers/factory/factory_test.go b/virtcontainers/factory/factory_test.go index 7ff3df8cc..e04fee41e 100644 --- a/virtcontainers/factory/factory_test.go +++ b/virtcontainers/factory/factory_test.go @@ -61,6 +61,7 @@ func TestNewFactory(t *testing.T) { } config.Template = true + config.TemplatePath = testDir f, err = NewFactory(ctx, config, false) assert.Nil(err) f.CloseFactory(ctx) @@ -210,7 +211,7 @@ func TestFactoryGetVM(t *testing.T) { f.CloseFactory(ctx) // template factory - f, err = NewFactory(ctx, Config{Template: true, VMConfig: vmConfig}, false) + f, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, VMConfig: vmConfig}, false) assert.Nil(err) vm, err = f.GetVM(ctx, vmConfig) @@ -222,10 +223,10 @@ func TestFactoryGetVM(t *testing.T) { f.CloseFactory(ctx) // fetch template factory - f, err = NewFactory(ctx, Config{Template: true, VMConfig: vmConfig}, false) + f, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, VMConfig: vmConfig}, false) assert.Nil(err) - _, err = NewFactory(ctx, Config{Template: true, VMConfig: vmConfig}, true) + _, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, VMConfig: vmConfig}, true) assert.Error(err) vm, err = f.GetVM(ctx, vmConfig) @@ -249,7 +250,7 @@ func TestFactoryGetVM(t *testing.T) { f.CloseFactory(ctx) // cache factory over template factory - f, err = NewFactory(ctx, Config{Template: true, Cache: 2, VMConfig: vmConfig}, false) + f, err = NewFactory(ctx, Config{Template: true, TemplatePath: testDir, Cache: 2, VMConfig: vmConfig}, false) assert.Nil(err) vm, err = f.GetVM(ctx, vmConfig) diff --git a/virtcontainers/factory/template/template.go b/virtcontainers/factory/template/template.go index d33246563..603c0f5cb 100644 --- a/virtcontainers/factory/template/template.go +++ b/virtcontainers/factory/template/template.go @@ -16,7 +16,6 @@ import ( pb "github.com/kata-containers/runtime/protocols/cache" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/factory/base" - "github.com/kata-containers/runtime/virtcontainers/store" ) type template struct { @@ -29,9 +28,8 @@ var templateWaitForAgent = 2 * time.Second // Fetch finds and returns a pre-built template factory. // TODO: save template metadata and fetch from storage. -func Fetch(config vc.VMConfig) (base.FactoryBase, error) { - statePath := store.RunVMStoragePath + "/template" - t := &template{statePath, config} +func Fetch(config vc.VMConfig, templatePath string) (base.FactoryBase, error) { + t := &template{templatePath, config} err := t.checkTemplateVM() if err != nil { @@ -42,13 +40,12 @@ func Fetch(config vc.VMConfig) (base.FactoryBase, error) { } // New creates a new VM template factory. -func New(ctx context.Context, config vc.VMConfig) (base.FactoryBase, error) { - statePath := store.RunVMStoragePath + "/template" - t := &template{statePath, config} +func New(ctx context.Context, config vc.VMConfig, templatePath string) (base.FactoryBase, error) { + t := &template{templatePath, config} err := t.checkTemplateVM() if err == nil { - return nil, fmt.Errorf("There is already a VM template in %s", statePath) + return nil, fmt.Errorf("There is already a VM template in %s", templatePath) } err = t.prepareTemplateFiles() diff --git a/virtcontainers/factory/template/template_test.go b/virtcontainers/factory/template/template_test.go index f2c4e9da8..834fd89ad 100644 --- a/virtcontainers/factory/template/template_test.go +++ b/virtcontainers/factory/template/template_test.go @@ -46,7 +46,7 @@ func TestTemplateFactory(t *testing.T) { ctx := context.Background() // New - f, err := New(ctx, vmConfig) + f, err := New(ctx, vmConfig, testDir) assert.Nil(err) // Config diff --git a/virtcontainers/pkg/oci/utils.go b/virtcontainers/pkg/oci/utils.go index dec81ce29..b901e8134 100644 --- a/virtcontainers/pkg/oci/utils.go +++ b/virtcontainers/pkg/oci/utils.go @@ -98,6 +98,9 @@ type FactoryConfig struct { // Template enables VM templating support in VM factory. Template bool + // TemplatePath specifies the path of template. + TemplatePath string + // VMCacheNumber specifies the the number of caches of VMCache. VMCacheNumber uint