mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #3824 from wanyaoqi/bugfix/wyq/fix-resize-disk
bugfix: fix disk resize, host register
This commit is contained in:
@@ -376,6 +376,9 @@ func (self *SKVMGuestDriver) GetDeployStatus() ([]string, error) {
|
||||
}
|
||||
|
||||
func (self *SKVMGuestDriver) ValidateResizeDisk(guest *models.SGuest, disk *models.SDisk, storage *models.SStorage) error {
|
||||
if guest.GetDiskIndex(disk.Id) <= 0 && guest.Status == api.VM_RUNNING {
|
||||
return fmt.Errorf("Cann't online resize root disk")
|
||||
}
|
||||
if !utils.IsInStringArray(guest.Status, []string{api.VM_READY, api.VM_RUNNING}) {
|
||||
return fmt.Errorf("Cannot resize disk when guest in status %s", guest.Status)
|
||||
}
|
||||
|
||||
@@ -4892,3 +4892,12 @@ func (self *SGuestManager) checkGuestImage(ctx context.Context, input *api.Serve
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGuest) GetDiskIndex(diskId string) int8 {
|
||||
for _, gd := range self.GetDisks() {
|
||||
if gd.DiskId == diskId {
|
||||
return gd.Index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -47,8 +47,14 @@ func (self *DiskResizeTask) SetDiskReady(ctx context.Context, disk *models.SDisk
|
||||
func (self *DiskResizeTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
disk := obj.(*models.SDisk)
|
||||
|
||||
var host *models.SHost
|
||||
storage := disk.GetStorage()
|
||||
host := storage.GetMasterHost()
|
||||
guest := disk.GetGuest()
|
||||
if guest != nil {
|
||||
host = guest.GetHost()
|
||||
} else {
|
||||
host = storage.GetMasterHost()
|
||||
}
|
||||
|
||||
reason := "Cannot find host for disk"
|
||||
if host == nil || host.HostStatus != api.HOST_ONLINE {
|
||||
|
||||
@@ -30,23 +30,27 @@ type SNBDManager struct {
|
||||
|
||||
var nbdManager *SNBDManager
|
||||
|
||||
func Init() {
|
||||
nbdManager = NewNBDManager()
|
||||
func Init() error {
|
||||
var err error
|
||||
nbdManager, err = NewNBDManager()
|
||||
return err
|
||||
}
|
||||
|
||||
func GetNBDManager() *SNBDManager {
|
||||
return nbdManager
|
||||
}
|
||||
|
||||
func NewNBDManager() *SNBDManager {
|
||||
func NewNBDManager() (*SNBDManager, error) {
|
||||
var ret = new(SNBDManager)
|
||||
ret.nbdDevs = make(map[string]bool, 0)
|
||||
ret.nbdLock = new(sync.Mutex)
|
||||
ret.findNbdDevices()
|
||||
return ret
|
||||
if err := ret.findNbdDevices(); err != nil {
|
||||
return ret, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (m *SNBDManager) findNbdDevices() {
|
||||
func (m *SNBDManager) findNbdDevices() error {
|
||||
var i = 0
|
||||
for {
|
||||
if fileutils2.Exists(fmt.Sprintf("/dev/nbd%d", i)) {
|
||||
@@ -57,6 +61,10 @@ func (m *SNBDManager) findNbdDevices() {
|
||||
}
|
||||
}
|
||||
log.Infof("NBD_DEVS: %#v", m.nbdDevs)
|
||||
if len(m.nbdDevs) == 0 {
|
||||
return fmt.Errorf("No nbd devices found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SNBDManager) AcquireNbddev() string {
|
||||
|
||||
@@ -82,7 +82,6 @@ func (host *SHostService) RunService() {
|
||||
guestman.Init(hostInstance, options.HostOptions.ServersPath)
|
||||
app_common.InitAuth(&options.HostOptions.CommonOptions, func() {
|
||||
log.Infof("Auth complete!!")
|
||||
// ??? Why wait 5 seconds
|
||||
|
||||
hostInstance.StartRegister(2, func() {
|
||||
guestman.GetGuestManager().Bootstrap()
|
||||
@@ -92,10 +91,8 @@ func (host *SHostService) RunService() {
|
||||
})
|
||||
})
|
||||
|
||||
go func() {
|
||||
<-hostinfo.Instance().IsRegistered // wait host and guest init
|
||||
host.initHandlers(app)
|
||||
}()
|
||||
<-hostinfo.Instance().IsRegistered // wait host and guest init
|
||||
host.initHandlers(app)
|
||||
|
||||
// Init Metadata handler
|
||||
go metadata.StartService(
|
||||
|
||||
@@ -228,9 +228,12 @@ func (s *SDeployService) PrepareEnv() error {
|
||||
}
|
||||
output, err = procutils.NewCommand("modprobe", "nbd", "max_part=16").Run()
|
||||
if err != nil {
|
||||
log.Errorf("Failed to activate nbd device: %s", output)
|
||||
return fmt.Errorf("Failed to activate nbd device: %s", output)
|
||||
}
|
||||
err = nbd.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nbd.Init()
|
||||
|
||||
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-bdi
|
||||
for i := 0; i < 16; i++ {
|
||||
|
||||
@@ -583,7 +583,8 @@ func (h *SHostInfo) register() {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SHostInfo) onFail() {
|
||||
func (h *SHostInfo) onFail(reason interface{}) {
|
||||
log.Errorf("register failed: %s", reason)
|
||||
h.StartRegister(30, nil)
|
||||
panic("register failed, try 30 seconds later...")
|
||||
}
|
||||
@@ -593,8 +594,7 @@ func (h *SHostInfo) tryCreateNetworkOnWire() {
|
||||
masterIp, mask := h.GetMasterNicIpAndMask()
|
||||
log.Debugf("Get master ip %s and mask %d", masterIp, mask)
|
||||
if len(masterIp) == 0 || mask == 0 {
|
||||
log.Errorf("master ip %s mask %d", masterIp, mask)
|
||||
h.onFail()
|
||||
h.onFail(fmt.Sprintf("master ip %s mask %d", masterIp, mask))
|
||||
}
|
||||
params := jsonutils.NewDict()
|
||||
params.Set("ip", jsonutils.NewString(masterIp))
|
||||
@@ -605,17 +605,14 @@ func (h *SHostInfo) tryCreateNetworkOnWire() {
|
||||
hostutils.GetComputeSession(context.Background()),
|
||||
"try-create-network", params)
|
||||
if err != nil {
|
||||
log.Errorf("try create network get error %s", err)
|
||||
h.onFail()
|
||||
h.onFail(fmt.Sprintf("try create network get error %s", err))
|
||||
}
|
||||
if !jsonutils.QueryBoolean(ret, "find_matched", false) {
|
||||
log.Errorf("Fail to get network info: no networks")
|
||||
h.onFail()
|
||||
h.onFail(fmt.Sprintf("try create network get error %s", err))
|
||||
}
|
||||
wireId, err := ret.GetString("wire_id")
|
||||
if err != nil {
|
||||
log.Errorf("Fail to get network info: no wire id")
|
||||
h.onFail()
|
||||
h.onFail(fmt.Sprintf("try create network get error %s", err))
|
||||
}
|
||||
h.onGetWireId(wireId)
|
||||
}
|
||||
@@ -623,7 +620,7 @@ func (h *SHostInfo) tryCreateNetworkOnWire() {
|
||||
func (h *SHostInfo) fetchAccessNetworkInfo() {
|
||||
masterIp := h.GetMasterIp()
|
||||
if len(masterIp) == 0 {
|
||||
panic("master ip not found")
|
||||
h.onFail("master ip not found")
|
||||
}
|
||||
log.Debugf("Master ip %s to fetch wire", masterIp)
|
||||
params := jsonutils.NewDict()
|
||||
@@ -633,8 +630,7 @@ func (h *SHostInfo) fetchAccessNetworkInfo() {
|
||||
|
||||
res, err := modules.Networks.List(h.GetSession(), params)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
}
|
||||
if len(res.Data) == 0 {
|
||||
h.tryCreateNetworkOnWire()
|
||||
@@ -642,21 +638,18 @@ func (h *SHostInfo) fetchAccessNetworkInfo() {
|
||||
wireId, _ := res.Data[0].GetString("wire_id")
|
||||
h.onGetWireId(wireId)
|
||||
} else {
|
||||
log.Errorf("Fail to get network info: no networks")
|
||||
h.onFail()
|
||||
h.onFail("Fail to get network info: no networks")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SHostInfo) onGetWireId(wireId string) {
|
||||
wire, err := hostutils.GetWireInfo(context.Background(), wireId)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
}
|
||||
h.ZoneId, err = wire.GetString("zone_id")
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
h.getZoneInfo(h.ZoneId, false)
|
||||
}
|
||||
@@ -673,8 +666,7 @@ func (h *SHostInfo) getZoneInfo(zoneId string, standalone bool) {
|
||||
res, err := modules.Zones.Get(h.GetSession(),
|
||||
zoneId, params)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
}
|
||||
|
||||
h.Zone, _ = res.GetString("name")
|
||||
@@ -692,14 +684,13 @@ func (h *SHostInfo) getZoneInfo(zoneId string, standalone bool) {
|
||||
func (h *SHostInfo) getHostInfo(zoneId string) {
|
||||
masterMac := h.getMasterMacWithRefresh(true)
|
||||
if len(masterMac) == 0 {
|
||||
panic("master mac not found")
|
||||
h.onFail("master mac not found")
|
||||
}
|
||||
params := jsonutils.NewDict()
|
||||
params.Set("any_mac", jsonutils.NewString(masterMac))
|
||||
res, err := modules.Hosts.List(h.GetSession(), params)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
}
|
||||
if len(res.Data) == 0 {
|
||||
h.updateHostRecord("")
|
||||
@@ -754,7 +745,7 @@ func (h *SHostInfo) updateHostRecord(hostId string) {
|
||||
content := jsonutils.NewDict()
|
||||
masterIp := h.GetMasterIp()
|
||||
if len(masterIp) == 0 {
|
||||
panic("master ip is none")
|
||||
h.onFail("master ip is none")
|
||||
}
|
||||
|
||||
if len(hostId) == 0 {
|
||||
@@ -813,8 +804,7 @@ func (h *SHostInfo) updateHostRecord(hostId string) {
|
||||
res, err = modules.Hosts.CreateInContext(h.GetSession(), content, &modules.Zones, h.ZoneId)
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
h.onUpdateHostInfoSucc(res)
|
||||
}
|
||||
@@ -837,8 +827,7 @@ func (h *SHostInfo) updateHostReservedMem() {
|
||||
res, err := modules.Hosts.Update(h.GetSession(),
|
||||
h.HostId, content)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
h.onUpdateHostInfoSucc(res)
|
||||
}
|
||||
@@ -859,8 +848,7 @@ func (h *SHostInfo) PutHostOffline() {
|
||||
_, err := modules.Hosts.PerformAction(
|
||||
h.GetSession(), h.HostId, "offline", nil)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
h.getNetworkInfo()
|
||||
}
|
||||
@@ -881,8 +869,7 @@ func (h *SHostInfo) getNetworkInfo() {
|
||||
h.GetSession(),
|
||||
h.HostId, params)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
for _, hostwire := range res.Data {
|
||||
bridge, _ := hostwire.GetString("bridge")
|
||||
@@ -916,8 +903,7 @@ func (h *SHostInfo) uploadNetworkInfo() {
|
||||
|
||||
wireInfo, err := hostutils.GetWireOfIp(context.Background(), kwargs)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
nic.Network, _ = wireInfo.GetString("name")
|
||||
h.doUploadNicInfo(nic)
|
||||
@@ -952,8 +938,7 @@ func (h *SHostInfo) doUploadNicInfo(nic *SNIC) {
|
||||
_, err := modules.Hosts.PerformAction(h.GetSession(),
|
||||
h.HostId, "add-netif", content)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
h.onUploadNicInfoSucc(nic)
|
||||
}
|
||||
@@ -968,16 +953,14 @@ func (h *SHostInfo) doSyncNicInfo(nic *SNIC) {
|
||||
_, err := modules.Hostwires.Update(h.GetSession(),
|
||||
h.HostId, nic.WireId, query, content)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SHostInfo) onUploadNicInfoSucc(nic *SNIC) {
|
||||
res, err := modules.Hostwires.Get(h.GetSession(), h.HostId, nic.Network, nil)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
bridge, _ := res.GetString("bridge")
|
||||
iface, _ := res.GetString("interface")
|
||||
@@ -992,8 +975,7 @@ func (h *SHostInfo) onUploadNicInfoSucc(nic *SNIC) {
|
||||
}
|
||||
nic.SetWireId(wire, wireId, bandwidth)
|
||||
} else {
|
||||
log.Errorln("GetMatchNic failed!!!")
|
||||
h.onFail()
|
||||
h.onFail("GetMatchNic failed!!!")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1006,8 +988,7 @@ func (h *SHostInfo) getStoragecacheInfo() {
|
||||
res, err := modules.Storagecaches.List(
|
||||
h.GetSession(), params)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
if len(res.Data) == 0 {
|
||||
body := jsonutils.NewDict()
|
||||
@@ -1018,8 +999,7 @@ func (h *SHostInfo) getStoragecacheInfo() {
|
||||
body.Set("external_id", jsonutils.NewString(h.HostId))
|
||||
sc, err := modules.Storagecaches.Create(h.GetSession(), body)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
scid, _ := sc.GetString("id")
|
||||
storageman.GetManager().
|
||||
@@ -1043,8 +1023,7 @@ func (h *SHostInfo) getStorageInfo() {
|
||||
h.GetSession(),
|
||||
h.HostId, params)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
h.onGetStorageInfoSucc(res.Data)
|
||||
}
|
||||
@@ -1100,8 +1079,7 @@ func (h *SHostInfo) uploadStorageInfo() {
|
||||
for _, s := range storageman.GetManager().Storages {
|
||||
res, err := s.SyncStorageInfo()
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
} else {
|
||||
h.onSyncStorageInfoSucc(s, res)
|
||||
}
|
||||
@@ -1125,8 +1103,7 @@ func (h *SHostInfo) attachStorage(storage storageman.IStorage) {
|
||||
_, err := modules.Hoststorages.Attach(h.GetSession(),
|
||||
h.HostId, storage.GetId(), content)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
h.onFail(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1137,9 +1114,7 @@ func (h *SHostInfo) getIsolatedDevices() {
|
||||
params.Set("host", jsonutils.NewString(h.GetHostId()))
|
||||
res, err := modules.IsolatedDevices.List(h.GetSession(), params)
|
||||
if err != nil {
|
||||
log.Errorf("getIsolatedDevices: %v", err)
|
||||
h.onFail()
|
||||
return
|
||||
h.onFail(fmt.Sprintf("getIsolatedDevices: %v", err))
|
||||
}
|
||||
h.onGetIsolatedDeviceSucc(res.Data)
|
||||
}
|
||||
@@ -1158,9 +1133,7 @@ func (h *SHostInfo) onGetIsolatedDeviceSucc(objs []jsonutils.JSONObject) {
|
||||
}
|
||||
h.IsolatedDeviceMan.StartDetachTask()
|
||||
if err := h.IsolatedDeviceMan.BatchCustomProbe(); err != nil {
|
||||
log.Errorf("Device probe error: %v", err)
|
||||
h.onFail()
|
||||
return
|
||||
h.onFail(fmt.Sprintf("Device probe error: %v", err))
|
||||
}
|
||||
h.uploadIsolatedDevices()
|
||||
}
|
||||
@@ -1168,9 +1141,7 @@ func (h *SHostInfo) onGetIsolatedDeviceSucc(objs []jsonutils.JSONObject) {
|
||||
func (h *SHostInfo) uploadIsolatedDevices() {
|
||||
for _, dev := range h.IsolatedDeviceMan.Devices {
|
||||
if err := dev.SyncDeviceInfo(h); err != nil {
|
||||
log.Errorf("Sync device %s: %v", dev.String(), err)
|
||||
h.onFail()
|
||||
return
|
||||
h.onFail(fmt.Sprintf("Sync device %s: %v", dev.String(), err))
|
||||
}
|
||||
}
|
||||
h.deployAdminAuthorizedKeys()
|
||||
@@ -1178,8 +1149,7 @@ func (h *SHostInfo) uploadIsolatedDevices() {
|
||||
|
||||
func (h *SHostInfo) deployAdminAuthorizedKeys() {
|
||||
onErr := func(format string, args ...interface{}) {
|
||||
log.Errorf(format, args...)
|
||||
h.onFail()
|
||||
h.onFail(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
sshDir := path.Join("/root", ".ssh")
|
||||
@@ -1187,7 +1157,6 @@ func (h *SHostInfo) deployAdminAuthorizedKeys() {
|
||||
if os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(sshDir, 0755); err != nil {
|
||||
onErr("Create ssh dir %s: %v", sshDir, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
onErr("Stat %s dir: %v", sshDir, err)
|
||||
@@ -1199,11 +1168,9 @@ func (h *SHostInfo) deployAdminAuthorizedKeys() {
|
||||
ret, err := modules.Sshkeypairs.List(h.GetSession(), query)
|
||||
if err != nil {
|
||||
onErr("Get admin sshkey: %v", err)
|
||||
return
|
||||
}
|
||||
if len(ret.Data) == 0 {
|
||||
onErr("Not found admin sshkey")
|
||||
return
|
||||
}
|
||||
keys := ret.Data[0]
|
||||
adminPublicKey, _ := keys.GetString("public_key")
|
||||
@@ -1215,11 +1182,9 @@ func (h *SHostInfo) deployAdminAuthorizedKeys() {
|
||||
newKeys := fsdriver.MergeAuthorizedKeys(oldKeys, pubKeys)
|
||||
if err := fileutils2.FilePutContents(authFile, newKeys, false); err != nil {
|
||||
onErr("Write public keys: %v", err)
|
||||
return
|
||||
}
|
||||
if err := os.Chmod(authFile, 0644); err != nil {
|
||||
onErr("Chmod %s to 0644: %v", authFile, err)
|
||||
return
|
||||
}
|
||||
h.onSucc()
|
||||
}
|
||||
|
||||
@@ -292,7 +292,6 @@ func NewNIC(desc string) (*SNIC, error) {
|
||||
nic.Bandwidth = 1000
|
||||
|
||||
log.Infof("IP %s/%s/%s", nic.Ip, nic.Bridge, nic.Inter)
|
||||
// 这是干啥呢 ???
|
||||
if len(nic.Ip) > 0 {
|
||||
var max, wait = 30, 0
|
||||
for wait < max {
|
||||
|
||||
Reference in New Issue
Block a user