auto gen disk device name

This commit is contained in:
TangBin
2018-10-29 17:35:03 +08:00
parent 5eca3c4c97
commit d3e5b17a6b
3 changed files with 51 additions and 2 deletions
+3
View File
@@ -3,6 +3,7 @@ package guestdrivers
import (
"context"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/mcclient"
@@ -47,6 +48,8 @@ func (self *SAwsGuestDriver) ValidateCreateData(ctx context.Context, userCred mc
}
func (self *SAwsGuestDriver) RequestDeployGuestOnHost(ctx context.Context, guest *models.SGuest, host *models.SHost, task taskman.ITask) error {
config := guest.GetDeployConfigOnHost(ctx, host, task.GetParams())
log.Debugf("RequestDeployGuestOnHost: %s", config)
return nil
}
+11 -2
View File
@@ -75,6 +75,7 @@ type SInstance struct {
NetworkInterfaces SNetworkInterfaces
EipAddress SEipAddress
Disks []string
DeviceNames []string
OSName string
OSType string
Description string
@@ -360,7 +361,11 @@ func (self *SInstance) AttachDisk(diskId string) error {
}
func (self *SInstance) DetachDisk(diskId string) error {
return self.host.zone.region.DetachDisk(self.InstanceId, diskId)
name, err := NextDeviceName(self.DeviceNames)
if err != nil {
return err
}
return self.host.zone.region.DetachDisk(self.InstanceId, diskId, name)
}
func (self *SInstance) getVpc() (*SVpc, error) {
@@ -403,9 +408,11 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit
tagspec.LoadingEc2Tags(instance.Tags)
disks := []string{}
devicenames := []string{}
for _, d := range instance.BlockDeviceMappings {
if d.Ebs != nil && d.Ebs.VolumeId != nil {
disks = append(disks, *d.Ebs.VolumeId)
devicenames = append(devicenames, *d.DeviceName)
}
}
@@ -455,6 +462,7 @@ func (self *SRegion) GetInstances(zoneId string, ids []string, offset int, limit
InstanceName: tagspec.GetNameTag(),
Description: tagspec.GetDescTag(),
Disks: disks,
DeviceNames: devicenames,
SecurityGroupIds: secgroups,
NetworkInterfaces: networkInterfaces,
VpcAttributes: vpcattr,
@@ -659,10 +667,11 @@ func (self *SRegion) ChangeVMConfig(zoneId string, instanceId string, ncpu int,
return nil
}
func (self *SRegion) DetachDisk(instanceId string, diskId string) error {
func (self *SRegion) DetachDisk(instanceId string, diskId string, deviceName string) error {
params := &ec2.DetachVolumeInput{}
params.SetInstanceId(instanceId)
params.SetVolumeId(diskId)
params.SetDevice(deviceName)
_, err := self.ec2Client.DetachVolume(params)
return err
+37
View File
@@ -351,4 +351,41 @@ func FillZero(i interface{}) error {
}
return nil
}
func NextDeviceName(curDeviceNames []string) (string, error) {
currents := []string{}
for _, item := range curDeviceNames{
currents = append(currents, strings.ToLower(item))
}
for i := 0; i < 25; i++ {
device := fmt.Sprintf("/dev/sd%s", string(98+i))
found := false
for _, item := range currents{
if strings.HasPrefix(item, device) {
found = true
}
}
if !found{
return device, nil
}
}
for i := 0; i < 25; i++ {
device := fmt.Sprintf("/dev/vxd%s", string(98+i))
found := false
for _, item := range currents {
if !strings.HasPrefix(item, device){
return device, nil
}
}
if !found{
return device, nil
}
}
return "", fmt.Errorf("disk devicename out of index, current deivces: %s", currents)
}