Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1354586
  • 博文数量: 166
  • 博客积分: 46
  • 博客等级: 民兵
  • 技术积分: 4061
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-11 13:45
个人简介

现任职北京某互联网公司运维经理,高级架构师,涉足互联网运维行业已经超过10年。曾服务于京东商城,互动百科等互联网公司,早期运维界新星。 长期专研,C语言开发,操作系统内核,大型互联网架构。http://www.bdkyr.com

文章分类

分类: 虚拟化

2016-01-18 10:03:59

#!/bin/sh
#----------------------------------#
# virt-clone auto script[虚拟机自动克隆脚本]
# create by xuekun
# date 2015-12-26
# 开源人
#----------------------------------#

# Source function library.
. /etc/rc.d/init.d/functions
#
SOUR_SERVER=$1
DIRE_SERVER=$2
IP_ADDRESS=$3
HOST_NAME=$4
FILE_NAME=${DIRE_SERVER}.img
FILE_PATH=/export/servers/${FILE_NAME}
XML_CONFIG_PATH=/etc/libvirt/qemu
MOUNT_DIR=/mnt
DEV_DIR=
IFCFG_FILE=/etc/sysconfig/network-scripts/ifcfg-eth0
IFCFG_FILE_PATH=${MOUNT_DIR}${IFCFG_FILE}

NET_WORK_FILE=/etc/sysconfig/network
NET_WORK_PATH=${MOUNT_DIR}${NET_WORK_FILE}

NET_RULES_FILE=/etc/udev/rules.d/70-persistent-net.rules
NET_RULES_PATH=${MOUNT_DIR}${NET_RULES_FILE}

XML_LIST_FILE=xml_list.log
XML_TEMP_DIR=/home/xuekun/${XML_LIST_FILE}

#log目录及文件
LOG_PATH=/app/log
LOG_FILE=${LOG_PATH}/server_clone.log

#判断虚拟机是否运行
RETVAL_S=$(/usr/bin/virsh list|grep ${SOUR_SERVER})
[ -n ${RETVAL_S} ] && exit 1
#判断目标主机是否存在
RETVAL_D=$(/bin/find ${XML_CONFIG_PATH} -name ${DIRE_SERVER}.xml)
[ -n ${RETVAL_D} ] && exit 1

#start virtclone
echo "[$(date +%Y-%m-%d-%H:%M:%S)] $$.............start" >> {LOG_FILE}
virt-clone -o ${SOUR_SERVER}  -n ${DIRE_SERVER} --file ${FILE_PATH} >>{LOG_FILE}
echo "[$(date +%Y-%m-%d-%H:%M:%S)] $$.............finished" >> {LOG_FILE}
action $"${DIRE_SERVER} clone successful: " /bin/true
sleep 2

#修改配置
echo "[$(date +%Y-%m-%d-%H:%M:%S)] $$.............config virt server" >> {LOG_FILE}
#get address from xml
str=$(grep "mac address=" ${XML_CONFIG_PATH}/*.xml)
HW_ADDRESS=${str:20:17}
echo "[$(date +%Y-%m-%d-%H:%M:%S)] $$.............IP_ADDRESS:${IP_ADDRESS}" >> {LOG_FILE}
echo "[$(date +%Y-%m-%d-%H:%M:%S)] $$.............HW_ADDRESS:${HW_ADDRESS}" >> {LOG_FILE}
#判断目录是否存在
[ ! -d ${MOUNT_DIR} ] && mkdir ${MOUNT_DIR}
#
kpartx -av ${FILE_PATH} >> {LOG_FILE}
DEV_DIR=$(kpartx -av ${FILE_PATH}|tail -n 1)|awk '{print $3}'
#挂载目录到MOUNT_DIR
mount /dev/mapper/${DEV_DIR} ${MOUNT_DIR}
#输出配置文件到LOG
/bin/cat ${IFCFG_FILE_PATH} >>{LOG_FILE}

#replace IP
IP_ADDR=$(grep "IPADDR" ${IFCFG_FILE_PATH})
sed -i "s%${IP_ADDR}%IPADDR=${IP_ADDRESS}%" ${IFCFG_FILE_PATH}

#replace MAC
HW_ADDR=$(grep "HWADDR" ${IFCFG_FILE_PATH})
sed -i "s%${HW_ADDR}%HWADDR=${HW_ADDRESS}%" ${IFCFG_FILE_PATH}

#replace hostname
HOST_NAME=$(grep HOSTNAME ${NET_WORK_PATH})
sed -i "s%${HOST_NAME}%HOSTNAME=${HOST_NAME}%" ${NET_WORK_PATH}
echo  >${NET_RULES_PATH}

umount ${MOUNT_DIR}
kpartx -d ${FILE_PATH}

#replace xml配置文件中的VNC端口
#get vnc port
/bin/ls -l ${XML_CONFIG_PATH}/*.xml|awk '{print $9}' >${XML_TEMP_DIR}
num=$(while read line ;do str=$(cat ${XML_CONFIG_PATH}/${line} |grep "autoport");echo ${str:31:4} ;done <${XML_TEMP_DIR} |sort|tail -n 1)
PORT_VNC=$((${num}+1))
echo "[$(date +%Y-%m-%d-%H:%M:%S)] $$.............PORT_VNC:${PORT_VNC}" >> {LOG_FILE}

VNC_PORT=$(grep "autoport" ${XML_CONFIG_PATH}/${DIRE_SERVER}.xml|awk '{print $3}')
sed -i "s%${VNC_PORT}%port='${PORT_VNC}'%"  ${XML_CONFIG_PATH}/${DIRE_SERVER}.xml


#Display  virt server  config
echo -e "\033[32m #--------------------------------------------------#  \033[0m"
echo -n "HOSTNAME="
echo -e "\033[32m ${HOST_NAME}  \033[0m"

echo -n "IPADDR="
echo -e "\033[32m ${IP_ADDRESS} \033[0m"

echo -n "VNCPORT="
echo -e "\033[32m ${PORT_VNC} \033[0m"
echo -e "\033[32m #--------------------------------------------------#  \033[0m"

#启动克隆完毕的虚拟机

阅读(4263) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~