利用shell或者python脚本完成KVM虚拟机的批量创建(上)

个人环境

(1)Centos 7.4(图形化)

答应我,看到了这里,一定把它带走,用的人越多,我越开心…
长话短说,直接上脚本:

#!/bin/bash
#Author:Janrry
#Blog:
#Time:2020-04-27 08:41:19
#Name:auto_install.sh
#Version:V1.0
#Description:The KVM virtual machine is created from a file


iso=/var/lib/libvirt/images/anliu01.img
configure_file=/etc/libvirt/qemu/anliu01.xml
location1=/var/lib/libvirt/images
location2=/etc/libvirt/qemu

while true
do
echo -e "\e[1;31m
1.创建默认配置虚拟机
2.创建自定义虚拟机
3.管理虚拟机
4.虚拟机快照
5.退出
\e[0m"

read -p "请输入你的选择:" choice

case $choice in
    1)
        read -p "请输入你想创建的虚拟机的个数:" count
        i=0
        while [ $i -lt $count ]
        do
            kvm_name=`openssl rand -hex 1`
            kvm_uuid=`uuidgen`
            kvm_port=`arr=($(seq 5900 65535));num=${#arr[*]};res=${arr[$(($RANDOM%num))]};echo $res`
            kvm_mac=`openssl rand -hex 6 | sed -r 's/..\B/&:/g'`
            echo "请稍候..."
                echo "正在复制......."
            cp $iso $location1/${kvm_name}.img
                cp $configure_file $location2/${kvm_name}.xml
            echo "复制已完成"
            sed -ri "s#anliu01#$kvm_name#g" $location2/${kvm_name}.xml
            sed -ri "s#814ad4be-8987-493c-98e8-203258ef1e46#$kvm_uuid#" $location2/${kvm_name}.xml
            sed -ri "s#52:54:00:e7:e1:44#$kvm_mac#" $location2/${kvm_name}.xml
            sed -ri "s#5930#$kvm_port#" $location2/${kvm_name}.xml
            virsh define $location2/${kvm_name}.xml &> /dev/null
            let i++
        done
            sleep 2
            virsh list --all
	    echo "$count 个虚拟机已成功创建"
        exit
        ;;
    2)
        read -p "请输入你想要的虚拟机的名字:" name
        read -p "请输入该虚拟机的cpu个数:" count
        read -p "请输入该虚拟的内存:" mem
        read -p "请输入该虚拟的端口(5900-65535):" port
        uuid=`uuidgen`
        mac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
         echo "请稍侯..."
        echo "正在复制......"
	cp $iso $location1/${name}.img
            cp $configure_file $location2/${name}.xml
        echo "复制已完成"
        sed -ri "s#anliu01#$name#g" $location2/${name}.xml
        sed -ri "s#814ad4be-8987-493c-98e8-203258ef1e46#$uuid#" $location2/${name}.xml
        sed -ri "s#e7:e1:44#$mac#" $location2/${name}.xml
        sed -ri "s#1#$count#g" $location2/${name}.xml
        sed -ri "s#2097152#$mem#g" $location2/${name}.xml
        sed -ri "s#5930#$port#" $location2/${name}.xml
        virsh define $location2/${name}.xml
        sleep 3
        virsh list --all
        echo "$name 已成功创建"
        ;;
    3)
        while true
        do
        echo -e "即将输出你可以操作的虚拟机列表:"
        sleep 3
        virsh list --all
        read -p "请输入你所要管理的虚拟机:" name
        echo -e "\e[1;31m
        1.开启虚拟机
        2.关闭虚拟机
        3.重启虚拟机
        4.删除虚拟机
        5.退出
        \e[0m"
        read -p "输入你的操作:" option
        case $option in
            1)
                virsh start $name
                ;;
            2)
                virsh shutdown $name
                ;;
            3)
                virsh reboot $name
                ;;
            4)
		virsh destroy $name
		virsh undefine $name
		rm -rf $location1/${name}.img
		rm -rf $location2/${name}.xml
                   echo "虚拟机已删除"
                ;;
            5)
                exit
                ;;
            *)
                echo "请输入正确的操作..."
                ;;
        esac
        done
        ;;
    4)
        while true
        do
        echo -e "\e[1;40m 即将输出你可以操作的虚拟机列表: \e[0m"
        sleep 3
        virsh list --all
        read -p "请输入你所要操作的虚拟机:" name
        echo -e "\e[1;31m
        1.创建快照
        2.删除快照
        3.恢复快照
        4.退出
        \e[0m"
        read -p "请输入你对该虚拟机进行的快照操作:" choice
        case $choice in
            1)
                echo -e "\e[1;40m 即将输出你可以操作的虚拟机快照列表: \e[0m"
                sleep 3
                virsh snapshot-list $name
                echo "即将创建快照,请注意快照名字不可以一样噢!"
                virsh snapshot-create-as $name ${name}.snap
		echo "快照已创建,正在为您生成快照列表,请稍候..."
		virsh snapshot-list $name
                ;;
            2)
                virsh snapshot-list $name
                virsh snapshot-delete --snapshotname ${name}.snap $name
                virsh snapshot-list $name
                ;;
            3)
                virsh snapshot-list $name
                virsh snapshot-revert $name ${name}.snap
		echo "快照已成功恢复"
                ;;
            4)
                exit
                ;;
            *)
                echo "请输入正确的操作..."
                ;;
        esac
        done
        ;;
    5)
        exit
        ;;
    *)
        echo "请输入正确的操作..."
esac
done

这个是用shell脚本写的,会用不?不会?开头定义的虚拟机配置文件和镜像换成你的就行,还有他们所在位置换成你自己的就行。(对了,一定是绝对路径)
优化:其实可优化的地方挺多,比如:1.把某些重复的写到一个函数里,直接调用;2.我的脚本只是实现了创建linux虚拟机,那windows的是不是你也可以把配置文件和镜像加上去,在总菜单那里加一个创建Linux和windows的总入口呢?还有很多,期待你的发掘,有兴趣可以试试!
python脚本呢? 我在下一篇等你。

你可能感兴趣的:(企业实战案例,linux,shell)