首頁 > 軟體

vmrun 批次建立VMware虛擬機器

2020-06-16 16:40:16

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:VMSCentos7template

3 vmrun結合指令碼批次建立虛擬機器

  1. vmrun.exe 程式
  2. git 軟體
  3. 編寫指令碼
  4. 設定IP 的指令碼

3.1 vmrun.exe

找到vmrun.exe 程式 

建立一個E:SOFTVM資料夾複製到新建立的資料夾

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:VMSCentos7"
#######################################
#PATH_TEM="/E/VMS/Debian8/template/template.vmx"
#PATH_SERVICE="/E/VMS/Debian8"
#PATH_SERVICE_CMD="E:VMSDebian8"
######################################
PATH_VMRUN_CMD="E:SOFTVM"
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.exen2.輸入: ${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的指令碼


IT145.com E-mail:sddin#qq.com