#!/bin/sh
newip=$1;
newpasswd=$2;
if [ ! -f /tmp/newserver.sh ];then
cat >> /tmp/newserver.sh << "EOF"
#!/bin/sh
sed -i 's#rhel6.2.0#pc#' /vm/vm/$1/$1.xml;
[ $? != 0 ] && exit 1;
sed -i 's#/usr/libexec/qemu-kvm#/usr/bin/kvm#' /vm/vm/$1/$1.xml;
[ $? != 0 ] && exit 2;
if [ -f /vm/vm/$1/snapshot/*.xml ];then
sed -i 's#rhel6.2.0#pc#' /vm/vm/$1/snapshot/*.xml;
[ $? != 0 ] && exit 3;
sed -i 's#/usr/libexec/qemu-kvm#/usr/bin/kvm#' /vm/vm/$1/snapshot/*.xml;
[ $? != 0 ] && exit 4;
mkdir -p /var/lib/libvirt/qemu/snapshot/$1 && cp /vm/vm/$1/snapshot/*.xml /var/lib/libvirt/qemu/snapshot/$1;
[ $? != 0 ] && exit 5;
fi
virsh define /vm/vm/$1/$1.xml;
virsh start $1;
hostip=$(less /vm/vm/$1/$1.xml| grep value |grep -o -P '(\d{1,3}\.){3}\d{1,3}')
if [ -n $hostip ];then
for pingnum in $(seq 1 10);
do
ping -c 2 $hostip
if [ $? = 0 ] ; then
echo `date +%F` `date +%T` $1 done >> /tmp/okhost
echo =================================== > /dev/pts/ttynum
echo ===========$1======done======= > /dev/pts/ttynum
echo =================================== > /dev/pts/ttynum
break
fi
if (( $pingnum == 6 )) ; then
echo `date +%F` `date +%T` $1 done but dead ping >> /tmp/okhost
echo =================================== > /dev/pts/ttynum
echo ===========$1======done======= > /dev/pts/ttynum
echo =================================== > /dev/pts/ttynum
fi
done
fi
EOF
fi
chmod 700 /tmp/newserver.sh
#scp newserver.sh to newserver
expect -c "
spawn scp -r /tmp/newserver.sh root@$newip:/tmp/
expect {
\"*assword\" {set timeout 9000; send \"$newpasswd\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
[ $? != 0 ] && echo "create newserver or scp wang!" && exit;
echo "create and scp newserver ok"
if [ ! -f /tmp/zcc.host ];then
virsh list --all | sed '1,2d' | sed '$d' | awk '{print $2}'| grep -v "sktest"| sort > /tmp/zcc.host
cp /tmp/zcc.host /tmp/zcc.hostbak
fi
while read host
do
virsh destroy $host 2>/dev/null
sleep 3
virsh snapshot-list $host > /tmp/$$.snapshot
snapshotname=$(grep -o -P '\d{14}' /tmp/$$.snapshot)
rm /tmp/$$.snapshot
if [ -n "$snapshotname" ];then
virsh snapshot-delete $host $snapshotname;
fi
virsh undefine $host
#scp -r /vm/vm/$host root@10.0.10.11:/vm/vm/
expect -c "
spawn scp -r /vm/vm/$host/ root@$newip:/vm/vm/
expect {
\"*assword\" {set timeout 9000; send \"$newpasswd\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
if [ $? = 0 ];then
echo =============================
echo ====$host=====scp======OK====
echo =============================
mv /vm/vm/$host /vm/vm/$host.$(date +%Y-%m-%d.%H:%M:%S).drop
sed -i "/^$host$/d" /tmp/zcc.host
fi
expect -c "
spawn ssh root@$newip \"nohup /tmp/newserver.sh $host\" &
expect {
\"*assword\" {set timeout 9000; send \"$newpasswd\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
done < /tmp/zcc.host