utils

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BootedFrom

func BootedFrom(runner v1.Runner, label string) bool

BootedFrom will check if we are booting from the given label

func CalcFileChecksum

func CalcFileChecksum(fs v1.FS, fileName string) (string, error)

CalcFileChecksum opens the given file and returns the sha256 checksum of it.

func ChrootedCallback

func ChrootedCallback(cfg *v1.Config, path string, bindMounts map[string]string, callback func() error) error

ChrootedCallback runs the given callback in a chroot environment

func ConcatFiles

func ConcatFiles(fs v1.FS, sources []string, target string) (err error)

ConcatFiles Copies source files to target file using Fs interface. Source files are concatenated into target file in the given order. If target is a directory source is copied into that directory using 1st source name file. The result keeps the file mode of the 1st source.

func CopyFile

func CopyFile(fs v1.FS, source string, target string) error

CopyFile Copies source file to target file using Fs interface. If target is directory source is copied into that directory using source name file. File mode is preserved

func CosignVerify

func CosignVerify(fs v1.FS, runner v1.Runner, image string, publicKey string, debug bool) (string, error)

CosignVerify runs a cosign validation for the give image and given public key. If no key is provided then it attempts a keyless validation (experimental feature).

func CreateDirStructure

func CreateDirStructure(fs v1.FS, target string) error

CreateDirStructure creates essentials directories under the root tree that might not be present within a container image (/dev, /run, etc.)

func CreateSquashFS

func CreateSquashFS(runner v1.Runner, logger v1.Logger, source string, destination string, options []string) error

CreateSquashFS creates a squash file at destination from a source, with options TODO: Check validity of source maybe?

func DirSize

func DirSize(fs v1.FS, path string) (int64, error)

DirSize returns the accumulated size of all files in folder. Result in bytes

func DirSizeMB added in v0.2.0

func DirSizeMB(fs v1.FS, path string) (uint, error)

DirSizeMB returns the accumulated size of all files in folder. Result in Megabytes

func Exists

func Exists(fs v1.FS, path string) (bool, error)

Check if a file or directory exists.

func FindFileWithPrefix

func FindFileWithPrefix(fs v1.FS, path string, prefixes ...string) (string, error)

FindFileWithPrefix looks for a file in the given path matching one of the given prefixes. Returns the found file path including the given path. It does not check subfolders recusively

func GetAllPartitions

func GetAllPartitions() (v1.PartitionList, error)

GetAllPartitions returns all partitions in the system for all disks

func GetDeviceByLabel

func GetDeviceByLabel(runner v1.Runner, label string, attempts int) (string, error)

GetDeviceByLabel will try to return the device that matches the given label. attempts value sets the number of attempts to find the device, it waits a second between attempts.

func GetFullDeviceByLabel

func GetFullDeviceByLabel(runner v1.Runner, label string, attempts int) (*v1.Partition, error)

GetFullDeviceByLabel works like GetDeviceByLabel, but it will try to get as much info as possible from the existing partition and return a v1.Partition object

func GetPartitionFS

func GetPartitionFS(partition string) (string, error)

GetPartitionFS gets the FS of a partition given

func GetSource

func GetSource(config *v1.Config, source string, destination string) error

GetSource copies given source to destination, if source is a local path it simply copies files, if source is a remote URL it tries to download URL to destination.

func GetTempDir

func GetTempDir(config *v1.Config, suffix string) string

GetTempDir returns the dir for storing related temporal files It will respect TMPDIR and use that if exists, fallback to try the persistent partition if its mounted and finally the default /tmp/ dir suffix is what is appended to the dir name elemental-suffix. If empty it will randomly generate a number

func IdentifySourceSystem

func IdentifySourceSystem(vfs v1.FS, path string) (string, error)

IdentifySourceSystem tries to find the os-release file in a given dir and identify the system based on the data in there

func IsDir

func IsDir(fs v1.FS, path string) (bool, error)

IsDir check if the path is a dir

func IsHTTPURI

func IsHTTPURI(uri string) (bool, error)

IsHTTPURI returns true if the uri has "http" or "https" scheme, returns false otherwise. Error is not nil only if the url can't be parsed.

func IsLocalURI

func IsLocalURI(uri string) (bool, error)

IsLocalURI returns true if the uri has "file" scheme or no scheme and URI is not prefixed with a domain (container registry style). Returns false otherwise. Error is not nil only if the url can't be parsed.

func IsMounted

func IsMounted(config *v1.Config, part *v1.Partition) (bool, error)

func LoadEnvFile

func LoadEnvFile(fs v1.FS, file string) (map[string]string, error)

LoadEnvFile will try to parse the file given and return a map with the kye/values

func MkdirAll

func MkdirAll(fs v1.FS, name string, mode os.FileMode) (err error)

MkdirAll directory and all parents if not existing

func RawDiskToFixedVhd

func RawDiskToFixedVhd(diskFile *os.File)

RawDiskToFixedVhd will write the proper header to a given os.File to convert it from a simple raw disk to a Fixed VHD RawDiskToFixedVhd makes no effort into opening/closing/checking if the file exists

func Reboot

func Reboot(runner v1.Runner, delay time.Duration) error

Reboot reboots the system afater the given delay (in seconds) time passed.

func RunStage

func RunStage(cfg *v1.Config, stage string, strict bool, cloudInitPaths ...string) error

RunStage will run yip

func Shutdown

func Shutdown(runner v1.Runner, delay time.Duration) error

Shutdown halts the system afater the given delay (in seconds) time passed.

func SyncData

func SyncData(log v1.Logger, fs v1.FS, source string, target string, excludes ...string) error

SyncData rsync's source folder contents to a target folder content, both are expected to exist before hand.

func TempDir

func TempDir(fs v1.FS, dir, prefix string) (name string, err error)

TempDir creates a temp file in the virtual fs Took from afero.FS code and adapted

func TempFile

func TempFile(fs v1.FS, dir, pattern string) (f *os.File, err error)

TempFile creates a temp file in the virtual fs Took from afero.FS code and adapted

func ValidContainerReference

func ValidContainerReference(ref string) bool

ValidContainerReferece returns true if the given string matches a container registry reference, false otherwise

func ValidTaggedContainerReference

func ValidTaggedContainerReference(ref string) bool

ValidTaggedContainerReferece returns true if the given string matches a container registry reference including a tag, false otherwise.

func WalkDirFs

func WalkDirFs(fs v1.FS, root string, fn fs.WalkDirFunc) error

WalkDirFs is the same as filepath.WalkDir but accepts a v1.Fs so it can be run on any v1.Fs type

Types

type Chroot

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

Chroot represents the struct that will allow us to run commands inside a given chroot

func NewChroot

func NewChroot(path string, config *v1.Config) *Chroot

func (*Chroot) Close

func (c *Chroot) Close() error

Close will unmount all active mounts created in Prepare on reverse order

func (*Chroot) Prepare

func (c *Chroot) Prepare() error

Prepare will mount the defaultMounts as bind mounts, to be ready when we run chroot

func (*Chroot) Run

func (c *Chroot) Run(command string, args ...string) (out []byte, err error)

Run executes a command inside a chroot

func (*Chroot) RunCallback

func (c *Chroot) RunCallback(callback func() error) (err error)

RunCallback runs the given callback in a chroot environment

func (*Chroot) SetExtraMounts

func (c *Chroot) SetExtraMounts(extraMounts map[string]string)

Sets additional bind mounts for the chroot enviornment. They are represented in a map where the key is the path outside the chroot and the value is the path inside the chroot.

type CleanJob

type CleanJob func() error

type CleanStack

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

Stack is a basic LIFO stack that resizes as needed.

func NewCleanStack

func NewCleanStack() *CleanStack

NewCleanStack returns a new stack.

func (*CleanStack) Cleanup

func (clean *CleanStack) Cleanup(err error) error

Cleanup runs the whole cleanup stack. In case of error it runs all jobs and returns the first error occurrence.

func (*CleanStack) Pop

func (clean *CleanStack) Pop() CleanJob

Pop removes and returns a node from the stack in last to first order.

func (*CleanStack) Push

func (clean *CleanStack) Push(job CleanJob)

Push adds a node to the stack

type Grub

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

Grub is the struct that will allow us to install grub to the target device

func NewGrub

func NewGrub(config *v1.Config) *Grub

func (Grub) ClearBootEntry

func (g Grub) ClearBootEntry() error

ClearBootEntry will go over the BootXXXX efi vars and remove any that matches our name Used in install as we re-create the partitions, so the UUID of those partitions is no longer valid for the old entry And we don't want to leave a broken entry around

func (Grub) CreateBootEntry

func (g Grub) CreateBootEntry(shimName string, relativeTo string, efiVariables eleefi.Variables) error

CreateBootEntry will create an entry in the efi vars for our shim and set it to boot first in the bootorder

func (Grub) DoEFIEntries added in v0.2.1

func (g Grub) DoEFIEntries(shimName, efiDir string, clearBootEntries bool) error

DoEFIEntries creates clears any previous entry if requested and creates a new one with the given shim name.

func (Grub) Install

func (g Grub) Install(target, rootDir, bootDir, grubConf string, efi bool, stateLabel string, disableBootEntry bool, clearBootEntries bool) (err error)

Install installs grub into the device, copy the config file and add any extra TTY to grub

func (Grub) InstallBIOS added in v0.2.1

func (g Grub) InstallBIOS(target, rootDir, bootDir string) error

InstallBIOS runs grub2-install for legacy BIOS firmware

func (Grub) InstallConfig added in v0.2.1

func (g Grub) InstallConfig(rootDir, bootDir, grubConf string) error

InstallConfig installs grub configuraton files to the expected location. rootDir is the root of the OS image, bootDir is the folder grub read the configuration from, usually state partition mountpoint

func (Grub) InstallEFI added in v0.2.1

func (g Grub) InstallEFI(rootDir, bootDir, efiDir, deviceLabel string) (string, error)

InstallEFI installs EFI binaries into the EFI location

func (Grub) SetPersistentVariables

func (g Grub) SetPersistentVariables(grubEnvFile string, vars map[string]string) error

Sets the given key value pairs into as grub variables into the given file

type VHDHeader

type VHDHeader struct {
	Cookie   [8]byte // Cookies are used to uniquely identify the original creator of the hard disk image
	Features [4]byte // This is a bit field used to indicate specific feature support.
	// Can be 0x00000000 (no features), 0x00000001 (Temporary, candidate for deletion on shutdown) or 0x00000002 (Reserved)
	FileFormatVersion  [4]byte   // Divided into a major/minor version and matches the version of the specification used in creating the file.
	DataOffset         [8]byte   // For fixed disks, this field should be set to 0xFFFFFFFF.
	Timestamp          [4]byte   // Sstores the creation time of a hard disk image. This is the number of seconds since January 1, 2000 12:00:00 AM in UTC/GMT.
	CreatorApplication [4]byte   // Used to document which application created the hard disk.
	CreatorVersion     [4]byte   // This field holds the major/minor version of the application that created the hard disk image.
	CreatorHostOS      [4]byte   // This field stores the type of host operating system this disk image is created on.
	OriginalSize       [8]byte   // This field stores the size of the hard disk in bytes, from the perspective of the virtual machine, at creation time. Info only
	CurrentSize        [8]byte   // This field stores the current size of the hard disk, in bytes, from the perspective of the virtual machine.
	DiskGeometry       [4]byte   // This field stores the cylinder, heads, and sectors per track value for the hard disk.
	DiskType           [4]byte   // Fixed = 2, Dynamic = 3, Differencing = 4
	Checksum           [4]byte   // This field holds a basic checksum of the hard disk footer. It is just a one’s complement of the sum of all the bytes in the footer without the checksum field.
	UniqueID           [16]byte  // This is a 128-bit universally unique identifier (UUID).
	SavedState         [1]byte   // This field holds a one-byte flag that describes whether the system is in saved state. If the hard disk is in the saved state the value is set to 1
	Reserved           [427]byte // This field contains zeroes.
}

Jump to

Keyboard shortcuts

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