#!/bin/bash
# shell 自动创建 qcow2 虚拟机,注意虚拟机镜像制作的时候需要自动获取ip
# virt-install --osinfo list | grep win | awk -F"," '{print $1}' # 查看ubuntu镜像系统支持的版本
eiscRunDir=/datadisk/vm/eiscAUTO
debianSourceQcow=/datadisk/myfolder/iso/public/qcow2/linux/debian12-username-eisc-000000.qcow2
#debianSourceQcow=/datadisk/myfolder/iso/qcow2/linux/debian12-username-eisc-000000.qcow2
#debianSourceQcow=/datadisk/myfolder/iso/public/qcow2/linux/debian12-manualConfig-homeip60-80GB-username-eisc-000000.qcow2
ubuntuSourceQcow=/datadisk/myfolder/iso/public/qcow2/linux/ubuntu22-username-eisc-000000.qcow2
sudo mkdir -p $eiscRunDir
sudo chmod 777 $eiscRunDir
echo "
######### kvm 虚拟机自动创建脚本 #########
1. 当前提供 debian12 和 ubuntu-lts-latest, ubuntu22.04 系统。 选择 1 或 2
2. 配置 cpu 和 内存,单位GB, 内存允许输入小数 1 线程 2.5G 为:空格隔开 1 2.5
3. 案例 debian12系统镜像,cpu为1线程,2.5G内存: 1 1 2.5
"
systemMessage(){
cputype=$(grep "model name" /proc/cpuinfo | uniq | awk -F ': ' '{print $2}')
cores=$(cat /proc/cpuinfo | grep "cores" | uniq | awk '{print $NF}')
processor=$(cat /proc/cpuinfo | grep "processor" | wc -l)
# 获取 CPU 核心数和处理器线程数
totalMemFree=$(free -h | awk 'NR==2{print $2}' | sed "s/[A-Za-z]//g" )
totalDiskFree=$(free -h | awk 'NR==3{print $2}' | sed "s/[A-Za-z]//g")
memFree=$(free -h | awk 'NR==2{print $NF}' | sed "s/[A-Za-z]//g" )
diskFree=$(free -h | awk 'NR==3{print $NF}' | sed "s/[A-Za-z]//g")
# 获取物理内存和虚拟内存的空闲量(单位:GB)
totalFree=$(echo "$memFree + $diskFree" | bc)
# 计算小数
echo "服务器CPU配置信息 $cputype 核心数:$cores; 线程数:$processor"
echo "物理内存单位(GB):$totalMemFree 空闲:$memFree; 虚拟内存:$totalDiskFree 空闲:$diskFree; 总内存空闲: $totalFree GB"
}
inputData()
{
read -p "请输入:" inputBuff ; buff=( $inputBuff )
result=$(echo "${buff[2]} < $totalFree" | bc)
if [ "${#buff[*]}" = "3" ] && [ ${
buff[1]} -le $processor ] && [ $result -eq 1 ]; then
echo "你输入的信息为: ${buff[*]}"
else
echo "输入错误, 原因: 1.输入参数超过3个 2.cpu 线程数超标 3.设置的内存超出空闲内存; 请重新输入"
exit
fi
case ${
buff[0]} in
"1") SystemVersion=debian12 ;
sourceQcow=$debianSourceQcow ;;
"2") SystemVersion=ubuntu22.04 ;
sourceQcow=$ubuntuSourceQcow ;;
*) echo "未设置源镜像,当前选择镜像序号: ${buff[0]}" ; exit ;;
esac
# case 每一句命令分割 如果是换行,必须在上一行加分号 ;
}
createImage(){
inputData
# 固定参数
sourceQcow=$sourceQcow
ImgexistFlag=0
SystemVersion=$SystemVersion
cpu=${
buff[1]}
free=$(echo "${buff[2]} * 1000" | bc | awk -F"." '{print $1}')
# 取整数
# 动态参数
for (( i=0;i<9999;i++ ))
do
findQcow=$(ls $eiscRunDir | grep "debian12-$i")
SystemIDName=debian12-$i
qcow2=$eiscRunDir/debian12-$i.qcow2
if [ "${#findQcow}" -lt "10" ]; then
sudo cp $sourceQcow $qcow2
echo "正在创建: $SystemIDName $qcow2"
break
else
echo "当前虚拟机存在:$SystemIDName $qcow2 正在继续创建中..."
fi
done
listimage=`virsh list --all | grep $SystemIDName`
if [ "${#listimage}" -lt "7" ]
then
echo "正在创建虚拟机: $SystemVersion 配置信息 cpu: ${buff[1]} 内存: ${buff[2]} "
ImgexistFlag=0
else
echo "虚拟机存在:$listimage "
ImgexistFlag=1
fi
if [ "$ImgexistFlag" = "0" ]
then
virt-install --import --name $SystemIDName --ram $free --vcpus=$cpu --cpu host-model --disk path=$qcow2,bus=virtio,format=qcow2 \
--network network=default,model=virtio --os-type=linux --video qxl,vgamem=16384,heads=1 --os-variant=$SystemVersion --noautoconsole
echo "等待6秒启动" ; sleep 6
fi
imageName=$SystemIDName
catImageIP
}
delectImage()
{
echo "";echo "";
echo "please back image , dir=$eiscRunDir"
read -p "Please input the name of the image to be deleted: " imageName
virsh destroy $imageName # 强制 关闭 镜像
virsh undefine $imageName # 删除虚拟机
sudo rm -rf $eiscRunDir/$imageName.qcow2 # 删除镜像文件
}
startImage()
{
read -p "Please input the name of the image to be start: " imageName
virsh start $imageName
}
stopImage()
{
read -p "Please input the name of the image to be shutdown: " imageName
virsh shutdown $imageName ; sleep 10
virsh destroy $imageName
}
catImageIP()
{
sudo netplan apply
virsh list --all
if [ "${#imageName}" -lt "6" ]
then
read -p "Please input the name of the image to be cat IP: " imageName
fi
mac=`virsh domiflist $imageName | grep -v MAC | awk -F " " '{print $5}'` ;
ip=`arp -n | grep $mac` # 通过max 地址查看 ip 地址
echo "host $imageName mac is: $mac ip is : $ip"
# 查看所有虚拟机,和创建虚拟的 ip地址
}
catMagess(){
imageList=(` virsh list --all | grep -vE "Id|----" | awk -F" " '{print $2}'`)
echo "当前所有虚拟机为: ${imageList[*]} "
}
main(){
systemMessage
catMagess
read -p "提供功能: 1.创建虚拟机 2.删除虚拟机 3.启动虚拟机 4.关闭虚拟机 5.查看虚拟机ip; please input number: " FunctionSelect
case $FunctionSelect in
"1") echo "create Image: " ; createImage ;;
"2") echo "delect Image: " ; delectImage ;;
"3") echo "start Image: " ; startImage ;;
"4") echo "stop Image: " ; stopImage ;;
"5") echo "cat ip Image: " ; catImageIP ;;
*) echo "There are no defined functions.";;
esac
}
main
# 一键下载使用: wget ddoss.cn/file/ubuntu/shell/server/kvmCreateQcow.sh ; chmod +x kvmCreateQcow.sh ; ./kvmCreateQcow.sh