日常工作中,由于條件以及成本的限制,我們會(huì)在自己本地電腦上安裝虛擬機(jī)裝服務(wù)器系統(tǒng),來滿足自己工作中對(duì)服務(wù)器的需求,但是很多時(shí)候,我們自己的電腦關(guān)機(jī)或者重啟后,虛擬機(jī)并不會(huì)自行啟動(dòng),需要我們手動(dòng)啟動(dòng)虛擬機(jī),如果我們只有一兩臺(tái)虛擬機(jī),那沒什么,啟動(dòng)很快,但是,如果我們有很多臺(tái)虛擬機(jī)呢?10 臺(tái),20 臺(tái),甚至更多,難道我們還要手動(dòng)一個(gè)個(gè)開機(jī)嗎?那真是太讓人頭疼,又太浪費(fèi)時(shí)間。下面,我們就講下,如何讓虛擬機(jī)在我們自己的主機(jī)開機(jī)時(shí)自動(dòng)啟動(dòng)。
做網(wǎng)站、成都網(wǎng)站制作服務(wù)團(tuán)隊(duì)是一支充滿著熱情的團(tuán)隊(duì),執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時(shí)竭誠為客戶提供服務(wù)是我們的理念。創(chuàng)新互聯(lián)把每個(gè)網(wǎng)站當(dāng)做一個(gè)產(chǎn)品來開發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!一、VMware workstation 虛擬機(jī)開機(jī)自啟動(dòng)的原理
要說 VMware workstation 虛擬機(jī)開機(jī)自啟動(dòng),在 VMware workstation 圖形界面是找不到設(shè)置方法的,但是圖形界面找不到設(shè)置方法,不代表它就沒有設(shè)置方法。
要設(shè)置開機(jī)自啟動(dòng),首先,我們得了解下 VMware workstation 所提供的命令行工具 vmrun 。
1、我們進(jìn)入到 VMware workstation 的安裝目錄,找一下 vmrun.exe 文件,如下圖,正式這個(gè)工具,為我們提供了虛擬機(jī)的自啟動(dòng)方法。
2、將 VMware workstation 的安裝目錄添加到系統(tǒng)的環(huán)境變量,如下圖,按照途中的順序,依次操作即可
注意:在第 5 步時(shí),需要找到自己的 VMware workstation 的安裝目錄,然后添加到這里。
3、在 DOS 界面中查看 vmrun 命令的語法格式及參數(shù)
Microsoft Windows [版本 10.0.17134.471] (c) 2018 Microsoft Corporation。保留所有權(quán)利。 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"
①、驗(yàn)證標(biāo)志(AUTHENTICATION-FLAGS):
-T 代表目標(biāo)類型,ws 表示工作站(即 Workstation ),一般使用此參數(shù)即可,其他很少使用,就不做解釋了。當(dāng)然還有 server、esx、vc 等等,這里沒有列出。
②、電源指令(POWER COMMANDS):
start:開啟虛擬機(jī)
stop:關(guān)閉虛擬機(jī)
reset:重置虛擬機(jī)
suspend:掛起虛擬機(jī)
③、快照指令(SNAPSHOT COMMANDS):
listSnapshots:列出虛擬機(jī)的快照數(shù)量及名稱
snapshot:為虛擬機(jī)創(chuàng)建快照
deleteSnapshot:刪除虛擬機(jī)的快照
revertToSnapshot:從一個(gè)快照恢復(fù)虛擬機(jī)
④、常用命令(GENERAL COMMANDS):
list:列出正在運(yùn)行的虛擬機(jī)的數(shù)量及名稱
upgradevm:升級(jí)虛擬機(jī)的文件格式,虛擬硬件
installTools:在 Guest 中安裝工具
checkToolsState:檢查工具狀態(tài)
deleteVM:刪除一個(gè)虛擬機(jī)
clone:克隆虛擬機(jī)
這里只介紹這么多命令,其他的命令很少用到,就不多做解釋。
言歸正傳,我們要設(shè)置虛擬機(jī)開機(jī)自啟動(dòng),就要使用上面 start 命令。那么,此命令如何使用呢,格式時(shí)這樣的:
vmrun -T ws start "c:\my VMs\myVM.vmx"
"c:\my VMs\myVM.vmx" 該目錄為虛擬機(jī)的工作目錄
我們可以在這條命令后面加上 nogui ,表示無圖形界面啟動(dòng),而默認(rèn)的啟動(dòng)模式是 gui 模式,即有圖形界面啟動(dòng)。
vmrun -T ws start "c:\my VMs\myVM.vmx" nogui
最后,我們將該命令修改為自己的虛擬機(jī)工作目錄后寫到批處理文件中(如下圖),添加開機(jī)啟動(dòng)腳本或者計(jì)劃任務(wù)。
@echo off & setlocal enabledelayedexpansion vmrun -T ws start "I:\Documents\Virtual Machines\centos\centos.vmx" nogui
附上關(guān)機(jī)腳本:
@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 中設(shè)置虛擬機(jī)開機(jī)自啟動(dòng)
在 Windows 中設(shè)置虛擬機(jī)開機(jī)自啟動(dòng)又兩種辦法
①、用計(jì)劃任務(wù)來實(shí)現(xiàn)。
創(chuàng)建計(jì)劃任務(wù):
如圖,我們將計(jì)劃任務(wù)的設(shè)置為使用最高權(quán)限運(yùn)行,觸發(fā)器設(shè)置為在系統(tǒng)啟動(dòng)時(shí)觸發(fā),而操作是啟動(dòng)批處理腳本。
計(jì)劃任務(wù)創(chuàng)建完成后,我們可以將自己的主機(jī)重啟,看看我們的虛擬機(jī)是否有自動(dòng)開機(jī)。
②、用組策略來實(shí)現(xiàn)
在 <運(yùn)行> 中輸入 gpedit.msc 打開組策略,如下圖
在組策略中,依次選擇 <用戶配置>
在 <登陸 屬性> 對(duì)話框中添加我們之前寫好的啟動(dòng)腳本即可,如下圖
設(shè)置完之后,我們重啟主機(jī),再看看虛擬機(jī)有沒有啟動(dòng)。
三、總結(jié)
在日常工作中,我們解決問題的辦法可能不止一種,但往往由于我們所學(xué)知識(shí)的局限性性,導(dǎo)致我們的思路會(huì)很單一,因此,我們就要通過不斷的學(xué)習(xí),來拓寬我們視界,才能更好的提高我們的深獲質(zhì)量。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。