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

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}')
	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


http://www.kler.cn/a/454142.html

相关文章:

  • spring cloud gateway 3
  • Vue中动态样式绑定+CSS变量实现切换明暗主题功能——从入门到进阶
  • Markdown语法字体字号讲解
  • Mac 查询IP配置,网络代理
  • 【漫话机器学习系列】022.微积分中的链式求导法则(chain rule of Calculus)
  • 企业销售人员培训系统|Java|SSM|VUE| 前后端分离
  • 【杂谈】-AI搜索引擎如何改变传统SEO及其在内容营销中的作用
  • Max AI prompt2:
  • 【网络云计算】2024第52周-每日【2024/12/23】小测-理论实操-解析
  • 项目亮点案例
  • 了解智能运维
  • Git和SVN有什么区别?
  • 微信V3支付报错 平台证书及平台证书序列号
  • iviewui Message/Notice 设置居顶高度方法
  • Linux中设置终端窗口大小
  • Pytorch | 利用SMI-FGRM针对CIFAR10上的ResNet分类器进行对抗攻击
  • Java小公司面试
  • 关于studywolf_control动态运动原语
  • redis 缓存雪崩
  • 重装荣耀X14笔记本电脑踩坑记
  • Linux 线程池
  • RocketMQ的集群架构是怎样的?
  • 深度学习中的并行策略概述:4 Tensor Parallelism
  • 【ES6复习笔记】模板字符串(3)
  • 运行Zr.Admin项目(前端)
  • C++软件设计模式之类型模式和对象型模式