将虚机从vcenter导入到pve
#!/bin/bash #脚本用来将vmware虚机通过ovftool导出到pve宿主机,然后再将其导入pve,可以设置pve虚机名称、OS类型、添加网卡配置mac地址,修改磁盘scsi改为ide,如果虚机之前在vmware中是开机状态,则将其开机 vms_path='/data1/scripts/vms.txt' #vm csv vms_ovf_path='/mnt/pve/dir-116/tmp_ovfbak/' #vm ovf path vc_username='la%40yx.dd' vc_password='password' pve_storage='dir-116' #pve storage #pve_vm_id=107 #pve vm id from #get pve current vm id. pve_id_max=`qm list |awk '{print $1}' |tail -n 1` pve_vm_id=`expr $pve_id_max + 1` 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}'` description=`echo $line |awk -F , '{print $7}'` echo "exporting $vmname ..." /root/vmware-ovftool/ovftool --noSSLVerify -dm=thin --powerOffSource vi://$vc_username:$vc_password@10.10.200.200/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 if [ $des ];then qm set $pve_vm_id --description=$description fi 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 qm set $pve_vm_id onboot #开机自启动
qm set $pve_vm_id --localtime 1 #Linux虚机使用本地时间,否则会变成utc时间 #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,vm description Dev-mq03_10.10.26.19,00:51:56:bf:13:6b,VLAN26,centos7_64Guest,PoweredOn,10.10.2.12,vm description
====================================================================================
qm命令参考:http://lihuaxi.xjx100.cn/news/1239580.html?action=onClick
qm list #查看虚机列表
qm config vmid #查看单个虚机信息