utils

package
v0.0.0-...-625f738 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: Apache-2.0, Apache-2.0, Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Byte MemUnit = 1
	KiB          = Byte << 10
	MiB          = KiB << 10
	GiB          = MiB << 10
)
View Source
const LoggerBinaryName = "systemd-cat"

Binary to use to log program output

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 CalculateMilliCPUs

func CalculateMilliCPUs(quota int64, period uint64) uint32

CalculateMilliCPUs converts CPU quota and period to milli-CPUs

func CalculateVCpusFromMilliCpus

func CalculateVCpusFromMilliCpus(mCPU uint32) uint32

CalculateVCpusFromMilliCpus converts from mCPU to CPU, taking the ceiling value when necessary

func CleanupFds

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

CleanupFds closed bundles of open fds in batch

func ConstraintsToVCPUs

func ConstraintsToVCPUs(quota int64, period uint64) uint

ConstraintsToVCPUs converts CPU quota and period to vCPUs

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 GetDevicePathAndFsType

func GetDevicePathAndFsType(mountPoint string) (devicePath, fsType string, err error)

GetDevicePathAndFsType gets the device for the mount point and the file system type of the mount.

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 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 ReverseString

func ReverseString(s string) string

ReverseString reverses whole string

func SupportsVsocks

func SupportsVsocks() bool

SupportsVsocks returns true if vsocks are supported, otherwise false

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.

type ProgramLogger

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

func NewProgramLogger

func NewProgramLogger(loggerLabel string) ProgramLogger

func (*ProgramLogger) StartLogger

func (p *ProgramLogger) StartLogger(output io.ReadCloser) error

func (ProgramLogger) String

func (p ProgramLogger) String() string

Jump to

Keyboard shortcuts

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