From cd054f3e4ff692cff13bb24cf348b0d7530e3b42 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 15 Aug 2022 07:44:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/VMHost.sh | 122 +++++++++++++++++++++++++++++++++++++++++ control/VMTemplate.sh | 26 +++++++++ template/template.conf | 13 +++++ template/template.xml | 106 +++++++++++++++++++++++++++++++++++ vdisks/.qcow2 | Bin 0 -> 197120 bytes 5 files changed, 267 insertions(+) create mode 100644 control/VMHost.sh create mode 100644 control/VMTemplate.sh create mode 100644 template/template.conf create mode 100644 template/template.xml create mode 100644 vdisks/.qcow2 diff --git a/control/VMHost.sh b/control/VMHost.sh new file mode 100644 index 0000000..9d93954 --- /dev/null +++ b/control/VMHost.sh @@ -0,0 +1,122 @@ +#!/usr/bin/bash +</dev/null + if [ $? -eq 0 ]; then + echo "IP地址已被使用,请更换" + exit 1 + fi + KVM_ADDRESS=$1 + shift + ;; + -p|--port) + shift + ss -tnlp | grep :$1 &>/dev/null + if [ $? -eq 0 ]; then + echo "端口已被使用,请更换" + exit 1 + fi + KVM_PORT=$1 + shift + ;; + -n|--name) + shift + virsh list --all | awk '{print $2}' | grep -E "^$1$" &>/dev/null + if [ $? -eq 0 ]; then + echo "名称已被使用,请更换" + exit 1 + fi + VM_HOSTNAME=$1 + shift + ;; + -c|--cpus) + shift + cpus=`lscpu | awk '/^CPU\(s\):/{print $2}'` + vmcpus=${1:-"1"} + if [ $vmcpus -ge $cpus ]; then + echo "CPU数量不能大于系统的CPU数量,宿主机cpu数量为:$cpus" + exit 1 + fi + KVM_CPUS=$vmcpus + shift + ;; + -m|--memory) + shift + vmmemory=${1:-"512"} + memory=`free -m | awk '/^Mem:/{print $2}'` + if [ $vmmemory -ge $memory ]; then + echo "内存不能大于系统的内存,宿主机内存为:$memory" + exit 1 + fi + KVM_MEMORY=`echo $vmmemory | grep -o -P '\d+'` + shift + ;; + --) + shift + break + ;; + *) + echo "参数错误!" + exit 1 + ;; + esac +done +qemu-img create -f qcow2 -b ${TEMPLATE_PATH}/${TEMPLATE_IMAGE_NAME} ${VDISKS_PATH}/${VM_HOSTNAME}.qcow2 +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 +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/.*/${VM_HOSTNAME}/" ${MOUNT_PATH}/etc/hostname +guestunmount ${MOUNT_PATH} + +cp ${TEMPLATE_PATH}/${TEMPLATE_CONFIG_NAME} ${CONFIG_PATH}/${VM_HOSTNAME}.xml +sed -ri "s/VM_NAME/${VM_HOSTNAME}/" ${CONFIG_PATH}/${VM_HOSTNAME}.xml +sed -ri "s/VM_VNC_PORT/${KVM_PORT}/" ${CONFIG_PATH}/${VM_HOSTNAME}.xml +sed -ri "s#IMAGE_PATH#${VDISKS_PATH}/${VM_HOSTNAME}.qcow2#" ${CONFIG_PATH}/${VM_HOSTNAME}.xml +sed -ri "s/VM_UUID/`uuidgen`/" ${CONFIG_PATH}/${VM_HOSTNAME}.xml +sed -ri "s/VM_MEMORY/$[${KVM_MEMORY} * 1024 ]/" ${CONFIG_PATH}/${VM_HOSTNAME}.xml +sed -ri "s/VM_CPUS/${KVM_CPUS}/" ${CONFIG_PATH}/${VM_HOSTNAME}.xml +virsh define ${CONFIG_PATH}/${VM_HOSTNAME}.xml +virsh start ${VM_HOSTNAME} diff --git a/control/VMTemplate.sh b/control/VMTemplate.sh new file mode 100644 index 0000000..27a1050 --- /dev/null +++ b/control/VMTemplate.sh @@ -0,0 +1,26 @@ +#!/usr/bin/bash + +# editor : NewRain +# date : 2020-08-14 +# for kvm virtual machine template + +MAIN_PATH=$(cd $(dirname $0); cd ..; pwd) +IMAGE_PATH=/tmp/CentOS-7-x86_64-Minimal-2009.iso +VM_CPUS=1 +VM_TEMPLATE_NAME=template +VM_MEMORY=2050 # 不能低于2G,单位为M +VM_DISK_SIZE=5 # 单位为G +VM_DISK_PATH=${MAIN_PATH}/template/${VM_TEMPLATE_NAME}.qcow2 + +cpus=`lscpu | awk '/^CPU\(s\):/{print $2}'` +if [ $VM_CPUS -ge $cpus ]; then + echo "CPU数量不能大于系统的CPU数量,宿主机cpu数量为:$cpus" + exit 1 +fi +memory=`free -m | awk '/^Mem:/{print $2}'` +if [ $VM_MEMORY -ge $memory ]; then + echo "内存不能大于系统的内存,宿主机内存为:$memory" + 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 diff --git a/template/template.conf b/template/template.conf new file mode 100644 index 0000000..e737b40 --- /dev/null +++ b/template/template.conf @@ -0,0 +1,13 @@ +TYPE="Ethernet" +PROXY_METHOD="none" +BROWSER_ONLY="no" +BOOTPROTO="static" +DEFROUTE="yes" +IPV4_FAILURE_FATAL="no" +DEVICE="eth0" +ONBOOT="yes" +IPADDR= +PREFIX=24 +GATEWAY= +DNS1=114.114.114.114 +DNS2=8.8.8.8 diff --git a/template/template.xml b/template/template.xml new file mode 100644 index 0000000..cad4750 --- /dev/null +++ b/template/template.xml @@ -0,0 +1,106 @@ + + + + VM_NAME + VM_UUID + VM_MEMORY + VM_MEMORY + VM_CPUS + + hvm + + + + + + + + Broadwell-noTSX-IBRS + + + + + + + + + + destroy + restart + destroy + + + + + + /usr/libexec/qemu-kvm + + + + +
+ + + + + +
+ + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + +
+ + + + +
+ + + + + + + + + + + +
+ + + + + + + +
+ + + /dev/urandom +
+ + + diff --git a/vdisks/.qcow2 b/vdisks/.qcow2 new file mode 100644 index 0000000000000000000000000000000000000000..ca52e67826db3e9f051d952f2999d6858f779073 GIT binary patch literal 197120 zcmeIuK~4fO6adgxTo6|tVNT#Ao`98!OINOO7y}WUVJsRvg~v5vV+Ay9AcmdfwdwzD zet(m`yPMlp2%(o(9`jGn#FT^*?sEJzHHi5KE2M$RL|P!Fx9`Ct9jMD zT_5UIBS3%v0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!Cu>2%MeFPke*`0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0tAi~h+#92?bk