From fc64ea3252ddcead0cf7e9b56c750fda0b6fc7cd Mon Sep 17 00:00:00 2001 From: TangBin Date: Tue, 15 Dec 2020 13:58:42 +0800 Subject: [PATCH] fix(region): aws create root device name fix --- pkg/multicloud/aws/host.go | 2 +- pkg/multicloud/aws/image.go | 4 +++- pkg/multicloud/aws/instance.go | 17 +++++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pkg/multicloud/aws/host.go b/pkg/multicloud/aws/host.go index 7f4af7aa16..40a2744135 100644 --- a/pkg/multicloud/aws/host.go +++ b/pkg/multicloud/aws/host.go @@ -263,7 +263,7 @@ func (self *SHost) _createVM(name, imgId string, sysDisk cloudprovider.SDiskInfo // 创建实例 if len(instanceType) > 0 { log.Debugf("Try instancetype : %s", instanceType) - vmId, err := self.zone.region.CreateInstance(name, imgId, instanceType, networkId, secgroupId, self.zone.ZoneId, desc, disks, ipAddr, keypair, userData, tags) + vmId, err := self.zone.region.CreateInstance(name, img, instanceType, networkId, secgroupId, self.zone.ZoneId, desc, disks, ipAddr, keypair, userData, tags) if err != nil { log.Errorf("Failed for %s: %s", instanceType, err) return "", fmt.Errorf("Failed to create specification %s.%s", instanceType, err.Error()) diff --git a/pkg/multicloud/aws/image.go b/pkg/multicloud/aws/image.go index d1369d556d..bf8fde0cbd 100644 --- a/pkg/multicloud/aws/image.go +++ b/pkg/multicloud/aws/image.go @@ -92,7 +92,8 @@ type SImage struct { Status ImageStatusType OwnerType string // Usage string - RootDevice RootDevice + RootDevice RootDevice + RootDeviceName string Public bool Hypervisor string @@ -510,6 +511,7 @@ func (self *SRegion) getImages(status ImageStatusType, owners []TImageOwnerType, OwnerType: *image.ImageOwnerAlias, EnaSupport: *image.EnaSupport, Platform: *image.Platform, + RootDeviceName: *image.RootDeviceName, Status: ImageStatusType(*image.State), CreationTime: createTime, SizeGB: size, diff --git a/pkg/multicloud/aws/instance.go b/pkg/multicloud/aws/instance.go index 7af28ba5d8..51732226ce 100644 --- a/pkg/multicloud/aws/instance.go +++ b/pkg/multicloud/aws/instance.go @@ -494,7 +494,7 @@ func (self *SInstance) RebuildRoot(ctx context.Context, desc *cloudprovider.SMan userdata = base64.StdEncoding.EncodeToString([]byte(data)) } - diskId, err := self.host.zone.region.ReplaceSystemDisk(ctx, self.InstanceId, desc.ImageId, desc.SysSizeGB, keypairName, userdata) + diskId, err := self.host.zone.region.ReplaceSystemDisk(ctx, self.InstanceId, image, desc.SysSizeGB, keypairName, userdata) if err != nil { return "", err } @@ -722,7 +722,7 @@ func (self *SRegion) GetInstanceIdByImageId(imageId string) (string, error) { return "", fmt.Errorf("instance launch with image %s not found", imageId) } -func (self *SRegion) CreateInstance(name string, imageId string, instanceType string, SubnetId string, securityGroupId string, +func (self *SRegion) CreateInstance(name string, image *SImage, instanceType string, SubnetId string, securityGroupId string, zoneId string, desc string, disks []SDisk, ipAddr string, keypair string, userData string, ntags map[string]string, ) (string, error) { @@ -747,7 +747,11 @@ func (self *SRegion) CreateInstance(name string, imageId string, instanceType st VolumeType: &disk.Category, } - deviceName = fmt.Sprintf("/dev/sda1") + if len(image.RootDeviceName) > 0 { + deviceName = image.RootDeviceName + } else { + deviceName = fmt.Sprintf("/dev/sda1") + } } else { var size int64 size = int64(disk.Size) @@ -804,8 +808,9 @@ func (self *SRegion) CreateInstance(name string, imageId string, instanceType st return "", err } + imgId := image.GetId() params := ec2.RunInstancesInput{ - ImageId: &imageId, + ImageId: &imgId, InstanceType: &instanceType, MaxCount: &count, MinCount: &count, @@ -967,7 +972,7 @@ func (self *SRegion) UpdateVM(instanceId string, hostname string) error { return fmt.Errorf("aws not support change hostname.") } -func (self *SRegion) ReplaceSystemDisk(ctx context.Context, instanceId string, imageId string, sysDiskSizeGB int, keypair string, userdata string) (string, error) { +func (self *SRegion) ReplaceSystemDisk(ctx context.Context, instanceId string, image *SImage, sysDiskSizeGB int, keypair string, userdata string) (string, error) { instance, err := self.GetInstance(instanceId) if err != nil { return "", err @@ -998,7 +1003,7 @@ func (self *SRegion) ReplaceSystemDisk(ctx context.Context, instanceId string, i // create tmp server tempName := fmt.Sprintf("__tmp_%s", instance.GetName()) _id, err := self.CreateInstance(tempName, - imageId, + image, instance.InstanceType, subnetId, "",