You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kvm/control/VMHost.sh

133 lines
4.4 KiB

2 years ago
#!/usr/bin/bash
<<!
editor : NewRain
date : 2022-08-14
for kvm virtual machine
!
# 脚本控制区域
MAIN_PATH=$(cd $(dirname $0); cd ..; pwd)
CONTROL_PATH=$MAIN_PATH/control
MOUNT_PATH=$MAIN_PATH/mount
VDISKS_PATH=$MAIN_PATH/vdisks
TEMPLATE_IMAGE_NAME="template.qcow2"
TEMPLATE_CONFIG_NAME="template.xml"
TEMPLATE_NETWORK_NAME="template.conf"
2 years ago
TEMPLATE_REGISTER_NAME="register_instance.py"
2 years ago
TEMPLATE_PATH=$MAIN_PATH/template
CONFIG_PATH=/etc/libvirt/qemu
2 years ago
KVM_CPUS=1
KVM_MEMORY=512
2 years ago
ERROR_INFO='
"Usage: $0 [OPTION]..."\n
\t -h, --help \t查看帮助"\n
\t -a, --address \t虚拟机的IP地址"\n
\t -p, --port \t虚拟机的VNC端口"\n
\t -n, --name \t虚拟机的名称"\n
\t -c, --cpus \t选填,虚拟机的CPU数量,默认为1"\n
\t -m, --memory \t选填,虚拟机的内存大小,单位M,默认为512M"\n
'
options=$(getopt -l "help,address:,port:,name:,cpus::,memory::" -o "a:p:n:c:m:" -a -- "$@")
if [ $# -lt 1 ];then
echo -e $ERROR_INFO
exit 0
fi
eval set -- "$options"
while true
do
case "$1" in
-h|--help)
echo -e $ERROR_INFO
exit
;;
-a|--address)
shift
ping -c 2 -w 1 $1 &>/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}'`
2 years ago
vmcpus=${1:-"$KVM_CPUS"}
2 years ago
if [ $vmcpus -ge $cpus ]; then
echo "CPU数量不能大于系统的CPU数量,宿主机cpu数量为:$cpus"
exit 1
fi
KVM_CPUS=$vmcpus
shift
;;
-m|--memory)
shift
2 years ago
vmmemory=${1:-"$KVM_MEMORY"}
2 years ago
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
2 years ago
if [ ! -d ${MOUNT_PATH} ];then
mkdir -p ${MOUNT_PATH}
fi
2 years ago
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%.*}.2/" ${MOUNT_PATH}/etc/sysconfig/network-scripts/ifcfg-eth0
2 years ago
sed -ri "s/.*/${VM_HOSTNAME}/" ${MOUNT_PATH}/etc/hostname
2 years ago
cp ${TEMPLATE_PATH}/${TEMPLATE_REGISTER_NAME} ${MOUNT_PATH}/opt && \
echo "python3 /opt/${TEMPLATE_REGISTER_NAME}" >> ${MOUNT_PATH}/etc/rc.d/rc.local && \
chmod a+x ${MOUNT_PATH}/etc/rc.d/rc.local
2 years ago
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}