提交安装服务脚本

master
root 2 years ago
parent c500b7cc7e
commit 429ae52e27
  1. 69
      control/KVMInstall.sh
  2. 2
      control/VMHost.sh
  3. 8
      control/VMTemplate.sh
  4. 4
      template/template.xml

@ -0,0 +1,69 @@
#!/usr/bin/bash
<<!
editor : NewRain
date : 2022-08-15
for kvm service install
!
INTERFACE_NAME="ens33"
color(){
unset c
declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
#echo -e "\033[${c[$1]}m[`date +%T`]($1) $2\033[0m"
printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
sleep 0.5
}
clean(){
color Info "开始进行环境清理"
yum remove `rpm -qa | egrep 'qemu|virt|kvm'` -y
rm -rf /var/lib/libvirt /etc/libvirt/
}
upgrade(){
if [ -f /etc/centos-release ];then
color Success "检测到系统为centos"
level=$(awk '{print $4}' /etc/centos-release | awk -F'.' '{print $2}')
if [ $level -le 7 ];then
color Warning "准备进行内核升级,ctrl^c退出"
yum upgrade
reboot
else
color Success "系统版本符合要求,开始进行kvm安装"
fi
else
color Error "请使用centos系统运行次脚本"
exit 1
fi
}
main(){
color Info "开始安装KVM服务"
yum install -y librbd1-devel qemu-kvm libvirt virt-manager virt-install libguestfs-tools
cat > /etc/sysconfig/network-scripts/ifcfg-br0 <<EOF
TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT="yes"
BOOTPROTO=static
IPADDR=$(ip a show dev ${INTERFACE_NAME} | awk '/\<inet\>/{print $2}' | awk -F'/' '{print $1}')
GATEWAY=$(ip r show dev ${INTERFACE_NAME} | awk '/default/{print $3}')
PREFIX=$(ip a show dev ${INTERFACE_NAME} | awk '/\<inet\>/{print $2}' | awk -F'/' '{print $2}')
DNS1=114.114.114.114
DNS2=8.8.8.8
EOF
cat > /etc/sysconfig/network-scripts/ifcfg-${INTERFACE_NAME} <<EOF
NAME=${INTERFACE_NAME}
DEVICE=${INTERFACE_NAME}
ONBOOT=yes
BRIDGE=br0
EOF
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p
systemctl enable libvirtd --now
color Success "安装并启动完成"
systemctl restart network
}
main

@ -111,7 +111,7 @@ qemu-img create -f qcow2 -b ${TEMPLATE_PATH}/${TEMPLATE_IMAGE_NAME} ${VDISKS_PAT
guestmount -a ${VDISKS_PATH}/${VM_HOSTNAME}.qcow2 -m /dev/centos/root ${MOUNT_PATH} guestmount -a ${VDISKS_PATH}/${VM_HOSTNAME}.qcow2 -m /dev/centos/root ${MOUNT_PATH}
/usr/bin/cp -f ${TEMPLATE_PATH}/${TEMPLATE_NETWORK_NAME} ${MOUNT_PATH}/etc/sysconfig/network-scripts/ifcfg-eth0 /usr/bin/cp -f ${TEMPLATE_PATH}/${TEMPLATE_NETWORK_NAME} ${MOUNT_PATH}/etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri "s/^IPADDR.*/IPADDR=${KVM_ADDRESS}/" ${MOUNT_PATH}/etc/sysconfig/network-scripts/ifcfg-eth0 sed -ri "s/^IPADDR.*/IPADDR=${KVM_ADDRESS}/" ${MOUNT_PATH}/etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri "s/^GATEWAY.*/GATEWAY=${KVM_ADDRESS%.*}.1/" ${MOUNT_PATH}/etc/sysconfig/network-scripts/ifcfg-eth0 sed -ri "s/^GATEWAY.*/GATEWAY=${KVM_ADDRESS%.*}.2/" ${MOUNT_PATH}/etc/sysconfig/network-scripts/ifcfg-eth0
sed -ri "s/.*/${VM_HOSTNAME}/" ${MOUNT_PATH}/etc/hostname sed -ri "s/.*/${VM_HOSTNAME}/" ${MOUNT_PATH}/etc/hostname
guestunmount ${MOUNT_PATH} guestunmount ${MOUNT_PATH}

@ -1,8 +1,10 @@
#!/usr/bin/bash #!/usr/bin/bash
# editor : NewRain <<!
# date : 2020-08-14 editor : NewRain
# for kvm virtual machine template date : 2022-08-14
for kvm virtual machine
!
MAIN_PATH=$(cd $(dirname $0); cd ..; pwd) MAIN_PATH=$(cd $(dirname $0); cd ..; pwd)
IMAGE_PATH=/tmp/CentOS-7-x86_64-Minimal-2009.iso IMAGE_PATH=/tmp/CentOS-7-x86_64-Minimal-2009.iso

@ -73,8 +73,8 @@ or other application using the libvirt API.
<controller type='virtio-serial' index='0'> <controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller> </controller>
<interface type='network'> <interface type='bridge'>
<source network='default'/> <source bridge='br0'/>
<model type='virtio'/> <model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface> </interface>

Loading…
Cancel
Save