diff --git a/control/KVMInstall.sh b/control/KVMInstall.sh index c1d170c..c76e177 100644 --- a/control/KVMInstall.sh +++ b/control/KVMInstall.sh @@ -62,8 +62,9 @@ BRIDGE=br0 EOF echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p systemctl enable libvirtd --now + brctl stp br0 on color Success "安装并启动完成" systemctl restart network } -main \ No newline at end of file +main diff --git a/control/VMHost.sh b/control/VMHost.sh index b9b162a..10c7155 100644 --- a/control/VMHost.sh +++ b/control/VMHost.sh @@ -110,6 +110,11 @@ done if [ ! -d ${MOUNT_PATH} ];then mkdir -p ${MOUNT_PATH} fi +status=$(virsh list | awk '$2 ~ /^template$/{print "yes"}') +if [ "$status" = "yes" ];then + echo "正在关闭模板服务器" +fi + qemu-img create -f qcow2 -b ${TEMPLATE_PATH}/${TEMPLATE_IMAGE_NAME} ${VDISKS_PATH}/${VM_HOSTNAME}.qcow2 rm -rf ${MOUNT_PATH}/* guestmount -a ${VDISKS_PATH}/${VM_HOSTNAME}.qcow2 -m /dev/centos/root ${MOUNT_PATH} diff --git a/control/VMTemplate.sh b/control/VMTemplate.sh index db9010f..5607c21 100644 --- a/control/VMTemplate.sh +++ b/control/VMTemplate.sh @@ -13,6 +13,8 @@ VM_TEMPLATE_NAME=template VM_MEMORY=2050 # 不能低于2G,单位为M VM_DISK_SIZE=5 # 单位为G VM_DISK_PATH=${MAIN_PATH}/template/${VM_TEMPLATE_NAME}.qcow2 +VM_PASSWORD=123456 +VM_JUMPSERVER_HOST=10.36.176.155 cpus=`lscpu | awk '/^CPU\(s\):/{print $2}'` if [ $VM_CPUS -ge $cpus ]; then @@ -25,4 +27,5 @@ if [ $VM_MEMORY -ge $memory ]; then exit 1 fi -virt-install --connect qemu:///system -n ${VM_TEMPLATE_NAME} -r ${VM_MEMORY} --disk path=${VM_DISK_PATH},size=${VM_DISK_SIZE} --os-type=linux --os-variant=centos7.0 --vcpus=${VM_CPUS} --location=${IMAGE_PATH} -x console=ttyS0 --nographics \ No newline at end of file +sed "s/PASSWORD/${VM_PASSWORD}/g;s/JUMPSERVER_HOST/${VM_JUMPSERVER_HOST}/g" ./ks.cfg.temp > ks.cfg +virt-install --connect qemu:///system -n ${VM_TEMPLATE_NAME} -r ${VM_MEMORY} --disk path=${VM_DISK_PATH},size=${VM_DISK_SIZE} --os-type=linux --os-variant=centos7.0 --network bridge=br0 --vcpus=${VM_CPUS} --location=${IMAGE_PATH} -x "console=ttyS0" --nographics --initrd-inject ${MAIN_PATH}/control/ks.cfg --extra-args="ks=file:ks.cfg" diff --git a/control/ks.cfg b/control/ks.cfg new file mode 100644 index 0000000..89c80f2 --- /dev/null +++ b/control/ks.cfg @@ -0,0 +1,59 @@ +#platform=x86, AMD64, or Intel EM64T +#version=DEVEL +# Firewall configuration +network +network --bootproto dhcp +firewall --disabled + +# Root password configuration +rootpw --plaintext 123456 +#url --url=https://mirrors.mit.edu/centos/7.9.2009/os/x86_64/ +# Use graphical install +#graphical + +# Firstboot configuration +firstboot disable + +# System keyboard +keyboard us + +# System language +lang en_US + +# SELinux configuration +selinux disabled + +# Installation logging level +logging level=info + +# System timezone +timezone Asia/Shanghai + +# System bootloader configuration +bootloader location=gpt + +# Partition clearing information +clearpart --all --initlabel + +# Disk partitioning information +part swap --asprimary --fstype="swap" --size=1024 +part /boot --fstype xfs --size=200 +part pv.01 --size=1 --grow +volgroup rootvg01 pv.01 +logvol / --fstype xfs --name=lv01 --vgname=rootvg01 --size=1 --grow + +# Reboot after installation +shutdown +%packages --ignoremissing +@core +%end + +%post +# Add local DNS resolution +rm -rf /etc/yum.repos.d/* +curl https://gitea.beyourself.org.cn/newrain001/shell-project/raw/branch/master/os/kvm-repo.sh |repo=10.35.187.67 sh +yum install -y python3 python3-devel gcc +echo "10.36.176.155 jumpserver.server" >> /etc/hosts +pip3 install requests httpsig -i https://mirrors.aliyun.com/pypi/simple +%end + diff --git a/control/ks.cfg.temp b/control/ks.cfg.temp new file mode 100644 index 0000000..7c28457 --- /dev/null +++ b/control/ks.cfg.temp @@ -0,0 +1,59 @@ +#platform=x86, AMD64, or Intel EM64T +#version=DEVEL +# Firewall configuration +network +network --bootproto dhcp +firewall --disabled + +# Root password configuration +rootpw --plaintext PASSWORD +#url --url=https://mirrors.mit.edu/centos/7.9.2009/os/x86_64/ +# Use graphical install +#graphical + +# Firstboot configuration +firstboot disable + +# System keyboard +keyboard us + +# System language +lang en_US + +# SELinux configuration +selinux disabled + +# Installation logging level +logging level=info + +# System timezone +timezone Asia/Shanghai + +# System bootloader configuration +bootloader location=gpt + +# Partition clearing information +clearpart --all --initlabel + +# Disk partitioning information +part swap --asprimary --fstype="swap" --size=1024 +part /boot --fstype xfs --size=200 +part pv.01 --size=1 --grow +volgroup rootvg01 pv.01 +logvol / --fstype xfs --name=lv01 --vgname=rootvg01 --size=1 --grow + +# Reboot after installation +shutdown +%packages --ignoremissing +@core +%end + +%post +# Add local DNS resolution +rm -rf /etc/yum.repos.d/* +curl https://gitea.beyourself.org.cn/newrain001/shell-project/raw/branch/master/os/kvm-repo.sh |repo=10.35.187.67 sh +yum install -y python3 python3-devel gcc +echo "JUMPSERVER_HOST jumpserver.server" >> /etc/hosts +pip3 install requests httpsig -i https://mirrors.aliyun.com/pypi/simple +%end + diff --git a/template/template.xml b/template/template.xml index d5b65ae..81c9df4 100644 --- a/template/template.xml +++ b/template/template.xml @@ -1,5 +1,5 @@