美文网首页
KVM虚拟机克隆移除脚本

KVM虚拟机克隆移除脚本

作者: 无处安放的躁动 | 来源:发表于2019-04-08 16:12 被阅读0次
[root@test2 SHELL]# cat REMOVE_virsh 
#!/bin/bash
#james.liu
#Mon Apr  8 09:07:08 CST 2019
#该脚本主要作用的是,移除不需要的主机相关配置

read -p "请输入需要移除的主机名称:" VMNAME
echo "您输入的主机名称为:$VMNAME"

HOST="/etc/hosts"
ALIAS="/etc/bashrc"
IMG_DIR="/opt/images"


#判断是否存在该机器
re=`virsh list --all |awk -v VMNAME=$VMNAME  '{if($2==VMNAME)print 1}'`
if [ $re != 1 ];then
    echo "没有虚拟机:$VMNAME,正在推出... "
    exit 100
fi
#需要移除的信息
echo -e "\033[33m虚拟机$VMNAME需要一次的信息如下\033[0m"
echo "############################################"
sed -n "/$VMNAME$/p" $HOST
sed -n "/$VMNAME\=/p" $ALIAS
find $IMG_DIR -name $VMNAME.xml -type f
find $IMG_DIR -name $VMNAME.qcow2 -type f
echo ""
virsh list --all |awk -v VMNAME="$VMNAME"  '{if(NR<=2)print $0 ;else if($2==VMNAME)print $0}'
echo "############################################"

read -p "请按任意键继续,ctrl+c退出"
read -n1
#移除
virsh undefine $VMNAME
sed -i "/$VMNAME$/d" $HOST
sed -i "/$VMNAME\=/d" $ALIAS

cd $IMG_DIR

if [ -f $VMNAME.qcow2 ];then
    rm -rf $VMNAME.qcow2
else 
    echo "文件$VMNAME.qcow2文件不存在"
fi

if [ -f $VMNAME.xml ];then
    rm -rf  $VMNAME.xml
else
    echo "文件$VMNAME.xml文件不存在"
fi

相关文章

网友评论

      本文标题:KVM虚拟机克隆移除脚本

      本文链接:https://www.haomeiwen.com/subject/kiquiqtx.html