Skip to content

Commit

Permalink
Automated cherry pick of #19558: Fix/guest image live migrate (#19559)
Browse files Browse the repository at this point in the history
* fix(host): slvm storage active on init

* fix(region): guest live migrate cache all of guestimage caches
  • Loading branch information
wanyaoqi authored Feb 28, 2024
1 parent 902749d commit d504b76
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
59 changes: 44 additions & 15 deletions pkg/compute/tasks/guest_live_migrate_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,55 @@ func (task *GuestMigrateTask) SaveScheduleResult(ctx context.Context, obj ISched
body.Set("is_local_storage", jsonutils.JSONFalse)
}

task.SetStage("OnCachedImageComplete", body)
// prepare disk for migration
if len(disk.TemplateId) > 0 && isLocalStorage {
targetStorageCache := targetHost.GetLocalStoragecache()
if targetStorageCache != nil {
input := api.CacheImageInput{
ImageId: disk.TemplateId,
Format: disk.DiskFormat,
IsForce: false,
SourceHostId: guest.HostId,
ParentTaskId: task.GetTaskId(),
}
err := targetStorageCache.StartImageCacheTask(ctx, task.UserCred, input)
if err != nil {
task.TaskFailed(ctx, guest, jsonutils.NewString(err.Error()))
templates := []string{}
guestdisks, _ := guest.GetDisks()
for i := range guestdisks {
if guestdisks[i].TemplateId != "" {
templates = append(templates, guestdisks[i].TemplateId)
}
return
}
if len(templates) > 0 {
body.Set("cache_templates", jsonutils.NewStringArray(templates))
}
}
task.SetStage("OnStartCacheImages", body)
task.OnStartCacheImages(ctx, guest, nil)
}

func (task *GuestMigrateTask) OnStartCacheImages(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
templates, _ := task.Params.GetArray("cache_templates")
if len(templates) == 0 {
task.OnCachedImageComplete(ctx, guest, nil)
return
}

templateId, _ := templates[0].GetString()
task.Params.Set("cache_templates", jsonutils.NewArray(templates[1:]...))
task.SetStage("OnStartCacheImages", nil)

targetHostId, _ := task.Params.GetString("target_host_id")
targetHost := models.HostManager.FetchHostById(targetHostId)
targetStorageCache := targetHost.GetLocalStoragecache()
if targetStorageCache != nil {
input := api.CacheImageInput{
ImageId: templateId,
IsForce: false,
SourceHostId: guest.HostId,
ParentTaskId: task.GetTaskId(),
}
err := targetStorageCache.StartImageCacheTask(ctx, task.UserCred, input)
if err != nil {
task.TaskFailed(ctx, guest, jsonutils.NewString(err.Error()))
}
return
}
task.OnCachedImageComplete(ctx, guest, nil)
task.OnStartCacheImages(ctx, guest, nil)
}

func (task *GuestMigrateTask) OnStartCacheImagesFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
task.TaskFailed(ctx, guest, data)
}

// For local storage get disk info
Expand Down
12 changes: 12 additions & 0 deletions pkg/hostman/storageman/lvmutils/lvmutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,15 @@ func VgDisplay(vgName string) error {
}
return nil
}

func VgActive(vgName string, active bool) error {
opts := "-ay"
if !active {
opts = "-an"
}
out, err := procutils.NewRemoteCommandAsFarAsPossible("lvm", "vgchange", opts, vgName).Output()
if err != nil {
return errors.Wrapf(err, "vgchange %s %s failed %s", opts, vgName, out)
}
return nil
}
4 changes: 4 additions & 0 deletions pkg/hostman/storageman/storage_slvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (s *SSLVMStorage) GetDiskById(diskId string) (IDisk, error) {
}

func (s *SSLVMStorage) Accessible() error {
if err := lvmutils.VgActive(s.Path, true); err != nil {
return err
}

if err := lvmutils.VgDisplay(s.Path); err != nil {
return err
}
Expand Down

0 comments on commit d504b76

Please sign in to comment.