Documentation ¶
Overview ¶
Package lxc provides Go Bindings for LXC (Linux Containers) C API.
LXC (LinuX Containers) is an operating system–level virtualization method for running multiple isolated Linux systems (containers) on a single control host.
LXC combines cgroups and namespace support to provide an isolated environment for applications.
Index ¶
- Constants
- Variables
- func Acquire(c *Container) bool
- func ActiveContainerNames(lxcpath ...string) []string
- func ContainerNames(lxcpath ...string) []string
- func DefaultConfigPath() string
- func DefaultLvmVg() string
- func DefaultZfsRoot() string
- func DefinedContainerNames(lxcpath ...string) []string
- func GlobalConfigItem(name string) string
- func NewError(msg string) error
- func Release(c *Container) bool
- func Version() string
- func VersionAtLeast(major int, minor int, micro int) bool
- func VersionNumber() (major int, minor int)
- type AttachOptions
- type BackendStore
- type ByteSize
- type CheckpointOptions
- type CloneOptions
- type ConsoleOptions
- type Container
- func (c *Container) AddDeviceNode(source string, destination ...string) error
- func (c *Container) AttachInterface(source, destination string) error
- func (c *Container) AttachShell(options AttachOptions) error
- func (c *Container) BlkioUsage() (ByteSize, error)
- func (c *Container) CPUStats() (map[string]int64, error)
- func (c *Container) CPUTime() (time.Duration, error)
- func (c *Container) CPUTimePerCPU() (map[int]time.Duration, error)
- func (c *Container) CgroupItem(key string) []string
- func (c *Container) Checkpoint(opts CheckpointOptions) error
- func (c *Container) ClearConfig()
- func (c *Container) ClearConfigItem(key string) error
- func (c *Container) Clone(name string, options CloneOptions) error
- func (c *Container) ConfigFileName() string
- func (c *Container) ConfigItem(key string) []string
- func (c *Container) ConfigKeys(key ...string) []string
- func (c *Container) ConfigPath() string
- func (c *Container) Console(options ConsoleOptions) error
- func (c *Container) ConsoleFd(ttynum int) (int, error)
- func (c *Container) Controllable() bool
- func (c *Container) Create(options TemplateOptions) error
- func (c *Container) CreateSnapshot() (*Snapshot, error)
- func (c *Container) Daemonize() bool
- func (c *Container) Defined() bool
- func (c *Container) Destroy() error
- func (c *Container) DestroyAllSnapshots() error
- func (c *Container) DestroySnapshot(snapshot Snapshot) error
- func (c *Container) DestroyWithAllSnapshots() error
- func (c *Container) DetachInterface(source string) error
- func (c *Container) DetachInterfaceRename(source, target string) error
- func (c *Container) Execute(args ...string) ([]byte, error)
- func (c *Container) Freeze() error
- func (c *Container) IPAddress(interfaceName string) ([]string, error)
- func (c *Container) IPAddresses() ([]string, error)
- func (c *Container) IPv4Address(interfaceName string) ([]string, error)
- func (c *Container) IPv4Addresses() ([]string, error)
- func (c *Container) IPv6Address(interfaceName string) ([]string, error)
- func (c *Container) IPv6Addresses() ([]string, error)
- func (c *Container) InitPid() int
- func (c *Container) InterfaceStats() (map[string]map[string]ByteSize, error)
- func (c *Container) Interfaces() ([]string, error)
- func (c *Container) KernelMemoryLimit() (ByteSize, error)
- func (c *Container) KernelMemoryUsage() (ByteSize, error)
- func (c *Container) LoadConfigFile(path string) error
- func (c *Container) LogFile() string
- func (c *Container) LogLevel() LogLevel
- func (c *Container) MemoryLimit() (ByteSize, error)
- func (c *Container) MemorySwapLimit() (ByteSize, error)
- func (c *Container) MemorySwapUsage() (ByteSize, error)
- func (c *Container) MemoryUsage() (ByteSize, error)
- func (c *Container) Migrate(cmd uint, opts MigrateOptions) error
- func (c *Container) Name() string
- func (c *Container) Reboot() error
- func (c *Container) RemoveDeviceNode(source string, destination ...string) error
- func (c *Container) Rename(name string) error
- func (c *Container) Restore(opts RestoreOptions) error
- func (c *Container) RestoreSnapshot(snapshot Snapshot, name string) error
- func (c *Container) RunCommand(args []string, options AttachOptions) (bool, error)
- func (c *Container) RunCommandStatus(args []string, options AttachOptions) (int, error)
- func (c *Container) Running() bool
- func (c *Container) RunningConfigItem(key string) []string
- func (c *Container) SaveConfigFile(path string) error
- func (c *Container) SetCgroupItem(key string, value string) error
- func (c *Container) SetConfigItem(key string, value string) error
- func (c *Container) SetConfigPath(path string) error
- func (c *Container) SetKernelMemoryLimit(limit ByteSize) error
- func (c *Container) SetLogFile(filename string) error
- func (c *Container) SetLogLevel(level LogLevel) error
- func (c *Container) SetMemoryLimit(limit ByteSize) error
- func (c *Container) SetMemorySwapLimit(limit ByteSize) error
- func (c *Container) SetSoftMemoryLimit(limit ByteSize) error
- func (c *Container) SetVerbosity(verbosity Verbosity)
- func (c *Container) Shutdown(timeout time.Duration) error
- func (c *Container) Snapshots() ([]Snapshot, error)
- func (c *Container) SoftMemoryLimit() (ByteSize, error)
- func (c *Container) Start() error
- func (c *Container) State() State
- func (c *Container) Stop() error
- func (c *Container) Unfreeze() error
- func (c *Container) Wait(state State, timeout time.Duration) bool
- func (c *Container) WaitIPAddresses(timeout time.Duration) ([]string, error)
- func (c *Container) WantCloseAllFds(state bool) error
- func (c *Container) WantDaemonize(state bool) error
- type Error
- type LogLevel
- type MigrateOptions
- type Personality
- type RestoreOptions
- type Snapshot
- type State
- type TemplateOptions
- type Verbosity
Constants ¶
const ( MIGRATE_PRE_DUMP = 0 MIGRATE_DUMP = 1 MIGRATE_RESTORE = 2 )
Variables ¶
var ( ErrAddDeviceNodeFailed = NewError("adding device to container failed") ErrAllocationFailed = NewError("allocating memory failed") ErrAlreadyDefined = NewError("container already defined") ErrAlreadyFrozen = NewError("container is already frozen") ErrAlreadyRunning = NewError("container is already running") ErrAttachFailed = NewError("attaching to the container failed") ErrAttachInterfaceFailed = NewError("attaching specified netdev to the container failed") ErrBlkioUsage = NewError("BlkioUsage for the container failed") ErrCheckpointFailed = NewError("checkpoint failed") ErrClearingCgroupItemFailed = NewError("clearing cgroup item for the container failed") ErrCloneFailed = NewError("cloning the container failed") ErrCloseAllFdsFailed = NewError("setting close_all_fds flag for container failed") ErrCreateFailed = NewError("creating the container failed") ErrCreateSnapshotFailed = NewError("snapshotting the container failed") ErrDaemonizeFailed = NewError("setting daemonize flag for container failed") ErrDestroyAllSnapshotsFailed = NewError("destroying all snapshots failed") ErrDestroyFailed = NewError("destroying the container failed") ErrDestroySnapshotFailed = NewError("destroying the snapshot failed") ErrDestroyWithAllSnapshotsFailed = NewError("destroying the container with all snapshots failed") ErrDetachInterfaceFailed = NewError("detaching specified netdev to the container failed") ErrExecuteFailed = NewError("executing the command in a temporary container failed") ErrFreezeFailed = NewError("freezing the container failed") ErrInsufficientNumberOfArguments = NewError("insufficient number of arguments were supplied") ErrInterfaces = NewError("getting interface names for the container failed") ErrIPAddresses = NewError("getting IP addresses of the container failed") ErrIPAddress = NewError("getting IP address on the interface of the container failed") ErrIPv4Addresses = NewError("getting IPv4 addresses of the container failed") ErrIPv6Addresses = NewError("getting IPv6 addresses of the container failed") ErrKMemLimit = NewError("your kernel does not support cgroup kernel memory controller") ErrLoadConfigFailed = NewError("loading config file for the container failed") ErrMemLimit = NewError("your kernel does not support cgroup memory controller") ErrMemorySwapLimit = NewError("your kernel does not support cgroup swap controller") ErrMethodNotAllowed = NewError("the requested method is not currently supported with unprivileged containers") ErrNewFailed = NewError("allocating the container failed") ErrNoSnapshot = NewError("container has no snapshot") ErrNotDefined = NewError("container is not defined") ErrNotFrozen = NewError("container is not frozen") ErrNotRunning = NewError("container is not running") ErrNotSupported = NewError("method is not supported by this LXC version") ErrRebootFailed = NewError("rebooting the container failed") ErrRemoveDeviceNodeFailed = NewError("removing device from container failed") ErrRenameFailed = NewError("renaming the container failed") ErrRestoreFailed = NewError("restore failed") ErrRestoreSnapshotFailed = NewError("restoring the container failed") ErrSaveConfigFailed = NewError("saving config file for the container failed") ErrSettingCgroupItemFailed = NewError("setting cgroup item for the container failed") ErrSettingConfigItemFailed = NewError("setting config item for the container failed") ErrSettingConfigPathFailed = NewError("setting config file for the container failed") ErrSettingKMemoryLimitFailed = NewError("setting kernel memory limit for the container failed") ErrSettingMemoryLimitFailed = NewError("setting memory limit for the container failed") ErrSettingMemorySwapLimitFailed = NewError("setting memory+swap limit for the container failed") ErrSettingSoftMemoryLimitFailed = NewError("setting soft memory limit for the container failed") ErrShutdownFailed = NewError("shutting down the container failed") ErrSoftMemLimit = NewError("your kernel does not support cgroup memory controller") ErrStartFailed = NewError("starting the container failed") ErrStopFailed = NewError("stopping the container failed") ErrTemplateNotAllowed = NewError("unprivileged users only allowed to use \"download\" template") ErrUnfreezeFailed = NewError("unfreezing the container failed") ErrUnknownBackendStore = NewError("unknown backend type") )
var BusyboxTemplateOptions = TemplateOptions{
Template: "busybox",
}
BusyboxTemplateOptions is a convenient set of options for "busybox" template.
var DefaultAttachOptions = AttachOptions{ Namespaces: -1, Arch: -1, Cwd: "/", UID: -1, GID: -1, ClearEnv: false, Env: nil, EnvToKeep: nil, StdinFd: os.Stdin.Fd(), StdoutFd: os.Stdout.Fd(), StderrFd: os.Stderr.Fd(), }
DefaultAttachOptions is a convenient set of options to be used.
var DefaultCloneOptions = CloneOptions{ Backend: Directory, }
DefaultCloneOptions is a convenient set of options to be used.
var DefaultConsoleOptions = ConsoleOptions{ Tty: -1, StdinFd: os.Stdin.Fd(), StdoutFd: os.Stdout.Fd(), StderrFd: os.Stderr.Fd(), EscapeCharacter: 'a', }
DefailtConsoleOptions is a convenient set of options to be used.
var DownloadTemplateOptions = TemplateOptions{
Template: "download",
Distro: "ubuntu",
Release: "trusty",
Arch: "amd64",
}
DownloadTemplateOptions is a convenient set of options for "download" template.
var StateMap = map[string]State{ "STOPPED": STOPPED, "STARTING": STARTING, "RUNNING": RUNNING, "STOPPING": STOPPING, "ABORTING": ABORTING, "FREEZING": FREEZING, "FROZEN": FROZEN, "THAWED": THAWED, }
StateMap provides the mapping betweens the state names and states
var UbuntuTemplateOptions = TemplateOptions{
Template: "ubuntu",
}
UbuntuTemplateOptions is a convenient set of options for "ubuntu" template.
Functions ¶
func ActiveContainerNames ¶
ActiveContainerNames returns the names of the active containers on the system.
func ContainerNames ¶
ContainerNames returns the names of defined and active containers on the system.
func DefaultConfigPath ¶
func DefaultConfigPath() string
DefaultConfigPath returns default config path.
func DefaultLvmVg ¶
func DefaultLvmVg() string
DefaultLvmVg returns the name of the default LVM volume group.
func DefaultZfsRoot ¶
func DefaultZfsRoot() string
DefaultZfsRoot returns the name of the default ZFS root.
func DefinedContainerNames ¶
DefinedContainerNames returns the names of the defined containers on the system.
func GlobalConfigItem ¶
GlobalConfigItem returns the value of the given global config key.
func VersionNumber ¶
Types ¶
type AttachOptions ¶
type AttachOptions struct { // Specify the namespaces to attach to, as OR'ed list of clone flags (syscall.CLONE_NEWNS | syscall.CLONE_NEWUTS ...). Namespaces int // Specify the architecture which the kernel should appear to be running as to the command executed. Arch Personality // Cwd specifies the working directory of the command. Cwd string // UID specifies the user id to run as. UID int // GID specifies the group id to run as. GID int // If ClearEnv is true the environment is cleared before running the command. ClearEnv bool // Env specifies the environment of the process. Env []string // EnvToKeep specifies the environment of the process when ClearEnv is true. EnvToKeep []string // StdinFd specifies the fd to read input from. StdinFd uintptr // StdoutFd specifies the fd to write output to. StdoutFd uintptr // StderrFd specifies the fd to write error output to. StderrFd uintptr }
AttachOptions type is used for defining various attach options.
type BackendStore ¶
type BackendStore int
BackendStore type specifies possible backend types.
const ( // Btrfs backendstore type Btrfs BackendStore = iota + 1 // Directory backendstore type Directory // LVM backendstore type LVM // ZFS backendstore type ZFS // Aufs backendstore type Aufs // Overlayfs backendstore type Overlayfs // Loopback backendstore type Loopback // Best backendstore type Best )
func (*BackendStore) Set ¶
func (t *BackendStore) Set(value string) error
Set is the method to set the flag value, part of the flag.Value interface.
type CheckpointOptions ¶
CheckpointOptions type is used for defining checkpoint options for CRIU
type CloneOptions ¶
type CloneOptions struct { // Backend specifies the type of the backend. Backend BackendStore // lxcpath in which to create the new container. If not set the original container's lxcpath will be used. ConfigPath string // Do not change the hostname of the container (in the root filesystem). KeepName bool // Use the same MAC address as the original container, rather than generating a new random one. KeepMAC bool // Create a snapshot rather than copy. Snapshot bool }
CloneOptions type is used for defining various clone options.
type ConsoleOptions ¶
type ConsoleOptions struct { // Tty number to attempt to allocate, -1 to allocate the first available tty, or 0 to allocate the console. Tty int // StdinFd specifies the fd to read input from. StdinFd uintptr // StdoutFd specifies the fd to write output to. StdoutFd uintptr // StderrFd specifies the fd to write error output to. StderrFd uintptr // EscapeCharacter (a means <Ctrl a>, b maens <Ctrl b>). EscapeCharacter rune }
ConsoleOptions type is used for defining various console options.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container struct
func ActiveContainers ¶
ActiveContainers returns the active containers on the system. Only containers that could retrieved successfully are returned.
func Containers ¶
Containers returns the defined and active containers on the system. Only containers that could retrieved successfully are returned.
func DefinedContainers ¶
DefinedContainers returns the defined containers on the system. Only containers that could retrieved successfully are returned.
func NewContainer ¶
NewContainer returns a new container struct.
func (*Container) AddDeviceNode ¶
AddDeviceNode adds specified device to the container.
func (*Container) AttachInterface ¶
AttachInterface attaches specifed netdev to the container.
func (*Container) AttachShell ¶
func (c *Container) AttachShell(options AttachOptions) error
AttachShell attaches a shell to the container. It clears all environment variables before attaching.
func (*Container) BlkioUsage ¶
BlkioUsage returns number of bytes transferred to/from the disk by the container.
func (*Container) CPUStats ¶
CPUStats returns the number of CPU cycles (in the units defined by USER_HZ on the system) consumed by tasks in this cgroup and its children in both user mode and system (kernel) mode.
func (*Container) CPUTime ¶
CPUTime returns the total CPU time (in nanoseconds) consumed by all tasks in this cgroup (including tasks lower in the hierarchy).
func (*Container) CPUTimePerCPU ¶
CPUTimePerCPU returns the CPU time (in nanoseconds) consumed on each CPU by all tasks in this cgroup (including tasks lower in the hierarchy).
func (*Container) CgroupItem ¶
CgroupItem returns the value of the given cgroup subsystem value.
func (*Container) Checkpoint ¶
func (c *Container) Checkpoint(opts CheckpointOptions) error
Checkpoint checkpoints the container.
func (*Container) ClearConfig ¶
func (c *Container) ClearConfig()
ClearConfig completely clears the containers in-memory configuration.
func (*Container) ClearConfigItem ¶
ClearConfigItem clears the value of given config item.
func (*Container) Clone ¶
func (c *Container) Clone(name string, options CloneOptions) error
Clone clones the container using given arguments with specified backend.
func (*Container) ConfigFileName ¶
ConfigFileName returns the container's configuration file's name.
func (*Container) ConfigItem ¶
ConfigItem returns the value of the given config item.
func (*Container) ConfigKeys ¶
ConfigKeys returns the names of the config items.
func (*Container) ConfigPath ¶
ConfigPath returns the configuration file's path.
func (*Container) Console ¶
func (c *Container) Console(options ConsoleOptions) error
Console allocates and runs a console tty from container
This function will not return until the console has been exited by the user.
func (*Container) ConsoleFd ¶
ConsoleFd allocates a console tty from container ttynum: tty number to attempt to allocate or -1 to allocate the first available tty
Returns "ttyfd" on success, -1 on failure. The returned "ttyfd" is used to keep the tty allocated. The caller should close "ttyfd" to indicate that it is done with the allocated console so that it can be allocated by another caller.
func (*Container) Controllable ¶
Controllable returns true if the caller can control the container.
func (*Container) Create ¶
func (c *Container) Create(options TemplateOptions) error
Create creates the container using given TemplateOptions
func (*Container) CreateSnapshot ¶
CreateSnapshot creates a new snapshot.
func (*Container) DestroyAllSnapshots ¶
DestroyAllSnapshots destroys all the snapshot.
func (*Container) DestroySnapshot ¶
DestroySnapshot destroys the specified snapshot.
func (*Container) DestroyWithAllSnapshots ¶
DestroyWithAllSnapshots destroys the container and its snapshots
func (*Container) DetachInterface ¶
DetachInterface detaches specifed netdev from the container.
func (*Container) DetachInterfaceRename ¶
DetachInterfaceRename detaches specifed netdev from the container and renames it.
func (*Container) IPAddresses ¶
IPAddresses returns all IP addresses.
func (*Container) IPv4Address ¶
IPv4Address returns the IPv4 address of the given network interface.
func (*Container) IPv4Addresses ¶
IPv4Addresses returns all IPv4 addresses.
func (*Container) IPv6Address ¶
IPv6Address returns the IPv6 address of the given network interface.
func (*Container) IPv6Addresses ¶
IPv6Addresses returns all IPv6 addresses.
func (*Container) InitPid ¶
InitPid returns the process ID of the container's init process seen from outside the container.
func (*Container) InterfaceStats ¶
InterfaceStats returns the stats about container's network interfaces
func (*Container) Interfaces ¶
Interfaces returns the names of the network interfaces.
func (*Container) KernelMemoryLimit ¶
KernelMemoryLimit returns kernel memory limit of the container in bytes.
func (*Container) KernelMemoryUsage ¶
KernelMemoryUsage returns current kernel memory allocation of the container in bytes.
func (*Container) LoadConfigFile ¶
LoadConfigFile loads the configuration file from given path.
func (*Container) MemoryLimit ¶
MemoryLimit returns memory limit of the container in bytes.
func (*Container) MemorySwapLimit ¶
MemorySwapLimit returns the memory+swap limit of the container in bytes.
func (*Container) MemorySwapUsage ¶
MemorySwapUsage returns memory+swap usage of the container in bytes.
func (*Container) MemoryUsage ¶
MemoryUsage returns memory usage of the container in bytes.
func (*Container) RemoveDeviceNode ¶
RemoveDeviceNode removes the specified device from the container.
func (*Container) Restore ¶
func (c *Container) Restore(opts RestoreOptions) error
Restore restores the container from a checkpoint.
func (*Container) RestoreSnapshot ¶
RestoreSnapshot creates a new container based on a snapshot.
func (*Container) RunCommand ¶
func (c *Container) RunCommand(args []string, options AttachOptions) (bool, error)
RunCommand attachs a shell and runs the command within the container. The process will wait for the command to finish and return a success status. An error is returned only when invocation of the command completely fails.
func (*Container) RunCommandStatus ¶
func (c *Container) RunCommandStatus(args []string, options AttachOptions) (int, error)
RunCommandStatus attachs a shell and runs the command within the container. The process will wait for the command to finish and return the result of waitpid(), i.e. the process' exit status. An error is returned only when invocation of the command completely fails.
func (*Container) RunningConfigItem ¶
RunningConfigItem returns the value of the given config item.
func (*Container) SaveConfigFile ¶
SaveConfigFile saves the configuration file to given path.
func (*Container) SetCgroupItem ¶
SetCgroupItem sets the value of given cgroup subsystem value.
func (*Container) SetConfigItem ¶
SetConfigItem sets the value of the given config item.
func (*Container) SetConfigPath ¶
SetConfigPath sets the configuration file's path.
func (*Container) SetKernelMemoryLimit ¶
SetKernelMemoryLimit sets kernel memory limit of the container in bytes.
func (*Container) SetLogFile ¶
SetLogFile sets the name of the logfile.
func (*Container) SetLogLevel ¶
SetLogLevel sets the level of the logfile.
func (*Container) SetMemoryLimit ¶
SetMemoryLimit sets memory limit of the container in bytes.
func (*Container) SetMemorySwapLimit ¶
SetMemorySwapLimit sets memory+swap limit of the container in bytes.
func (*Container) SetSoftMemoryLimit ¶
SetSoftMemoryLimit sets soft memory limit of the container in bytes.
func (*Container) SetVerbosity ¶
SetVerbosity sets the verbosity level of some API calls
func (*Container) SoftMemoryLimit ¶
SoftMemoryLimit returns soft memory limit of the container in bytes.
func (*Container) WaitIPAddresses ¶
WaitIPAddresses waits until IPAddresses call returns something or time outs
func (*Container) WantCloseAllFds ¶
WantCloseAllFds determines whether container wishes all file descriptors to be closed on startup.
func (*Container) WantDaemonize ¶
WantDaemonize determines if the container wants to run daemonized.
type Error ¶
type Error struct {
Message string
}
Error represents a basic error that implies the error interface.
type MigrateOptions ¶
type Personality ¶
type Personality int64
Personality allows to set the architecture for the container.
const ( X86 Personality = 0x0008 X86_64 = 0x0000 )
type RestoreOptions ¶
RestoreOptions type is used for defining restore options for CRIU
type State ¶
type State int
State type specifies possible container states.
const ( // STOPPED means container is not running STOPPED State = iota + 1 // STARTING means container is starting STARTING // RUNNING means container is running RUNNING // STOPPING means container is stopping STOPPING // ABORTING means container is aborting ABORTING // FREEZING means container is freezing FREEZING // FROZEN means containe is frozen FROZEN // THAWED means container is thawed THAWED )
type TemplateOptions ¶
type TemplateOptions struct { // Template specifies the name of the template. Template string // Backend specifies the type of the backend. Backend BackendStore // Distro specifies the name of the distribution. Distro string // Release specifies the name/version of the distribution. Release string // Arch specified the architecture of the container. Arch string // Variant specifies the variant of the image (default: "default"). Variant string // Image server (default: "images.linuxcontainers.org"). Server string // GPG keyid (default: 0x...). KeyID string // GPG keyserver to use. KeyServer string // Disable GPG validation (not recommended). DisableGPGValidation bool // Flush the local copy (if present). FlushCache bool // Force the use of the local copy even if expired. ForceCache bool // ExtraArgs provides a way to specify template specific args. ExtraArgs []string }
TemplateOptions type is used for defining various template options.