KVM虚拟机自定义创建脚本

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值