十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
日常工作中,由于条件以及成本的限制,我们会在自己本地电脑上安装虚拟机装服务器系统,来满足自己工作中对服务器的需求,但是很多时候,我们自己的电脑关机或者重启后,虚拟机并不会自行启动,需要我们手动启动虚拟机,如果我们只有一两台虚拟机,那没什么,启动很快,但是,如果我们有很多台虚拟机呢?10 台,20 台,甚至更多,难道我们还要手动一个个开机吗?那真是太让人头疼,又太浪费时间。下面,我们就讲下,如何让虚拟机在我们自己的主机开机时自动启动。
做网站、成都网站制作服务团队是一支充满着热情的团队,执着、敏锐、追求更好,是创新互联的标准与要求,同时竭诚为客户提供服务是我们的理念。创新互联把每个网站当做一个产品来开发,精雕细琢,追求一名工匠心中的细致,我们更用心!一、VMware workstation 虚拟机开机自启动的原理
要说 VMware workstation 虚拟机开机自启动,在 VMware workstation 图形界面是找不到设置方法的,但是图形界面找不到设置方法,不代表它就没有设置方法。
要设置开机自启动,首先,我们得了解下 VMware workstation 所提供的命令行工具 vmrun 。
1、我们进入到 VMware workstation 的安装目录,找一下 vmrun.exe 文件,如下图,正式这个工具,为我们提供了虚拟机的自启动方法。
2、将 VMware workstation 的安装目录添加到系统的环境变量,如下图,按照途中的顺序,依次操作即可
注意:在第 5 步时,需要找到自己的 VMware workstation 的安装目录,然后添加到这里。
3、在 DOS 界面中查看 vmrun 命令的语法格式及参数
Microsoft Windows [版本 10.0.17134.471] (c) 2018 Microsoft Corporation。保留所有权利。 C:\Users\CaoBo>vmrun vmrun version 1.17.0 build-10952284 Usage: vmrun [AUTHENTICATION-FLAGS] COMMAND [PARAMETERS] AUTHENTICATION-FLAGS -------------------- These must appear before the command and any command parameters. -T(ws|fusion||player) -vp -gu -gp 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"
①、验证标志(AUTHENTICATION-FLAGS):
-T 代表目标类型,ws 表示工作站(即 Workstation ),一般使用此参数即可,其他很少使用,就不做解释了。当然还有 server、esx、vc 等等,这里没有列出。
②、电源指令(POWER COMMANDS):
start:开启虚拟机
stop:关闭虚拟机
reset:重置虚拟机
suspend:挂起虚拟机
③、快照指令(SNAPSHOT COMMANDS):
listSnapshots:列出虚拟机的快照数量及名称
snapshot:为虚拟机创建快照
deleteSnapshot:删除虚拟机的快照
revertToSnapshot:从一个快照恢复虚拟机
④、常用命令(GENERAL COMMANDS):
list:列出正在运行的虚拟机的数量及名称
upgradevm:升级虚拟机的文件格式,虚拟硬件
installTools:在 Guest 中安装工具
checkToolsState:检查工具状态
deleteVM:删除一个虚拟机
clone:克隆虚拟机
这里只介绍这么多命令,其他的命令很少用到,就不多做解释。
言归正传,我们要设置虚拟机开机自启动,就要使用上面 start 命令。那么,此命令如何使用呢,格式时这样的:
vmrun -T ws start "c:\my VMs\myVM.vmx"
"c:\my VMs\myVM.vmx" 该目录为虚拟机的工作目录
我们可以在这条命令后面加上 nogui ,表示无图形界面启动,而默认的启动模式是 gui 模式,即有图形界面启动。
vmrun -T ws start "c:\my VMs\myVM.vmx" nogui
最后,我们将该命令修改为自己的虚拟机工作目录后写到批处理文件中(如下图),添加开机启动脚本或者计划任务。
@echo off & setlocal enabledelayedexpansion vmrun -T ws start "I:\Documents\Virtual Machines\centos\centos.vmx" nogui
附上关机脚本:
@echo off & setlocal enabledelayedexpansion vmrun list > vmlist.txt for %%i in (vmlist.txt) do ( set "f=%%i" for /f "usebackq delims=" %%j in ("!f!") do set/a n+=1 for /f "delims=" %%m in ('"type "!f!"|more /E +1 & cd. 2^>!f!"') do set/a x+=1&if !x! leq !n! echo;%%m>>!f! set/a n=0,x=0 ) for /f "delims=" %%a in (vmlist.txt) do ( vmrun -T ws stop "%%a" nogui ) pause
二、Windows 中设置虚拟机开机自启动
在 Windows 中设置虚拟机开机自启动又两种办法
①、用计划任务来实现。
创建计划任务:
如图,我们将计划任务的设置为使用最高权限运行,触发器设置为在系统启动时触发,而操作是启动批处理脚本。
计划任务创建完成后,我们可以将自己的主机重启,看看我们的虚拟机是否有自动开机。
②、用组策略来实现
在 <运行> 中输入 gpedit.msc 打开组策略,如下图
在组策略中,依次选择 <用户配置>
在 <登陆 属性> 对话框中添加我们之前写好的启动脚本即可,如下图
设置完之后,我们重启主机,再看看虚拟机有没有启动。
三、总结
在日常工作中,我们解决问题的办法可能不止一种,但往往由于我们所学知识的局限性性,导致我们的思路会很单一,因此,我们就要通过不断的学习,来拓宽我们视界,才能更好的提高我们的深获质量。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。