1.xxx
#!/bin/bash
CPU_MODE=$(virsh capabilities | grep "<model>.*</model>" | awk -F[\<\>] '{print $3}' | head -1)
IMAGE_DIR=/var/lib/libvirt/images
XML_DIR=/etc/libvirt/qemu
function CreateVM() {
local VM_SIZE=20
local VM_NAME=$1
local QCOW2_IMG=stream9.qcow2
rm -rf ${IMAGE_DIR}/${VM_NAME}.img
qemu-img create -f qcow2 -F qcow2 -b ${IMAGE_DIR}/${QCOW2_IMG} ${IMAGE_DIR}/${VM_NAME}.img ${VM_SIZE}G
# /usr/libexec/qemu-kvm -machine help
cat >$XML_DIR/${VM_NAME}.xml <<EOF
<domain type='kvm'>
<name>${VM_NAME}</name>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>${CPU_MODE}</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/${VM_NAME}.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='bridge'>
<source bridge='vbr1'/>
<model type='virtio'/>
</interface>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='5800' autoport='yes' listen='0.0.0.0' keymap='en-us'>
<listen type='address' address='0.0.0.0'/>
</graphics>
</devices>
</domain>
EOF
virsh define $XML_DIR/${VM_NAME}.xml
sleep 1
virsh start ${VM_NAME}
}
function DelStoppedVM() {
for vm in $(virsh list --all | awk '/shut off/{print $2}'); do
virsh undefine $vm
rm -rf $IMAGE_DIR/${vm}.img
rm -rf $XML_DIR/${vm}.xml
done
}
function DelAllVM() {
for vm in $(virsh list --all | awk '{print $2}' | egrep -v "^Name|^$"); do
virsh destroy $vm
virsh undefine $vm
rm -rf $IMAGE_DIR/${vm}.img
rm -rf $XML_DIR/${vm}.xml
done
}
function main() {
local AIM=
local VM_NUM=0
local VM_NAME=
local DEFAULT_VM=()
if [[ "$@" == "" ]]; then
while true; do
printf "%s\n%s\n%s\n" "1.Create VM" "2.Release the stopped VM" "3.Release all VM"
read -p "What do you want to do: " AIM
[[ $AIM -ge 1 && $AIM -le 3 ]] && break
echo "The number you entered must be within 1~3!"
done
if [ $AIM -eq 1 ]; then
while true; do
read -p "How many VMs do you want: " VM_NUM
if [ $VM_NUM -gt 0 ]; then
break
else
echo "The number you entered must be greater than 0!"
fi
done
for i in $(seq $VM_NUM); do
read -p "Name:" VM_NAME
CreateVM $VM_NAME
done
elif [ $AIM -eq 2 ]; then
DelStoppedVM
elif [ $AIM -eq 3 ]; then
DelAllVM
fi
else
for VM_NAME in $@; do
CreateVM $VM_NAME
done
fi
# /var/lib/libvirt/dnsmasq
virsh list --all
}
main $@
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律