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
|
package drivers
import (
"errors"
"fmt"
"slices"
"sync"
internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/db/warningtype"
"github.com/lxc/incus/v6/internal/server/device"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/operations"
"github.com/lxc/incus/v6/internal/server/project"
"github.com/lxc/incus/v6/internal/server/state"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/revert"
)
// Instance driver definitions.
var instanceDrivers = map[string]func() instance.Instance{
"lxc": func() instance.Instance { return &lxc{} },
"qemu": func() instance.Instance { return &qemu{} },
}
// DriverStatus definition.
type DriverStatus struct {
Info instance.Info
Warning *cluster.Warning
Supported bool
}
// Supported instance drivers cache variables.
var (
driverStatusesMu sync.Mutex
driverStatuses map[instancetype.Type]*DriverStatus
)
// Temporary instance reference storage (for hooks).
var (
instanceRefsMu sync.Mutex
instanceRefs map[string]instance.Instance
)
func init() {
// Expose load to the instance package, to avoid circular imports.
instance.Load = load
// Expose validDevices to the instance package, to avoid circular imports.
instance.ValidDevices = validDevices
// Expose create to the instance package, to avoid circular imports.
instance.Create = create
}
// load creates the underlying instance type struct and returns it as an Instance.
func load(s *state.State, args db.InstanceArgs, p api.Project) (instance.Instance, error) {
var inst instance.Instance
var err error
if args.Type == instancetype.Container {
inst, err = lxcLoad(s, args, p)
} else if args.Type == instancetype.VM {
inst, err = qemuLoad(s, args, p)
} else {
return nil, fmt.Errorf("Invalid instance type for instance %s", args.Name)
}
if err != nil {
return nil, err
}
return inst, nil
}
// validDevices validate instance device configs.
func validDevices(state *state.State, p api.Project, instanceType instancetype.Type, localDevices deviceConfig.Devices, expandedDevices deviceConfig.Devices) error {
instConf := &common{
dbType: instanceType,
localDevices: localDevices.Clone(),
expandedDevices: expandedDevices.Clone(),
project: p,
}
var checkedDevices []string
checkDevices := func(devices deviceConfig.Devices, expanded bool) error {
// Check each device individually using the device package.
for deviceName, deviceConfig := range devices {
if expanded && slices.Contains(checkedDevices, deviceName) {
continue // Don't check the device twice if present in both local and expanded.
}
// Enforce a maximum name length of 64 characters.
// This is a safe maximum allowing use for sockets and other filesystem use.
if len(deviceName) > 64 {
return errors.New("The maximum device name length is 64 characters")
}
err := device.Validate(instConf, state, deviceName, deviceConfig)
if err != nil {
if expanded && errors.Is(err, device.ErrUnsupportedDevType) {
// Skip unsupported devices in expanded config.
// This allows mixed instance type profiles to be used where some devices
// are only supported with specific instance types.
continue
}
return fmt.Errorf("Device validation failed for %q: %w", deviceName, err)
}
checkedDevices = append(checkedDevices, deviceName)
}
return nil
}
// Check each local device individually using the device package.
// Use the cloned config from instConf.localDevices so that the driver cannot modify it.
err := checkDevices(instConf.localDevices, false)
if err != nil {
return err
}
if len(expandedDevices) > 0 {
// Check we have a root disk if in expanded validation mode.
_, _, err := internalInstance.GetRootDiskDevice(expandedDevices.CloneNative())
if err != nil {
return fmt.Errorf("Failed detecting root disk device: %w", err)
}
// Check each expanded device individually using the device package.
// Use the cloned config from instConf.expandedDevices so that the driver cannot modify it.
err = checkDevices(instConf.expandedDevices, true)
if err != nil {
return err
}
}
return nil
}
func create(s *state.State, args db.InstanceArgs, p api.Project, op *operations.Operation) (instance.Instance, revert.Hook, error) {
if args.Type == instancetype.Container {
return lxcCreate(s, args, p, op)
} else if args.Type == instancetype.VM {
return qemuCreate(s, args, p, op)
}
return nil, nil, errors.New("Instance type invalid")
}
// DriverStatuses returns a map of DriverStatus structs for all instance type drivers.
// The first time this function is called each of the instance drivers will be probed for support and the result
// will be cached internally to make subsequent calls faster.
func DriverStatuses() map[instancetype.Type]*DriverStatus {
driverStatusesMu.Lock()
defer driverStatusesMu.Unlock()
if driverStatuses != nil {
return driverStatuses
}
driverStatuses = make(map[instancetype.Type]*DriverStatus, len(instanceDrivers))
for _, instanceDriver := range instanceDrivers {
driverStatus := &DriverStatus{}
driverInfo := instanceDriver().Info()
driverStatus.Info = driverInfo
driverStatus.Supported = true
if driverInfo.Error != nil || driverInfo.Version == "" {
logger.Warn("Instance type not operational", logger.Ctx{"type": driverInfo.Type, "driver": driverInfo.Name, "err": driverInfo.Error})
driverStatus.Supported = false
driverStatus.Warning = &cluster.Warning{
TypeCode: warningtype.InstanceTypeNotOperational,
LastMessage: fmt.Sprintf("%v", driverInfo.Error),
}
} else {
logger.Info("Instance type operational", logger.Ctx{"type": driverInfo.Type, "driver": driverInfo.Name})
}
driverStatuses[driverInfo.Type] = driverStatus
}
return driverStatuses
}
// instanceRefGet retrieves an instance reference.
func instanceRefGet(projectName string, instName string) instance.Instance {
instanceRefsMu.Lock()
defer instanceRefsMu.Unlock()
return instanceRefs[project.Instance(projectName, instName)]
}
// instanceRefSet stores a reference to an instance.
func instanceRefSet(inst instance.Instance) {
instanceRefsMu.Lock()
defer instanceRefsMu.Unlock()
if instanceRefs == nil {
instanceRefs = make(map[string]instance.Instance)
}
instanceRefs[project.Instance(inst.Project().Name, inst.Name())] = inst
}
// instanceRefClear removes an instance reference.
func instanceRefClear(inst instance.Instance) {
instanceRefsMu.Lock()
defer instanceRefsMu.Unlock()
delete(instanceRefs, project.Instance(inst.Project().Name, inst.Name()))
}
|