diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index 4911716317a..d6f9d2976bd 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -2205,6 +2205,25 @@ func (self *SDisk) GetLastAttachedHost(ctx context.Context, userCred mcclient.To return self.GetMetadata(ctx, api.DISK_META_LAST_ATTACHED_HOST, userCred) } +func (self *SDisk) RecordDiskSnapshotsLastHost(ctx context.Context, userCred mcclient.TokenCredential, hostId string) error { + storage, err := self.GetStorage() + if err != nil { + return err + } + if storage.StorageType != api.STORAGE_SLVM { + return nil + } + // record disk snapshots master host + snaps := SnapshotManager.GetDiskSnapshots(self.Id) + for i := range snaps { + err = snaps[i].SetMetadata(ctx, api.DISK_META_LAST_ATTACHED_HOST, hostId, userCred) + if err != nil { + log.Errorf("snapshot %s failed set last attached host: %s", snaps[i].Id, err) + } + } + return nil +} + // 同步磁盘状态 func (self *SDisk) PerformSyncstatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.DiskSyncstatusInput) (jsonutils.JSONObject, error) { var openTask = true diff --git a/pkg/compute/storagedrivers/base.go b/pkg/compute/storagedrivers/base.go index 8ac614b5a0f..6dccf5abeec 100644 --- a/pkg/compute/storagedrivers/base.go +++ b/pkg/compute/storagedrivers/base.go @@ -97,11 +97,29 @@ func (self *SBaseStorageDriver) RequestDeleteSnapshot(ctx context.Context, snaps } } if guest == nil { - storage := snapshot.GetStorage() - host, err := storage.GetMasterHost() - if err != nil { - return err + var host *models.SHost + disk, err := models.DiskManager.FetchById(snapshot.DiskId) + if err != nil && err != sql.ErrNoRows { + return errors.Wrap(err, "get disk by snapshot") + } + if disk != nil { + sDisk := disk.(*models.SDisk) + if hostId := sDisk.GetLastAttachedHost(ctx, task.GetUserCred()); hostId != "" { + host = models.HostManager.FetchHostById(hostId) + } + } else { + if hostId := snapshot.GetMetadata(ctx, api.DISK_META_LAST_ATTACHED_HOST, task.GetUserCred()); hostId != "" { + host = models.HostManager.FetchHostById(hostId) + } } + if host == nil { + storage := snapshot.GetStorage() + host, err = storage.GetMasterHost() + if err != nil { + return err + } + } + convertSnapshot, err := models.SnapshotManager.GetConvertSnapshot(snapshot) if err != nil && err != sql.ErrNoRows { return errors.Wrap(err, "get convert snapshot") @@ -109,10 +127,7 @@ func (self *SBaseStorageDriver) RequestDeleteSnapshot(ctx context.Context, snaps params := jsonutils.NewDict() params.Set("delete_snapshot", jsonutils.NewString(snapshot.Id)) params.Set("disk_id", jsonutils.NewString(snapshot.DiskId)) - disk, err := models.DiskManager.FetchById(snapshot.DiskId) - if err != nil && err != sql.ErrNoRows { - return errors.Wrap(err, "get disk by snapshot") - } + if disk != nil { sDisk, _ := disk.(*models.SDisk) if sDisk.IsEncrypted() { diff --git a/pkg/compute/tasks/disk_delete_task.go b/pkg/compute/tasks/disk_delete_task.go index 3a72b81f802..592db2b79e1 100644 --- a/pkg/compute/tasks/disk_delete_task.go +++ b/pkg/compute/tasks/disk_delete_task.go @@ -130,6 +130,10 @@ func (self *DiskDeleteTask) startDeleteDisk(ctx context.Context, disk *models.SD } } + if host != nil { + disk.RecordDiskSnapshotsLastHost(ctx, self.UserCred, host.Id) + } + isPurge := false if (host == nil || !host.GetEnabled()) && purgeParams { isPurge = true