From f4e98cc717371cc0e0cea91295ec74376aa3246b Mon Sep 17 00:00:00 2001 From: TangBin Date: Mon, 22 Feb 2021 11:37:29 +0800 Subject: [PATCH] aws change flavor fix --- pkg/multicloud/aws/image.go | 6 ++++++ pkg/multicloud/aws/instance.go | 37 +++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/pkg/multicloud/aws/image.go b/pkg/multicloud/aws/image.go index 161a5c9bf4..710116137d 100644 --- a/pkg/multicloud/aws/image.go +++ b/pkg/multicloud/aws/image.go @@ -99,6 +99,8 @@ type SImage struct { // Usage string RootDevice RootDevice RootDeviceName string + // devices + BlockDevicesNames []string Public bool Hypervisor string @@ -512,12 +514,15 @@ func (self *SRegion) getImages(status ImageStatusType, owners []TImageOwnerType, } var rootDevice RootDevice + devicesName := []string{} for _, block := range image.BlockDeviceMappings { if len(*image.RootDeviceName) > 0 && *block.DeviceName == *image.RootDeviceName { rootDevice.SnapshotId = *block.Ebs.SnapshotId rootDevice.Category = *block.Ebs.VolumeType rootDevice.Size = int(*block.Ebs.VolumeSize) } + + devicesName = append(devicesName, *block.DeviceName) } osType := "" @@ -547,6 +552,7 @@ func (self *SRegion) getImages(status ImageStatusType, owners []TImageOwnerType, EnaSupport: *image.EnaSupport, Platform: *image.Platform, RootDeviceName: *image.RootDeviceName, + BlockDevicesNames: devicesName, Status: ImageStatusType(*image.State), CreationTime: createTime, SizeGB: size, diff --git a/pkg/multicloud/aws/instance.go b/pkg/multicloud/aws/instance.go index 33d5476922..6c50dc7923 100644 --- a/pkg/multicloud/aws/instance.go +++ b/pkg/multicloud/aws/instance.go @@ -27,6 +27,7 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/osprofile" + "yunion.io/x/pkg/utils" billing_api "yunion.io/x/onecloud/pkg/apis/billing" api "yunion.io/x/onecloud/pkg/apis/compute" @@ -65,6 +66,7 @@ type SInstance struct { multicloud.SInstanceBase host *SHost + img *SImage RegionId string ZoneId string InstanceId string @@ -582,7 +584,33 @@ func (self *SInstance) GetVNCInfo() (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotSupported } +func (self *SInstance) GetImage() (*SImage, error) { + if self.img != nil { + return self.img, nil + } + + img, err := self.host.zone.region.GetImage(self.ImageId) + if err != nil { + return nil, errors.Wrap(err, "GetImage") + } + + self.img = img + return self.img, nil +} + func (self *SInstance) AttachDisk(ctx context.Context, diskId string) error { + img, err := self.GetImage() + if err != nil { + return errors.Wrap(err, "GetImage") + } + + // mix in image block device names + for i := range img.BlockDevicesNames { + if !utils.IsInStringArray(img.BlockDevicesNames[i], self.DeviceNames) { + self.DeviceNames = append(self.DeviceNames, img.BlockDevicesNames[i]) + } + } + name, err := NextDeviceName(self.DeviceNames) if err != nil { return err @@ -803,6 +831,7 @@ func (self *SRegion) CreateInstance(name string, image *SImage, instanceType str for i := range disks { var ebs ec2.EbsBlockDevice var deviceName string + var err error disk := disks[i] if i == 0 { @@ -832,9 +861,11 @@ func (self *SRegion) CreateInstance(name string, image *SImage, instanceType str VolumeSize: &size, VolumeType: &disk.Category, } - // todo: generator device name - // todo: 这里还需要测试预置硬盘的实例。deviceName是否会冲突。 - deviceName = fmt.Sprintf("/dev/sd%s", string(98+i)) + + deviceName, err = NextDeviceName(image.BlockDevicesNames) + if err != nil { + return "", errors.Wrap(err, "NextDeviceName") + } } // io1类型的卷需要指定IOPS参数。这里根据aws网站的建议值进行设置