utils

package
v0.0.0-...-e5e0983 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Byte MemUnit = 1
	KiB          = Byte << 10
	MiB          = KiB << 10
	GiB          = MiB << 10
)
View Source
const DefaultRateLimiterRefillTimeMilliSecs = 1000

The DefaultRateLimiterRefillTime is used for calculating the rate at which a TokenBucket is replinished, in cases where a RateLimiter is applied to either network or disk I/O.

View Source
const MaxSocketPathLen = 107

MaxSocketPathLen is the effective maximum Unix domain socket length.

See unix(7).

View Source
const MibToBytesShift = 20

MibToBytesShift the number to shift needed to convert MiB to Bytes

Variables

View Source
var StartCmd = func(c *exec.Cmd) error {
	return c.Start()
}

StartCmd pointer to a function to start a command. Defined this way to allow mock testing.

View Source
var VHostVSockDevicePath = "/dev/vhost-vsock"

VHostVSockDevicePath path to vhost-vsock device

Functions

func BuildSocketPath

func BuildSocketPath(elements ...string) (string, error)

BuildSocketPath concatenates the provided elements into a path and returns it. If the resulting path is longer than the maximum permitted socket path on Linux, it will return an error.

func CalculateCPUsF

func CalculateCPUsF(quota int64, period uint64) float32

CalculateCPUsF converts CPU quota and period to a fraction number

func ChownToParent

func ChownToParent(path string) error

ChownToParent changes the owners of the path to the same of parent directory. The path needs to be absolute and the method doesn't handle symlink.

func CleanupFds

func CleanupFds(fds []*os.File, numFds int)

CleanupFds closed bundles of open fds in batch

func ConvertAddressFamily

func ConvertAddressFamily(family int32) pbTypes.IPFamily

func DeepCompare

func DeepCompare(foo, bar interface{}) bool

DeepCompare compare foo and bar.

func FileCopy

func FileCopy(srcPath, dstPath string) error

FileCopy copys files from srcPath to dstPath

func FindContextID

func FindContextID() (*os.File, uint64, error)

FindContextID finds a unique context ID by generating a random number between 3 and max unsigned int (maxUint). Using the ioctl VHOST_VSOCK_SET_GUEST_CID, findContextID asks to the kernel if the given context ID (N) is available, when the context ID is not available, incrementing by 1 findContextID iterates from N to maxUint until an available context ID is found, otherwise decrementing by 1 findContextID iterates from N to 3 until an available context ID is found, this is the last chance to find a context ID available. On success vhost file and a context ID greater or equal than 3 are returned, otherwise 0 and an error are returned. vhost file can be used to send vhost file decriptor to QEMU. It's the caller's responsibility to close vhost file descriptor.

Benefits of using random context IDs:

  • Reduce the probability of a *DoS attack*, since other processes don't know whatis the initial context ID used by findContextID to find a context ID available

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GenerateRandomBytes generate n random bytes

func GetDevicePathAndFsTypeOptions

func GetDevicePathAndFsTypeOptions(mountPoint string) (devicePath, fsType string, fsOptions []string, err error)

GetDevicePathAndFsTypeOptions gets the device for the mount point, the file system type and mount options

func GetSCSIAddress

func GetSCSIAddress(index int) (string, error)

GetSCSIAddress gets scsiID and lun from index, and combined them into a scsi ID

func GetSCSIIdLun

func GetSCSIIdLun(index int) (int, int, error)

GetSCSIIdLun gets the SCSI id and lun, based on the index of the drive being inserted. qemu code suggests that scsi-id can take values from 0 to 255 inclusive, while lun can take values from 0 to 16383 inclusive. But lun values over 255 do not seem to follow consistent SCSI addressing. Hence we limit to 255.

func GetVirtDriveName

func GetVirtDriveName(index int) (string, error)

GetVirtDriveName returns the disk name format for virtio-blk Reference: https://github.com/torvalds/linux/blob/master/drivers/block/virtio_blk.c @c0aa3e0916d7e531e69b02e426f7162dfb1c6c0

func Ioctl

func Ioctl(fd uintptr, request, data uintptr) error

func IsDockerContainer

func IsDockerContainer(spec *specs.Spec) bool

IsDockerContainer returns if the container is managed by docker This is done by checking the prestart hook for `libnetwork` arguments.

func MakeNameID

func MakeNameID(namedType, id string, maxLen int) string

MakeNameID is generic function for creating a named-id for passing on the hypervisor commandline

func MkdirAllWithInheritedOwner

func MkdirAllWithInheritedOwner(path string, perm os.FileMode) error

MkdirAllWithInheritedOwner creates a directory named path, along with any necessary parents. It creates the missing directories with the ownership of the last existing parent. The path needs to be absolute and the method doesn't handle symlink.

func RevertBytes

func RevertBytes(num uint64) uint64

In Cloud Hypervisor, as well as in Firecracker, the crate used by the VMMs accepts the size of rate limiter in scaling factors of 2^10(1024). But in kata-defined rate limiter, for better Human-readability, we prefer scaling factors of 10^3(1000).

func revertBytes reverts num from scaling factors of 1000 to 1024, e.g. 10000000(10MB) to 10485760.

func SupportsIfb

func SupportsIfb() (bool, error)

SupportsIfb returns true if ifb are supported, otherwise false

func SupportsVsocks

func SupportsVsocks() (bool, error)

SupportsVsocks returns true if vsocks are supported, otherwise false

func WaitLocalProcess

func WaitLocalProcess(pid int, timeoutSecs uint, initialSignal syscall.Signal, logger *logrus.Entry) error

WaitLocalProcess waits for the specified process for up to timeoutSecs seconds.

Notes:

  • If the initial signal is zero, the specified process is assumed to be attempting to stop itself.
  • If the initial signal is not zero, it will be sent to the process before checking if it is running.
  • If the process has not ended after the timeout value, it will be forcibly killed.

func WriteToFile

func WriteToFile(path string, data []byte) error

WriteToFile opens a file in write only mode and writes bytes to it

Types

type MemUnit

type MemUnit uint64

func (MemUnit) AlignMem

func (m MemUnit) AlignMem(blockSize MemUnit) MemUnit

AlignMem align memory provided to a block size

func (MemUnit) ToBytes

func (m MemUnit) ToBytes() uint64

func (MemUnit) ToMiB

func (m MemUnit) ToMiB() uint64

type Proc

type Proc struct {
	*procfs.Proc
}

func NewProc

func NewProc(pid int) (*Proc, error)

func (*Proc) Children

func (p *Proc) Children() ([]*Proc, error)

We should try to upstream this but let's keep it until upstream supports it.

Directories

Path Synopsis
copy from https://github.com/containerd/nydus-snapshotter/blob/38b23bcd0658f2bd9d99083d320b727bf73129b7/pkg/utils/retry/retry.go
copy from https://github.com/containerd/nydus-snapshotter/blob/38b23bcd0658f2bd9d99083d320b727bf73129b7/pkg/utils/retry/retry.go

Jump to

Keyboard shortcuts

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