Files
Shayon Mukherjee 112a1dac24 runsc/cmd/sandboxsetup: export gofer setup helpers for reuse
Building a custom gVisor gofer that serves storage volumes over LisaFS
requires duplicating unexported setup helpers from runsc/cmd/ like
applyCaps, resolveSymlinks, adjustMountOptions, pivotRoot, copyFile,
prepareArgs, execProcUmounter, setCapsAndCallSelf, and intFlags.
These must be manually diffed and re-synced on every gVisor version bump.

The functions are already well-factored: they take explicit parameters,
carry no hidden state, and are unexported only because they live in a CLI
package rather than because they have invariants worth protecting. This
change adds exported equivalents in a new runsc/cmd/sandboxsetup/ package
(with public visibility) and updates all internal callers to use the new
package directly, leaving no stub wrappers behind.

Also fixes a pre-existing bug in CallSelfAsNobody where the error
messages for SYS_SETGID and SYS_SETUID were swapped.
2026-04-10 07:14:21 -04:00

52 lines
1.7 KiB
Go

// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cmd holds implementations of the runsc commands.
package cmd
import (
"fmt"
"gvisor.dev/gvisor/runsc/config"
"gvisor.dev/gvisor/runsc/container"
"gvisor.dev/gvisor/runsc/flag"
)
// containerLoader is an embeddable struct for util.SubCommand implementations
// that take the container ID as an argument. It helps load the container and
// caches the result to avoid loading the same container multiple times.
type containerLoader struct {
cachedContainer *container.Container
}
// loadContainer
func (c *containerLoader) loadContainer(conf *config.Config, f *flag.FlagSet, loadOpts container.LoadOpts) (*container.Container, error) {
if c.cachedContainer != nil {
// Container is already loaded.
return c.cachedContainer, nil
}
// Assumes that the first argument is the container ID.
if f.NArg() < 1 {
f.Usage()
return nil, fmt.Errorf("a container-id is required")
}
id := f.Arg(0)
cont, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, loadOpts)
if err != nil {
return nil, err
}
c.cachedContainer = cont
return c.cachedContainer, nil
}