vmrun 批量创建vmware虚拟机

Posted keme

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vmrun 批量创建vmware虚拟机相关的知识,希望对你有一定的参考价值。

1 准备模板机

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

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

  

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

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

1.1 安装系统

 

1.2 模板机的设置

修改网卡名eth0

 

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

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

# 修改为静态ip
[[email protected] 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
[[email protected] ~]# 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配置并更新内核参数
[[email protected] ~]# 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

  

技术分享图片

 

 关闭selinux

[[email protected] ~]# sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/sysconfig/selinux 
[[email protected] ~]# egrep -v "^#|^$" /etc/sysconfig/selinux 
SELINUX=disabled
SELINUXTYPE=targeted 

  

关闭firewalld

[[email protected] ~]# systemctl disable firewalld.service 

  

重启系统

[[email protected] ~]# reboot

  

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

[[email protected] ~]# 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
[[email protected] ~]# chmod +x /root/first.sh 

  

关机创建快照

技术分享图片

 

2 目录规划

  1. 模板虚拟机所在目录

2.1 模板机所在目录

E:VMSCentos7	emplate

  

技术分享图片

 

 

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

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

3.1 vmrun.exe

找到vmrun.exe 程序 

技术分享图片

 

创建一个E:SOFTVM文件夹复制到新创建的文件夹

vmrun使用 

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

3.2 git 软件作用

方便使用linux 的shell 命令

技术分享图片

3.3 编写脚本

技术分享图片

 

 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:VMSCentos7"
#######################################
#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 "运行命令:
1.打开cmd.exe
2.输入: ${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地址第四位

技术分享图片

 

 设置IP的脚本

技术分享图片

 

 E:VMSset_ip.bat

E:SOFTVMvmrun.exe -T ws -gu root -gp 123456 runProgramInGuest  "E:VMSCentos7FDFS-clusterFDSF-80FDSF-80.vmx" /bin/bash /root/first.sh 80 FDSF-80
ping -n 5 127.0.0.1 >nul
E:SOFTVMvmrun.exe -T ws -gu root -gp 123456 runProgramInGuest  "E:VMSCentos7FDFS-clusterFDSF-81FDSF-81.vmx" /bin/bash /root/first.sh 81 FDSF-81
ping -n 5 127.0.0.1 >nul
E:SOFTVMvmrun.exe -T ws -gu root -gp 123456 runProgramInGuest  "E:VMSCentos7FDFS-clusterFDSF-82FDSF-82.vmx" /bin/bash /root/first.sh 82 FDSF-82
ping -n 5 127.0.0.1 >nul

  

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

  1. 创建虚拟机

技术分享图片

 

 

  1. 开启所创建的虚拟机

技术分享图片

ok 都启动了

 技术分享图片

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

 

技术分享图片

 

 

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

 技术分享图片

 

FAST-81,OK

技术分享图片

 

FAST-82,OK

技术分享图片

 

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

以上是关于vmrun 批量创建vmware虚拟机的主要内容,如果未能解决你的问题,请参考以下文章

运用vmrun命令行管理vmware虚拟机实例

vmrun命令

VMware 自动开多台虚拟机脚本

Windows环境下VMware虚拟机的自启动与自动关机--命令行操作

如何使用 VMware vmrun 控制可移动 USB 设备

在VMware Workstation中批量创建上千台虚拟机(下)