KVM虚拟机管理脚本
思路:
在/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}')dovirsh destroy ${vm_name} &> /dev/nullvirsh undefine ${vm_name} &> /dev/nulldonerm -rf /var/lib/libvirt/images/*
}# 删除指定虚拟机
delete_one_vm () {virsh list --allread -p "please input the vm you want to delete:" namevirsh destroy ${name} &> /dev/nullvirsh undefine ${name} &> /dev/nullrm -rf /var/lib/libvirt/images/${name}.qcow2
}#创建单台虚拟机
create_one_vm () {kvm_config_file=/opt/kvm/vm_base.xmlkvm_disk_file=/opt/kvm/vm_base.qcow2read -p "please input vm name:" create_namecp ${kvm_config_file} /etc/libvirt/qemu/${create_name}.xmlqemu-img create -f qcow2 -b ${kvm_disk_file} /var/lib/libvirt/images/${create_name}.qcow2 &> /dev/nullsed -ri "s/vm_base/${create_name}/g" /etc/libvirt/qemu/${create_name}.xmlsed -ri "/uuid/c \ <uuid>$(uuidgen)</uuid>" /etc/libvirt/qemu/${create_name}.xmlvm_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}.xmlvirsh define /etc/libvirt/qemu/${create_name}.xml &> /dev/null}# 批量创建虚拟机
banch_create_vm () {kvm_config_file=/opt/kvm/vm_base.xmlkvm_disk_file=/opt/kvm/vm_base.qcow2read -p "please input vm numbers: " numberfor i in $(seq $number)dovm_name=vm${i}_centos79cp ${kvm_config_file} /etc/libvirt/qemu/${vm_name}.xmlqemu-img create -f qcow2 -b ${kvm_disk_file} /var/lib/libvirt/images/${vm_name}.qcow2 &> /dev/nullsed -ri "s/vm_base/${vm_name}/g" /etc/libvirt/qemu/${vm_name}.xmlsed -ri "/uuid/c \ <uuid>$(uuidgen)</uuid>" /etc/libvirt/qemu/${vm_name}.xmlvm_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}.xmlvirsh define /etc/libvirt/qemu/${vm_name}.xml &> /dev/nulldone
}#扩容虚拟机cpu
expasion_vm_cpu() {virsh list --allread -p "please input vm'cpu you want to expasion:" expa_nameread -p "please input cpu number:" cpu_numif [[ $state == running ]];thenvirsh setvcpus ${expa_name} ${cpu_num} --live &> /dev/null elsevirsh start ${expa_name} &> /dev/nullvirsh setvcpus ${expa_name} ${cpu_num} --live &> /dev/null fivirsh dominfo ${expa_name}}#扩容虚拟机内存
expasion_vm_mem() {virsh list --allread -p "please input vm'mem you want to expasion:" mem_namevirsh start ${mem_name} &> /dev/nullvirsh 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_sizevirsh qemu-monitor-command ${mem_name} --hmp balloon ${mem_size} &> /dev/nullsleep 2virsh 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;domenuread -p "please choise your operate:" choicecase $choice in1)delete_all_vmclear_vm;;2)delete_one_vmclear_vm;;3)banch_create_vm clear_vm;;4)create_one_vmclear_vm;;5)expasion_vm_cpuclear_vm;;6)expasion_vm_memclear_vm;;7)list_vmclear_vm;;0)exit 88;;esacdone