mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
change way to fetch VM UUID from VM
This commit is contained in:
parent
e537cec9b0
commit
6886d69f12
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user