KVM虚拟机自定义创建脚本
说明:快速定义(取消)kvm虚拟机
#!/bin/bash
#author:tang
#version:3.1-1
#date:2019-03-10
#comment: this script can add or delete virtual machine immediately
# 5 ----> define domain uuid conflict
# 9 ----> images messing
# 8 ----> describe file messing
qcow='/var/lib/libvirt/images/'
template='.rh7_template.img'
template_xfile='.rhel7.xml'
confpath='/etc/libvirt/qemu/'
machine=$2
Define_new(){
if [ -z "${machine}" ];then
echo -e "\e[35m The name is empty !!!\e[0m"
echo ${machine}
exit 4
elif [ -f ${confpath}${machine}.xml ];then
echo -e "\e[35m The name is exist !!!\e[0m"
exit 3
else
sleep 0.1
fi
#create qcow
if [ -e ${qcow}${template} ];then
echo -en "Creating virtual machine......"
qemu-img create -f qcow2 -b ${qcow}${template} ${qcow}${machine}.img &> /dev/null || exit 6
sleep 0.2
echo -en "\e[34m \t\t\t[OK] \n\e[0m"
else
echo "ERROR ${qcow}${template} not found"
exit 9
fi
#copy the describe file
if [ -e ${qcow}${template_xfile} ];then
cp ${qcow}${template_xfile} ${confpath}${machine}.xml 2> /dev/null || exit 7
sed -i "39 s/rh7_template/${machine}/" ${confpath}${machine}.xml
sed -i "/<name>/ s/rh7_template/${machine}/" ${confpath}${machine}.xml
else
echo "ERROR ${qcow}${template_xfile} not found"
exit 8
fi
virsh define ${confpath}${machine}.xml > /dev/null || exit 5
echo -en "Defining virtual machine......"
sleep 0.2
echo -en "\e[34m \t\t\t[OK] \n\e[0m"
}
Undefine(){
virsh dominfo ${machine} &> /dev/null
if [ $? -eq 0 ];then
virsh undefine ${machine} 2> /dev/null || exit 4
else
echo "ERROR ${machine} not exist"
exit 2
fi
}
case $1 in
add)
Define_new
;;
delete)
Undefine
;;
*)
echo "Useage vm [add|delete] <name>"
;;
esac