KumpelJ
KumpelJ

Reputation: 45

Set VM's hostname automatically in debian as the VM's name

I want to set the VM's hostname (it is a debian squeeze system with open-vm-tools installed) automatically to the VM's name which I set and see in the vSphere Client..

I tried

    ~# vmtoolsd --cmd "info-get guestinfo.name" 2> /etc/hostname

but the command returns "No value found"

Upvotes: 1

Views: 9352

Answers (2)

Anon Ymous
Anon Ymous

Reputation: 56

Found this on another site. Seems that you have to set the values before you can query them.

There may be situations where you want to determine the vCenter display name for a VM from within the OS of the VM.

This could be useful if cloning multiple virtual desktops from a syspreped template to enable the option to set the machine name to be the same as the vCenter display name. It could also be useful in many other scenarios.

However, by default there is no way to do this using the standard VM tools that are installed into the virtual machine.

It is possible though to set a custom attribute on the VM object in vCenter and then query this from within the the OS of the virtual machine.

The following script can be run using the vSphere PowerCLI to set a custom attribute to be the same as the vCenter display name:

$vServer= “vCenter.server.fqdn”
$vmName = “VM display name”

If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}

Connect-VIServer $vServer | out-null

$vmSet = GET-VM $vmName | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$gInfo = New-Object VMware.Vim.optionvalue
$ginfo.Key=”guestinfo.hostname”
$gInfo.Value=$vmSet.Name
$vmConfigSpec.extraconfig += $gInfo
$vmSet.ReconfigVM($vmConfigSpec)

Disconnect-VIServer $vServer -Confirm:$false | out-null

Once this is set, it is possible to query it within the VM, using the VM tools and the following command:

vmtoolsd.exe –cmd “info-get guestinfo.hostname”

This can of course, be added to scripts to set this attribute against multiple machines.

Credit goes to Richard Parmiter!

Upvotes: 4

jkhines
jkhines

Reputation: 1139

I did this with a Python script on my Linux guest operating system by using VMware's pyVmomi module.

First I retrieved the system UUID by reading the system file at /sys/devices/virtual/dmi/id/product_uuid. Then I searched the vCenter server for the virtual machine via UUID based on the find_by_uuid.py example from the pyvmomi-community-samples site. Another alternative is to seach by IP address, which would be more platform-independent. The pyVmomi module provides a FindByIp() method that enables this approach.

#!/usr/bin/env python
import atexit
import pyVmomi
from pyVmomi import vim, vmodl
from pyVim.connect import SmartConnect, Disconnect

si = SmartConnect(host='<host>', port='<port>', user='<user>', pwd='<password>')
atexit.register(Disconnect, si)
file = open('/sys/devices/virtual/dmi/id/product_uuid')
uuid = file.read().strip().lower()
file.close()

search_index = si.content.searchIndex
vm = search_index.FindByUuid(None, uuid, True, False)
#Alternatively: vm = search_index.FindByIp(None, <ip_address>, True)
print vm.summary.config.name

Once you have the name of the virtual machine, you can use your guest operating system's commands (e.g. hostname) to perform the rename.

Upvotes: 7

Related Questions