首页 > 其他 > 详细

KVM虚机克隆脚本

时间:2014-07-02 16:14:32      阅读:832      评论:0      收藏:0      [点我收藏+]

#!/bin/sh 


#############################################

###         Auto Clone VM                 ###

###         2014-5-9                      ###

###         Owner: YiQiang.Wei            ###

###         Lastedit: 5-11                ###

###                                       ###

#############################################





default(){

echo 

echo 

echo -e "\033[32;49;1m             [Default Mode] \033[39;49;0m"

echo 

echo

TEM=centos65

IMGDIR=/var/lib/libvirt/images

echo -n "Please Input New VM Name: "

read NNAME

if [ -z $NNAME ];then

echo

#echo "Input Error New VM Name is Empty ! "

echo -e "\033[31mInput Error New VM Name is Empty !  \033[0m" 

echo

exit 2 

fi 

virsh list --all |sed -e ‘1,2d‘|awk ‘{print $2}‘|grep -q $NNAME

retval=$?

if [ $retval -eq 0 ];then 

echo 

#echo "Input Error New VM Name is Already Use ! "

echo -e "\033[31mError VM $NNAME is Already Use !   \033[0m" 

echo 

exit 2 

fi


echo 


echo -e "\033[32;49;1m[$TEM to $NNAME] \033[39;49;0m"

echo 

echo -e "\033[32;49;1mstarting Clone... \033[39;49;0m"

#echo "Starting Clone..."

echo

/usr/bin/virt-clone -o $TEM -n $NNAME --file $IMGDIR/$NNAME.img --file $IMGDIR/$NNAME-2.img


}



custom(){

echo 

        echo 

        #echo "Custom Mode"

echo -e "\033[32;49;1m             [Custom Mode] \033[39;49;0m"

echo

echo

echo -n "Please Input Template VM Name [Default is template ! ] :  "

read TEM

TEM=${TEM:-"centos65"}

echo $TEM

ST=`virsh list --all |grep $TEM|awk ‘{print $NF}‘`

if [ "$ST" != "off" ];then 

echo -e "\033[31mError VM $TEM is Running !   \033[0m"

exit 2 

fi

echo -n "Please Input New VM Name: "

read NNAME

NNAME=${NNAME:-"vm-$RANDOM"}

echo $NNAME

virsh list --all |sed -e ‘1,2d‘|awk ‘{print $2}‘|grep -q $NNAME

        retval=$?

        if [ $retval -eq 0 ];then

                echo 

                echo -e "\033[31mError VM $NNAME is Already Use !   \033[0m" 

                echo 

                exit 2

        fi

echo -n "Please Input Img Path 1 [Default is /var/lib/libvirt/images/$NNAME.img ! ] :  "

read PATH1

PATH1=${PATH1:-"/var/lib/libvirt/images/$NNAME.img"}

echo -n "Please Input Img Path 2 [Default is /var/lib/libvirt/images/$NNAME-2.img ! ] :  "

read PATH2

PATH2=${PATH2:-"/var/lib/libvirt/images/$NNAME-2.img"}

if [  ! -z $TEM -a ! -x $NNAME -a ! -z $PATH1 -a ! -z $PATH2 ]; then 

echo -e "\033[32;49;1m [$TEM to $NNAME] \033[39;49;0m"

        echo 

        echo -e "\033[32;49;1mstarting Clone... \033[39;49;0m"

/usr/bin/virt-clone -o $TEM -n $NNAME --file $PATH1 --file $PATH2

else 

echo -e "\033[31mUnknown Error !   \033[0m" 

fi


}




multiple(){

echo 

        echo 

echo -e "\033[32;49;1m             [Mutiple Mode] \033[39;49;0m"

echo

echo

TEM=centos65

        IMGDIR=/var/lib/libvirt/images

        echo -n "Please Input New VM Name: "

        read NNAME

        if [ -z $NNAME ];then

                echo

                #echo "Input Error New VM Name is Empty ! "

echo -e "\033[31mInput Error New VM Name is Empty !  \033[0m" 

                echo

                exit 2

        fi

for i in $NNAME 

do 

virsh list --all |sed -e ‘1,2d‘|awk ‘{print $2}‘|grep -q $i

        retval=$?

        if [ $retval -eq 0 ];then

                echo 

                #echo "Input Error New VM Name is Already Use ! "

                echo -e "\033[31mInput Error VM $i is Already Use !   \033[0m" 

                echo 

                exit 2

        fi


echo -e "\033[32;49;1m [$TEM to $i] \033[39;49;0m"

        echo 

        #echo "Starting Clone..."

echo -e "\033[32;49;1mstarting Clone... \033[39;49;0m"

        echo

        /usr/bin/virt-clone -o $TEM -n $i --file $IMGDIR/$i.img --file $IMGDIR/$i-2.img

echo

done 


}


#############################################################################################################



#Default

#Custom


echo

echo

echo  ‘1) Default  Mode‘

echo  ‘2) Custom   Mode ‘

echo  ‘3) Multiple Mode ‘

echo 

echo -n "Please Select: "

read SMODE


if [ -z $SMODE ];then 

SMODE=1

fi


case $SMODE in 

1)

default 

;;

2)

custom

;;

3)

multiple

;;

*)

#Select error

echo -e "\033[31mSelect Error !  \033[0m" 

;;

esac




#EOF

bubuko.com,布布扣

Crazy_Linux ! 

本文出自 “Crazy_Linux” 博客,请务必保留此出处http://mkernel.blog.51cto.com/8015041/1433376

KVM虚机克隆脚本,布布扣,bubuko.com

KVM虚机克隆脚本

原文:http://mkernel.blog.51cto.com/8015041/1433376

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!