kvm 創建虛擬機腳本(centos7)
阿新 • • 發佈:2019-03-15
qcow 分配 自動 gre 目前 rep cati 例如 存在
#!/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
kvm 創建虛擬機腳本(centos7)