Track container name in the kernel

This is done to allow re-mapping of container IDs after the
container has been restored. Upon restore, container names
remain the same, but container ID may be (and likely are)
different.

Updates #1956

PiperOrigin-RevId: 621996848
This commit is contained in:
Fabricio Voznika
2024-04-04 16:01:28 -07:00
committed by gVisor bot
parent c9964aa985
commit 1a5bd5cfdf
3 changed files with 18 additions and 3 deletions
+2 -3
View File
@@ -266,9 +266,8 @@ func (proc *Proc) execAsync(args *ExecArgs) (*kernel.ThreadGroup, kernel.ThreadI
initArgs.Filename = resolved
}
// TODO(gvisor.dev/issue/1956): Container name is not really needed because
// exec processes are not restored, but add it for completeness.
ttyFile, err := fdimport.Import(ctx, fdTable, args.StdioIsPty, args.KUID, args.KGID, fdMap, "")
containerName := proc.Kernel.ContainerNameGivenID(args.ContainerID)
ttyFile, err := fdimport.Import(ctx, fdTable, args.StdioIsPty, args.KUID, args.KGID, fdMap, containerName)
if err != nil {
return nil, 0, nil, err
}
+14
View File
@@ -348,6 +348,9 @@ type Kernel struct {
// devGofers maps container ID to its device gofer client.
devGofers map[string]*devutil.GoferClient `state:"nosave"`
devGofersMu sync.Mutex `state:"nosave"`
// cid -> name.
containerNames map[string]string
}
// InitKernelArgs holds arguments to Init.
@@ -454,6 +457,7 @@ func (k *Kernel) Init(args InitKernelArgs) error {
args.MaxFDLimit = MaxFdLimit
}
k.MaxFDLimit.Store(args.MaxFDLimit)
k.containerNames = make(map[string]string)
ctx := k.SupervisorContext()
if err := k.vfs.Init(ctx); err != nil {
@@ -1956,3 +1960,13 @@ func (k *Kernel) cleaupDevGofers() {
}
k.devGofers = nil
}
// RegisterContainerName registers a container name for a given container ID.
func (k *Kernel) RegisterContainerName(cid, containerName string) {
k.containerNames[cid] = containerName
}
// ContainerNameGivenID returns the container name for a given container ID.
func (k *Kernel) ContainerNameGivenID(cid string) string {
return k.containerNames[cid]
}
+2
View File
@@ -543,6 +543,7 @@ func New(args Args) (*Loader, error) {
}
}
k.RegisterContainerName(args.ID, info.containerName)
eid := execID{cid: args.ID}
l := &Loader{
k: k,
@@ -954,6 +955,7 @@ func (l *Loader) startSubcontainer(spec *specs.Spec, conf *config.Config, cid st
})
}
l.k.RegisterContainerName(cid, info.containerName)
l.k.StartProcess(ep.tg)
// No more failures from this point on.
cu.Release()