kvm批量创建用户
AI-摘要
KunKunYu GPT
AI初始化中...
介绍自己
生成本文简介
推荐相关文章
前往主页
前往tianli博客
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
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
作者编辑不易,如有转载请注明出处。完整转载来自https://wangairui.com 网站名称:猫扑linux
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果