1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
package instance
import (
"context"
"crypto/x509"
"io"
"net"
"os"
"time"
liblxc "gopkg.in/lxc/go-lxc.v2"
"github.com/pkg/sftp"
"google.golang.org/protobuf/proto"
"github.com/lxc/incus/v6/internal/server/backup"
"github.com/lxc/incus/v6/internal/server/cgroup"
"github.com/lxc/incus/v6/internal/server/db"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/instance/operationlock"
"github.com/lxc/incus/v6/internal/server/metrics"
"github.com/lxc/incus/v6/internal/server/operations"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/idmap"
"github.com/lxc/incus/v6/shared/ioprogress"
)
// HookStart hook used when instance has started.
const HookStart = "onstart"
// HookStopNS hook used when instance has stopped but before namespaces have been destroyed.
const HookStopNS = "onstopns"
// HookStop hook used when instance has stopped.
const HookStop = "onstop"
// Possible values for the protocol argument of the Instance.Console() method.
const (
ConsoleTypeConsole = "console"
ConsoleTypeVGA = "vga"
)
// TemplateTrigger trigger name.
type TemplateTrigger string
// TemplateTriggerCreate for when an instance is created.
const TemplateTriggerCreate TemplateTrigger = "create"
// TemplateTriggerCopy for when an instance is copied.
const TemplateTriggerCopy TemplateTrigger = "copy"
// TemplateTriggerRename for when an instance is renamed.
const TemplateTriggerRename TemplateTrigger = "rename"
// PowerStateRunning represents the power state stored when an instance is running.
const PowerStateRunning = "RUNNING"
// PowerStateStopped represents the power state stored when an instance is stopped.
const PowerStateStopped = "STOPPED"
// ConfigReader is used to read instance config.
type ConfigReader interface {
Project() api.Project
Type() instancetype.Type
Architecture() int
ID() int
Name() string
ExpandedConfig() map[string]string
ExpandedDevices() deviceConfig.Devices
LocalConfig() map[string]string
LocalDevices() deviceConfig.Devices
}
// Instance interface.
type Instance interface {
ConfigReader
// Instance actions.
Freeze() error
Shutdown(timeout time.Duration) error
Start(stateful bool) error
Stop(stateful bool) error
Restart(timeout time.Duration) error
Rebuild(img *api.Image, op *operations.Operation) error
Unfreeze() error
ReloadDevice(devName string) error
RegisterDevices()
Info() Info
IsPrivileged() bool
// Snapshots & migration & backups.
Restore(source Instance, stateful bool) error
Snapshot(name string, expiry time.Time, stateful bool) error
Snapshots() ([]Instance, error)
Backups() ([]backup.InstanceBackup, error)
UpdateBackupFile() error
CanLiveMigrate() bool
// Config handling.
Rename(newName string, applyTemplateTrigger bool) error
Update(newConfig db.InstanceArgs, userRequested bool) error
Delete(force bool) error
Export(meta io.Writer, roofs io.Writer, properties map[string]string, expiration time.Time, tracker *ioprogress.ProgressTracker) (*api.ImageMetadata, error)
// Live configuration.
CGroup() (*cgroup.CGroup, error)
VolatileSet(changes map[string]string) error
// File handling.
FileSFTPConn() (net.Conn, error)
FileSFTP() (*sftp.Client, error)
// Console - Allocate and run a console tty or a spice Unix socket.
Console(protocol string) (*os.File, chan error, error)
Exec(req api.InstanceExecPost, stdin *os.File, stdout *os.File, stderr *os.File) (Cmd, error)
// Status
Render() (any, any, error)
RenderWithUsage() (any, any, error)
RenderFull(hostInterfaces []net.Interface) (*api.InstanceFull, any, error)
RenderState(hostInterfaces []net.Interface) (*api.InstanceState, error)
IsRunning() bool
IsFrozen() bool
IsEphemeral() bool
IsSnapshot() bool
IsStateful() bool
LockExclusive() (*operationlock.InstanceOperation, error)
// Hooks.
DeviceEventHandler(*deviceConfig.RunConfig) error
OnHook(hookName string, args map[string]string) error
// Properties.
Location() string
CloudInitID() string
Description() string
CreationDate() time.Time
LastUsedDate() time.Time
Profiles() []api.Profile
InitPID() int
State() string
ExpiryDate() time.Time
FillNetworkDevice(name string, m deviceConfig.Device) (deviceConfig.Device, error)
ETag() []any
// Paths.
Path() string
ExecOutputPath() string
RootfsPath() string
TemplatesPath() string
StatePath() string
LogFilePath() string
ConsoleBufferLogPath() string
LogPath() string
RunPath() string
DevicesPath() string
// Storage.
StoragePool() (string, error)
// Migration.
CanMigrate() string
MigrateSend(args MigrateSendArgs) error
MigrateReceive(args MigrateReceiveArgs) error
// Progress reporting.
SetOperation(op *operations.Operation)
Operation() *operations.Operation
DeferTemplateApply(trigger TemplateTrigger) error
Metrics(hostInterfaces []net.Interface) (*metrics.MetricSet, error)
}
// Container interface is for container specific functions.
type Container interface {
Instance
CurrentIdmap() (*idmap.Set, error)
DiskIdmap() (*idmap.Set, error)
NextIdmap() (*idmap.Set, error)
ConsoleLog(opts liblxc.ConsoleLogOptions) (string, error)
InsertSeccompUnixDevice(prefix string, m deviceConfig.Device, pid int) error
DevptsFd() (*os.File, error)
IdmappedStorage(path string, fstype string) idmap.StorageType
}
// VM interface is for VM specific functions.
type VM interface {
Instance
AgentCertificate() *x509.Certificate
ConsoleLog() (string, error)
ConsoleScreenshot(screenshotFile *os.File) error
DumpGuestMemory(w *os.File, format string) error
}
// CriuMigrationArgs arguments for CRIU migration.
type CriuMigrationArgs struct {
Cmd uint
StateDir string
Function string
Stop bool
ActionScript bool
DumpDir string
PreDumpDir string
Features liblxc.CriuFeatures
Op *operationlock.InstanceOperation
}
// Info represents information about an instance driver.
type Info struct {
Name string // Name of an instance driver, e.g. "lxc"
Version string // Version number of a loaded instance driver
Error error // Whether there is an operational impediment.
Type instancetype.Type // Instance type that the driver provides support for.
Features map[string]any // Map of supported features.
}
// MigrateArgs represent arguments for instance migration send and receive.
type MigrateArgs struct {
ControlSend func(m proto.Message) error
ControlReceive func(m proto.Message) error
StateConn func(ctx context.Context) (io.ReadWriteCloser, error)
FilesystemConn func(ctx context.Context) (io.ReadWriteCloser, error)
Snapshots bool
Live bool
Disconnect func()
ClusterMoveSourceName string // Will be empty if not a cluster move, othwise indicates the source instance.
StoragePool string
}
// MigrateSendArgs represent arguments for instance migration send.
type MigrateSendArgs struct {
MigrateArgs
AllowInconsistent bool
}
// MigrateReceiveArgs represent arguments for instance migration receive.
type MigrateReceiveArgs struct {
MigrateArgs
InstanceOperation *operationlock.InstanceOperation
Refresh bool
RefreshExcludeOlder bool
}
|