VMWare提供了vmrun与VIX API两种手段使用户可以通过程序对虚拟机进行控制。
在官方文档中给出了详细的说明和示例代码。
vmrun:http://www.vmware.com/pdf/vix162_vmrun_command.pdf
VIX API: http://www.vmware.com/support/developer/vix-api/
vmrun是一种命令行控制的手段,但是可以通过Shell脚本实现连续多个操作的自动化。而VIXAPI则是一种程序设计的方案,支持C、Perl和VB三种语言。从功能上来说,VIXAPI提供虚拟机状态查询函数,有更强的能力。例如:控制一个虚拟机恢复到某个镜像,然后登录到GuestOS中,运行其中的某个可执行文件在这3个动作中,恢复到指定镜像是异步操作,如果使用VIXAPI实现,可以查询恢复操作是否完成,恢复完成之后再尝试登录到GuestOS;而要通过vmrun实现则比较困难,因为vmrun不具有查询虚拟机状态的能力,所以只能sleep足够长的时间之后再去执行登录操作。
但vmrun也不是完全没有优势。分别用vmrun和VIX API实现每隔一段时间就将虚拟机恢复到指定虚拟机镜像的功能,当双方都只需要操作一台虚拟机的时候,都能做到长期稳定工作;当它们需要面对10台甚至更多
的虚拟机的时候,VIX API很快就会出故障(1),而vmrun仍然能够稳定地工作。
(1)通常遇到的故障是:虚拟机提示缺少某个.vmdk文件。原因是VMWare虚拟机启动时会创建临时的.vmdk文件,并通过修改.vmx中的硬盘路径,使当前的硬盘为临时的.vmdk。当VIXAPI面对大量虚拟机时,常常会与需要操作的虚拟机失去连接,此时.vmx文件中的硬盘路径被修改,但是临时.vmdk文件被删除,于是虚拟机报错。要修复这个故障,只需要把.vmx文件中的硬盘 路径改为实际存在的某个.vmdk文件即可
1、简介
VMWare提供了vmrun与VIX API两种手段使用户可以通过程序对虚拟机进行控制。vmrun是一种命令行控制的手段,但是可以通过Shell脚本实现连续多个操作的自动化。
2、语法
Usage: vmrun COMMAND [PARAMETERS]
Authentication flags
-gu <userName in guest OS>
-gp <password in guest OS>
POWER COMMANDS PARAMETERS DESCRIPTION
start Path to vmx file Start a VM or Team
or vmtm file
[gui|nogui]
stop Path to vmx file Stop a VM or Team
or vmtm file
[hard|soft]
reset Path to vmx file Reset a VM or Team
or vmtm file
[hard|soft]
suspend Path to vmx file Suspend a VM or Team
or vmtm file
[hard|soft]
SNAPSHOT COMMANDS PARAMETERS DESCRIPTION
listSnapshots Path to vmx file List all snapshots in a VM
snapshot Path to vmx file Create a snapshot of a VM
Snapshot name
deleteSnapshot Path to vmx file Remove a snapshot from a VM
Snapshot name
revertToSnapshot Path to vmx file Set VM state to a snapshot
Snapshot name
GUEST OS COMMANDS PARAMETERS DESCRIPTION
runProgramInGuest Path to vmx file Run a program in Guest OS
Program
[Program arguments]
fileExistsInGuest Path to vmx file Check if a file exists in Guest OS
Path to file in guest
setSharedFolderState Path to vmx file Modify a Host-Guest shared folder
Share name
New host path
addSharedFolder Path to vmx file Add a Host-Guest shared folder
Share name
Host path
removeSharedFolder Path to vmx file Remove a Host-Guest shared folder
Share name
listProcessesInGuest Path to vmx file List running processes in Guest OS
killProcessInGuest Path to vmx file Kill a process in Guest OS
process id
runScriptInGuest Path to vmx file Run a script in Guest OS
Interpreter path
script_text
deleteFileInGuest Path to vmx file Delete a file in Guest OS
File
createDirectoryInGuest Path to vmx file Create a directory in Guest OS
Directory path
deleteDirectoryInGuest Path to vmx file Delete a directory in Guest OS
Directory path
listDirectoryInGuest Path to vmx file List a directory in Guest OS
Directory path
copyFileFromHostToGuest Path to vmx file Copy a file from host OS to guest OS
Path on host
Path in guest
copyFileFromGuestToHost Path to vmx file Copy a file from guest OS to host OS
Path in guest
Path on host
renameFileInGuest Path to vmx file Rename a file in Guest OS
Original name
New name
GENERAL COMMANDS PARAMETERS DESCRIPTION
list List all running VMs
upgradevm Path to vmx file Upgrade VM file format, virtual hw
installtools Path to vmx file Install Tools in Guest OS
3、示例
1)开启虚拟机
vmrun start “/opt/VM_OS/RH_OS_B/Red Hat Enterprise Linux 5 64-bit.vmx” nogui|gui
2)停止虚拟机
vmrun stop “/opt/VM_OS/RH_OS_B/Red Hat Enterprise Linux 5 64-bit.vmx” nogui|gui
3)重启虚拟机
vmrun restart “/opt/VM_OS/RH_OS_B/Red Hat Enterprise Linux 5 64-bit.vmx” nogui|gui
4)列出正在运行的虚拟机
vmrun list
下面整理了一发命令:
vmrun -T ws start “/vmware/lvs1/lvs.vmx” nogui #无图形界面启动虚拟机(-T是区分宿主机的类型,ws|server|server1|fusion|esx|vc|player,常用的是ws|esx|player)
vmrun start “/vmware/lvs1/lvs.vmx” gui #带图形界面启动虚拟机
vmrun stop “/vmware/lvs1/lvs.vmx” #可跟参数hard强制关闭虚拟机(相当于直接关电源)|soft正常关闭虚拟机
vmrun reset “/vmware/lvs1/lvs.vmx” #可跟参数hard冷重启虚拟机|soft热重启虚拟机
vmrun suspend “/vmware/lvs1/lvs.vmx” #可分参数hard|soft挂起虚拟机
vmrun pause “/vmware/lvs1/lvs.vmx” #暂停虚拟机
vmrun unpause “/vmware/lvs1/lvs.vmx” #停止暂停虚拟机
vmrun list #列出正在运行的虚拟机
vmrun -T ws snapshot “/vmware/lvs1/lvs.vmx” snapshotName #创建一个快照
vmrun -T ws reverToSnapshot “/vmware/lvs1/lvs.vmx” snapshotName #从一个快照中恢复虚拟机
vmrun -T ws listSnapshots “/vmware/lvs1/lvs.vmx” #列出快照
vmrun -T ws deleteSnapshot “/vmware/lvs1/lvs.vmx” snapshotName #删除一个快照
vmrun命令帮助
EPP:/vmware # vmrun
vmrun version 1.15.0 build-2985596
Usage: vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS]
AUTHENTICATION-FLAGS
——————–
These must appear before the command and any command parameters.
-h <hostName> (not needed for Workstation)
-P <hostPort> (not needed for Workstation)
-T <hostType> (ws|server|server1|fusion|esx|vc|player)
for example, use ‘-T server’ for Server 2.0
use ‘-T server1’ for Server 1.0
use ‘-T ws’ for VMware Workstation
use ‘-T ws-shared’ for VMware Workstation (shared mode)
use ‘-T esx’ for VMware ESX
use ‘-T vc’ for VMware vCenter Server
-u <userName in host OS> (not needed for Workstation)
-p <password in host OS> (not needed for Workstation)
-vp <password for encrypted virtual machine>
-gu <userName in guest OS>
-gp <password in guest OS>
POWER COMMANDS PARAMETERS DESCRIPTION
————– ———- ———–
start Path to vmx file Start a VM or Team
[gui|nogui]
stop Path to vmx file Stop a VM or Team
[hard|soft]
reset Path to vmx file Reset a VM or Team
[hard|soft]
suspend Path to vmx file Suspend a VM or Team
[hard|soft]
pause Path to vmx file Pause a VM
unpause Path to vmx file Unpause a VM
SNAPSHOT COMMANDS PARAMETERS DESCRIPTION
—————– ———- ———–
listSnapshots Path to vmx file List all snapshots in a VM
[showTree]
snapshot Path to vmx file Create a snapshot of a VM
Snapshot name
deleteSnapshot Path to vmx file Remove a snapshot from a VM
Snapshot name
[andDeleteChildren]
revertToSnapshot Path to vmx file Set VM state to a snapshot
Snapshot name
GUEST OS COMMANDS PARAMETERS DESCRIPTION
—————– ———- ———–
runProgramInGuest Path to vmx file Run a program in Guest OS
[-noWait]
[-activeWindow]
[-interactive]
Complete-Path-To-Program
[Program arguments]
fileExistsInGuest Path to vmx file Check if a file exists in Guest OS
Path to file in guest
directoryExistsInGuest Path to vmx file Check if a directory exists in Guest OS
Path to directory in guest
setSharedFolderState Path to vmx file Modify a Host-Guest shared folder
Share name
Host path
writable | readonly
addSharedFolder Path to vmx file Add a Host-Guest shared folder
Share name
New host path
removeSharedFolder Path to vmx file Remove a Host-Guest shared folder
Share name
enableSharedFolders Path to vmx file Enable shared folders in Guest
[runtime]
disableSharedFolders Path to vmx file Disable shared folders in Guest
[runtime]
listProcessesInGuest Path to vmx file List running processes in Guest OS
killProcessInGuest Path to vmx file Kill a process in Guest OS
process id
runScriptInGuest Path to vmx file Run a script in Guest OS
[-noWait]
[-activeWindow]
[-interactive]
Interpreter path
Script text
deleteFileInGuest Path to vmx file Delete a file in Guest OS
Path in guest
createDirectoryInGuest Path to vmx file Create a directory in Guest OS
Directory path in guest
deleteDirectoryInGuest Path to vmx file Delete a directory in Guest OS
Directory path in guest
CreateTempfileInGuest Path to vmx file Create a temporary file in Guest OS
listDirectoryInGuest Path to vmx file List a directory in Guest OS
Directory path in guest
CopyFileFromHostToGuest Path to vmx file Copy a file from host OS to guest OS
Path on host Path in guest
CopyFileFromGuestToHost Path to vmx file Copy a file from guest OS to host OS
Path in guest Path on host
renameFileInGuest Path to vmx file Rename a file in Guest OS
Original name
New name
captureScreen Path to vmx file Capture the screen of the VM to a local file
Path on host
writeVariable Path to vmx file Write a variable in the VM state
[runtimeConfig|guestEnv|guestVar]
variable name
variable value
readVariable Path to vmx file Read a variable in the VM state
[runtimeConfig|guestEnv|guestVar]
variable name
getGuestIPAddress Path to vmx file Gets the IP address of the guest
[-wait]
GENERAL COMMANDS PARAMETERS DESCRIPTION
—————- ———- ———–
list List all running VMs
upgradevm Path to vmx file Upgrade VM file format, virtual hw
installTools Path to vmx file Install Tools in Guest
checkToolsState Path to vmx file Check the current Tools state
register Path to vmx file Register a VM
unregister Path to vmx file Unregister a VM
listRegisteredVM List registered VMs
deleteVM Path to vmx file Delete a VM
clone Path to vmx file Create a copy of the VM
Path to destination vmx file
full|linked
[-snapshot=Snapshot Name]
[-cloneName=Name]
Examples:
Starting a virtual machine with Workstation on a Windows host
vmrun -T ws start “c:\my VMs\myVM.vmx”
Stopping a virtual machine on an ESX host
vmrun -T esx -h https://myHost.com/sdk -u hostUser -p hostPassword stop “[storage1] vm/myVM.vmx”
Running a program in a virtual machine with Workstation on a Windows host with Windows guest
vmrun -T ws -gu guestUser -gp guestPassword runProgramInGuest “c:\my VMs\myVM.vmx” “c:\Program Files\myProgram.exe”
Running a program in a virtual machine with Server on a Linux host with Linux guest
vmrun -T server -h https://myHost.com:8333/sdk -u hostUser -p hostPassword -gu guestUser -gp guestPassword runProgramInGuest “[standard] vm/myVM.vmx” /usr/bin/X11/xclock -display :0
Creating a snapshot of a virtual machine with Workstation on a Windows host
vmrun -T ws snapshot “c:\my VMs\myVM.vmx” mySnapshot
Reverting to a snapshot with Workstation on a Windows host
vmrun -T ws revertToSnapshot “c:\my VMs\myVM.vmx” mySnapshot
Deleting a snapshot with Workstation on a Windows host
vmrun -T ws deleteSnapshot “c:\my VMs\myVM.vmx” mySnapshot
Enabling Shared Folders with Workstation on a Windows host
vmrun -T ws enableSharedFolders “c:\my VMs\myVM.vmx”
EPP:/vmware #
本文永久更新链接地址:https://www.linuxidc.com/Linux/2019-01/156357.htm