vmrun 批量创建vmware虚拟机

时间:2022-01-22 10:32:43

1 准备模板机

具体步骤如下: 
1. 下载镜像安装系统

https://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/

  

2. 安装完成配置好IP ,关闭SELINUX ,关闭firewalld ,修改网卡名

3. 预设置好修改其他机器IP脚本

1.1 安装系统

 

1.2 模板机的设置

修改网卡名eth0

 

# 当前系统版本
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)

# 修改网卡配置文件为eth0
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# mv ifcfg-enxxxx eth0

# 修改为静态ip
[root@localhost network-scripts]# cat ifcfg-eth0 
TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
IPADDR=10.0.0.79
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=10.0.0.2

# 禁用该可预测命名规则 
# 在 grup 文件里面的 GRUB_CMDLINE_LINUX 变量添加 net.ifnames=0 biosdevname=0
[root@localhost ~]# cat /etc/default/grub 
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap net.ifnames=0 biosdevname=0 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

#运行命令重新生成GRUB配置并更新内核参数
[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-327.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-327.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-692c580a413f41d8b51ddc4724500560
Found initrd image: /boot/initramfs-0-rescue-692c580a413f41d8b51ddc4724500560.img
done

  

vmrun 批量创建vmware虚拟机

 

 关闭selinux

[root@localhost ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux 
[root@localhost ~]# egrep -v "^#|^$" /etc/sysconfig/selinux 
SELINUX=disabled
SELINUXTYPE=targeted 

  

关闭firewalld

[root@localhost ~]# systemctl disable firewalld.service 

  

重启系统

[root@localhost ~]# reboot

  

然后给批量创建虚拟机设置自定义的IP和主机名的脚本

[root@localhost ~]# cat /root/first.sh 
#!/bin/bash
#Author:keme
#Blog:https://www.cnblogs.com/keme
#Time:2018-11-23
#Name:first.sh
#Version:V1.0
#Description:This is a test script.

ip=$1
hostname=$2

if [ $# -ne 2 ]
then
    echo "sh $0 ip hostname"
fi


set_ip(){
    sed -ri "/IPADDR/s#(.*.)\..*#\1.${ip}#" /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -ri "/IPADDR/s#(.*.)\..*#\1.${ip}#" /etc/sysconfig/network-scripts/ifcfg-eth1

    echo "=============================================="
    echo "now you can running : service network restart"
    service network restart
}

set_hostname(){
    sed -i "s#.*#${hostname}#" /etc/hostname
    hostnamectl set-hostname ${hostname}
    echo "=============================================="
    echo "hostname is : $(cat /etc/hostname)"
    echo "=============================================="
}


main(){
   set_ip
   set_hostname
}

main
[root@localhost ~]# chmod +x /root/first.sh 

  

关机创建快照

vmrun 批量创建vmware虚拟机

 

2 目录规划

  1. 模板虚拟机所在目录

2.1 模板机所在目录

E:\VMS\Centos7\template

  

vmrun 批量创建vmware虚拟机

 

 

3 vmrun结合脚本批量创建虚拟机

  1. vmrun.exe 程序
  2. git 软件
  3. 编写脚本
  4. 设置IP 的脚本

3.1 vmrun.exe

找到vmrun.exe 程序 

vmrun 批量创建vmware虚拟机

 

创建一个E:\SOFT\VM文件夹复制到新创建的文件夹

vmrun使用 

https://www.cnblogs.com/qiaoyanlin/p/6883487.html 
https://www.vmware.com/pdf/vix162_vmrun_command.pdf

3.2 git 软件作用

方便使用linux 的shell 命令

vmrun 批量创建vmware虚拟机

3.3 编写脚本

vmrun 批量创建vmware虚拟机

 

 auto_VM.sh内容如下

#!/bin/bash
#Author:keme
#Blog:https://www.cnblogs.com/keme/
#Time:2018-11-23
#Name:auto_vm.sh
#Version:V1.0

PATH_TEM="/E/VMS/Centos7/template/template.vmx"
PATH_SERVICE="/E/VMS/Centos7"
PATH_SERVICE_CMD="E:\VMS\Centos7"
#######################################
#PATH_TEM="/E/VMS/Debian8/template/template.vmx"
#PATH_SERVICE="/E/VMS/Debian8"
#PATH_SERVICE_CMD="E:\\VMS\\Debian8"
######################################
PATH_VMRUN_CMD="E:\\SOFT\\VM"
PATH_VMS="E:\\VMS"
SERVER_NAME="FDSF"
CLUSTER_NAME="FDFS-cluster"
SHOT_NAME="$2"


# 创建克隆链接的虚拟机
VM_create(){
    for i in $(cat host-list.txt) 
    do
        echo "开始创建链接克隆: ${SERVER_NAME}-${i}"
        /E/SOFT/VM/vmrun.exe -T ws clone  "${PATH_TEM}"  ${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx linked -snapshot=CLEAN  -cloneName=${SERVER_NAME}-${i}
        sleep 2 
    done
}

# 启动创建好的虚拟机
VM_start(){
    for i in $(cat host-list.txt) 
    do
        echo "开始启动: ${SERVER_NAME}-${i}"
        /E/SOFT/VM/vmrun.exe -T ws start ${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx
        sleep 10
    done
}

# 批量修改IP地址
VM_set_IP(){
    >  ${PATH_VMS}\\set_ip.bat
    for i in $(cat host-list.txt) 
    do
         echo "${PATH_VMRUN_CMD}\\vmrun.exe -T ws -gu root -gp 123456 runProgramInGuest  \"${PATH_SERVICE_CMD}\\${CLUSTER_NAME}\\${SERVER_NAME}-${i}\\${SERVER_NAME}-${i}.vmx\" /bin/bash /root/first.sh ${i} ${SERVER_NAME}-${i}" >>  ${PATH_VMS}\\set_ip.bat
         echo "ping -n 5 127.0.0.1 >nul" >> ${PATH_VMS}\\set_ip.bat
    done
    echo "修改IP脚本生成路径: ${PATH_VMS}\\set_ip.bat"
    echo -e "运行命令:\n1.打开cmd.exe\n2.输入: ${PATH_VMS}\\set_ip.bat"
}

# 修改好后批量关机
VM_stop(){
    for i in $(cat host-list.txt) 
    do
        echo "开始关闭: ${SERVER_NAME}-${i}"
        /E/SOFT/VM/vmrun.exe -T ws stop ${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx
        sleep 2 
    done
}

# 关机后批量创建快照
VM_snapshot(){
    for i in $(cat host-list.txt) 
    do
        echo "开始快照: ${SERVER_NAME}-${i}"
        /E/SOFT/VM/vmrun.exe -T ws snapshot "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx" ${SHOT_NAME}
        sleep 2 
    done
}

# 获取快照列表
VM_shot_list(){
    for i in $(cat host-list.txt) 
    do
        echo "获取快照列表: ${SERVER_NAME}-${i}"
        /E/SOFT/VM/vmrun.exe -T ws listSnapshots "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx"
        sleep 2 
    done
}

# 批量恢复快照
VM_shot_back(){
    for i in $(cat host-list.txt) 
    do
        echo "开始恢复快照: ${SERVER_NAME}-${i} ${SHOT_NAME}"
        /E/SOFT/VM/vmrun.exe -T ws revertToSnapshot "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx" ${SHOT_NAME}
        sleep 2 
    done
}

# 批量删除快照
VM_shot_del(){
    for i in $(cat host-list.txt) 
    do
        echo "开始删除快照: ${SERVER_NAME}-${i} ${SHOT_NAME}"
        /E/SOFT/VM/vmrun.exe -T ws deleteSnapshot "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx" ${SHOT_NAME}
        sleep 2 
    done
}

# 获取当前正在运行的虚拟机 
VM_list(){
    echo "当前正在运行的虚拟机:"
    /E/SOFT/VM/vmrun.exe -T ws list
}

case $1 in 
    create)
        VM_create
        ;;
    start)
        VM_start
        ;;
    stop)
        VM_stop
        ;;
    shot)
        VM_snapshot
        ;;
    set_ip)
        VM_set_IP
        ;;
    shot_back)
        VM_shot_back
        ;;
    shot_del)
        VM_shot_del
        ;;
    shot_list)
        VM_shot_list
        ;;
    list)
        VM_list
        ;;

    *)
        echo "bash $0 {create|start|set_ip|stop|shot|shot_list|shot_back|shot_del|list}"
esac            

  

除了 set_ip 这个函数在windows 里面需要单独拿出来放到cmd 窗口使用

host-list.txt表示创建虚拟机的数量,80,81,82表示最后的ip地址第四位

vmrun 批量创建vmware虚拟机

 

 设置IP的脚本

vmrun 批量创建vmware虚拟机

 

 E:\VMS\set_ip.bat

E:\SOFT\VM\vmrun.exe -T ws -gu root -gp 123456 runProgramInGuest  "E:\VMS\Centos7\FDFS-cluster\FDSF-80\FDSF-80.vmx" /bin/bash /root/first.sh 80 FDSF-80
ping -n 5 127.0.0.1 >nul
E:\SOFT\VM\vmrun.exe -T ws -gu root -gp 123456 runProgramInGuest  "E:\VMS\Centos7\FDFS-cluster\FDSF-81\FDSF-81.vmx" /bin/bash /root/first.sh 81 FDSF-81
ping -n 5 127.0.0.1 >nul
E:\SOFT\VM\vmrun.exe -T ws -gu root -gp 123456 runProgramInGuest  "E:\VMS\Centos7\FDFS-cluster\FDSF-82\FDSF-82.vmx" /bin/bash /root/first.sh 82 FDSF-82
ping -n 5 127.0.0.1 >nul

  

4 模拟演示创建,修改ip和主机名

  1. 创建虚拟机

vmrun 批量创建vmware虚拟机

 

 

  1. 开启所创建的虚拟机

vmrun 批量创建vmware虚拟机

ok 都启动了

 vmrun 批量创建vmware虚拟机

  1. 设置所创建虚拟机的ip 和 主机名

 

vmrun 批量创建vmware虚拟机

 

 

    1. 查看是否修改成功了 
      FAST-80 ,OK

 vmrun 批量创建vmware虚拟机

 

FAST-81,OK

vmrun 批量创建vmware虚拟机

 

FAST-82,OK

vmrun 批量创建vmware虚拟机

 

注: 只是为了方便做实验用,方便自己快速搭建环境练习测试