#!/bin/bash
#Time:2020-04-27 08:41:19
#Name:auto_install.sh
#Version:V1.0
#Description:The KVM virtual machine is created from a file
#菜单栏目
menu() {
echo -e "e[1;32m
+---------------------------------------------------------+
| ================================ |
| kvm 虚拟机管理 |
| ================================ |
| 1. 批量安装虚拟机 |
| 2. 自定义安装虚拟机 |
| 3. 修改虚拟机的ip |
| 5. 退出管理程序 |
| |
+---------------------------------------------------------+
e[0m"
}
iso=/var/lib/libvirt/images/centos7-5.img #使用已有虚拟机的镜像文件
configure_file=/etc/libvirt/qemu/centos7.5.xml #使用已有虚拟机的xml文件
image_home=/var/lib/libvirt/images #创建虚拟机之后镜像的目录
xml_home=/etc/libvirt/qemu #创建虚拟机之后xml文件目录
#定义一个函数用来批量创建虚拟机
deployment_create_vm() {
read -p "请输入你要创建虚拟机的个数: " NUM
for i in `seq 1 $NUM`
do
kvm_num=`virsh list --all | grep vm | wc -l`
kvm_name=vm`expr $kvm_num + 1`
uuid=`uuidgen`
kvm_mac="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed -r 's/^(..)(..)(..)(..).*$/1:2:3:4/')"
kvm_port=`expr $i + 9000`
echo "请稍侯..."
echo "正在复制......"
cp $iso $image_home/$kvm_name.img
cp $configure_file $xml_home/$kvm_name.xml
echo "复制已完成"
sed -ri "s#centos7.5#$kvm_name#g" $xml_home/$kvm_name.xml
sed -ri "s#f6432f66-d5da-4e82-adbb-05cebaacf77c#$uuid#" $xml_home/$kvm_name.xml
sed -ri "s#52:54:00:9b:91:56#$kvm_mac#" $xml_home/$kvm_name.xml
#sed -ri "s#1#$cpu_num#g" $xml_home/${name}.xml
#sed -ri "s#524288#$mem#g" $xml_home/$kvm_name.xml
sed -ri "s#5960#$kvm_port#" $xml_home/$kvm_name.xml
virsh define $xml_home/$kvm_name.xml
sleep 6
virsh list --all
echo "$kvm_name 已成功创建"
get_vm_name=$kvm_name
get_mac=`cat /etc/libvirt/qemu/${get_vm_name}.xml | grep 52:54 | awk -F"'" '{ print $2 }'`
#获取虚拟机mac对应的ip地址
get_vm_ip=`arp -a | grep $get_mac | awk -F"(" '{print $2}' | awk -F")" '{print $1}'`
#注意请提前检查/root/vm_ip/目录,没有的话自己建立
`cat > /root/vm_ip/ifcfg-eth0 <<EOF
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=${get_vm_ip}
NETMASK=255.255.255.0
GATEWAY=192.168.21.2
EOF`
virt-copy-in /root/vm_ip/ifcfg-eth0 -a $image_home/${get_vm_name}.img /etc/sysconfig/network-scripts/
virsh start $get_vm_name
echo -e "e[1;32m 该虚拟机的ip为:${get_vm_ip} e[0m"
done
}
define_create_vm() {
while true
do
read -p "请输入你想要的虚拟机的名字:" name
n=`virsh list --all | grep $name | wc -l`
if [ $n -eq 0 ];then
break
fi
done
#read -p "请输入该虚拟机的cpu个数:" cpu_num
read -p "请输入该虚拟的内存(524288-1048576):" mem
read -p "请输入该虚拟的端口(5970-65535):" port
read -p "请输入你虚拟机的ip地址:" vm_ip
uuid=`uuidgen`
kvm_mac="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed -r 's/^(..)(..)(..)(..).*$/1:2:3:4/')"
echo "请稍侯..."
echo "正在复制......"
cp $iso $image_home/${name}.img
cp $configure_file $xml_home/${name}.xml
echo "复制已完成"
sed -ri "s#centos7.5#$name#g" $xml_home/${name}.xml
sed -ri "s#f6432f66-d5da-4e82-adbb-05cebaacf77c#$uuid#" $xml_home/${name}.xml
sed -ri "s#52:54:00:9b:91:56#$kvm_mac#" $xml_home/${name}.xml
#sed -ri "s#1#$cpu_num#g" $xml_home/${name}.xml
sed -ri "s#524288#$mem#g" $xml_home/${name}.xml
sed -ri "s#5960#$port#" $xml_home/${name}.xml
virsh define $xml_home/${name}.xml
sleep 6
virsh list --all
echo "$name 已成功创建"
#注意请提前检查/root/vm_ip/目录,没有的话自己建立
`cat > /root/vm_ip/ifcfg-eth0 <<EOF
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=$vm_ip
NETMASK=255.255.255.0
GATEWAY=192.168.21.2
EOF`
get_vm_name=$name
virt-copy-in /root/vm_ip/ifcfg-eth0 -a $image_home/${get_vm_name}.img /etc/sysconfig/network-scripts/
virsh start $get_vm_name
echo -e "e[1;32m 该虚拟机的ip为:${get_vm_ip} e[0m"
}
#定义函数用来给虚拟机自动配置ip,想要该脚本能够成功执行的前提是:你自己制作的模板镜像必须执行下面操作
#echo "dhclient" >> /etc/rc.d/rc.local
#chmod +x /etc/rc.d/rc.local
#为了方便更加容易管理kvm虚拟机建议在镜像模板中开启console
#sed -i 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap console=tty0 rhgb quiet"/g' /etc/sysconfig/grub
#grub2-mkconfig -o /boot/grub2/grub.cfg
#systemctl start getty@ttyS0 && systemctl enable getty@ttyS0
change_ip() {
#获取配置虚拟机的mac地址
read -p "请输入你要修改虚拟机的名称: " kvm_name
read -p "请输入你要修改的ip地址: " kvm_ip
#get_mac=`cat /etc/libvirt/qemu/${get_vm_name}.xml | grep 52:54 | awk -F"'" '{ print $2 }'`
#获取虚拟机mac对应的ip地址
#get_vm_ip=`arp -a | grep $get_mac | awk -F"(" '{print $2}' | awk -F")" '{print $1}'`
#注意请提前检查/root/vm_ip/目录,没有的话自己建立
`cat > /root/vm_ip/ifcfg-eth0 << EOF
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=${kvm_ip}
NETMASK=255.255.255.0
GATEWAY=192.168.21.2
EOF`
#注意修改完之后会重启虚拟机
virsh shutdown $kvm_name
sleep 10
virt-copy-in -d $kvm_name /root/vm_ip/ifcfg-eth0 /etc/sysconfig/network-scripts/
virsh start $kvm_name
ping -c 30 $kvm_ip
if [ $? -eq 0 ];then
echo -e "e[1;32m 该虚拟机ip已经成功修改 e[0m"
else
echo -e "e[1;32m 请联系管理查看,或则手动连接虚拟机修改 e[0m"
fi
}
#define_create_vm
#deployment_create_vm
while true
do
menu
read -p "请输入你的选择: " choice
case $choice in
1)
deployment_create_vm
;;
2)
define_create_vm
;;
3)
change_ip
;;
5)
exit
;;
esac
done