🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 前言 上面一篇介绍了使用vm的工具vmrun.exe来操作vm创建,克隆,启动关闭和删除虚拟机. 但是在windows下调用命令需要使用bat批量执行脚本来完成,语法需要重新学习,成本有点高 我比较熟悉shell,那有没有可能在windows下使用shell命令或者shell脚本来操作呢,那岂不是很爽? 答案是肯定的,下面我们就来操作 ## 实验环境 ``` 操作系统: windows10 vm版本: VMware Workstation Pro12或以上 gitbash: 官网下载最新版即可,作用是在windows下可以执行linux的shell命令.因为windows的命令很难用 vm虚拟机操作系统: linux和windwos都可以,但是如果想在windows下执行虚拟机里面的脚本或命令则必须安装vm-tools ``` ## vm命令说明 批量操作虚拟机本质上就是调用vm自己的命令vmrun.exe来执行一些操作 这个工具位置是你的vm软件的安装目录, 执行的时候需要使用全路径来执行,或者加入环境变量PATH 使用帮助说明: ``` $ /E/SOFT/VM/vmrun.exe /? vmrun version 1.15.8 build-5813279 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 VMs\myVM.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 VMs\myVM.vmx" "c:\Program Files\myProgram.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 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.exe可以实现以下功能,仅列出我们目前需要的命令,更多的命令可以查看帮助 ``` - 创建虚拟机 - 启动虚拟机 - 关闭虚拟机 - 创建虚拟机快照并指定快照名称 - 从指定快照中恢复 - 从指定快照克隆虚拟机 - 执行虚拟机中的脚本 ``` ## 实验步骤 这里我们分为两个阶段 第一个阶段是用windows自己的cmd命令操作 第二阶段是使用shell脚本批量操作 ### 创建虚拟机模版 这里我们先把要实现的效果说一下: 1.首先手动安装一台虚拟机,并把相关配置都配置好,然后就当作是模版机,以后这台机器的作用只有一个.就是创建链接克隆其他的虚拟机. 2.当这台模版机已经配置好之后,我们需要做一个快照,以后的克隆虚拟机都从这个快照克隆 3.命名规范,为了后面的脚本,我们需要把路径,机器名和快照名都规范下来.规范如下 ``` 软件安装目录名: D:\soft\VM vmrun路径: D:\soft\VM\vmrun.exe 虚拟机目录: D:\VMS\集群名\服务名-端口号\服务名-端口号.vmx 例如我们需要创建一个nginx的集群2台机器,那么目录名就是: D:\VMS\nginx-cluster\nginx-21\nginx-21.vmx D:\VMS\nginx-cluster\nginx-22\nginx-22.vmx ``` 注意: windwos下是D:\VMS\这样的命名 但是linux的命名规则是/D/VMS 所以写脚本的时候需要转换成linux的命名格式 ###