首頁 > 軟體

vmrun命令使用詳解

2020-06-16 16:40:19

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 VMsmyVM.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 VMsmyVM.vmx" "c:Program FilesmyProgram.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 VMsmyVM.vmx" mySnapshot
 
 
Reverting to a snapshot with Workstation on a Windows host
  vmrun -T ws revertToSnapshot "c:my VMsmyVM.vmx" mySnapshot
 
 
Deleting a snapshot with Workstation on a Windows host
  vmrun -T ws deleteSnapshot "c:my VMsmyVM.vmx" mySnapshot
 
Enabling Shared Folders with Workstation on a Windows host
  vmrun -T ws enableSharedFolders "c:my VMsmyVM.vmx"
EPP:/vmware #


IT145.com E-mail:sddin#qq.com