雑木林

頭の中の整理と忘れないための確認メモ

VMware Workstation Playerでもheadlessで制御したい

これまでVMUG Advantageで取得したWorkstation Proを使っていましたが、有効期限切れになってしまいました。
渋々Workstation Playerに戻したのですが、複数の仮想マシンを起動するとウィンドウが邪魔になりますよね。
VirtualBoxのようにヘッドレス起動ができれば良いのですが、無かったので調べてみました。

要約

環境

vmrunでできること

WorkstationやFusionの仮想マシンを操作する方法としてvmrunがあります。
Workstation 16以降では本体に同梱されWindows環境であれば、標準で以下のパスに格納されています。
C:\Program Files (x86)\VMware\VMware Player

vmrunを引数なしで実行すると、使い方が表示されます。
ネット上の記事では 引数 -T が使われていることが多いですが、オプションなので省略可能です。

PS C:\> & 'C:\Program Files (x86)\VMware\VMware Player\vmrun.exe'

vmrun version 1.17.0 build-21139696

Usage: vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS]



AUTHENTICATION-FLAGS
--------------------
These must appear before the command and any command parameters.

   -T <hostType> (ws|fusion||player)
   -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



HOST NETWORK COMMANDS    PARAMETERS           DESCRIPTION
---------------------    ----------           -----------
listHostNetworks                              List all networks in the host

listPortForwardings      Host network name    List all available port forwardings on a host network


setPortForwarding        Host network name    Add or update a port forwarding on a host network
                         Protocol
                         Host port
                         Guest ip
                         Guest port
                         [Description]

deletePortForwarding     Host network name    Delete a port forwarding on a host network
                         Protocol
                         Host port




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

typeKeystrokesInGuest    Path to vmx file     Type Keystrokes in Guest OS
                         keystroke string

connectNamedDevice       Path to vmx file     Connect the named device in the Guest OS
                         device name

disconnectNamedDevice    Path to vmx file     Disconnect the named device in the Guest OS
                         device 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

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]



Template VM COMMANDS     PARAMETERS           DESCRIPTION
---------------------    ----------           -----------
downloadPhotonVM         Path for new VM      Download Photon VM





Examples:


Starting a virtual machine with Workstation on a Windows host
   vmrun -T ws start "c:\my VMs\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"


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"

vmrunを使った仮想マシンの起動、停止

基本的にはvmrun.exe コマンド vmxファイルで操作可能です。
以下に基本的な操作方法を記載します(vmxファイルのパスは適宜読み替えてください。)

仮想マシンをヘッドレスで起動する

ヘッドレスで起動する場合は、startコマンドに nogui を追加します。

PS C:\> & 'C:\Program Files (x86)\VMware\VMware Player\vmrun.exe' start C:\Users\XXXX\Documents\VirtualMachines\alma8\alma8.vmx nogui

ヘッドレスで起動した後にウィンドウを表示したい場合は、Workstation Playerを起動し、対象のVMをダブルクリックすると表示されます。

起動中の仮想マシンを表示する

PS C:\> & 'C:\Program Files (x86)\VMware\VMware Player\vmrun.exe' list
Total running VMs: 1
C:\Users\XXXX\Documents\VirtualMachines\alma8\alma8.vmx

仮想マシンサスペンドする

PS C:\> & 'C:\Program Files (x86)\VMware\VMware Player\vmrun.exe' suspend C:\Users\XXXX\Documents\VirtualMachines\alma8\alma8.vmx

Workstation Player上に表示されるVM一覧

Workstation Playerを起動した際に表示されるVM一覧は以下のファイルに記載されています。
%USERPROFILE%\Application Data\VMware\preferences.ini
Linuxの場合は~/.vmware/preferencesにあるみたい)

pref.mruVM0.filename = "C:\Users\XXXX\Documents\VirtualMachines\alma8\alma8.vmx"
pref.mruVM0.displayName = "alma8"
pref.mruVM0.index = "0"
pref.mruVM1.filename = "C:\Users\XXXX\Documents\VirtualMachines\rhel9\rhel9.vmx"
pref.mruVM1.displayName = "rhel9"
pref.mruVM1.index = "1"
pref.mruVM2.filename = "C:\Users\XXXX\Documents\VirtualMachines\rhel\rhel.vmx"
pref.mruVM2.displayName = "rhel"
pref.mruVM2.index = "2"
pref.mruVM3.filename = "C:\Users\XXXX\Documents\VirtualMachines\ubuntu22.04\ubuntu22.04.vmx"
pref.mruVM3.displayName = "ubuntu22.04"
pref.mruVM3.index = "3"

簡単に使うためのPowerShellスクリプト

vurunを使うにせよvmxファイルのパスを記載するのは面倒なので、簡易的に管理するためのスクリプトを作ってみました。
preferences.iniから仮想マシンの一覧を取得し、仮想マシン名のみで管理できるようにしています。
使い方は、以下のgithubのページを見てください。

github.com

VMware Workstation PlayerはLinux版もありますし、vmrunはFusionもコントロールできるのでもう少し発展できそうです。
先々クロスプラットフォームな何かを作れるとよいな…。