#!/bin/bash
IFS_OLD=$IFS
IFS=$'\n'
init()
{
  ls_temp=`lsmod | grep kvm`
  if [[ "${ls_temp}" != "" ]];then
     echo "install software:"
     yum install qemu-kvm qemu-kvm-tools qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools -y
     #yum -y install qemu* libvirt* virt-install virt-manager
     echo "createbridge"
     createbridge
     #passthruvga
  else
   echo "error:no kvm"
   exit
  fi
}
passthruvga()
{
  echo "passthrough the nvidia card"
  ls_temp=`lspci -Dnn| grep -E "NVIDIA|d500"`
  echo ${ls_temp}
  ls_ids=""
  if [[ "${ls_temp}" != "" ]];then
    for ls_line in ${ls_temp}
    do
      ls_id=`echo ${ls_line} | awk -F "[" '{print $NF}' | awk -F "]" '{print $1}'`
      if [[ ${ls_ids} != "" ]];then
         ls_ids=${ls_ids}",${ls_id}"
      else
         ls_ids=${ls_id}
      fi
    done
    grubby --args="pci-stub.ids=${ls_ids}" --update-kernel DEFAULT
    grubby --args="intel_iommu=on iommu_pt" --update-kernel DEFAULT
    echo "options vfio-pci ids=${ls_ids}" >/etc/modprobe.d/vfio.conf
    echo "vfio-pci" > /etc/modules-load.d/vfio-pci.conf
  else
    echo "error:no nvidia card"
  fi
  
}
createbridge()
{
  read -p "please input the bridge-infterace:" ls_brinterface
  read -p "please input the bridge parameter(ip/mask dns gateway):" ls_brparameter
  ls_brip=`echo ${ls_brparameter} | awk '{print $1}'`
  ls_brdns=`echo ${ls_brparameter} | awk '{print $2}'`
  ls_brgateway=`echo ${ls_brparameter} | awk '{print $3}'`
  nmcli con add con-name br0-vm ifname br0-vm type bridge
  nmcli con add con-name ${ls_brinterface}-vm ifname ${ls_brinterface} type ethernet slave-type bridge master br0-vm autoconnect yes
  nmcli con modify br0-vm ipv4.address ${ls_brip} ipv4.dns ${ls_brdns} ipv4.gateway ${ls_brgateway} ipv4.method manual ipv6.method ignore autoconnect yes
  #nmcli con modify ${ls_brinterface} autoconnect no
  nmcli con delete ${ls_brinterface}
  read -p  "reboot the machine.please use the bridge ip login:" ls_temp
  reboot
}
createkvm()
{
  echo "createkvm
1.centos7
2.qilinv10
3.win10
4.qilintongxin"
  read -p "please input the operation system(num name capacity vncport):" ls_kvm
  ls_num=`echo ${ls_kvm} | awk '{print $1}'` 
  ls_kvmname=`echo ${ls_kvm} | awk '{print $2}'`
  ls_kvmcapacity=`echo ${ls_kvm} | awk '{print $3}'`
  ls_kvmvncport=`echo ${ls_kvm} | awk '{print $NF}'`
  ls_kvmimagepath="${ls_shellpath}/kvmimages/${ls_kvmname}.qcow2"
  ls_kvmcento7image="${ls_shellpath}/mirror/CentOS-7-x86_64-DVD-1810.iso"
  ls_kvmqilinv10image="${ls_shellpath}/mirror/Kylin-Server-V10-SP3-2403-Release-20240426-x86_64.iso"
  ls_kvmwin10image="${ls_shellpath}/mirror/cn_windows_10_business_editions_version_1909_x64_dvd_0ca83907.iso"

  export ls_kvmname=${ls_kvmname}
  export ls_kvmimagepath=${ls_kvmimagepath}
  export ls_kvmcento7image=${ls_kvmcento7image}
  export ls_kvmqilinv10image=${ls_kvmqilinv10image}
  export ls_kvmwin10image=${ls_kvmwin10image}
  export ls_kvmvncport=${ls_kvmvncport}
  
  if [ -d ${ls_shellpath}/kvmimages ];then
    ########### create qcow2 #################
    echo "create ${ls_kvmname}.qcow2"
    qemu-img create -f qcow2 ${ls_shellpath}/kvmimages/${ls_kvmname}.qcow2 ${ls_kvmcapacity}G
    
    if [[ "${ls_num}" -eq 1 ]];then
       ############## edit yaml and run #################
       envsubst '${ls_kvmname},${ls_kvmimagepath},${ls_kvmcento7image}'< ${ls_shellpath}/mirror/centos7.sh  >${ls_shellpath}/mirror/${ls_kvmname}.sh
       #vi ${ls_shellpath}/mirror/${ls_kvmname}.sh
       #/bin/sh ${ls_shellpath}/mirror/${ls_kvmname}.sh    
    elif [[ "${ls_num}"  -eq 2 ]];then
       envsubst '${ls_kvmname},${ls_kvmimagepath},${ls_kvmqilinv10image},${ls_kvmvncport}'< ${ls_shellpath}/mirror/qilinv10.sh  >${ls_shellpath}/mirror/${ls_kvmname}.sh
       #vi ${ls_shellpath}/mirror/${ls_kvmname}.sh
       #/bin/sh ${ls_shellpath}/mirror/${ls_kvmname}.sh
       
    elif [[ "${ls_num}" -eq 3 ]];then
       envsubst '${ls_kvmname},${ls_kvmimagepath},${ls_kvmwin10image}'< ${ls_shellpath}/mirror/win10.sh  >${ls_shellpath}/mirror/${ls_kvmname}.sh
       #vi ${ls_shellpath}/mirror/${ls_kvmname}.sh
       #/bin/sh ${ls_shellpath}/mirror/${ls_kvmname}.sh
    fi
    vi ${ls_shellpath}/mirror/${ls_kvmname}.sh
    /bin/sh ${ls_shellpath}/mirror/${ls_kvmname}.sh
    virsh start ${ls_kvmname}
  else
    echo "error:${ls_shellpath}/kvmimages not exists"
    exit
  fi
   
}
chooseitem()
{
  echo "1.init:install software;install bridge
2.passthroughvga
3.createkvm"
  read -p "please input the item: " item
  case ${item} in
  1)
    init
  ;;
  2)
    passthruvga
  ;;
  3)
    createkvm  
  ;;
  *)
    echo "error:no fun"
  ;;
  esac
}
ls_shellpath=$(cd $(dirname $0);pwd)
chooseitem
IFS=${IFS_OLD}
