博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
KVM 虚拟机自动克隆脚本
阅读量:5951 次
发布时间:2019-06-19

本文共 3248 字,大约阅读时间需要 10 分钟。

#!/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 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"
#启动克隆完毕的虚拟机

转载地址:http://ikpxx.baihongyu.com/

你可能感兴趣的文章
Shell编程基础
查看>>
Shell之Sed常用用法
查看>>
3.1
查看>>
校验表单如何摆脱 if else ?
查看>>
<气场>读书笔记
查看>>
领域驱动设计,构建简单的新闻系统,20分钟够吗?
查看>>
web安全问题分析与防御总结
查看>>
React 组件通信之 React context
查看>>
Linux下通过配置Crontab实现进程守护
查看>>
ios 打包上传Appstore 时报的错误 90101 90149
查看>>
密码概述
查看>>
jQuery的技巧01
查看>>
基于泛型实现的ibatis通用分页查询
查看>>
gopacket 使用
查看>>
AlertDialog对话框
查看>>
我的友情链接
查看>>
linux安全---cacti+ntop监控
查看>>
鸟哥的linux私房菜-shell简单学习-1
查看>>
nagios配置监控的一些思路和工作流程
查看>>
通讯组基本管理任务三
查看>>