ns

package
v0.0.0-...-f7a5b1a Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: Apache-2.0 Imports: 20 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDirectory

func CopyDirectory(source, destination string, overWrite bool) (err error)

CopyDirectory switches to the host namespace and copies the content from source to destination. It will overwrite the destination if overWrite is true. Top level directory is prohibited.

func CopyFiles

func CopyFiles(sourcePath, destinationPath string, doOverWrite bool) (err error)

CopyFiles switches to the host namespace and copies the all files from source to destination. It will overwrite the destination if overWrite is true.

func CreateDirectory

func CreateDirectory(path string, modTime time.Time) (result string, err error)

CreateDirectory switches to the host namespace and creates a directory at the specified path.

func DeleteDirectory

func DeleteDirectory(directory string) (err error)

DeleteDirectory switches to the host namespace and removes the directory at the specified path.

func DeletePath

func DeletePath(path string) error

DeletePath switches to the host namespace and removes the file or directory at the specified path.

func GetDefaultProcessName

func GetDefaultProcessName() string

GetDefaultProcessName returns the default process name for namespace switching based on the OS distro.

func GetDiskStat

func GetDiskStat(path string) (*types.DiskStat, error)

GetDiskStat switches to the host namespace and returns the disk stat of the disk at the specified path.

func GetEmptyFiles

func GetEmptyFiles(directory string) (result []string, err error)

GetEmptyFiles switches to the host namespace and retrieves a list of paths for all empty files within the specified directory.

func GetFileInfo

func GetFileInfo(path string) (result fs.FileInfo, err error)

GetFileInfo switches to the host namespace and returns the file info of the file at the specified path.

func GetKernelRelease

func GetKernelRelease() (string, error)

GetKernelRelease switches to the host namespace and retrieves the kernel release.

func GetOSDistro

func GetOSDistro() (result string, err error)

GetOSDistro switches to the host namespace and retrieves the OS distro.

func GetSystemBlockDevices

func GetSystemBlockDevices() (result map[string]types.BlockDeviceInfo, err error)

GetSystemBlockDevices switches to the host namespace and retrieves the system block devices.

func LockFile

func LockFile(path string) (result *os.File, err error)

LockFile switches to the host namespace and locks a file at the specified path. It returns the file handle.

func ReadDirectory

func ReadDirectory(directory string) (result []fs.DirEntry, err error)

ReadDirectory switches to the host namespace and reads the content of the directory at the specified path.

func ReadFileContent

func ReadFileContent(filePath string) (result string, err error)

ReadFileContent switches to the host namespace and returns the content of the file at the specified path.

func RunFunc

func RunFunc(fn func() (interface{}, error), timeout time.Duration) (interface{}, error)

RunFunc runs the given function in the host namespace. Returns the result of the function and any error that occurred.

func Sync

func Sync() (err error)

Sync switches to the host namespace and calls sync.

func SyncFile

func SyncFile(filePath string) (err error)

SyncFile switches to the host namespace and syncs the file at the specified path.

func WriteFile

func WriteFile(filePath, data string) error

WriteFile switches to the host namespace and writes the data to the file at the specified path.

Types

type Executor

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

Executor is a struct resonpsible for executing commands in a specific namespace using nsenter.

func NewNamespaceExecutor

func NewNamespaceExecutor(processName, procDirectory string, namespaces []types.Namespace) (*Executor, error)

NewNamespaceExecutor creates a new namespace executor for the given process name, namespaces and proc directory. If the process name is not empty, it will try to use the process namespace directory. Otherwise, it will use the host namespace directory. The namespaces are the namespaces to enter. The proc directory is the directory where the process information is stored. It will also verify the existence of the nsenter binary.

func (*Executor) Cryptsetup

func (nsexec *Executor) Cryptsetup(args []string, timeout time.Duration) (stdout string, err error)

Cryptsetup runs cryptsetup without passphrase. It will return 0 on success and a non-zero value on error.

func (*Executor) CryptsetupWithPassphrase

func (nsexec *Executor) CryptsetupWithPassphrase(passphrase string, args []string, timeout time.Duration) (stdout string, err error)

CryptsetupWithPassphrase runs cryptsetup with passphrase. It will return 0 on success and a non-zero value on error. 1 wrong parameters, 2 no permission (bad passphrase), 3 out of memory, 4 wrong device specified, 5 device already exists or device is busy.

func (*Executor) Execute

func (nsexec *Executor) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error)

Execute executes the command in the namespace. If NsDirectory is empty, it will execute the command in the current namespace.

func (*Executor) ExecuteWithStdin

func (nsexec *Executor) ExecuteWithStdin(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error)

ExecuteWithStdin executes the command in the namespace with stdin. If NsDirectory is empty, it will execute the command in the current namespace.

func (*Executor) ExecuteWithStdinPipe

func (nsexec *Executor) ExecuteWithStdinPipe(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error)

ExecuteWithStdinPipe executes the command in the namespace with stdin pipe. If NsDirectory is empty, it will execute the command in the current namespace.

func (*Executor) LuksClose

func (nsexec *Executor) LuksClose(volume string, timeout time.Duration) (stdout string, err error)

LuksClose runs cryptsetup luksClose and returns the stdout and error.

func (*Executor) LuksFormat

func (nsexec *Executor) LuksFormat(devicePath, passphrase, keyCipher, keyHash, keySize, pbkdf string, timeout time.Duration) (stdout string, err error)

LuksFormat runs cryptsetup luksFormat with the given passphrase and returns the stdout and error.

func (*Executor) LuksOpen

func (nsexec *Executor) LuksOpen(volume, devicePath, passphrase string, timeout time.Duration) (stdout string, err error)

LuksOpen runs cryptsetup luksOpen with the given passphrase and returns the stdout and error.

func (*Executor) LuksResize

func (nsexec *Executor) LuksResize(volume, passphrase string, timeout time.Duration) (stdout string, err error)

LuksResize runs cryptsetup resize with the given passphrase and returns the stdout and error.

func (*Executor) LuksStatus

func (nsexec *Executor) LuksStatus(volume string, timeout time.Duration) (stdout string, err error)

LuksStatus runs cryptsetup status and returns the stdout and error.

type FileLock

type FileLock struct {
	FilePath string        // The path of the file to lock.
	File     *os.File      // The file handle acquired after successful lock.
	Timeout  time.Duration // The maximum time to wait for lock acquisition.
	// contains filtered or unexported fields
}

FileLock is a struct responsible for locking a file.

func NewLock

func NewLock(filepath string, timeout time.Duration) *FileLock

NewLock creates a new FileLock instance.

func (*FileLock) Lock

func (lock *FileLock) Lock() error

Lock locks a file. It starts a goroutine to lock the file and returns the file handle. If the lock acquisition exceeds the specified timeout, the function unlocks the file and returns an error. It also starts another goroutine to wait for lock to release and unlock the file.

func (*FileLock) Unlock

func (lock *FileLock) Unlock()

Unlock closes the done channel to signal the lock to release.

type Joiner

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

Joiner is a context with information about a namespace.

type JoinerDescriptor

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

JoinerDescriptor is a struct that holds information about the namespaces to join.

func (*JoinerDescriptor) Join

func (jd *JoinerDescriptor) Join() (err error)

Join joins the target namespaces.

func (*JoinerDescriptor) OpenNamespaceFiles

func (jd *JoinerDescriptor) OpenNamespaceFiles() (err error)

OpenNamespaceFiles opens required namespace files.

func (*JoinerDescriptor) Revert

func (jd *JoinerDescriptor) Revert() (err error)

Revert reverts to the original namespaces.

func (*JoinerDescriptor) Run

func (jd *JoinerDescriptor) Run(fn func() (interface{}, error)) (interface{}, error)

Run executes the function in the target namespace. The function is executed in a goroutine with a locked OS thread to ensure namespace isolation.

type JoinerInterface

type JoinerInterface interface {
	Revert() error
	Run(fn func() (interface{}, error)) (interface{}, error)
}

type Joiners

type Joiners []*Joiner

func (*Joiners) CloseFiles

func (joiners *Joiners) CloseFiles()

CloseFiles closes all the files in the Joiners.

func (*Joiners) Join

func (joiners *Joiners) Join() (err error)

Join joins all the namespaces in the Joiners.

func (*Joiners) JoinReverse

func (joiners *Joiners) JoinReverse() (err error)

JoinReverse joins all the namespaces in the Joiners in reverse order.

func (*Joiners) OpenFile

func (joiners *Joiners) OpenFile(path string) (fd int, err error)

OpenFile opens a file in the Joiners.

func (*Joiners) Reset

func (joiners *Joiners) Reset() (err error)

Reset resets all the Joiners.

func (*Joiners) ReverseOrder

func (joiners *Joiners) ReverseOrder() Joiners

ReverseOrder returns a reversed copy of the Joiners.

type NewJoinerFunc

type NewJoinerFunc func(string, time.Duration) (JoinerInterface, error)
var NewJoiner NewJoinerFunc = newJoiner

NewJoiner is a variable holding the function responsible for creating a new JoinerInterface. By using a variable for the creation function, it allows for easier unit testing by substituting a mock implementation.

Jump to

Keyboard shortcuts

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