#改脚本用来将vmware虚机通过ovftool导出到pve宿主机,然后再将其导入pve,可以设置pve虚机名称、OS类型、添加网卡配置mac地址,修改磁盘scsi改为ide,如果虚机之前在vmware中是开机状态,则将其开机 vms_path='/data1/scripts/vms.txt' vms_ovf_path='/mnt/pve/dir-116/tmp_ovfbak/' vc_username='ly%40yx.dd' vc_password='password' pve_storage='dir-116' pve_vm_id=107 #定义导入pve的虚机开始的id for line in $(cat $vms_path) do #export vmware vm to ovf vmname=`echo $line |awk -F , '{print $1}'` vmmac=`echo $line |awk -F , '{print $2}'` vlan=`echo $line |awk -F , '{print $3}'` vlanid=`echo $vlan | sed 's/VLAN//g'` os=`echo $line |awk -F , '{print $4}'` status=`echo $line |awk -F , '{print $5}'` echo "exporting $vmname ..." /root/vmware-ovftool/ovftool --noSSLVerify -dm=thin --powerOffSource vi://$vc_username:[email protected]/SH/vm/$vmname $vms_ovf_path sleep 2 #import ovf vm to pve qm importovf $pve_vm_id $vms_ovf_path/$vmname/$vmname.ovf $pve_storage --format qcow2 sleep 2 #configure netcard with mac and vlan id echo "Configuring vm name,mac,disk..." echo "pve vm id is: $pve_vm_id , mac is $vmmac , vlan id is $vlanid" vmname_new=`echo $vmname | sed 's/_/-/g'` qm set $pve_vm_id --name $vmname_new qm set $pve_vm_id -net0 e1000=$vmmac,bridge=vmbr1,firewall=1,tag=$vlanid #configure vm os type os_windows=`echo $os |grep -i windows |wc -l` if [ $os_windows -eq 1 ];then qm set $pve_vm_id --ostype=win11 else qm set $pve_vm_id --ostype=l26 fi #change disk scsi to ide disk_num=`qm config $pve_vm_id |grep scsi |grep -v 'order=' |wc -l` if [ $disk_num -gt 0 ];then echo "find $disk_num disks with $vmname_new" for ((i=0;i<$disk_num;i++)) do echo "changing disk $i scsi to ide..." scsi_id="scsi$i" qm set $pve_vm_id --delete $scsi_id #dispatch scsi disk sleep 1 disk_unused=`qm config $pve_vm_id |grep unused0 |awk '{print $2}'` #discover unused disk echo $disk_unused sleep 1 qm set $pve_vm_id -ide$i $disk_unused #add unused disk to vm done #elif [ $disk_num -eq 1 ];then # qm set $pve_vm_id --delete scsi0 # sleep 1 # disk_unused=`qm config 107 |grep unused0 |awk '{print $2}'` # sleep 1 # qm set $pve_vm_id -ide0 $disk_unused else echo "Not find scsi disk with $vmname_new" fi sleep 2 #check if start the vm if [ $status = 'PoweredOn' ];then echo "starting $vmname_new..." qm start $pve_vm_id fi pve_vm_id=`expr $pve_vm_id + 1` sleep 30 done
cat vms.txt,内容如下:
虚机名称,虚机MAC,VLAN,OSid,虚机状态,宿主机IP,虚机备注
Dev-mq01_10.10.20.11,00:51:56:bf:cf:87,VLAN26,centos7_64Guest,PoweredOn,10.10.2.13,notes Dev-mq03_10.10.26.19,00:51:56:bf:13:6b,VLAN26,centos7_64Guest,PoweredOn,10.10.2.12,notes
标签:导入到,vm,echo,pve,虚机,vcenter,vmname,id From: https://www.cnblogs.com/dreamer-fish/p/17735861.html