当前位置: 首页 > news >正文

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


http://www.mrgr.cn/news/81637.html

相关文章:

  • 突围边缘:OpenAI开源实时嵌入式API,AI触角延伸至微观世界
  • 论文解读 | EMNLP2024 一种用于大语言模型版本更新的学习率路径切换训练范式
  • 【FAQ】HarmonyOS SDK 闭源开放能力 — Vision Kit(2)
  • YOLOv9-0.1部分代码阅读笔记-anchor_generator.py
  • lua和C API库一些记录
  • 深度解析 Pytest 中的 conftest.py
  • 攻防世界web第三题file_include
  • VSCode调试
  • Oracle 数据库执行计划的查看与分析技巧
  • webauthn介绍及应用
  • 实用工具推荐----Doxygen使用方法
  • Dockerfile教程
  • redis基础知识
  • Git如何设置和修改当前分支跟踪的上游分支
  • 关于DataGridView的使用注意事项
  • 【漏洞复现】BIG-IP Next Central Manager OData 注入漏洞(CVE-2024-21793)
  • uniapp 文本转语音
  • 机器学习之KNN算法预测数据和数据可视化
  • 爆改RagFlow
  • WPF 绘制过顶点的圆滑曲线(样条,贝塞尔)
  • pg数据库主备库切换
  • Vue.use()和Vue.component()
  • 文件路径与Resource接口详解
  • 算法题(16):原地移除元素
  • 学习笔记 --C#基础其他知识点(持续更新)
  • 【Lua之·Lua与C/C++交互·Lua CAPI访问栈操作】