MCPcopy
hub / github.com/lxc/incus / CreateCustomVolumeSnapshot

Method CreateCustomVolumeSnapshot

internal/server/storage/backend.go:6361–6465  ·  view source on GitHub ↗

CreateCustomVolumeSnapshot creates a snapshot of a custom volume.

(projectName, volName string, newSnapshotName string, newExpiryDate time.Time, instanceStateful bool, op *operations.Operation)

Source from the content-addressed store, hash-verified

6359
6360// CreateCustomVolumeSnapshot creates a snapshot of a custom volume.
6361func (b *backend) CreateCustomVolumeSnapshot(projectName, volName string, newSnapshotName string, newExpiryDate time.Time, instanceStateful bool, op *operations.Operation) error {
6362 l := b.logger.AddContext(logger.Ctx{"project": projectName, "volName": volName, "newSnapshotName": newSnapshotName, "newExpiryDate": newExpiryDate})
6363 l.Debug("CreateCustomVolumeSnapshot started")
6364 defer l.Debug("CreateCustomVolumeSnapshot finished")
6365
6366 if internalInstance.IsSnapshot(volName) {
6367 return errors.New("Volume does not support snapshots")
6368 }
6369
6370 if internalInstance.IsSnapshot(newSnapshotName) {
6371 return errors.New("Snapshot name is not a valid snapshot name")
6372 }
6373
6374 fullSnapshotName := drivers.GetSnapshotVolumeName(volName, newSnapshotName)
6375
6376 // Check snapshot volume doesn't exist already.
6377 volume, err := VolumeDBGet(b, projectName, fullSnapshotName, drivers.VolumeTypeCustom)
6378 if err != nil && !response.IsNotFoundError(err) {
6379 return err
6380 } else if volume != nil {
6381 return api.StatusErrorf(http.StatusConflict, "Snapshot by that name already exists")
6382 }
6383
6384 // Load parent volume information and check it exists.
6385 parentVol, err := VolumeDBGet(b, projectName, volName, drivers.VolumeTypeCustom)
6386 if err != nil {
6387 if response.IsNotFoundError(err) {
6388 return api.StatusErrorf(http.StatusNotFound, "Parent volume doesn't exist")
6389 }
6390
6391 return err
6392 }
6393
6394 volDBContentType, err := VolumeContentTypeNameToContentType(parentVol.ContentType)
6395 if err != nil {
6396 return err
6397 }
6398
6399 contentType, err := VolumeDBContentTypeToContentType(volDBContentType)
6400 if err != nil {
6401 return err
6402 }
6403
6404 if contentType != drivers.ContentTypeFS && contentType != drivers.ContentTypeBlock {
6405 return fmt.Errorf("Volume of content type %q does not support snapshots", contentType)
6406 }
6407
6408 reverter := revert.New()
6409 defer reverter.Fail()
6410
6411 // Validate config and create database entry for new storage volume.
6412 // Copy volume config from parent.
6413 err = VolumeDBCreate(b, projectName, fullSnapshotName, parentVol.Description, drivers.VolumeTypeCustom, true, parentVol.Config, time.Now().UTC(), newExpiryDate, drivers.ContentType(parentVol.ContentType), false, true)
6414 if err != nil {
6415 return err
6416 }
6417
6418 reverter.Add(func() { _ = VolumeDBDelete(b, projectName, fullSnapshotName, drivers.VolumeTypeCustom) })

Callers

nothing calls this directly

Calls 15

GetVolumeMethod · 0.95
qcow2CreateSnapshotMethod · 0.95
qcow2DeleteSnapshotMethod · 0.95
GetSnapshotVolumeNameFunction · 0.92
IsNotFoundErrorFunction · 0.92
StatusErrorfFunction · 0.92
NewFunction · 0.92
ContentTypeTypeAlias · 0.92
StorageVolumeFunction · 0.92
LockFunction · 0.92
OperationLockNameFunction · 0.92

Tested by

no test coverage detected