kvm镜像复制

#!/bin/bash
#本脚本用于拷贝kvm镜像文件,只需要选择拷贝哪个镜像,拷贝几次即可
#user:falconhero
#qq  : 40594324
#date: 2012.12.20
cd /home/vm
ls |grep qcow2
numeric=`ls|grep qcow2|wc -l`
if [ $numeric -eq 1 ]
    then
        MIRROR=`ls |grep qcow2`
else
    echo "请选择你要拷贝的镜像文件"
    read MIRROR
fi

echo "此镜像文件你想要拷贝几次?"
read num    

for i in `ls $MIRROR`
do
    NAME=`echo $i|sed -n 's/^\(.*[-_]\)\([[:digit:]]\+\).qcow2$/\1/p'`
    NUMBER=`echo $i|sed -n 's/^\(.*[-_]\)\([[:digit:]]\+\).qcow2$/\2/p'`
    #NUMBER=10#${NUMBER}
    NAME1=`echo $i|sed -n 's/^\(.*\).qcow2/\1/p'`
    vncmaxport=`grep vnc /etc/libvirt/qemu/*.xml|sed -n 's/^.*port\(.*\)auto.*$/\1/p'|cut -b 3-6|sort -rn|head -1`
    vncmirrorport=`grep vnc /etc/libvirt/qemu/${NAME1}.xml|sed -n 's/^.*port\(.*\)auto.*$/\1/p'|cut -b 3-6`
    elq=/etc/libvirt/qemu/  
    Mn=`ps -ef |grep $MIRROR|wc -l `
    #let "A = $NUMBER + 1"
    A=`expr $NUMBER + 1`
    B=`expr $NUMBER + $num`
    C=`expr $vncmaxport + 1`
    D=`expr $vncmaxport + $num`
    if [ $Mn -eq 2  ]
       then
           echo "*****镜像现在是启动状态,需要关闭,请确认是否关闭****** "
           echo "1,y 为关闭镜像,2,n 为退出操作"
           echo "1|y)yes"
           echo "2|n)no"
           read judge
           
           case $judge in
                1|y) virsh destroy $NAME1
                     sleep 10
                     ;;
                2|n) exit
                     ;;
                  *) echo "输入错误,请重输"
                     ;;
           esac
    fi
    
    for ((j=$A,k=$C;j<=$B,k<=$D;j++,k++))
        do
        if [ $j -le 9  ]    
           then
              if [ ! -f ${NAME}0$j.qcow2 ]
                 then
                    cp -i $MIRROR  ${NAME}0$j.qcow2 &
                    cp ${elq}${NAME1}.xml               ${elq}${NAME}0$j.xml
                    sed -i s/${NAME1}/${NAME}0$j/g      ${elq}${NAME}0$j.xml
                    sed -i '/^.*uuid/ s/.*//g'          ${elq}${NAME}0$j.xml
                    sed -i '/^.*<mac address/ s/.*//g'  ${elq}${NAME}0$j.xml
                    sed -i s/$vncmirrorport/$k/g        ${elq}${NAME}0$j.xml
                    virsh define                        ${elq}${NAME}0$j.xml
              fi
        else
              if [ ! -f ${NAME}$j.qcow2 ]
                 then
                    cp -i $MIRROR ${NAME}$j.qcow2  &
                    cp  ${elq}${NAME1}.xml              ${elq}${NAME}$j.xml
                    sed -i s/${NAME1}/${NAME}$j/g       ${elq}${NAME}$j.xml
                    sed -i '/^.*uuid/ s/.*//g'          ${elq}${NAME}$j.xml
                    sed -i '/^.*<mac address/ s/.*//g'  ${elq}${NAME}$j.xml
                    sed -i s/$vncmirrorport/$k/g        ${elq}${NAME}$j.xml           
                    virsh define                        ${elq}${NAME}$j.xml
             fi
        fi
    done
done

wait
for ((j=$A;j<=$B;j++))
    do
    virsh start ${NAME1}
    virsh start ${NAME}0$j
    virsh start ${NAME}$j
done

你可能感兴趣的:(镜像,复制,kvm)