#!/bin/bash
#创建单个虚拟机(需要电脑中存在模板虚拟机)
#获取新虚拟机名称(单个创建模式)
get_newname(){
while true
do
read -p "请输入新虚拟机名称:" newname
if [ $newname ];then
break
else
echo "************"
echo "请输入姓名!"
echo "************"
fi
done
}
get_newmemary(){
while true
do
current_free_mem=`free -h|awk ‘/^Mem/{print $4}‘|sed -r ‘s/(.*).$/\1/‘`
mem_total=`free -h|awk ‘/^Mem/{print $2}‘|sed -r ‘s/(.*).$/\1/‘`
echo "目前本机内存总大小:${mem_total} G"
echo "当前空闲内存大小为:${current_free_mem} MB"
read -p "请输入新虚拟机内存大小(单位G):" newmemary
if [ $newmemary ];then
if [[ $newmemary < $mem_total ]];then
break
else
echo "**********************************"
echo "输入的数值必须小于当前内存总大小!"
echo "**********************************"
fi
else
echo "********************"
echo "请输入新虚拟机内存!"
echo "********************"
fi
done
}
get_newcpu(){
while true
do
core=`cat /proc/cpuinfo| grep "processor"| wc -l`
echo "可用core个数:${core}"
read -p "请输入新虚拟机处理器核数:" newcpu
if [ $newcpu ];then
if [ $newcpu -le $core ];then
break
else
echo "******************************"
echo "不能超过可用个数或者输入错误!"
echo "******************************"
fi
else
echo "**************"
echo "输入不能为空!"
echo "**************"
fi
done
}
#设置虚拟机名称(自定义创建多个虚拟机)
set_group_name(){
#虚拟机个数
read -p "新虚拟机个数:" vir_num
free_vir=$vir_num
#功能分组
echo "**********************************************************************************"
echo "设置好组名之后,虚拟机将每组下的虚拟机自动用【组名01、组名01、组名03、...】的形式命名"
echo "**********************************************************************************"
read -p "需要分几种功能类型(大于1):" vir_type
if [ ${vir_type} -gt ${vir_num} ];then
echo "输入不符合规则!"
exit
fi
#设置名字,组名+数字
k=1
for((i=1;i<=${vir_type};i++))
do
read -p "第${i}组组名:" group[$i]
read -p "分配新虚拟机个数:" group_num_vir
#检测输入
free_vir=$((${free_vir}-${group_num_vir}))
if [ ${group_num_vir} -gt ${vir_num} ];then
echo "输入错误,超过自定义虚拟机个数!"
exit
elif [ ${free_vir} -lt $((${vir_type}-${i})) ];then
echo "分配不均,请重新分配!"
exit
fi
#记录组内虚拟机个数
for((j=1;j<=${group_num_vir};j++))
do
name_n="${group[$i]}${j}"
name[${k}]=${name_n}
k=$(($k+1))
done
done
echo "准备创建如下虚拟机:"
for((i=1;i<=${vir_num};i++))
do
echo ${name[${i}]}
done
}
#设置xml所需参数
set_xml_args_single(){
uuid=`uuidgen`
src_path=/mnt/lv2/centos7.0.qcow2
src_xml=/etc/libvirt/qemu/centos7.0.xml
new_path=/mnt/lv1/${newname}.qcow2
new_path_sed="\/mnt\/lv1\/${newname}.qcow2"
new_xml=/etc/libvirt/qemu/${newname}.xml
}
#设置xml所需参数(多个),例如:【set_xml_args ${name[${i}]} $new_memary $new_cpu】
set_xml_args(){
newname=$1
newmemary=$2
newcpu=$3
uuid=`uuidgen`
src_path=/mnt/lv2/centos7.0.qcow2
src_xml=/etc/libvirt/qemu/centos7.0.xml
new_path=/mnt/lv1/${newname}.qcow2
new_path_sed="\/mnt\/lv1\/${newname}.qcow2"
new_xml=/etc/libvirt/qemu/${newname}.xml
}
#复制模板、xml
copy_model_xml(){
cp $src_path $new_path
cp $src_xml $new_xml
}
#修改xml文件
modification_xml(){
sed -ri "s/(<name>).*(<\/name>)/\1${newname}\2/" $new_xml
sed -ri "s/(<uuid>).*(<\/uuid>)/\1${uuid}\2/" $new_xml
mem_kb=$((${newmemary}*1024*1024))
sed -ri "s/(<memory.*>).*(<\/memory>)/\1${mem_kb}\2/" $new_xml
sed -ri "s/(<currentMemory.*>).*(<\/currentMemory>)/\1${mem_kb}\2/" $new_xml
sed -ri "s/(<vcpu.*>).*(<\/vcpu>)/\1${newcpu}\2/" $new_xml
sed -ri "s/(<source file=‘).*(‘\/>)/\1${new_path_sed}\2/" $new_xml
mac_addr=`openssl rand -hex 3 | sed -r ‘s/..\B/&:/g‘`
sed -ri "s/(<mac address=‘..:..:..:).*(‘\/>)/\1${mac_addr}\2/" $new_xml
}
#define
define_vir_single(){
virsh define $new_xml
echo "**********"
echo "${newname}建完成!"
echo "**********"
}
#------------运行分界线------------------------------------
echo "1)single"
echo "2)group"
read -p "选择你的操作:" str
case $str in
single)
get_newname
get_newmemary
get_newcpu
set_xml_args_single
copy_model_xml
modification_xml
define_vir_single
;;
group)
set_group_name
get_newmemary
get_newcpu
for((i=1;i<=${vir_num};i++))
do
set_xml_args ${name[${i}]} $newmemary $newcpu
copy_model_xml
modification_xml
define_vir_single
done
;;
*)
exit
;;
esac
原文地址:https://blog.51cto.com/lingxudong/2363332