一、简介
VMware vSphere APIs (ESXi and/or vCenter)的go语言客户端
GitHub地址:https://github.com/vmware/govmomi
govc下载地址:https://github.com/vmware/govmomi/releases
govc使用手册:https://github.com/vmware/govmomi/blob/master/govc/USAGE.md
二、安装配置
1、安装
在govc下载地址下载对应平台的二进制包
curl -L $URL_TO_BINARY | gunzip > /usr/local/bin/govc
chmod +x /usr/local/bin/govc
2、配置
govc是通过设置环境变量进行配置的。
GOVC_URL
:ESXi或vCenter实例的地址默认协议
https
,URL路径为/sdk
。可在URL中设置用户名密码,例如:https://user:pass@host/sdk
.如果用户名密码中包含特殊字符(
\
,#
,:
),可以在GOVC_USERNAME
,GOVC_PASSWORD
单独设置用户名密码。GOVC_USERNAME
:用户名GOVC_PASSWORD
:密码GOVC_TLS_CA_CERTS
:指定CA证书$ export GOVC_TLS_CA_CERTS=~/.govc_ca.crt # 多证书设置 $ export GOVC_TLS_CA_CERTS=~/ca-certificates/bar.crt:~/ca-certificates/foo.crt
GOVC_TLS_KNOWN_HOSTS
:指定验证证书的指纹$ export GOVC_TLS_KNOWN_HOSTS=~/.govc_known_hosts $ govc about.cert -u host -k -thumbprint | tee -a $GOVC_TLS_KNOWN_HOSTS $ govc about -u user:pass@host
GOVC_TLS_HANDSHAKE_TIMEOUT
: TLS握手的超时时间GOVC_INSECURE
:关闭证书验证export GOVC_INSECURE=1
GOVC_DATACENTER
:GOVC_DATASTORE
:GOVC_NETWORK
:GOVC_RESOURCE_POOL
:GOVC_HOST
:GOVC_GUEST_LOGIN
:GOVC_VIM_NAMESPACE
:GOVC_VIM_VERSION
:
以上变量可在~/.zshrc或/etc/profile或~/.bashrc
中设置,同时可使用govc env
查看设置。
三、VM的创建
命令详情
Usage: govc vm.create [OPTIONS] NAME
Create VM.
For a list of possible '-g' IDs, see:
http://pubs.vmware.com/vsphere-6-5/topic/com.vmware.wssdk.apiref.doc/vim.vm.GuestOsDescriptor.GuestOsIdentifier.html
Examples:
govc vm.create -on=false vm-name
govc vm.create -cluster cluster1 vm-name # use compute cluster placement
govc vm.create -datastore-cluster dscluster vm-name # use datastore cluster placement
govc vm.create -m 2048 -c 2 -g freebsd64Guest -net.adapter vmxnet3 -disk.controller pvscsi vm-name
Options:
-annotation= VM description
-c=1 Number of CPUs
-cluster= Use cluster for VM placement via DRS
-datastore-cluster= Datastore cluster [GOVC_DATASTORE_CLUSTER]
-disk= Disk path (to use existing) OR size (to create new, e.g. 20GB)
-disk-datastore= Datastore for disk file
-disk.controller=scsi Disk controller type
-ds= Datastore [GOVC_DATASTORE]
-firmware=bios Firmware type [bios|efi]
-folder= Inventory folder [GOVC_FOLDER]
-force=false Create VM if vmx already exists
-g=otherGuest Guest OS ID
-host= Host system [GOVC_HOST]
-iso= ISO path
-iso-datastore= Datastore for ISO file
-link=true Link specified disk
-m=1024 Size in MB of memory
-net= Network [GOVC_NETWORK]
-net.adapter=e1000 Network adapter type
-net.address= Network hardware address
-on=true Power on VM
-pool= Resource pool [GOVC_RESOURCE_POOL]
-version= ESXi hardware version [5.0|5.5|6.0|6.5|6.7]
示例
govc vm.create -m 2048 -c 2 -disk=30G -host.ip=192.168.1.8 test1
四、VM的管理
具体命令详解可查看文档:https://github.com/vmware/govmomi/blob/master/govc/USAGE.md
1、查询操作
查看所有VM
govc find . -type m
查看所有开机的VM
govc find . -type m -runtime.powerState poweredOn
2、VM电源的开启与关闭
vmname=test
# 开启VM电源
govc vm.power -on -M $vmname
# 关闭VM电源
govc vm.power -off -M $vmname
3、VM的销毁
使用VMWare OVF Tool部署OVF/OVA模板到远程ESXI,详见OVF 管理工具VMWare OVF Tool
关闭VM电源,并删除VM
govc vm.destroy vm_name
4、在VM中进行的操作
Prerequisite
VM安装VMware-Tools工具后进行重启
- 可直接使用包管理工具安装,例如:
yum install -y open-vm-tools
- 可直接使用包管理工具安装,例如:
设置要访问VM的名字及登录用户密码
GOVC_GUEST_LOGIN="root:******" # 如果密码中包含特殊字符“!”,使用"\"进行转义."@"不需要转义 vmname="test"
命令
govc guest.* -vm $vmname
guest.chmod:修改VM中文件的权限
guest.chown:设置VM中文件的所有者
guest.df:显示VM中文件的使用情况
govc guest.df -vm $vmname
guest.download:拷贝VM中的文件到本地
guest.getenv:查看VM中的环境变量
guest.kill:杀掉VM中的进程
guest.ls:查看VM中的文件系统
# 例如查看指定VM中“/root”下的文件夹 govc guest.ls -vm $vmname /root
guest.mkdir:在VM中创建文件夹
govc guest.mkdir -vm $vmname /root/test
guest.mktemp:在VM中创建临时文件或文件夹
guest.mv:在VM中移动文件
guest.ps:查看VM中的进程
guest.rm:删除VM中的文件
guest.rmdir:删除VM中的文件夹
guest.run:在VM中运行命令,并显示输出结果
Usage: govc guest.run [OPTIONS] PATH [ARG]... Examples: govc guest.run -vm $vmname ifconfig govc guest.run -vm $vmname ifconfig eth0 cal | govc guest.run -vm $vmname -d - cat govc guest.run -vm $vmname -d "hello $USER" cat govc guest.run -vm $vmname curl -s :invalid: || echo $? # exit code 6 govc guest.run -vm $vmname -e FOO=bar -e BIZ=baz -C /tmp env Options: -C= The absolute path of the working directory for the program to start -d= Input data string. A value of '-' reads from OS stdin -e=[] Set environment variables -i=false Interactive session -l=: Guest VM credentials [GOVC_GUEST_LOGIN] -vm= Virtual machine [GOVC_VM] govc guest.run -l $GOVC_GUEST_LOGIN -vm $vmname sh -c /root/beforeShutDown.sh
guest.start:在VM中启动程序,并显示输出结果
Usage: govc guest.start [OPTIONS] PATH [ARG]... Examples: govc guest.start -vm $vmname /bin/mount /dev/hdb1 /data pid=$(govc guest.start -vm $vmname /bin/long-running-thing) govc guest.ps -vm $vmname -p $pid -X Options: -C= The absolute path of the working directory for the program to start -e=[] Set environment variable (key=val) -i=false Interactive session -l=: Guest VM credentials [GOVC_GUEST_LOGIN] -vm= Virtual machine [GOVC_VM]
guest.touch:在VM中创建文件
guest.upload:上传本地文件到VM中
govc guest.upload -vm $vmname ./**.tar.gz /root/***.tar.gz
5、查看VM信息
Usage: govc vm.info [OPTIONS] VM...
Display info for VM.
The '-r' flag displays additional info for CPU, memory and storage usage,
along with the VM's Datastores, Networks and PortGroups.
Examples:
govc vm.info $vm
govc vm.info -r $vm | grep Network:
govc vm.info -json $vm
govc find . -type m -runtime.powerState poweredOn | xargs govc vm.info
Options:
-e=false Show ExtraConfig
-g=true Show general summary
-r=false Show resource summary
-t=false Show ToolsConfigInfo
-waitip=false Wait for VM to acquire IP address