思路:
在/opt/kvm下创建一个磁盘文件,做差异镜像,创建一个虚拟机配置文件,做虚拟机模版
[root@node01 ~]# ls /opt/kvm/
vm_base.qcow2 vm_base.xml
创建虚拟机的步骤:首先创建虚拟机的差异镜像,然后复制虚拟机的配置文件
修改虚拟机配置文件:主机名,uuid,网卡mac地址,系统磁盘的地址(差异镜像)
这里将模版文件vm_base.xml里面的主机名和磁盘地址改为vm_base,为了方便后续修改
<name>vm_base</name>
<uuid>4d17c8a1-a3e2-420a-8315-65f2172d93ac</uuid>
<source file='/var/lib/libvirt/images/vm_base.qcow2'/>
<mac address='52:54:00:27:de:00'/>
执行效果如图所示:
完整代码如下:
#!/bin/bash
#查看虚拟机状态
list_vm() {
virsh list --all
}
# 删除所有虚拟机
delete_all_vm () {
for vm_name in $(virsh list --all | awk 'NR>2{print $2}')
do
virsh destroy ${vm_name} &> /dev/null
virsh undefine ${vm_name} &> /dev/null
done
rm -rf /var/lib/libvirt/images/*
}
# 删除指定虚拟机
delete_one_vm () {
virsh list --all
read -p "please input the vm you want to delete:" name
virsh destroy ${name} &> /dev/null
virsh undefine ${name} &> /dev/null
rm -rf /var/lib/libvirt/images/${name}.qcow2
}
#创建单台虚拟机
create_one_vm () {
kvm_config_file=/opt/kvm/vm_base.xml
kvm_disk_file=/opt/kvm/vm_base.qcow2
read -p "please input vm name:" create_name
cp ${kvm_config_file} /etc/libvirt/qemu/${create_name}.xml
qemu-img create -f qcow2 -b ${kvm_disk_file} /var/lib/libvirt/images/${create_name}.qcow2 &> /dev/null
sed -ri "s/vm_base/${create_name}/g" /etc/libvirt/qemu/${create_name}.xml
sed -ri "/uuid/c \ <uuid>$(uuidgen)</uuid>" /etc/libvirt/qemu/${create_name}.xml
vm_mac=52:54:00:$(openssl rand -hex 10 | sed -r 's/(..)(..)(..).*/\1:\2:\3/')
sed -ri "/<mac/c \ <mac address='${vm_mac}'/>" /etc/libvirt/qemu/${create_name}.xml
virsh define /etc/libvirt/qemu/${create_name}.xml &> /dev/null
}
# 批量创建虚拟机
banch_create_vm () {
kvm_config_file=/opt/kvm/vm_base.xml
kvm_disk_file=/opt/kvm/vm_base.qcow2
read -p "please input vm numbers: " number
for i in $(seq $number)
do
vm_name=vm${i}_centos79
cp ${kvm_config_file} /etc/libvirt/qemu/${vm_name}.xml
qemu-img create -f qcow2 -b ${kvm_disk_file} /var/lib/libvirt/images/${vm_name}.qcow2 &> /dev/null
sed -ri "s/vm_base/${vm_name}/g" /etc/libvirt/qemu/${vm_name}.xml
sed -ri "/uuid/c \ <uuid>$(uuidgen)</uuid>" /etc/libvirt/qemu/${vm_name}.xml
vm_mac=52:54:00:$(openssl rand -hex 10 | sed -r 's/(..)(..)(..).*/\1:\2:\3/')
sed -ri "/<mac/c \ <mac address='${vm_mac}'/>" /etc/libvirt/qemu/${vm_name}.xml
virsh define /etc/libvirt/qemu/${vm_name}.xml &> /dev/null
done
}
#扩容虚拟机cpu
expasion_vm_cpu() {
virsh list --all
read -p "please input vm'cpu you want to expasion:" expa_name
read -p "please input cpu number:" cpu_num
if [[ $state == running ]];then
virsh setvcpus ${expa_name} ${cpu_num} --live &> /dev/null
else
virsh start ${expa_name} &> /dev/null
virsh setvcpus ${expa_name} ${cpu_num} --live &> /dev/null
fi
virsh dominfo ${expa_name}
}
#扩容虚拟机内存
expasion_vm_mem() {
virsh list --all
read -p "please input vm'mem you want to expasion:" mem_name
virsh start ${mem_name} &> /dev/null
virsh qemu-monitor-command ${mem_name} --hmp info balloon | awk 'NR==1{print"memory" ,$2}'
maxmem=$(awk -F'[<>]' '/memory/ {print $3}' /etc/libvirt/qemu/${mem_name}.xml)
mem=$((${maxmem}/1024))
echo "${mem_name} max size is $mem"
read -p "please input mem size:" mem_size
virsh qemu-monitor-command ${mem_name} --hmp balloon ${mem_size} &> /dev/null
sleep 2
virsh qemu-monitor-command ${mem_name} --hmp info balloon | awk 'NR==1{print"memory" ,$2}'
}
menu () {
cat << eof
----------kvm虚拟机管理工具---------
* 1.删除所有虚拟机 *
* 2.删除指定虚拟机 *
* 3.批量创建虚拟机 *
* 4.添加单台虚拟机 *
* 5.扩容指定虚拟机CPU *
* 6.扩/缩容指定虚拟机的内存 *
* 7.查看虚拟机状态 *
* 0.退出 *
------------------------------------
eof
}
clear_vm () {
echo "按任意键继续..."
# 使用 read 命令等待用户输入
read -n 1 -s -r -p ""
# 清屏
clear
}
while true;do
menu
read -p "please choise your operate:" choice
case $choice in
1)
delete_all_vm
clear_vm
;;
2)
delete_one_vm
clear_vm
;;
3)
banch_create_vm
clear_vm
;;
4)
create_one_vm
clear_vm
;;
5)
expasion_vm_cpu
clear_vm
;;
6)
expasion_vm_mem
clear_vm
;;
7)
list_vm
clear_vm
;;
0)
exit 88
;;
esac
done