fix(region,host): kvm guest change disk driver reset uefi vars (#25068)

This commit is contained in:
wanyaoqi
2026-06-26 10:20:25 +08:00
committed by GitHub
parent 3b796dc0e8
commit 372fae41c6
11 changed files with 96 additions and 5 deletions
-4
View File
@@ -77,16 +77,12 @@ func init() {
type ServerDiskUpdateOptions struct {
SERVER string `help:"ID or Name of server"`
DISK string `help:"ID or Name of Disk"`
Driver string `help:"Driver of vDisk" choices:"virtio|ide|sata|scsi|pvscsi"`
Cache string `help:"Cache mode of vDisk" choices:"writethrough|none|writeback|directsync"`
Aio string `help:"Asynchronous IO mode of vDisk" choices:"native|threads"`
Index int64 `help:"Index of vDisk" default:"-1"`
}
R(&ServerDiskUpdateOptions{}, "server-disk-update", "Update details of a virtual disk of a virtual server", func(s *mcclient.ClientSession, args *ServerDiskUpdateOptions) error {
params := jsonutils.NewDict()
if len(args.Driver) > 0 {
params.Add(jsonutils.NewString(args.Driver), "driver")
}
if len(args.Cache) > 0 {
params.Add(jsonutils.NewString(args.Cache), "cache_mode")
}
+1
View File
@@ -158,6 +158,7 @@ func init() {
cmd.Perform("set-network-secgroup", new(options.ServerNetworkSecGroupsOptions))
cmd.Perform("add-network-secgroup", new(options.ServerNetworkSecGroupsOptions))
cmd.Perform("revoke-network-secgroup", new(options.ServerNetworkSecGroupsOptions))
cmd.Perform("change-disk-driver", new(options.ServerChangeDiskDriverOptions))
cmd.GetProperty(&options.ServerStatusStatisticsOptions{})
cmd.GetProperty(&options.ServerProjectStatisticsOptions{})
+5
View File
@@ -1062,6 +1062,11 @@ type ServerChangeDiskStorageInput struct {
KeepOriginDisk bool `json:"keep_origin_disk"`
}
type ServerChangeDiskDriverInput struct {
DiskId string `json:"disk_id"`
Driver string `json:"driver"`
}
type ServerChangeDiskStorageInternalInput struct {
ServerChangeDiskStorageInput
StorageId string `json:"storage_id"`
+4
View File
@@ -478,6 +478,10 @@ func (drv *SBaseGuestDriver) RequestChangeDiskStorage(ctx context.Context, userC
return cloudprovider.ErrNotImplemented
}
func (drv *SBaseGuestDriver) RequestResetUefiFirmwareVars(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest) error {
return nil
}
func (drv *SBaseGuestDriver) RequestSwitchToTargetStorageDisk(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, input *api.ServerChangeDiskStorageInternalInput, task taskman.ITask) error {
return cloudprovider.ErrNotImplemented
}
+12
View File
@@ -958,6 +958,18 @@ func (self *SKVMGuestDriver) RequestChangeDiskStorage(ctx context.Context, userC
return err
}
func (self *SKVMGuestDriver) RequestResetUefiFirmwareVars(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest) error {
host, err := guest.GetHost()
if err != nil {
return err
}
body := jsonutils.NewDict()
header := mcclient.GetTokenHeaders(userCred)
url := fmt.Sprintf("%s/servers/%s/reset-uefi-vars", host.ManagerUri, guest.GetId())
_, _, err = httputils.JSONRequest(httputils.GetDefaultClient(), ctx, "POST", url, header, body, false)
return err
}
func (self *SKVMGuestDriver) RequestSwitchToTargetStorageDisk(ctx context.Context, userCred mcclient.TokenCredential, guest *models.SGuest, input *api.ServerChangeDiskStorageInternalInput, task taskman.ITask) error {
host, err := guest.GetHost()
if err != nil {
+34
View File
@@ -6648,6 +6648,40 @@ func (self *SGuest) StartGuestChangeStorageTask(ctx context.Context, userCred mc
return nil
}
func (self *SGuest) PerformChangeDiskDriver(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input *api.ServerChangeDiskDriverInput) (jsonutils.JSONObject, error) {
if self.Status != api.VM_READY {
return nil, httperrors.NewInvalidStatusError("can't change disk driver in guest status %s", self.Status)
}
gd := self.GetGuestDisk(input.DiskId)
if gd == nil {
return nil, httperrors.NewBadRequestError("failed get guest disk by disk id %s", input.DiskId)
}
if input.Driver == gd.Driver {
return nil, nil
}
if !utils.IsInStringArray(input.Driver, []string{api.DISK_DRIVER_VIRTIO, api.DISK_DRIVER_PVSCSI, api.DISK_DRIVER_IDE, api.DISK_DRIVER_SCSI}) {
return nil, httperrors.NewInputParameterError("unknown driver %s", input.Driver)
}
_, err := db.Update(gd, func() error {
gd.Driver = input.Driver
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed update disk driver")
}
if self.Bios == api.VM_BOOT_MODE_UEFI {
drv, err := self.GetDriver()
if err != nil {
return nil, err
}
err = drv.RequestResetUefiFirmwareVars(ctx, userCred, self)
if err != nil {
return nil, errors.Wrap(err, "failed reset uefi fw vars")
}
}
return nil, nil
}
func (self *SGuest) PerformChangeDiskStorage(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input *api.ServerChangeDiskStorageInput) (*api.ServerChangeDiskStorageInput, error) {
// validate input
if input.DiskId == "" {
+1 -1
View File
@@ -65,7 +65,7 @@ type SGuestdisk struct {
ImagePath string `width:"256" charset:"ascii" nullable:"false" get:"user" create:"required"` // Column(VARCHAR(256, charset='ascii'), nullable=False)
Driver string `width:"32" charset:"ascii" nullable:"true" list:"user" update:"user"` // Column(VARCHAR(32, charset='ascii'), nullable=True)
Driver string `width:"32" charset:"ascii" nullable:"true" list:"user"` // Column(VARCHAR(32, charset='ascii'), nullable=True)
CacheMode string `width:"32" charset:"ascii" nullable:"true" list:"user" update:"user"` // Column(VARCHAR(32, charset='ascii'), nullable=True)
AioMode string `width:"32" charset:"ascii" nullable:"true" get:"user" update:"user"` // Column(VARCHAR(32, charset='ascii'), nullable=True)
Iops int `nullable:"true" default:"0" list:"user" update:"user"`
+2
View File
@@ -233,6 +233,8 @@ type IGuestDriver interface {
RequestChangeDiskStorage(ctx context.Context, userCred mcclient.TokenCredential, guest *SGuest, input *api.ServerChangeDiskStorageInternalInput, task taskman.ITask) error
RequestSwitchToTargetStorageDisk(ctx context.Context, userCred mcclient.TokenCredential, guest *SGuest, input *api.ServerChangeDiskStorageInternalInput, task taskman.ITask) error
RequestResetUefiFirmwareVars(ctx context.Context, userCred mcclient.TokenCredential, guest *SGuest) error
RequestSyncIsolatedDevice(ctx context.Context, guest *SGuest, task taskman.ITask) error
RequestCPUSet(ctx context.Context, userCred mcclient.TokenCredential, host *SHost, guest *SGuest, input *api.ServerCPUSetInput) (*api.ServerCPUSetResp, error)
@@ -115,6 +115,7 @@ func AddGuestTaskHandler(prefix string, app *appsrv.Application) {
"start-rescue": guestStartRescue,
"guest-screen-dump": guestScreenDump,
"upload-status": guestUploadStatus,
"reset-uefi-vars": guestResetUefiVars,
} {
app.AddHandler("POST",
fmt.Sprintf("%s/%s/<sid>/%s", prefix, keyWord, action),
@@ -1027,6 +1028,11 @@ func guestScreenDump(ctx context.Context, userCred mcclient.TokenCredential, sid
return gm.RequestGuestScreenDump(sid)
}
func guestResetUefiVars(ctx context.Context, userCred mcclient.TokenCredential, sid string, body jsonutils.JSONObject) (interface{}, error) {
gm := guestman.GetGuestManager()
return gm.ResetGuestUefiVars(sid)
}
// prepare rescue files
func guestStartRescue(ctx context.Context, userCred mcclient.TokenCredential, sid string, body jsonutils.JSONObject) (interface{}, error) {
return guestman.GetGuestManager().GuestStartRescue(ctx, userCred, sid, body)
+21
View File
@@ -2044,6 +2044,27 @@ func SyncGuestNicsTraffics(guestNicsTraffics *compute.GuestNicTrafficSyncInput)
}
}
func (m *SGuestManager) ResetGuestUefiVars(sid string) (*jsonutils.JSONDict, error) {
guest, _ := m.GetServer(sid)
if guest == nil {
return nil, httperrors.NewNotFoundError("guest %s not found", sid)
}
kvmGuest, ok := guest.(*SKVMGuestInstance)
if !ok {
return nil, httperrors.NewBadRequestError("guest %s not kvm instance", sid)
}
if kvmGuest.IsRunning() {
return nil, httperrors.NewBadRequestError("Can't reset ovmf vars in guest %s running", sid)
}
varsPath := kvmGuest.getOvmfVarsPath()
if fileutils2.Exists(varsPath) {
if err := os.Remove(varsPath); err != nil {
return nil, errors.Wrapf(err, "remove ovmf vars file %s", varsPath)
}
}
return nil, nil
}
var guestManager *SGuestManager
func Stop() {
+10
View File
@@ -1646,6 +1646,16 @@ func (o *ServerScreenDumpOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(o), nil
}
type ServerChangeDiskDriverOptions struct {
ServerIdOptions
DISK_ID string
DRIVER string `help:"Driver of vDisk" choices:"virtio|ide|sata|scsi|pvscsi"`
}
func (o *ServerChangeDiskDriverOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(o), nil
}
type ServerSetNetworkNumQueues struct {
ServerIdOptions
MacAddr string `help:"server network mac addr"`