kvm多台虚拟机管理脚本

1. 引言

在Deepin系统上,安装了kvm虚拟机,基本都是以虚拟集群的方式来操作,所以免不了总是销毁,创建,启停等操作。所以就花了点时间 ,创建一个脚本,来管理这些虚拟机。

2. 正文

我一般是切换到root进行操作的。

所在在/root目录下创建mykvmtools目录,创建ktools脚本,内容如下:

#!/bin/bash
# kvm虚拟机管理脚本

NODENUM=$2
IMGPATH="/var/lib/libvirt/images"

get_kvm_name(){
	IFS=','
	for i in $*
	do
		has_flog=$(echo "$i"|grep "-"|wc -l)
		if [ "$a" != "0" ];then
			for j in $(seq ${i%-*} ${i#*-} 2>/dev/null)
			do
				echo "$j"
			done
		else
			echo -ne "\b"
		fi
	done | tr '\n' ','|sed 's/.$//g'
	echo 
}

#检查
check_node(){
	local num=$1
	local cnt=$(virsh list --all|grep node${num}|wc -l)
	if [ "${cnt}" == "1" ];then
		echo "true"
	else
		echo "false"
	fi
}
#开机
start_node(){
	IFS=','
	for n in $*
	do
		local s=$(check_node ${n})
		if [ "${s}" == "true" ];then
			virsh start node${n} >/dev/null  2>&1
			if [ $? -ne 0 ];then
				echo "node${n} 开机失败"
			fi
		else
			echo "node${n} 不存在"
		fi
	done
}

#关机
stop_node(){
	IFS=','
	for n in $*
	do
		local s=$(check_node ${n})
		if [ "${s}" == "true" ];then
			virsh destroy node${n} >/dev/null  2>&1
			if [ $? -ne 0 ];then
				echo "node${n} 关机失败"
			fi
		else
			echo "node${n} 不存在"
		fi
	done
}

#删除
delete_node(){
	IFS=','
	for n in $*
	do
		stop_node ${n}
		virsh undefine node${n} >/dev/null  2>&1
		if [ $? -eq 0 ];then
			rm -rf ${IMGPATH}/node${n}.qcow2
		else
			echo "node${n} 删除失败"
		fi
	done
}

#克隆
clone_node(){
	IFS=','
	for n in $*
	do
		local s=$(check_node ${n})
		if [ "${s}" == "false" ];then
			virt-clone -o base -n node${n} -f ${IMGPATH}/node${n}.qcow2 -m 00:00:00:80:00:$n >/dev/null 2>&1
			if [ $? -ne 0 ];then
				echo "node${n} 克隆失败"
			fi
		else
			echo "node${n} 已经存在"
		fi
	done
}
#重置
reset_node(){
	IFS=','
	for n in $*
	do
		local s=$(check_node ${n})
		if [ "${s}" == "false" ];then
			echo "node${n} 不存在"
		else
			delete_node ${n}
			clone_node ${n}
			start_node ${n}
		fi
	done
}

#暂停
pauze_node(){
	IFS=','
	for n in $*
	do
		local s=$(check_node ${n})
		if [ "${s}" == "false" ];then
			echo "node${n} 不存在"
		else
			virsh suspend ${n} >/dev/null 2>&1
			if [ $? -ne 0 ];then
				echo "node${n} 暂停失败"
			fi
		fi
	done

}
#恢复
resume_node(){
	IFS=','
	for n in $*
	do
		local s=$(check_node ${n})
		if [ "${s}" == "false" ];then
			echo "node${n} 不存在"
		else
			virsh resume ${n} >/dev/null 2>&1
			if [ $? -ne 0 ];then
				echo "node${n} 暂停失败"
			fi
		fi
	done

}

main(){

	if [ $# -eq 1 ] || [ $# -lt 2 ];then
		echo "参数个数错误"
		echo "使用方式:$(basename $0) [选项] [值]"
		echo "         $(basename $0) [create|clone] [xx,xx-xx,xx] 克隆/创建"
		echo "         $(basename $0) [delete] [xx,xx-xx,xx] 删除"
		echo "         $(basename $0) [reset] [xx,xx-xx,xx] 重置(删除并创建)"
		echo "         $(basename $0) [restart] [xx,xx-xx,xx] 重启。"
		echo "         $(basename $0) [resume] [xx,xx-xx,xx] 恢复挂起"
		echo "         $(basename $0) [start|stop] [xx,xx-xx,xx] 开机/停机"
		echo "         $(basename $0) [suspend|pauze] [xx,xx-xx,xx] 挂起/暂停"
		exit 1
	fi

	NODELIST=$(get_kvm_name $*)

	case $1 in
		clone|create)
			clone_node ${NODELIST}
			;;
		delete)
			delete_node ${NODELIST}
			;;
		reset)
			reset_node ${NODELIST}
			;;
		restart)
			stop_node ${NODELIST}
			start_node ${NODELIST}
			;;
		resume)
			resume_node ${NODELIST}
			;;
		suspend|pauze)
			pauze_node ${NODELIST}
			;;
		start)
			start_node ${NODELIST}
			;;
		stop)
			stop_node ${NODELIST}
			;;
		*)
			echo "无效参数"
			exit 0
	esac
}
main $*

然后修改权限chmod +x ktools

最后把/root/mykvltools添加到系统环境变量中,这样就可以通过ktools create 11,12-15,18的方式来管理多台虚拟机了。

3. 结语

祝有幸看到本篇博客和使用该脚本的同学玩的愉快

你可能感兴趣的:(kvm多台虚拟机管理脚本)