esxi主机,自动创建30个vm,并自动完成vm初始化操作
1 [root@jump-server shell]# cat for.sh 2 #!/bin/bash 3 4 ESXIPASSWD=****** 5 HOSTPASSWD=****** 6 7 for i in `seq 101 130` 8 do 9 N=$i 10 echo -e "\n================= 虚拟机 Jump-Node$N 开始制作 ================" 11 12 echo -e "\n01、在Esxi主机上创建 Jump-Node$N 虚拟机目录" 13 sshpass -p $ESXIPASSWD ssh root@100.100.100.251 'mkdir /vmfs/volumes/datastore-ssd/Jump-Node'$N'' 14 15 echo -e "\n02、在本地创建 Jump-Node$N 虚拟机配置文件" 16 cat Jump-Nodexx.vmx | sed -e "s/xx/$N/" > Jump-Node$N.vmx 17 18 echo -e "\n03、传输 Jump-Node$N 虚拟机配置文件到Esxi主机" 19 sshpass -p $ESXIPASSWD scp Jump-Node$N.vmx 100.100.100.251:/vmfs/volumes/datastore-ssd/Jump-Node$N/Jump-Node$N.vmx 20 21 echo -e "\n04、清理 Jump-Node$N vmx文件" 22 rm -rf Jump-Node$N.vmx 23 24 echo -e "\n05、在Esxi主机上基于模板克隆 Jump-Node$N 磁盘文件,精简置备" 25 sshpass -p $ESXIPASSWD ssh root@100.100.100.251 'vmkfstools -d thin -i /vmfs/volumes/datastore-nvme/centos7.9/centos7.9.vmdk /vmfs/volumes/datastore-ssd/Jump-Node'$N'/Jump-Node'$N'.vmdk' 26 27 echo -e "\n06、在Esxi主机上注册虚拟机 Jump-Node$N ,并获取 VMID" 28 VMID=$(sshpass -p $ESXIPASSWD ssh root@100.100.100.251 'vim-cmd solo/registervm /vmfs/volumes/datastore-ssd/Jump-Node'$N'/Jump-Node'$N'.vmx') 29 30 echo -e "\n07、在Esxi主机上给虚拟机 Jump-Node$N 开机" 31 sshpass -p $ESXIPASSWD ssh root@100.100.100.251 'vim-cmd vmsvc/power.on '$VMID'' 32 33 echo -e "\n08、检查虚拟机 Jump-Node$N 是否开机成功" 34 while true ; do ping -c 1 -w 1 192.168.77.1 &>/dev/null && sshpass -p $HOSTPASSWD ssh root@192.168.77.1 "hostname" &>/dev/null && break || echo -en "." ; done 35 36 echo -e "\n\n09、修改虚拟机 Jump-Node$N 网卡配置文件" 37 sshpass -p $HOSTPASSWD ssh root@192.168.77.1 'sed -i -e '/UUID/d' -e 's@77.1@77.$N@' /etc/sysconfig/network-scripts/ifcfg-ens192' 38 39 echo -e "\n10、重启虚拟机 Jump-Node$N 网络" 40 sshpass -p $HOSTPASSWD ssh root@192.168.77.1 'nohup systemctl restart network &>/dev/null &' 41 42 echo -e "\n11、检查虚拟机 Jump-Node$N 网络配置" 43 while true ; do ping -c 1 -w 1 192.168.77.$N &>/dev/null && sshpass -p $HOSTPASSWD ssh root@192.168.77.$N "hostname" &>/dev/null && break || echo -en "." ; done 44 45 echo -e "\n\n12、虚拟机 Jump-Node$N 关机" 46 sshpass -p $HOSTPASSWD ssh root@192.168.77.$N 'init 0' 47 48 echo -e "\n13、检查虚拟机 Jump-Node$N 关机是否成功" 49 while true ; do sshpass -p $ESXIPASSWD ssh root@100.100.100.251 'vim-cmd vmsvc/power.getstate '$VMID'' | grep 'Powered off' && break || echo -en '.' ; done 50 51 echo -e "\n14、给虚拟机 Jump-Node$N 创建快照s1" 52 sshpass -p $ESXIPASSWD ssh root@100.100.100.251 'vim-cmd vmsvc/snapshot.create '$VMID' s1' 53 54 echo -e "\n15、虚拟机 Jump-Node$N 开机" 55 sshpass -p $ESXIPASSWD ssh root@100.100.100.251 'vim-cmd vmsvc/power.on '$VMID' ' 56 57 echo -e "\n16、检查虚拟机 Jump-Node$N 开机机是否成功" 58 while true ; do ping -c 1 -w 1 192.168.77.$N &>/dev/null && break || echo -en "." ; done 59 60 echo -e "\n\n17、虚拟机 Jump-Node$N 制作完毕\n" 61 done 62 63 64 [root@jump-server shell]# cat Jump-Nodexx.vmx 65 .encoding = "UTF-8" 66 config.version = "8" 67 virtualHW.version = "20" 68 vmci0.present = "TRUE" 69 floppy0.present = "FALSE" 70 numvcpus = "2" 71 memSize = "4096" 72 powerType.suspend = "soft" 73 tools.upgrade.policy = "manual" 74 sched.cpu.units = "mhz" 75 scsi0.virtualDev = "pvscsi" 76 scsi0.present = "TRUE" 77 sata0.present = "TRUE" 78 scsi0:0.deviceType = "scsi-hardDisk" 79 scsi0:0.fileName = "Jump-Nodexx.vmdk" 80 scsi0:0.present = "TRUE" 81 sata0:0.startConnected = "FALSE" 82 sata0:0.deviceType = "atapi-cdrom" 83 sata0:0.fileName = "CD/DVD drive 0" 84 sata0:0.present = "TRUE" 85 ethernet0.virtualDev = "vmxnet3" 86 ethernet0.networkName = "Jump77" 87 ethernet0.addressType = "generated" 88 ethernet0.wakeOnPcktRcv = "FALSE" 89 ethernet0.uptCompatibility = "TRUE" 90 ethernet0.present = "TRUE" 91 displayName = "Jump-Nodexx" 92 guestOS = "centos7-64" 93 toolScripts.afterPowerOn = "TRUE" 94 toolScripts.afterResume = "TRUE" 95 toolScripts.beforeSuspend = "TRUE" 96 toolScripts.beforePowerOff = "TRUE" 97 tools.syncTime = "FALSE" 98 cpuid.coresPerSocket = "1" 99 tools.guest.desktop.autolock = "TRUE" 100 svga.present = "TRUE" 101 pciBridge0.present = "TRUE" 102 pciBridge4.present = "TRUE" 103 pciBridge4.virtualDev = "pcieRootPort" 104 pciBridge4.functions = "8" 105 pciBridge5.present = "TRUE" 106 pciBridge5.virtualDev = "pcieRootPort" 107 pciBridge5.functions = "8" 108 pciBridge6.present = "TRUE" 109 pciBridge6.virtualDev = "pcieRootPort" 110 pciBridge6.functions = "8" 111 pciBridge7.present = "TRUE" 112 pciBridge7.virtualDev = "pcieRootPort" 113 pciBridge7.functions = "8" 114 hpet0.present = "TRUE" 115 RemoteDisplay.maxConnections = "-1" 116 sched.cpu.latencySensitivity = "normal" 117 svga.autodetect = "TRUE" 118 numa.autosize.cookie = "20012" 119 numa.autosize.vcpu.maxPerVirtualNode = "2" 120 pciBridge0.pciSlotNumber = "17" 121 pciBridge4.pciSlotNumber = "21" 122 pciBridge5.pciSlotNumber = "22" 123 pciBridge6.pciSlotNumber = "23" 124 pciBridge7.pciSlotNumber = "24" 125 scsi0.pciSlotNumber = "160" 126 ethernet0.pciSlotNumber = "192" 127 sata0.pciSlotNumber = "32" 128 ethernet0.generatedAddressOffset = "0" 129 monitor.phys_bits_used = "45" 130 softPowerOff = "TRUE" 131 svga.guestBackedPrimaryAware = "TRUE" 132 guestInfo.detailed.data = "bitness='64' distroName='CentOS Linux' distroVersion='7' familyName='Linux' kernelVersion='3.10.0-1160.71.1.el7.x86_64' prettyName='CentOS Linux 7 (Core)'" 133 sched.cpu.shares = "normal" 134 scsi0:0.redo = "" 135 cleanShutdown = "TRUE"