感谢支持
我们一直在努力

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

# 当前系统版本
[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

关闭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

关机创建快照

2 目录规划

  1. 模板虚拟机所在目录

2.1 模板机所在目录

E:\VMS\Centos7\template

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

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

3.1 vmrun.exe

找到vmrun.exe 程序 

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

vmrun使用 

https://www.linuxidc.com/Linux/2019-01/156357.htm

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:\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地址第四位

 设置IP的脚本

 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. 创建虚拟机

  1. 开启所创建的虚拟机

ok 都启动了

 

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

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

 

FAST-81,OK

FAST-82,OK

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

相关PDF文档可以到Linux公社资源站下载:

——————————————分割线——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2019年资料/1月/14日/vmrun 批量创建VMware虚拟机/

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

——————————————分割线——————————————

本文永久更新链接地址:https://www.linuxidc.com/Linux/2019-01/156349.htm

赞(0) 打赏
转载请注明出处:服务器评测 » vmrun 批量创建VMware虚拟机
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏