脚本管理KVM

脚本管理KVM

 1、快速开启/关闭所有虚拟机

#/bin/bash
virsh list --all
HELP(){
echo "功能:快速开启/关闭所有虚拟机"
echo "用法:$(basename $0) <domain> [1 open all|2 close all]"
}
echo "0. close all
1. open all"
read -p "choose your option(1/2): " op
#close_all函数:强制关闭所有的虚拟机
close_all(){
    for i in `virsh list|egrep -v "^$"|awk 'NR>2{print $2}'`;do
        virsh destroy $i
    done
}
#open_all函数:开启所有关闭的虚拟机
open_all(){
for i in `virsh list --inactive|egrep -v "^$"|awk 'NR>2{print $2}'`;do
        virsh start $i
    done
}
case "$op" in
0)
    close_all;;
1)
    open_all;;
*)
    echo "error: Please again choose" && HELP && exit;;
esac

 

 

 

2、快速添加、删除、列出虚拟机磁盘

#/bin/bash
virsh list --all
HELP(){
echo "功能:快速添加、删除、列出虚拟机磁盘"
echo "用法:$(basename $0) <domain> [1 add|2 del|3 list]"
}
read -p "Please input disk on which virtual: " v
[[ -z "$v" || "$v" == "-h" || "$v" == "--help" ]] && HELP && exit #判断输入是否存在
stats=$(virsh list --all |awk '$2=="'$v'"{print $3}')
[ -z $stats ] && echo "error: virtual not exist" && exit
[ "$stats" != "running" ] && echo "error: virtual $v not running" && exit  #热添加判断运行情况
echo "1. add
2. delect
3. list"
read -p "choose your option(1/2/3): " op
case "$op" in
1)
    read -p "Please input your disk size(G):" size
    [[ !$size =~ ^[0-9] ]]  && echo "error: size must munber" && exit
    pool=/mnt/lvm1
    cd $pool
    i=1
    while :;do             #遍历查找,防止重命名qcow...
        [ -f "$v-add-$i.qcow2" ] && let i++ && continue
        diskfile=$v-add-$i.qcow2&& break
    done
    qemu-img create -f qcow2 $diskfile $size
    bus=$(virsh dumpxml centos7.0 |awk -F "'" '/[shv]da/{print $4}'|sed -n "1p")  #磁盘类型
    FirstDiskName=$(virsh domblklist $v |awk 'NR==3{print $1}')
    DiskType=${FirstDiskName::-1}         #第一块磁盘前缀
    for j in {b..z};do   #循环查找,防止重命名vdb...
        ! virsh domblklist $v|grep "^$DiskType$j" &> /dev/null && diskName=$DiskType$j && break
    done
    #磁盘配置文件
    tmpfile=/tmp/diskfile
        cat > $tmpfile <<- eof
        <disk type='file' device='disk'>
                <driver name='qemu' type='qcow2'/>
                <source file='$pool/$diskfile'/>
                <target dev='$diskName' bus='$bus'/>
        </disk>
        eof
    #热添加磁盘并列出
    virsh attach-device $v $tmpfile --persistent
    virsh domblklist $v
;;
2)
    virsh domblklist $v
    read -p "Please input delete disk: " disk
    ! virsh domblklist $v |grep -w "^$disk" && echo "error: disk $disk not exist" && exit #判断输入磁盘是否存在
    [[ $disk =~ a$ ]] && echo "error: systemd disk $disk not allow delete" && exit  #系统磁盘不允许删除
    virsh detach-disk $v $disk --persistent
    rm -rf $pool/$diskfile
;;
3)
    virsh domblklist $v
;;
*)
    echo "error: Please again choose" && HELP && exit
;;
esac

 

 

3、虚拟机网卡

#/bin/bash
HELP(){
echo "功能:添加虚拟机网卡"
}
virsh list --all
read -p"输入你要添加硬件虚拟机: " h
[[ -z "$v" || "$v" == "-h" || "$v" == "--help" ]] && HELP && exit #判断输入是否>存在
stats=$(virsh list --all |awk '$2=="'$v'"{print $3}')
[ -z $stats ] && echo "error: virtual not exist" && exit
##创建网卡配置文件
netfine=/tmp/network.xml
cat > $netfine <<- EOF
<interface type='network'>
<source network='default' bridge='virbr0'/>
<model type='virtio'/>
</interface>
EOF
virsh attach-device $h $netfine --persistent

 

 

4、快照管理

#/bin/bash
HELP(){
echo "功能:快照管理虚拟机"
echo "用法:$(basename $0) <domain> [virtname]"
}
clear
virsh list --all
read -p "Please input snapshot virtname: " virtname
[[ -z "$virtname" || "$virtname" == "-h" || "$virtname" == "--help" ]] && HELP && exit
stats=$(virsh list --all |awk '$2=="'$virtname'"{print $3}')
[ -z $stats ] && echo "error: virtual not exits" && exit
read -p "Please input snapshot name:" snaName
virsh snapshot-create-as $virtname $snaName
virsh snapshot-list $virtname

 

 

5、快速克隆虚拟机(连接克隆 && 完全克隆)

#/bin/bash
HELP(){
clear
echo "功能:快速克隆虚拟机"
echo "用法:$(basename $0) <domain> [0 link clone|1 full clone]"
virsh list --inactive
echo "(ps:开机的虚拟机不能克隆如需要克隆开机的虚拟机则先执行关机)"
}
virsh list --inactive
read -p "Please input clone-master virtual(--help): " clone
[[ -z "$clone" || "$clone" == "-h" || "$clone" == "--help" ]] && HELP && exit
stats=$(virsh list --all |awk '$2=="'$clone'"{print $3}')
[ -z $stats ] && echo "error: virtual not exits"  && exit
[ "$stats" != "shut" ] && echo "error: virtual $clone not shutdown" && exit
echo "0. link clone
1. full clone"
read -p "choose your option(0/1): " op
read -p "请输入新克隆虚机名称:" KVMname
case "$op" in
0)
    sourceimage=/var/lib/libvirt/images/$clone.qcow2
    sourcexml=/etc/libvirt/qemu/$clone.xml
    newimg=/mnt/lvm1/${KVMname}.qcow2
    newxml=/etc/libvirt/qemu/${KVMname}.xml
    qemu-img create -f qcow2 -b $sourceimage $newimg &> /dev/null
    cp $sourcexml $newxml
    sed -i -e "/uuid/d" -e "/<mac/d" $newxml
    sed -i "/<name/ s/$clone/$KVMname/" $newxml
    sed -i "/<source file/ s#$sourceimage#$newimg#" $newxml
    virsh define $newxml
    virsh list --all
;;
1)
    virt-clone -o $clone -n $KVMname -f /mnt/lvm1/$KVMname.img
    virsh list --all
;;
*)
    echo "error: Please again choose" && HELP && exit
;;
esac

 

 

 

6、批量创建虚机脚本

#!/bin/bash
#KVM batch create vm tool
#version: 0.1
#author: sun
#需要事先准备模板镜像和配置文件模板
echo "1.创建自定义配置单个虚拟机
2.批量创建自定义配置虚拟机
3.批量创建默认配置虚拟机
4.删除虚拟机"
read -p "选取你的操作(1/2/3/4):" op
batch_self_define() {
KVMname=`openssl rand -hex 5`
sourceimage=/var/lib/libvirt/images/centos7.0.qcow2
sourcexml=/etc/libvirt/qemu/kvmmodel.xml
newimg=/mnt/lvm1/${KVMname}.qcow2
newxml=/etc/libvirt/qemu/${KVMname}.xml
cp $sourceimage $newimg
cp $sourcexml $newxml
KVMuuid=`uuidgen`
KVMmem=${1}000000
KVMcpu=$2
KVMimg=$newimg
KVMmac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
sed -i "s@KVMname@$KVMname@;s@KVMuuid@$KVMuuid@;s@KVMmem@$KVMmem@;s@KVMcpu@$KVMcpu@;s@KVMimg@$KVMimg@;s@KVMmac@$KVMmac@" $newxml
virsh define $newxml
virsh list --all
}
self_define() {
read -p "请输入新虚机名称:" newname
read -p "请输入新虚机内存大小(G):" newmem
read -p "请输入新虚机cpu个数:" newcpu
sourceimage=/var/lib/libvirt/images/centos7.0.qcow2
sourcexml=/etc/libvirt/qemu/kvmmodel.xml
newimg=/mnt/lvm1/${newname}.qcow2
newxml=/etc/libvirt/qemu/${newname}.xml
cp $sourceimage $newimg
cp $sourcexml $newxml
KVMname=$newname
KVMuuid=`uuidgen`
KVMmem=${newmem}000000
KVMcpu=$newcpu
KVMimg=$newimg
KVMmac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
sed -i "s@KVMname@$KVMname@;s@KVMuuid@$KVMuuid@;s@KVMmem@$KVMmem@;s@KVMcpu@$KVMcpu@;s@KVMimg@$KVMimg@;s@KVMmac@$KVMmac@" $newxml
virsh define $newxml
virsh list --all
}
case $op in
1)self_define;;
2)
    read -p "请输入新虚机名称:" newname
    read -p "请输入新虚机内存大小(G):" newmem
    read -p "请输入新虚机cpu个数:" newcpu
    for((i=1;i<=$num;i++));do
        batch_self_define $newmem $newcpu
    done
;;
3)
    read -p "请输入要创建的虚拟机的个数:" num
    for((i=1;i<=$num;i++));do
        batch_self_define 1 1
    done
;;
4)
    virsh list --all
    read -p "输入要删除的虚拟机:" del_virtual
    read -p "请确认是要是否要删除$del_virtual yes/no:" pd
    virsh domblklist $del_virtual |egrep -v "^$"|awk 'NR>2{print $2}' > /tmp/$del_virtual
    [ $pd == "yes" ] && virsh undefine $del_virtual
    i=1
    while read line
    do
        rm -rf $line
        let i++
    done < /tmp/$del_virtual
    echo "Domain $del_virtual disk has been rm -rf"
;;
*)
    echo "输入错误,请重新执行脚本"
    exit
;;
esac

 

posted @ 2020-11-03 17:22  梦里花落知多少sl  阅读(252)  评论(0编辑  收藏  举报