cat > kvmcrate.sh <<EOF
#!/bin/bash
##批量创建kvm虚拟机,根据已有的做拷贝复制

##IP段
ipnat=10.255.xxx.

##IP区间
iprange='50 52'

##现有镜像
demo_disk=/data/kvm/disk/moban-500G.img
new_disk_path=/data/kvm/disk
##现有xml文件
demo_xml=/etc/libvirt/qemu/moban-500G.xml
new_xml_path=/etc/libvirt/qemu
cpu=8

##内存
mem=16777216
for i in `seq $iprange`;do 
    if [ -f ${new_disk_path}/${ipnat}${i}.img ];then
        echo "${new_disk_path}/${ipnat}${i}.img 已经存在了!"
        exit 1;
    fi
    cp ${demo_disk} ${new_disk_path}/${ipnat}${i}.img
    echo "${new_disk_path}/${ipnat}${i}.img 磁盘复制完成!"
    cp ${demo_xml} ${new_xml_path}/${ipnat}${i}.xml
    uuid=`uuidgen`
    mac1=`openssl rand -base64 8 | md5sum |  cut -c1-2`
    mac2=`openssl rand -base64 8 | md5sum |  cut -c1-2`
    mac3=`openssl rand -base64 8 | md5sum |  cut -c1-2`
    name="${ipnat}${i}"
    sed -i 's@<name>.*<@<name>'${name}'<@g' ${new_xml_path}/${ipnat}${i}.xml
    sed -i 's@<uuid>.*<@<uuid>'${uuid}'<@g' ${new_xml_path}/${ipnat}${i}.xml
    sed -i "s@<source file='.*'></source>@<source file='${new_disk_path}/${ipnat}${i}.img'></source>@g" ${new_xml_path}/${ipnat}${i}.xml
    sed -i "s@<mac address='.*'></mac>@<mac address='52:54:00:${mac1}:${mac2}:${mac3}'></mac>@g" ${new_xml_path}/${ipnat}${i}.xml
    sed -i "s@<vcpu placement='static'>.*</vcpu>@<vcpu placement='static'>${cpu}</vcpu>@g" ${new_xml_path}/${ipnat}${i}.xml
    sed -i "s@<memory unit='KiB'>.*</memory>@<memory unit='KiB'>${mem}</memory>@g" ${new_xml_path}/${ipnat}${i}.xml
    sed -i "s@<currentMemory unit='KiB'>.*</currentMemory>@<currentMemory unit='KiB'>${mem}</currentMemory>@g" ${new_xml_path}/${ipnat}${i}.xml
    virt-customize -a ${new_disk_path}/${ipnat}${i}.img --run-command "sed -i 's@IPADDR=.*@IPADDR=${ipnat}${i}@g' /etc/sysconfig/network-scripts/ifcfg-eth0"
    virsh define ${new_xml_path}/${ipnat}${i}.xml
    virsh start ${name}        
done
EOF