#!/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
Crazy_Linux !
本文出自 “Crazy_Linux” 博客,请务必保留此出处http://mkernel.blog.51cto.com/8015041/1433376
原文:http://mkernel.blog.51cto.com/8015041/1433376