vm

package
v0.0.0-...-683b059 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: BSD-3-Clause Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TerminaComponentName is the name of the Chrome component for the VM kernel and rootfs.
	TerminaComponentName = "cros-termina"

	// TerminaMountDir is a path to the location where we will mount the termina component.
	TerminaMountDir = "/run/imageloader/cros-termina/99999.0.0"

	// ImageServerURLComponentName is the name of the Chrome component for the image server URL.
	ImageServerURLComponentName = "cros-crostini-image-server-url"
)
View Source
const (
	// DefaultVMName is the default crostini VM name.
	DefaultVMName = "termina"
	// DefaultContainerName is the default crostini container name.
	DefaultContainerName = "penguin"
	// DefaultDiskSize is the default disk size for VM. 2.5 GB by default.
	DefaultDiskSize = 5 * 512 * 1024 * 1024 // 2.5 GiB default disk size

)
View Source
const (
	TerminaImage = "/usr/local/cros-termina/image.ext4"
)

The directory where images will be downloaded/extracted to.

Variables

This section is empty.

Functions

func ArchitectureAlias

func ArchitectureAlias(t ContainerDebianVersion) string

ArchitectureAlias returns the alias subpath of the chosen container architecture, i.e. part of the path used to compute the container's gsutil URL.

func Artifact

func Artifact() testing.Precondition

Artifact returns a precondition that Crostini's artifact such as the guest kernel is available before the test runs.

When adding a test with this precondition, the return value of ArtifactData() must be included in Data:

testing.AddTest(&testing.Test{
	...
	Data:  []string{vm.ArtifactData()},
	Pre:   vm.Artifact(),
})

Later, in the main test function, the VM artifacts are available via PreData.

func ArtifactData

func ArtifactData() string

ArtifactData returns the name of the data file that must be specified for tests using the Artifact() precondition.

func CreateVSHCommand

func CreateVSHCommand(ctx context.Context, cid int, command string, args ...string) *testexec.Cmd

CreateVSHCommand creates a command to be run in a VM over vsh. The command parameter is required followed by an optional variatic list of strings as args. The command object is returned.

func DefaultContainerCommand

func DefaultContainerCommand(ctx context.Context, ownerID string, vshArgs ...string) *testexec.Cmd

DefaultContainerCommand returns a testexec.Cmd with a vsh command that will run in the default termina/penguin container.

func DeleteImages

func DeleteImages() error

DeleteImages deletes all images downloaded or extracted for the test by the other functions in this file.

func DownloadStagingTermina

func DownloadStagingTermina(ctx context.Context) (string, error)

DownloadStagingTermina downloads the current staging termina image from Google Storage.

func EnableCrostini

func EnableCrostini(ctx context.Context, tconn *chrome.TestConn) error

EnableCrostini sets the preference for Crostini being enabled as this is required for some of the Chrome integration tests to function properly.

func ExtractTermina

func ExtractTermina(ctx context.Context, artifactPath string) (string, error)

ExtractTermina extracts the termina images from the artifact tarball.

func GetEncodedName

func GetEncodedName(name string) string

GetEncodedName returns the encoded version of the user-chosen name which concierge uses to identify several pieces of the VM (its stateful image, log files, and ssh keys).

func Lock

func Lock()

Lock prevents container creation/destruction until Unlock is called. It can only be called by preconditions and is idempotent.

func MountComponent

func MountComponent(ctx context.Context, image string) error

MountComponent mounts a component image from the provided image path.

func RestartDefaultVMContainer

func RestartDefaultVMContainer(ctx context.Context, dir string, container *Container) error

RestartDefaultVMContainer restarts a VM and container that were previously shut down.

func ShrinkDefaultContainer

func ShrinkDefaultContainer(ctx context.Context, ownerID string) error

ShrinkDefaultContainer deletes a lot of large files in the container to make the image size smaller. This makes a big speed difference on slow devices for backup and restore.

func StopConcierge

func StopConcierge(ctx context.Context) error

StopConcierge stops the vm_concierge service, which stops all running VMs.

func TargetArch

func TargetArch() string

TargetArch returns the name of the VM architecture that should be used

func TerminaImageExists

func TerminaImageExists() bool

TerminaImageExists returns true if TerminaImage file exists and is readable.

func Unlock

func Unlock()

Unlock allows container creation after an earlier call to Lock. It can only be called by preconditions and is idempotent.

func UnmountComponent

func UnmountComponent(ctx context.Context) error

UnmountComponent unmounts any active VM component.

Types

type ComponentType

type ComponentType int

ComponentType represents the VM component type.

const (
	// ComponentUpdater indicates that the live component should be fetched from the component updater service.
	ComponentUpdater ComponentType = iota
	// StagingComponent indicates that the current staging component should be fetched from the GS component testing bucket.
	StagingComponent
)

type Concierge

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

Concierge interacts with the vm_concierge daemon, which starts, stops, and monitors VMs. It also interacts with the cicerone daemon, which interacts with containers inside those VMs.

func GetRunningConcierge

func GetRunningConcierge(ctx context.Context, user string) (*Concierge, error)

GetRunningConcierge returns a concierge instance without restarting concierge service. Returns an error if concierge is not available.

func NewConcierge

func NewConcierge(ctx context.Context, user string) (*Concierge, error)

NewConcierge restarts the vm_concierge service, which stops all running VMs.

func (*Concierge) GetOwnerID

func (c *Concierge) GetOwnerID() string

GetOwnerID returns the cryptohome hash for the logged-in user.

func (*Concierge) GetVMDiskInfo

func (c *Concierge) GetVMDiskInfo(ctx context.Context, vmName string) (*vmpb.VmDiskInfo, error)

GetVMDiskInfo returns a VmDiskInfo proto for the given VM via ListVmDisks

func (*Concierge) SyncTimes

func (c *Concierge) SyncTimes(ctx context.Context) error

SyncTimes runs the SyncVmTimes dbus method in concierge.

type Container

type Container struct {
	// VM is the VM in which this container is running.
	VM *VM
	// contains filtered or unexported fields
}

Container encapsulates a container running in a VM.

func DefaultContainer

func DefaultContainer(ctx context.Context, userEmail string) (*Container, error)

DefaultContainer returns a container object with default settings.

func GetRunningContainer

func GetRunningContainer(ctx context.Context, user string) (*Container, error)

GetRunningContainer returns a Container struct for a currently running container. This is useful when the container was started by some other means, eg the installer. Will return an error if no container is currently running.

func (*Container) CheckFileContent

func (c *Container) CheckFileContent(ctx context.Context, filePath, testString string) error

CheckFileContent checks that the content of the specified file equals to the given string. Returns error if fail to read content or the contest does not equal to the given string.

func (*Container) CheckFileDoesNotExistInDir

func (c *Container) CheckFileDoesNotExistInDir(ctx context.Context, path string, files ...string) error

CheckFileDoesNotExistInDir checks files do not exist in the given path in container. Return error if any file exists or any other error.

func (*Container) CheckFilesExistInDir

func (c *Container) CheckFilesExistInDir(ctx context.Context, path string, files ...string) error

CheckFilesExistInDir checks files exist in the given path in container. Returns error if any file does not exist or any other error.

func (*Container) Cleanup

func (c *Container) Cleanup(ctx context.Context, path string) error

Cleanup removes all the files under the specific path.

func (*Container) Command

func (c *Container) Command(ctx context.Context, vshArgs ...string) *testexec.Cmd

Command returns a testexec.Cmd with a vsh command that will run in this container.

func (*Container) Connect

func (c *Container) Connect(ctx context.Context, user string) error

Connect connects the container to the running VM and cicerone instances.

func (*Container) Create

func (c *Container) Create(ctx context.Context, t ContainerType) error

Create will create a Linux container in an existing VM. It returns without waiting for the creation to complete. One must listen on cicerone D-Bus signals to know the creation is done.

func (*Container) GetFile

func (c *Container) GetFile(ctx context.Context, containerPath, localPath string) error

GetFile copies a remote file from the container's filesystem.

func (*Container) GetFileList

func (c *Container) GetFileList(ctx context.Context, path string) (fileList []string, err error)

GetFileList returns a list of the files in the given path in the container.

func (*Container) GetIPv4Address

func (c *Container) GetIPv4Address(ctx context.Context) (ip string, err error)

GetIPv4Address returns the IPv4 address of the container.

func (*Container) GetUsername

func (c *Container) GetUsername(ctx context.Context) (string, error)

GetUsername returns the default user in a container.

func (*Container) InstallPackage

func (c *Container) InstallPackage(ctx context.Context, path string) error

InstallPackage installs a Linux package file into the container.

func (*Container) LinuxPackageInfo

func (c *Container) LinuxPackageInfo(ctx context.Context, path string) (packageID string, err error)

LinuxPackageInfo queries the container for information about a Linux package file. The packageID returned corresponds to the package ID for an installed package based on the PackageKit specification which is of the form 'package_id;version;arch;repository'.

func (*Container) PushFile

func (c *Container) PushFile(ctx context.Context, localPath, containerPath string) error

PushFile copies a local file to the container's filesystem.

func (*Container) ReadFile

func (c *Container) ReadFile(ctx context.Context, filePath string) (content string, err error)

ReadFile reads the content of file using command cat and returns it as a string.

func (*Container) RemoveAll

func (c *Container) RemoveAll(ctx context.Context, path string) error

RemoveAll removes a path from the container's file system using 'rm -rf'.

func (*Container) SetUpUser

func (c *Container) SetUpUser(ctx context.Context) error

SetUpUser sets up the default user in a container.

func (*Container) StartAndWait

func (c *Container) StartAndWait(ctx context.Context, dir string) error

StartAndWait starts up an already created container and waits for that startup to complete before returning. The directory dir may be used to store logs on failure.

func (*Container) UninstallPackageOwningFile

func (c *Container) UninstallPackageOwningFile(ctx context.Context, desktopFileID string) error

UninstallPackageOwningFile uninstalls the package owning a particular desktop file in the container.

func (*Container) WriteFile

func (c *Container) WriteFile(ctx context.Context, filePath, fileContent string) error

WriteFile creates a file in the container using echo.

type ContainerArch

type ContainerArch string

ContainerArch represents the architecture of the container

const (
	// Amd64 indicates that the container is built for amd64
	Amd64 ContainerArch = "amd64"
	// Arm indicates that the container is build for arm
	Arm ContainerArch = "arm"
)

type ContainerCreationWatcher

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

ContainerCreationWatcher is a wrapper of SignalWatcher to trace container creation progress.

func NewContainerCreationWatcher

func NewContainerCreationWatcher(ctx context.Context, cont *Container) (*ContainerCreationWatcher, error)

NewContainerCreationWatcher returns a ContainerCreationWatcher.

func (*ContainerCreationWatcher) Close

Close cleans up the SignalWatcher.

func (*ContainerCreationWatcher) WaitForCreationComplete

func (c *ContainerCreationWatcher) WaitForCreationComplete(ctx context.Context) error

WaitForCreationComplete waits for the container to be created.

func (*ContainerCreationWatcher) WaitForDownload

func (c *ContainerCreationWatcher) WaitForDownload(ctx context.Context, pct int32) error

WaitForDownload waits for cicerone to send a container download notification. If pct is negative, this method returns after the next notification is received. Otherwise, it returns only after a notification with percent pct in [0, 100] is received. An error is returned if ctx's deadline is reached.

type ContainerDebianVersion

type ContainerDebianVersion string

ContainerDebianVersion represents the OS version of the container's image.

const (
	// DebianBuster refers to the "buster" distribution of debian (a.k.a. debian 10).
	DebianBuster ContainerDebianVersion = "buster"
	// DebianBullseye refers to the "bullseye" distribution of debian (a.k.a. debian 11).
	DebianBullseye ContainerDebianVersion = "bullseye"
)

type ContainerImageType

type ContainerImageType int

ContainerImageType represents the mechanism/bucket that we should use to get the container.

const (
	// LiveImageServer indicates that the current live container image should be downloaded.
	LiveImageServer ContainerImageType = iota
	// StagingImageServer indicates that the current staging container image should be downloaded.
	StagingImageServer
	// Tarball indicates that the container image is available as tarball shared over 9P.
	Tarball
)

type ContainerType

type ContainerType struct {
	// Image is the image source for this container.
	Image ContainerImageType
	// DebianVersion is the version of debian that the image has.
	DebianVersion ContainerDebianVersion
}

ContainerType defines the type of container.

type Crosvm

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

Crosvm holds info about a running instance of the crosvm command.

func NewCrosvm

func NewCrosvm(ctx context.Context, params *CrosvmParams) (*Crosvm, error)

NewCrosvm starts a crosvm instance with the optional disk path as an additional disk.

func (*Crosvm) Close

func (vm *Crosvm) Close(ctx context.Context) error

Close stops the crosvm process (and underlying VM) started by NewCrosvm.

func (*Crosvm) Stdin

func (vm *Crosvm) Stdin() io.Writer

Stdin is attached to the crosvm process's stdin. It can be used to run commands.

func (*Crosvm) Stdout

func (vm *Crosvm) Stdout() io.Reader

Stdout is attached to the crosvm process's stdout. It receives all console output.

func (*Crosvm) WaitForOutput

func (vm *Crosvm) WaitForOutput(ctx context.Context, re *regexp.Regexp) (string, error)

WaitForOutput waits until a line matched by re has been written to stdout, crosvm's stdout is closed, or the deadline is reached. It returns the full line that was matched. This function will consume output from stdout until it returns.

type CrosvmParams

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

CrosvmParams - Parameters for starting a crosvm instance.

func NewCrosvmParams

func NewCrosvmParams(kernel string, opts ...Option) *CrosvmParams

NewCrosvmParams constructs a set of crosvm parameters.

func (*CrosvmParams) ToArgs

func (p *CrosvmParams) ToArgs() []string

ToArgs converts CrosvmParams to an array of strings that can be used as crosvm's command line flags.

type LogReader

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

LogReader keeps a persistent view of the log files created by a VM, and can be used to save them to an output directory for tast.

func NewLogReaderForVM

func NewLogReaderForVM(ctx context.Context, vmName, user string) (*LogReader, error)

NewLogReaderForVM creates a new LogReader which can be used to save the daemon-store logs from a running VM.

func (*LogReader) Close

func (r *LogReader) Close() error

Close closes the underlying syslog.LineReader.

func (*LogReader) TrySaveLogs

func (r *LogReader) TrySaveLogs(ctx context.Context, dir string) error

TrySaveLogs attempts to save the VM logs to the given directory. The logs will be saved in a file called "<vm name>_logs.txt".

type Option

type Option func(s *CrosvmParams)

Option configures a CrosvmParams

func DisableSandbox

func DisableSandbox() Option

DisableSandbox disables the sandbox (sandbox is enabled by default without this option)

func Disks

func Disks(paths ...string) Option

Disks adds paths to disks that will be mounted read only.

func KernelArgs

func KernelArgs(args ...string) Option

KernelArgs sets extra kernel command line arguments.

func RWDisks

func RWDisks(paths ...string) Option

RWDisks adds paths to disks that will be mounted read/write.

func Rootfs

func Rootfs(path string) Option

Rootfs sets a path to the VM rootfs.

func SerialOutput

func SerialOutput(file string) Option

SerialOutput sets a file that serial log will be written.

func SharedDir

func SharedDir(src, tag, fsType, cache string) Option

SharedDir sets a config for directory to be shared with the VM.

func Socket

func Socket(path string) Option

Socket sets a path to the control socket.

func VhostUserNet

func VhostUserNet(socket string) Option

VhostUserNet sets a socket to be used by a vhost-user net device.

type PreData

type PreData struct {
	Kernel string // Path to the guest kernel.
	Rootfs string // Path to the guest rootfs image.
}

The PreData object is made available to users of this precondition via:

func DoSomething(ctx context.Context, s *testing.State) {
	d := s.PreValue().(vm.PreData)
	...
}

type StartupListenerServer

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

StartupListenerServer is struct to manage an instance of a StartupListener gRPC server. It is designed to listen for a single VmReady call from a VM at the provided CID.

func NewStartupListenerServer

func NewStartupListenerServer(vsockPort uint32) (*StartupListenerServer, error)

NewStartupListenerServer accepts a vsock port to listen on as a parameter, and returns a new StatupListenerServer struct. At this point the server has not been started.

func (*StartupListenerServer) Start

func (s *StartupListenerServer) Start() error

Start creates the vsock port listener and starts the gRPC server in a goroutine.

func (*StartupListenerServer) Stop

func (s *StartupListenerServer) Stop()

Stop will stop the gRPC server.

func (*StartupListenerServer) VmReady

VmReady is the implementation of the StartupListenerServer gRPC stub. It sends a signal through an empty channel when a message is received. Note: golint flags this function because Vm is not fully capitalized, however this is due to the proto definition.

func (*StartupListenerServer) WaitReady

func (s *StartupListenerServer) WaitReady(ctx context.Context) error

WaitReady will block until the VmReady call is received. If the message is not received before the context timeout, an error is returned.

type VM

type VM struct {
	// Concierge is the Concierge instance managing this VM.
	Concierge *Concierge

	ContextID int64 // cid for the crosvm process

	EnableGPU bool   // hardware GPU support
	DiskPath  string // the location of the stateful disk
	// contains filtered or unexported fields
}

VM encapsulates a virtual machine managed by the concierge/cicerone daemons.

func GetRunningVM

func GetRunningVM(ctx context.Context, user string) (*VM, error)

GetRunningVM creates a VM struct for the VM that is currently running.

func NewDefaultVM

func NewDefaultVM(c *Concierge, enableGPU bool, diskSize uint64) *VM

NewDefaultVM gets a default VM instance. enableGPU enabled the hardware gpu support for the VM. diskSize set the targeted disk size of the VM.

func (*VM) Command

func (vm *VM) Command(ctx context.Context, vshArgs ...string) *testexec.Cmd

Command returns a testexec.Cmd with a vsh command that will run in this VM.

func (*VM) LXCCommand

func (vm *VM) LXCCommand(ctx context.Context, lxcArgs ...string) error

LXCCommand runs lxc inside the VM with the specified args.

func (*VM) Name

func (vm *VM) Name() string

Name returns the human-readable name of this VM (as opposed to the encoded one from vm.GetEncodedName()).

func (*VM) ShareDownloadsPath

func (vm *VM) ShareDownloadsPath(ctx context.Context, path string, writable bool) (string, error)

ShareDownloadsPath shares a path relative to Downloads with the VM.

func (*VM) Start

func (vm *VM) Start(ctx context.Context) error

Start launches the VM.

func (*VM) StartLxd

func (vm *VM) StartLxd(ctx context.Context) error

StartLxd starts the LXD daemon inside the VM. This is a required step before performing any container operations.

func (*VM) Stop

func (vm *VM) Stop(ctx context.Context) error

Stop shuts down VM. It can be restarted again later.

func (*VM) UnshareDownloadsPath

func (vm *VM) UnshareDownloadsPath(ctx context.Context, path string) error

UnshareDownloadsPath un-shares a path that was previously shared by calling ShareDownloadsPath.

Jump to

Keyboard shortcuts

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