mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(host): delete snapshot on guest deleted (#20640)
This commit is contained in:
@@ -637,7 +637,7 @@ func (s *SLocalStorage) CreateSnapshotFormUrl(
|
||||
}
|
||||
|
||||
func (s *SLocalStorage) DeleteSnapshots(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
input, ok := params.(SStorageDeleteSnapshots)
|
||||
input, ok := params.(*SStorageDeleteSnapshots)
|
||||
if !ok {
|
||||
return nil, hostutils.ParamsError
|
||||
}
|
||||
@@ -650,14 +650,21 @@ func (s *SLocalStorage) DeleteSnapshots(ctx context.Context, params interface{})
|
||||
}
|
||||
|
||||
func (s *SLocalStorage) DeleteSnapshot(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
input, ok := params.(SStorageDeleteSnapshot)
|
||||
input, ok := params.(*SStorageDeleteSnapshot)
|
||||
if !ok {
|
||||
return nil, hostutils.ParamsError
|
||||
}
|
||||
|
||||
log.Errorf("input %s", jsonutils.Marshal(input))
|
||||
snapshotDir := path.Join(s.GetSnapshotDir(), input.DiskId+options.HostOptions.SnapshotDirSuffix)
|
||||
diskPath := path.Join(s.GetPath(), input.DiskId)
|
||||
return nil, DeleteLocalSnapshot(snapshotDir, input.SnapshotId, diskPath, input.ConvertSnapshot, input.BlockStream)
|
||||
err := DeleteLocalSnapshot(snapshotDir, input.SnapshotId, diskPath, input.ConvertSnapshot, input.BlockStream)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := jsonutils.NewDict()
|
||||
res.Set("deleted", jsonutils.JSONTrue)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func DeleteLocalSnapshot(snapshotDir, snapshotId, diskPath, convertSnapshot string, blockStream bool) error {
|
||||
@@ -717,10 +724,12 @@ func DeleteLocalSnapshot(snapshotDir, snapshotId, diskPath, convertSnapshot stri
|
||||
return err
|
||||
}
|
||||
}
|
||||
err := procutils.NewCommand("rm", "-f", snapshotPath).Run()
|
||||
if err != nil {
|
||||
log.Errorf("rm snapshot file: %s", err)
|
||||
return errors.Wrap(err, "rm snapshot file")
|
||||
if fileutils2.Exists(snapshotPath) {
|
||||
out, err := procutils.NewCommand("rm", "-f", snapshotPath).Output()
|
||||
if err != nil {
|
||||
log.Errorf("rm snapshot file: %s %s", out, err)
|
||||
return errors.Wrap(err, "rm snapshot file")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ func (s *SLVMStorage) GetSnapshotPathByIds(diskId, snapshotId string) string {
|
||||
}
|
||||
|
||||
func (s *SLVMStorage) DeleteSnapshots(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
input := params.(SStorageDeleteSnapshots)
|
||||
input := params.(*SStorageDeleteSnapshots)
|
||||
for i := range input.SnapshotIds {
|
||||
lvPath := path.Join("/dev", s.GetPath(), "snap_"+input.SnapshotIds[i])
|
||||
if err := lvmutils.LvRemove(lvPath); err != nil {
|
||||
@@ -205,7 +205,7 @@ func (s *SLVMStorage) DeleteSnapshots(ctx context.Context, params interface{}) (
|
||||
}
|
||||
|
||||
func (s *SLVMStorage) DeleteSnapshot(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) {
|
||||
input, ok := params.(SStorageDeleteSnapshot)
|
||||
input, ok := params.(*SStorageDeleteSnapshot)
|
||||
if !ok {
|
||||
return nil, hostutils.ParamsError
|
||||
}
|
||||
@@ -220,7 +220,14 @@ func (s *SLVMStorage) DeleteSnapshot(ctx context.Context, params interface{}) (j
|
||||
}
|
||||
}
|
||||
snapId := path.Join("/dev", s.GetPath(), input.SnapshotId)
|
||||
return nil, lvmutils.LvRemove(snapId)
|
||||
err := lvmutils.LvRemove(snapId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := jsonutils.NewDict()
|
||||
res.Set("deleted", jsonutils.JSONTrue)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *SLVMStorage) IsSnapshotExist(diskId, snapshotId string) (bool, error) {
|
||||
|
||||
@@ -140,7 +140,14 @@ func (s *SSLVMStorage) DeleteSnapshot(ctx context.Context, params interface{}) (
|
||||
}
|
||||
|
||||
snapId := path.Join("/dev", s.GetPath(), input.SnapshotId)
|
||||
return nil, lvmutils.LvRemove(snapId)
|
||||
err := lvmutils.LvRemove(snapId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := jsonutils.NewDict()
|
||||
res.Set("deleted", jsonutils.JSONTrue)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *SSLVMStorage) Accessible() error {
|
||||
|
||||
@@ -447,7 +447,13 @@ func storageDeleteSnapshot(ctx context.Context, w http.ResponseWriter, r *http.R
|
||||
}
|
||||
diskId, err := body.GetString("disk_id")
|
||||
if err != nil {
|
||||
hostutils.Response(ctx, w, httperrors.NewImageNotFoundError("disk_id"))
|
||||
hostutils.Response(ctx, w, httperrors.NewMissingParameterError("disk_id"))
|
||||
return
|
||||
}
|
||||
|
||||
snapshotId, err := body.GetString("delete_snapshot")
|
||||
if err != nil {
|
||||
hostutils.Response(ctx, w, httperrors.NewMissingParameterError("snapshot_id"))
|
||||
return
|
||||
}
|
||||
// blockStream indicate snapshot<-disk
|
||||
@@ -457,6 +463,7 @@ func storageDeleteSnapshot(ctx context.Context, w http.ResponseWriter, r *http.R
|
||||
input := &storageman.SStorageDeleteSnapshot{
|
||||
DiskId: diskId,
|
||||
BlockStream: blockStream,
|
||||
SnapshotId: snapshotId,
|
||||
}
|
||||
|
||||
if !blockStream && !autoDeleted {
|
||||
|
||||
Reference in New Issue
Block a user