From 6072004b8616544f3ee17645c552bb2115b8d566 Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Wed, 22 Jan 2025 22:35:43 -0500 Subject: [PATCH] Add time namespace / TimeOffsets check and helper Signed-off-by: Etienne Champetier --- generate/config.go | 7 +++++++ generate/generate.go | 10 +++++++++- validate/validate_linux.go | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/generate/config.go b/generate/config.go index 48f281d2..04db6bb0 100644 --- a/generate/config.go +++ b/generate/config.go @@ -66,6 +66,13 @@ func (g *Generator) initConfigLinuxIntelRdt() { } } +func (g *Generator) initConfigLinuxTimeOffsets() { + g.initConfigLinux() + if g.Config.Linux.TimeOffsets == nil { + g.Config.Linux.TimeOffsets = map[string]rspec.LinuxTimeOffset{} + } +} + func (g *Generator) initConfigLinuxSysctl() { g.initConfigLinux() if g.Config.Linux.Sysctl == nil { diff --git a/generate/generate.go b/generate/generate.go index ae5a9984..db9d5b27 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -16,7 +16,7 @@ import ( var ( // Namespaces include the names of supported namespaces. - Namespaces = []string{"network", "pid", "mount", "ipc", "uts", "user", "cgroup"} + Namespaces = []string{"network", "pid", "mount", "ipc", "uts", "user", "cgroup", "time"} // we don't care about order...and this is way faster... removeFunc = func(s []string, i int) []string { @@ -627,6 +627,12 @@ func (g *Generator) SetLinuxIntelRdtL3CacheSchema(schema string) { g.Config.Linux.IntelRdt.L3CacheSchema = schema } +// SetLinuxTimeOffset sets g.Config.Linux.TimeOffsets[clock] +func (g *Generator) SetLinuxTimeOffset(clock string, offset rspec.LinuxTimeOffset) { + g.initConfigLinuxTimeOffsets() + g.Config.Linux.TimeOffsets[clock] = offset +} + // SetLinuxMountLabel sets g.Config.Linux.MountLabel. func (g *Generator) SetLinuxMountLabel(label string) { g.initConfigLinux() @@ -1479,6 +1485,8 @@ func mapStrToNamespace(ns string, path string) (rspec.LinuxNamespace, error) { return rspec.LinuxNamespace{Type: rspec.UserNamespace, Path: path}, nil case "cgroup": return rspec.LinuxNamespace{Type: rspec.CgroupNamespace, Path: path}, nil + case "time": + return rspec.LinuxNamespace{Type: rspec.TimeNamespace, Path: path}, nil default: return rspec.LinuxNamespace{}, fmt.Errorf("unrecognized namespace %q", ns) } diff --git a/validate/validate_linux.go b/validate/validate_linux.go index 9aaff120..5153346a 100644 --- a/validate/validate_linux.go +++ b/validate/validate_linux.go @@ -54,6 +54,7 @@ func (v *Validator) CheckLinux() (errs error) { rspec.UTSNamespace: {0, false}, rspec.UserNamespace: {0, false}, rspec.CgroupNamespace: {0, false}, + rspec.TimeNamespace: {0, false}, } for index := 0; index < len(v.spec.Linux.Namespaces); index++ { @@ -93,6 +94,10 @@ func (v *Validator) CheckLinux() (errs error) { errs = multierror.Append(errs, fmt.Errorf("on Linux, hostname requires a new UTS namespace to be specified as well")) } + if !nsTypeList[rspec.TimeNamespace].newExist && len(v.spec.Linux.TimeOffsets) > 0 { + errs = multierror.Append(errs, fmt.Errorf("TimeOffsets requires a new time namespace to be specified as well")) + } + // Linux devices validation devList := make(map[string]bool) devTypeList := make(map[string]bool)