safechroot

package
v0.0.0-...-3139816 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const BindMountPointFlags = unix.MS_BIND | unix.MS_MGC_VAL

BindMountPointFlags is a set of flags to do a bind mount.

Variables

This section is empty.

Functions

func AddFilesToDestination

func AddFilesToDestination(destDir string, filesToCopy ...FileToCopy) error

Types

type Chroot

type Chroot struct {
	// contains filtered or unexported fields
}

Chroot represents a Chroot environment with automatic synchronization protections and guaranteed cleanup code even on SIGTERM so long as registerSIGTERMCleanup is invoked.

func NewChroot

func NewChroot(rootDir string, isExistingDir bool) *Chroot

NewChroot creates a new Chroot struct

func (*Chroot) AddDirs

func (c *Chroot) AddDirs(dirToCopy DirToCopy) (err error)

AddDirs copies each directory 'Src' to the relative path chrootRootDir/'Dest' in the chroot.

func (*Chroot) AddFiles

func (c *Chroot) AddFiles(filesToCopy ...FileToCopy) (err error)

AddFiles copies each file 'Src' to the relative path chrootRootDir/'Dest' in the chroot.

func (*Chroot) Close

func (c *Chroot) Close(leaveOnDisk bool) (err error)

Close will unmount the chroot and cleanup its files. This call will block until the chroot cleanup runs. Only one Chroot will close at a given time.

func (*Chroot) CopyOutFile

func (c *Chroot) CopyOutFile(srcPath string, destPath string) (err error)

CopyOutFile copies file 'srcPath' in the chroot to the host at 'destPath'

func (*Chroot) GetMountPoints

func (c *Chroot) GetMountPoints() []*MountPoint

GetMountPoints gets a copy of the list of mounts the Chroot was initialized with.

func (*Chroot) Initialize

func (c *Chroot) Initialize(tarPath string, extraDirectories []string, extraMountPoints []*MountPoint,
	includeDefaultMounts bool,
) (err error)

Initialize initializes a Chroot, creating directories and mount points.

  • tarPath is an optional path to a tar file that will be extracted at the root of the chroot.
  • extraDirectories is an optional slice of additional directories that should be created before attempting to mount inside the chroot.
  • extraMountPoints is an optional slice of additional mount points that should be created inside the chroot, they will automatically be unmounted on a Chroot Close.

This call will block until the chroot initializes successfully. Only one Chroot will initialize at a given time.

func (*Chroot) MoveOutFile

func (c *Chroot) MoveOutFile(srcPath string, destPath string) (err error)

MoveOutFile moves file 'srcPath' in the chroot to the host at 'destPath', deleting the 'srcPath' file.

func (*Chroot) RootDir

func (c *Chroot) RootDir() string

RootDir returns the Chroot's root directory.

func (*Chroot) Run

func (c *Chroot) Run(toRun func() error) (err error)

Run runs a given function inside the Chroot. This function will synchronize with all other Chroots to ensure only one Chroot command is executed at a given time.

func (*Chroot) UnsafeRun

func (c *Chroot) UnsafeRun(toRun func() error) (err error)

UnsafeRun runs a given function inside the Chroot. This function will not synchronize with other Chroots. The invoker is responsible for ensuring safety.

func (*Chroot) WriteFiles

func (c *Chroot) WriteFiles() error

type ChrootInterface

type ChrootInterface interface {
	RootDir() string
	Run(toRun func() error) error
	UnsafeRun(toRun func() error) error
	AddFiles(filesToCopy ...FileToCopy) error
}

type DirToCopy

type DirToCopy struct {
	Src                  string
	Dest                 string
	NewDirPermissions    os.FileMode
	ChildFilePermissions os.FileMode
	MergedDirPermissions *os.FileMode
}

DirToCopy represents a directory to copy into a chroot using AddDirs. Dest is relative to the chroot directory.

type DummyChroot

type DummyChroot struct {
}

DummyChroot is a placeholder that implements ChrootInterface.

func (*DummyChroot) AddFiles

func (d *DummyChroot) AddFiles(filesToCopy ...FileToCopy) (err error)

func (*DummyChroot) RootDir

func (d *DummyChroot) RootDir() string

func (*DummyChroot) Run

func (d *DummyChroot) Run(toRun func() error) (err error)

func (*DummyChroot) UnsafeRun

func (d *DummyChroot) UnsafeRun(toRun func() error) (err error)

type FileToCopy

type FileToCopy struct {
	// The source file path.
	// Mutually exclusive with 'Content'.
	Src string
	// The contents of the file to write.
	// Mutually exclusive with 'Src'.
	Content *string
	// The destination path to write/copy the file to.
	Dest        string
	Permissions *os.FileMode
	// Set to true to copy symlinks as symlinks.
	NoDereference bool
}

FileToCopy represents a file to copy into a chroot using AddFiles. Dest is relative to the chroot directory.

type MountPoint

type MountPoint struct {
	// contains filtered or unexported fields
}

MountPoint represents a system mount point used by a Chroot. It is guaranteed to be unmounted on application exit even on a SIGTERM so long as registerSIGTERMCleanup is invoked. The fields of MountPoint mirror those of the `mount` syscall.

func NewMountPoint

func NewMountPoint(source, target, fstype string, flags uintptr, data string) (mountPoint *MountPoint)

NewMountPoint creates a new MountPoint struct to be created by a Chroot

func NewOverlayMountPoint

func NewOverlayMountPoint(chrootDir, source, target, lowerDir, upperDir, workDir string) (mountPoint *MountPoint, extaDirsNeeds []string)

NewOverlayMountPoint creates a new MountPoint struct and extra directories slice configured for a given overlay

func NewPreDefaultsMountPoint

func NewPreDefaultsMountPoint(source, target, fstype string, flags uintptr, data string) (mountPoint *MountPoint)

NewPreDefaultsMountPoint creates a new MountPoint struct to be created by a Chroot but before the default mount points.

func (*MountPoint) GetFSType

func (m *MountPoint) GetFSType() string

GetFSType gets the file-system type of the mount.

func (*MountPoint) GetFlags

func (m *MountPoint) GetFlags() uintptr

GetFlags gets the flags of the mount.

func (*MountPoint) GetSource

func (m *MountPoint) GetSource() string

GetSource gets the source device of the mount.

func (*MountPoint) GetTarget

func (m *MountPoint) GetTarget() string

GetTarget gets the target directory path of the mount.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL