VMware 虚拟机的快速启动

 

快速启动 VMware 虚拟机

借助 vmrun.exe 可实现虚拟机的快速启动,包括后台启动以及前台有界面启动;也可以实现虚拟机内应用程序的快速启动。


vmrun.exe 基础

命令行中进入 VMware Workstation Pro 安装目录,不带参数运行 vmrun.exe ,即可看到该程序的命令帮助:

(为了不影响阅读,该命令帮助已移动到了文章末尾部分)

也可以在命令行中运行 vmware.exe ,其命令选项及使用实例可以于在线文档中看到,但是该 .exe 可接收参数有限,其能力远不如 vmrun.exe ,故这里不考虑使用。


提出需求

  • 需求一
    • 我有一个 Windows 7 虚拟机,安装了一些常用的但是自认为不够单纯的软件,比如 xx、xxxx等。不求快速启动能直达应用本身,但需要有这么一个图标,可放在桌面以双击或者放在快速启动栏以通过 Win + num 唤起,从而打开 Windows 7 虚拟机。
  • 需求二
    • 有一个 Ubuntu Server 虚拟机,提供有纯粹的 Linux 环境,使用时通过 ssh 远程连接。一般情况下,打开该虚拟机需要首先打开 VMware Workstation Pro ,而后才能打开该虚机,最后当虚机打开后,为了使其在后台运行(这样没有图形界面(CLI),会少些 消耗),还需要关闭 VMware Workstation Pro (注意:需要在 编辑 -> 首选项 里勾选 -> Workstation 关闭后保持虚拟机运行)。现在需要有这么一个图标,通过其便可在后台启动该虚机。不过关闭或挂起,还是需要手动进行的。

解决思路

编写 .bat 脚本以运行 vmrun.exe 命令来实现相应动作。但是运行 .bat 脚本会打开一个cmd黑框,看起来很不舒服。这个问题可以借助.vbs脚本来解决:在 .vbs脚本中静默(后台)调用.bat 脚本。
当脚本准备好后,创建脚本的快捷方式,修改其名称及图标即可。
随后便可以将该快捷方式放到桌面、“开始”菜单等处(暂时不知道如何固定在快速启动栏)。


具体实现

以需求二的为例,该需求实现步骤及效果如下:

步骤

  • 确定相关路径:
    • vmrun.exe 路径:D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe
    • 虚拟机配置文件(.vmx)路径:D:\Virtual Machines\usvr_free\usvr_free.vmx
  • 参照 vmrun.exe 基础 编写 start_nogui.bat 脚本如下:
 "D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" -T ws start "D:\Virtual Machines\usvr_free\usvr_free.vmx" nogui

如果路径带空格,则路径整体需要用双引号包括,否则无法正常识别。

  • 编写 start_nogui.vbs 脚本如下:
set ws=wscript.createobject("wscript.shell")
ws.run "start_nogui.bat /start",0
  • 在 .vbs脚本文件之上右键,后点击“创建快捷方式”,或按住 Alt 键并拖动以创建快捷方式。
  • 从互联网上搜索 ubuntu 的图标(.ico格式)
  • 选中快捷方式,Alt Enter 以打开其属性对话框,修改其图标。
  • 将快捷方式复制到桌面。

效果

 

后记

更好的实现

对于需求二的更好的实现:双击快捷方式以执行脚本,当虚机打开时,将其挂起;否则将其打开。同时将操作及时间记录到一文件中。

效果

步骤

首先将 .bat 文件中内容修改为:

set VMRUN="D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"
set Machine="D:\Virtual Machines\usvr_free\usvr_free.vmx"
set LOG_FILE=".\operationLog.txt"

%VMRUN% list | find %Machine%

if errorlevel 1 (
	echo %date% %time% __start__ %Machine% >> %LOG_FILE%
	%VMRUN% -T ws start %Machine% nogui
) else (
	echo %date% %time% _suspend_ %Machine% >> %LOG_FILE%
	%VMRUN% -T ws suspend %Machine%
)

然后修改文件名,最后对应地,修改 .vbs 中调用的文件名,以及快捷方式指向的文件名。

 

vmrun 的命令帮助

vmrun version 1.17.0 build-14665864

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



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"

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VMware虚拟机用户手册是一本详细介绍了如何使用虚拟机软件的指南。这本手册由VMware公司编写,旨在帮助用户了解虚拟机的安装、配置和操作。 手册的第一部分介绍了虚拟化技术的概念和原理,以及VMware虚拟机软件的特点和优势。它解释了为什么虚拟机是一种强大的工具,可以提供灵活性和效率优势。 接下来,手册详细介绍了虚拟机的安装过程。它包括如何选择和下载合适的虚拟机软件、安装和配置虚拟机的步骤,以及如何创建虚拟机的实例。 手册还提供了有关如何配置虚拟机的信息。它包括了虚拟机的网络设置、存储设置和硬件配置。使用者可以根据自己的需求和设备的限制来调整这些设置。 另外,手册还解释了虚拟机的操作过程。它详细介绍了如何启动、暂停和关闭虚拟机,如何在虚拟机和宿主机之间进行文件共享,以及如何使用虚拟机快照来保存和恢复虚拟机的状态。 此外,手册还包括了一些高级特性和技巧的介绍。例如,如何使用虚拟机模板来快速创建多个相似的虚拟机,如何进行迁移和复制虚拟机,以及如何配置虚拟机的自动化管理。 总之,VMware虚拟机用户手册是一本非常实用的指南,它为初学者提供了基本的了解和操作指导,并为有经验的用户提供了进一步的信息和技巧。无论是个人用户还是企业用户,都可以从这本手册中获得宝贵的帮助和指导。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值