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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"slices"
internalInstance "github.com/lxc/incus/v6/internal/instance"
internalRecover "github.com/lxc/incus/v6/internal/recover"
"github.com/lxc/incus/v6/internal/server/auth"
"github.com/lxc/incus/v6/internal/server/backup"
backupConfig "github.com/lxc/incus/v6/internal/server/backup/config"
"github.com/lxc/incus/v6/internal/server/db"
dbCluster "github.com/lxc/incus/v6/internal/server/db/cluster"
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/project"
"github.com/lxc/incus/v6/internal/server/response"
"github.com/lxc/incus/v6/internal/server/state"
storagePools "github.com/lxc/incus/v6/internal/server/storage"
storageDrivers "github.com/lxc/incus/v6/internal/server/storage/drivers"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/osarch"
"github.com/lxc/incus/v6/shared/revert"
)
// Define API endpoints for recover actions.
var internalRecoverValidateCmd = APIEndpoint{
Path: "recover/validate",
Post: APIEndpointAction{Handler: internalRecoverValidate, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalRecoverImportCmd = APIEndpoint{
Path: "recover/import",
Post: APIEndpointAction{Handler: internalRecoverImport, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
// init recover adds API endpoints to handler slice.
func init() {
apiInternal = append(apiInternal, internalRecoverValidateCmd, internalRecoverImportCmd)
}
// internalRecoverScan provides the discovery and import functionality for both recovery validate and import steps.
func internalRecoverScan(ctx context.Context, s *state.State, userPools []api.StoragePoolsPost, validateOnly bool) response.Response {
var err error
var projects map[string]*api.Project
var projectProfiles map[string][]*api.Profile
var projectNetworks map[string]map[int64]api.Network
// Retrieve all project, profile and network info in a single transaction so we can use it for all
// imported instances and volumes, and avoid repeatedly querying the same information.
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
// Load list of projects for validation.
ps, err := dbCluster.GetProjects(ctx, tx.Tx())
if err != nil {
return err
}
// Convert to map for lookups by name later.
projects = make(map[string]*api.Project, len(ps))
for i := range ps {
project, err := ps[i].ToAPI(ctx, tx.Tx())
if err != nil {
return err
}
projects[ps[i].Name] = project
}
// Load list of project/profile names for validation.
profiles, err := dbCluster.GetProfiles(ctx, tx.Tx())
if err != nil {
return err
}
profileConfigs, err := dbCluster.GetAllProfileConfigs(ctx, tx.Tx())
if err != nil {
return err
}
profileDevices, err := dbCluster.GetAllProfileDevices(ctx, tx.Tx())
if err != nil {
return err
}
// Convert to map for lookups by project name later.
projectProfiles = make(map[string][]*api.Profile)
for _, profile := range profiles {
if projectProfiles[profile.Project] == nil {
projectProfiles[profile.Project] = []*api.Profile{}
}
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
if err != nil {
return err
}
projectProfiles[profile.Project] = append(projectProfiles[profile.Project], apiProfile)
}
// Load list of project/network names for validation.
projectNetworks, err = tx.GetCreatedNetworks(ctx)
if err != nil {
return err
}
return nil
})
if err != nil {
return response.SmartError(fmt.Errorf("Failed getting validate dependency check info: %w", err))
}
res := internalRecover.ValidateResult{}
reverter := revert.New()
defer reverter.Fail()
// addDependencyError adds an error to the list of dependency errors if not already present in list.
addDependencyError := func(err error) {
errStr := err.Error()
if !slices.Contains(res.DependencyErrors, errStr) {
res.DependencyErrors = append(res.DependencyErrors, errStr)
}
}
// Used to store the unknown volumes for each pool & project.
poolsProjectVols := make(map[string]map[string][]*backupConfig.Config)
// Used to store a handle to each pool containing user supplied config.
pools := make(map[string]storagePools.Pool)
// Iterate the pools finding unknown volumes and perform validation.
for _, p := range userPools {
pool, err := storagePools.LoadByName(s, p.Name)
if err != nil {
if response.IsNotFoundError(err) {
// If the pool DB record doesn't exist, and we are clustered, then don't proceed
// any further as we do not support pool DB record recovery when clustered.
if s.ServerClustered {
return response.BadRequest(errors.New("Storage pool recovery not supported when clustered"))
}
// If pool doesn't exist in DB, initialize a temporary pool with the supplied info.
poolInfo := api.StoragePool{
Name: p.Name,
Driver: p.Driver,
StoragePoolPut: p.StoragePoolPut,
Status: api.StoragePoolStatusCreated,
}
pool, err = storagePools.NewTemporary(s, &poolInfo)
if err != nil {
return response.SmartError(fmt.Errorf("Failed to initialize unknown pool %q: %w", p.Name, err))
}
// Populate configuration with default values.
err := pool.Driver().FillConfig()
if err != nil {
return response.SmartError(fmt.Errorf("Failed to evaluate the default configuration values for unknown pool %q: %w", p.Name, err))
}
err = pool.Driver().Validate(poolInfo.Config)
if err != nil {
return response.SmartError(fmt.Errorf("Failed config validation for unknown pool %q: %w", p.Name, err))
}
} else {
return response.SmartError(fmt.Errorf("Failed loading existing pool %q: %w", p.Name, err))
}
}
// Record this pool to be used during import stage, assuming validation passes.
pools[p.Name] = pool
// Try to mount the pool.
ourMount, err := pool.Mount()
if err != nil {
return response.SmartError(fmt.Errorf("Failed mounting pool %q: %w", pool.Name(), err))
}
// Unmount pool when done if not existing in DB after function has finished.
// This way if we are dealing with an existing pool or have successfully created the DB record then
// we won't unmount it. As we should leave successfully imported pools mounted.
if ourMount {
defer func() {
cleanupPool := pools[pool.Name()]
if cleanupPool != nil && cleanupPool.ID() == storagePools.PoolIDTemporary {
_, _ = cleanupPool.Unmount()
}
}()
reverter.Add(func() {
cleanupPool := pools[pool.Name()]
_, _ = cleanupPool.Unmount() // Defer won't do it if record exists, so unmount on failure.
})
}
// Get list of unknown volumes on pool.
poolProjectVols, err := pool.ListUnknownVolumes(nil)
if err != nil {
if errors.Is(err, storageDrivers.ErrNotSupported) {
continue // Ignore unsupported storage drivers.
}
return response.SmartError(fmt.Errorf("Failed checking volumes on pool %q: %w", pool.Name(), err))
}
// Store for consumption after validation scan to avoid needing to reprocess.
poolsProjectVols[p.Name] = poolProjectVols
// Check dependencies are met for each volume.
for projectName, poolVols := range poolProjectVols {
// Check project exists in database.
projectInfo := projects[projectName]
// Look up effective project names for profiles and networks.
var profileProjectname string
var networkProjectName string
if projectInfo != nil {
profileProjectname = project.ProfileProjectFromRecord(projectInfo)
networkProjectName = project.NetworkProjectFromRecord(projectInfo)
} else {
addDependencyError(fmt.Errorf("Project %q", projectName))
continue // Skip further validation if project is missing.
}
for _, poolVol := range poolVols {
if poolVol.Container == nil {
continue // Skip dependency checks for non-instance volumes.
}
// Check that the instance's profile dependencies are met.
for _, poolInstProfileName := range poolVol.Container.Profiles {
foundProfile := false
for _, profile := range projectProfiles[profileProjectname] {
if profile.Name == poolInstProfileName {
foundProfile = true
}
}
if !foundProfile {
addDependencyError(fmt.Errorf("Profile %q in project %q", poolInstProfileName, projectName))
}
}
// Check that the instance's NIC network dependencies are met.
for _, devConfig := range poolVol.Container.ExpandedDevices {
if devConfig["type"] != "nic" {
continue
}
if devConfig["network"] == "" {
continue
}
foundNetwork := false
for _, n := range projectNetworks[networkProjectName] {
if n.Name == devConfig["network"] {
foundNetwork = true
break
}
}
if !foundNetwork {
addDependencyError(fmt.Errorf("Network %q in project %q", devConfig["network"], projectName))
}
}
}
}
}
// If in validation mode or if there are dependency errors, return discovered unknown volumes, along with
// any dependency errors.
if validateOnly || len(res.DependencyErrors) > 0 {
for poolName, poolProjectVols := range poolsProjectVols {
for projectName, poolVols := range poolProjectVols {
for _, poolVol := range poolVols {
var displayType, displayName string
var displaySnapshotCount int
// Build display fields for scan results.
if poolVol.Container != nil {
displayType = poolVol.Container.Type
displayName = poolVol.Container.Name
displaySnapshotCount = len(poolVol.Snapshots)
} else if poolVol.Bucket != nil {
displayType = "bucket"
displayName = poolVol.Bucket.Name
displaySnapshotCount = 0
} else {
displayType = "volume"
displayName = poolVol.Volume.Name
displaySnapshotCount = len(poolVol.VolumeSnapshots)
}
res.UnknownVolumes = append(res.UnknownVolumes, internalRecover.ValidateVolume{
Pool: poolName,
Project: projectName,
Type: displayType,
Name: displayName,
SnapshotCount: displaySnapshotCount,
})
}
}
}
return response.SyncResponse(true, &res)
}
// If in import mode and no dependency errors, then re-create missing DB records.
// Create the pools themselves.
for _, pool := range pools {
// Create missing storage pool DB record if needed.
if pool.ID() == storagePools.PoolIDTemporary {
var instPoolVol *backupConfig.Config // Instance volume used for new pool record.
var poolID int64 // Pool ID of created pool record.
var poolVols []*backupConfig.Config
for _, value := range poolsProjectVols[pool.Name()] {
poolVols = append(poolVols, value...)
}
// Search unknown volumes looking for an instance volume that can be used to
// restore the pool DB config from. This is preferable over using the user
// supplied config as it will include any additional settings not supplied.
for _, poolVol := range poolVols {
if poolVol.Pool != nil && poolVol.Pool.Config != nil {
instPoolVol = poolVol
break // Stop search once we've found an instance with pool config.
}
}
if instPoolVol != nil {
// Create storage pool DB record from config in the instance.
logger.Info("Creating storage pool DB record from instance config", logger.Ctx{"name": instPoolVol.Pool.Name, "description": instPoolVol.Pool.Description, "driver": instPoolVol.Pool.Driver, "config": instPoolVol.Pool.Config})
poolID, err = dbStoragePoolCreateAndUpdateCache(ctx, s, instPoolVol.Pool.Name, instPoolVol.Pool.Description, instPoolVol.Pool.Driver, instPoolVol.Pool.Config)
if err != nil {
return response.SmartError(fmt.Errorf("Failed creating storage pool %q database entry: %w", pool.Name(), err))
}
} else {
// Create storage pool DB record from config supplied by user if not
// instance volume pool config found.
poolDriverName := pool.Driver().Info().Name
poolDriverConfig := pool.Driver().Config()
logger.Info("Creating storage pool DB record from user config", logger.Ctx{"name": pool.Name(), "driver": poolDriverName, "config": poolDriverConfig})
poolID, err = dbStoragePoolCreateAndUpdateCache(ctx, s, pool.Name(), "", poolDriverName, poolDriverConfig)
if err != nil {
return response.SmartError(fmt.Errorf("Failed creating storage pool %q database entry: %w", pool.Name(), err))
}
}
reverter.Add(func() {
_ = dbStoragePoolDeleteAndUpdateCache(context.Background(), s, pool.Name())
})
// Set storage pool node to storagePoolCreated.
// Must come before storage pool is loaded from the database.
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
return tx.StoragePoolNodeCreated(poolID)
})
if err != nil {
return response.SmartError(fmt.Errorf("Failed marking storage pool %q local status as created: %w", pool.Name(), err))
}
logger.Debug("Marked storage pool local status as created", logger.Ctx{"pool": pool.Name()})
newPool, err := storagePools.LoadByName(s, pool.Name())
if err != nil {
return response.SmartError(fmt.Errorf("Failed loading created storage pool %q: %w", pool.Name(), err))
}
// Record this newly created pool so that defer doesn't unmount on return.
pools[pool.Name()] = newPool
}
}
// Recover the storage volumes and buckets.
for _, pool := range pools {
for projectName, poolVols := range poolsProjectVols[pool.Name()] {
projectInfo := projects[projectName]
if projectInfo == nil {
// Shouldn't happen as we validated this above, but be sure for safety.
return response.SmartError(fmt.Errorf("Project %q not found", projectName))
}
customStorageProjectName := project.StorageVolumeProjectFromRecord(projectInfo, db.StoragePoolVolumeTypeCustom)
// Recover unknown custom volumes (do this first before recovering instances so that any
// instances that reference unknown custom volume disk devices can be created).
for _, poolVol := range poolVols {
if poolVol.Container != nil || poolVol.Bucket != nil {
continue // Skip instance volumes and buckets.
} else if poolVol.Container == nil && poolVol.Volume == nil {
return response.SmartError(errors.New("Volume is neither instance nor custom volume"))
}
// Import custom volume and any snapshots.
cleanup, err := pool.ImportCustomVolume(customStorageProjectName, poolVol, nil)
if err != nil {
return response.SmartError(fmt.Errorf("Failed importing custom volume %q in project %q: %w", poolVol.Volume.Name, projectName, err))
}
reverter.Add(cleanup)
}
// Recover unknown buckets.
for _, poolVol := range poolVols {
// Skip non bucket volumes.
if poolVol.Bucket == nil {
continue
}
// Import bucket.
cleanup, err := pool.ImportBucket(projectName, poolVol, nil)
if err != nil {
return response.SmartError(fmt.Errorf("Failed importing bucket %q in project %q: %w", poolVol.Bucket.Name, projectName, err))
}
reverter.Add(cleanup)
}
}
}
// Finally restore the instances.
for _, pool := range pools {
for projectName, poolVols := range poolsProjectVols[pool.Name()] {
projectInfo := projects[projectName]
if projectInfo == nil {
// Shouldn't happen as we validated this above, but be sure for safety.
return response.SmartError(fmt.Errorf("Project %q not found", projectName))
}
profileProjectName := project.ProfileProjectFromRecord(projectInfo)
// Recover unknown instance volumes.
for _, poolVol := range poolVols {
if poolVol.Container == nil && (poolVol.Volume != nil || poolVol.Bucket != nil) {
continue // Skip custom volumes, invalid volumes and buckets.
}
// Recover instance volumes and any snapshots.
profiles := make([]api.Profile, 0, len(poolVol.Container.Profiles))
for _, profileName := range poolVol.Container.Profiles {
for i := range projectProfiles[profileProjectName] {
if projectProfiles[profileProjectName][i].Name == profileName {
profiles = append(profiles, *projectProfiles[profileProjectName][i])
}
}
}
inst, cleanup, err := internalRecoverImportInstance(s, pool, projectName, poolVol, profiles)
if err != nil {
return response.SmartError(fmt.Errorf("Failed creating instance %q record in project %q: %w", poolVol.Container.Name, projectName, err))
}
reverter.Add(cleanup)
// Recover instance volume snapshots.
for _, poolInstSnap := range poolVol.Snapshots {
profiles := make([]api.Profile, 0, len(poolInstSnap.Profiles))
for _, profileName := range poolInstSnap.Profiles {
for i := range projectProfiles[profileProjectName] {
if projectProfiles[profileProjectName][i].Name == profileName {
profiles = append(profiles, *projectProfiles[profileProjectName][i])
}
}
}
cleanup, err := internalRecoverImportInstanceSnapshot(s, pool, projectName, poolVol, poolInstSnap, profiles)
if err != nil {
return response.SmartError(fmt.Errorf("Failed creating instance %q snapshot %q record in project %q: %w", poolVol.Container.Name, poolInstSnap.Name, projectName, err))
}
reverter.Add(cleanup)
}
// Recreate instance mount path and symlinks (must come after snapshot recovery).
cleanup, err = pool.ImportInstance(inst, poolVol, nil)
if err != nil {
return response.SmartError(fmt.Errorf("Failed importing instance %q in project %q: %w", poolVol.Container.Name, projectName, err))
}
reverter.Add(cleanup)
// Reinitialize the instance's root disk quota even if no size specified (allows the storage driver the
// opportunity to reinitialize the quota based on the new storage volume's DB ID).
_, rootConfig, err := internalInstance.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
if err == nil {
err = pool.SetInstanceQuota(inst, rootConfig["size"], rootConfig["size.state"], nil)
if err != nil {
return response.SmartError(fmt.Errorf("Failed reinitializing root disk quota %q for instance %q in project %q: %w", rootConfig["size"], poolVol.Container.Name, projectName, err))
}
}
}
}
}
reverter.Success()
return response.EmptySyncResponse
}
// internalRecoverImportInstance recreates the database records for an instance and returns the new instance.
// Returns a revert fail function that can be used to undo this function if a subsequent step fails.
func internalRecoverImportInstance(s *state.State, pool storagePools.Pool, projectName string, poolVol *backupConfig.Config, profiles []api.Profile) (instance.Instance, revert.Hook, error) {
if poolVol.Container == nil {
return nil, nil, errors.New("Pool volume is not an instance volume")
}
// Add root device if needed.
if poolVol.Container.Devices == nil {
poolVol.Container.Devices = make(map[string]map[string]string)
}
if poolVol.Container.ExpandedDevices == nil {
poolVol.Container.ExpandedDevices = make(map[string]map[string]string)
}
internalImportRootDevicePopulate(pool.Name(), poolVol.Container.Devices, poolVol.Container.ExpandedDevices, profiles)
dbInst, err := backup.ConfigToInstanceDBArgs(s, poolVol, projectName, true)
if err != nil {
return nil, nil, err
}
if dbInst.Type < 0 {
return nil, nil, errors.New("Invalid instance type")
}
inst, instOp, cleanup, err := instance.CreateInternal(s, *dbInst, nil, false, true)
if err != nil {
return nil, nil, fmt.Errorf("Failed creating instance record: %w", err)
}
defer instOp.Done(err)
return inst, cleanup, err
}
// internalRecoverImportInstance recreates the database records for an instance snapshot.
func internalRecoverImportInstanceSnapshot(s *state.State, pool storagePools.Pool, projectName string, poolVol *backupConfig.Config, snap *api.InstanceSnapshot, profiles []api.Profile) (revert.Hook, error) {
if poolVol.Container == nil || snap == nil {
return nil, errors.New("Pool volume is not an instance volume")
}
// Add root device if needed.
if snap.Devices == nil {
snap.Devices = make(map[string]map[string]string)
}
if snap.ExpandedDevices == nil {
snap.ExpandedDevices = make(map[string]map[string]string)
}
internalImportRootDevicePopulate(pool.Name(), snap.Devices, snap.ExpandedDevices, profiles)
arch, err := osarch.ArchitectureID(snap.Architecture)
if err != nil {
return nil, err
}
instanceType, err := instancetype.New(poolVol.Container.Type)
if err != nil {
return nil, err
}
_, snapInstOp, cleanup, err := instance.CreateInternal(s, db.InstanceArgs{
Project: projectName,
Architecture: arch,
BaseImage: snap.Config["volatile.base_image"],
Config: snap.Config,
CreationDate: snap.CreatedAt,
Type: instanceType,
Snapshot: true,
Devices: deviceConfig.NewDevices(snap.Devices),
Ephemeral: snap.Ephemeral,
LastUsedDate: snap.LastUsedAt,
Name: poolVol.Container.Name + internalInstance.SnapshotDelimiter + snap.Name,
Profiles: profiles,
Stateful: snap.Stateful,
}, nil, false, true)
if err != nil {
return nil, fmt.Errorf("Failed creating instance snapshot record %q: %w", snap.Name, err)
}
defer snapInstOp.Done(err)
return cleanup, err
}
// internalRecoverValidate validates the requested pools to be recovered.
func internalRecoverValidate(d *Daemon, r *http.Request) response.Response {
// Parse the request.
req := &internalRecover.ValidatePost{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return response.BadRequest(err)
}
return internalRecoverScan(r.Context(), d.State(), req.Pools, true)
}
// internalRecoverImport performs the pool volume recovery.
func internalRecoverImport(d *Daemon, r *http.Request) response.Response {
// Parse the request.
req := &internalRecover.ImportPost{}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return response.BadRequest(err)
}
return internalRecoverScan(r.Context(), d.State(), req.Pools, false)
}
|