I recently needed to do some parameter configuration, virtual switch/net creation, virtual machine creation, VIB installation, PCI direct access, virtual machine creation, etc. on ESXi host for work reasons. So I took the time to sort out some of the pitfalls I encountered while using govc.
govc
govc is a wrapper implementation of the official VMware govmomi library. It can be used to perform a number of operations on ESXi hosts or vCenter. For example, creating virtual machines, managing snapshots, etc. Basically, everything that can be done on ESXi or vCenter has an implementation in govmomi. Currently govc supports ESXi / vCenter versions 7.0, 6.7, 6.5 , 6.0 (5.x is too old, just give up), and also supports some versions of VMware Workstation.
Using govc to connect to an ESXi host or vCenter can be done by setting environment variables or command line arguments. It is recommended to use environment variables, as there is some security risk if the user name and password are output explicitly through the command line flag.
|
|
Specify the URL of the ESXi host or vCenter with the GOVC_URL
environment variable. The login username and password can be set in the GOVC_URL or separately with GOVC_USERNAME
and GOVC_PASSWORD
. If the https certificate is a self-signed domain or IP, you need to set the GOVC_INSECURE=true
parameter to allow unsecured https connections.
|
|
If the username and password have special characters like \ @ /
, it is recommended to set GOVC_URL
, GOVC_USERNAME
and GOVC_PASSWORD
respectively. This will avoid some strange problems with special characters in GOVC_URL
.
Get host information
govc host.info
You can get basic information about an ESXi host by using the host.info self-command
- Path: The resource path of the current host in the cluster
- Manufacturer: Hardware device manufacturer
- Logical CPUs: The number of logical CPUs and the base frequency of the CPUs
- Processor type: The specific model of the CPU, since I have ES version E-2126G, I can’t show the specific model 🤣
- CPU usage: the load of CPU usage
- Memory: the amount of memory installed in the host
- Memory usage: the load of memory usage
- Boot time: Boot time
- State: Connection status
|
|
If you add the -json
parameter you will get a json output of at least 3w lines containing all the information about the ESXi host, and then you can use the jq command to filter out some of the parameters you need.
|
|
If you add the -dump
argument, the output is in the format of a Golang structure, which also contains all the information about the ESXi host, and you can use it to locate the structure of a certain piece of information relatively easily. This is very convenient for developing other functions based on govmomi. In particular, when writing unit tests, you can dump some data from here to mock. Note that not all subcommands support output in json format.
|
|
When writing unit tests, I often use it to mock some special hardware device information, which is much easier than writing these structs by hand. For example, a drive named mpx.vmhba<Adapter>:C<Channel>:T<Target>:L<LUN>
can be used to get the NAA number of the drive using the PlugStoreTopology
structure.
|
|
For example, an NVMe drive can get its serial number from the NvmeTopology
data object.
|
|
Configure ESXi host parameters
The host.options.ls
allows you to list all configuration options for the current ESXi host.
|
|
ESXi host parameters can be set via host.option.set
, for example if you want to configure the NFS storage heartbeat timeout you can do so as follows.
|
|
Enabling the ssh service
The host.service
allows you to perform related operations on the services on the ESXi host.
Create a virtual machine
|
|