From 6886d69f1207ee251e36b7158c0f7bc4ced8ed93 Mon Sep 17 00:00:00 2001 From: divyenpatel Date: Wed, 3 May 2017 11:39:03 -0700 Subject: [PATCH] change way to fetch VM UUID from VM --- .../providers/vsphere/vsphere.go | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/pkg/cloudprovider/providers/vsphere/vsphere.go b/pkg/cloudprovider/providers/vsphere/vsphere.go index 93746a1fc3d..063769eb495 100644 --- a/pkg/cloudprovider/providers/vsphere/vsphere.go +++ b/pkg/cloudprovider/providers/vsphere/vsphere.go @@ -80,6 +80,8 @@ const ( NonSupportedControllerTypeErrMsg = "Disk is attached to non-supported controller type" FileAlreadyExistErrMsg = "File requested already exist" CleanUpDummyVMRoutine_Interval = 5 + UUIDPath = "/sys/class/dmi/id/product_serial" + UUIDPrefix = "VMware-" ) // Controller types that are currently supported for hot attach of disks @@ -227,24 +229,46 @@ func init() { }) } +// UUID gets the BIOS UUID via the sys interface. This UUID is known by vsphere +func getvmUUID() (string, error) { + id, err := ioutil.ReadFile(UUIDPath) + if err != nil { + return "", fmt.Errorf("error retrieving vm uuid: %s", err) + } + uuidFromFile := string(id[:]) + //strip leading and trailing white space and new line char + uuid := strings.TrimSpace(uuidFromFile) + // check the uuid starts with "VMware-" + if !strings.HasPrefix(uuid, UUIDPrefix) { + return "", fmt.Errorf("Failed to match Prefix, UUID read from the file is %v", uuidFromFile) + } + // Strip the prefix and while spaces and - + uuid = strings.Replace(uuid[len(UUIDPrefix):(len(uuid))], " ", "", -1) + uuid = strings.Replace(uuid, "-", "", -1) + if len(uuid) != 32 { + return "", fmt.Errorf("Length check failed, UUID read from the file is %v", uuidFromFile) + } + // need to add dashes, e.g. "564d395e-d807-e18a-cb25-b79f65eb2b9f" + uuid = fmt.Sprintf("%s-%s-%s-%s-%s", uuid[0:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:32]) + return uuid, nil +} + // Returns the name of the VM on which this code is running. -// Prerequisite: this code assumes VMWare vmtools or open-vm-tools to be installed in the VM. // Will attempt to determine the machine's name via it's UUID in this precedence order, failing if neither have a UUID: // * cloud config value VMUUID // * sysfs entry func getVMName(client *govmomi.Client, cfg *VSphereConfig) (string, error) { var vmUUID string + var err error if cfg.Global.VMUUID != "" { vmUUID = cfg.Global.VMUUID } else { // This needs root privileges on the host, and will fail otherwise. - vmUUIDbytes, err := ioutil.ReadFile("/sys/devices/virtual/dmi/id/product_uuid") + vmUUID, err = getvmUUID() if err != nil { return "", err } - - vmUUID = string(vmUUIDbytes) cfg.Global.VMUUID = vmUUID }