其他分享
首页 > 其他分享> > KVM 一键批量创建虚拟机

KVM 一键批量创建虚拟机

作者:互联网

目录

一、原理

  1. 需要先创建基础镜像,后续的镜像都是基于该镜像创建的差量镜像。基础镜像不允许修改(需要注意)。
  2. 基础镜像需要设置要一些基本内容。
  3. 通过 virt-edit 实现的设置ip.
  4. ip 信息统一用 <title> 来进行标记。 使用virsh list --title进行查看 机器和IP

完成的功能:
输入机器名称和ip,就能创建我们想要的虚拟机了。ip会自动帮我们设置好。

解决的问题:

  1. 之前创建机器后需要手动去设置IP. virt-edit 进行编辑网络文件。
  2. 创建完机器,我们不知道机器对应的哪个IP. 使用 <title> 来进行标记。 使用virsh list --title进行查看 机器和IP对应关系

二、基础镜像

2.1、创建基础镜像

virt-install --name base-image --virt-type kvm --memory 8096 --vcpus 2 --cdrom=/home/kvm/iso/CentOS-7-x86_64-Minimal-1908.iso --disk /home/kvm/data/base-image.qcow2,size=50,format=qcow2  --graphics vnc,listen=0.0.0.0  --network network=net-br --autostart  

2.2、 完善基础镜像

  1. 增加 console 登陆

    grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
    
  2. vnc 端口设置随机

  3. 使用桥接网络

  4. 设置网络配置

    BOOTPROTO=static  
    ONBOOT=yes
    IPADDR=192.168.1.12
    NETMASK=255.255.255.0
    GATEWAY=192.168.1.1
    DNS1=192.168.1.1
    # 注释uuid
    

2.3、基础镜像设置权限

由于使用的是差量镜像的模式,所以基础镜像不允许修改。

chattr +i   /opt/kvm/data/base-image.qcow2

3.4 设置 title

编辑基础镜像的 xml 文件,增加 title。

 <title>192.168.1.156</title>

3.5、基础镜像XML

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit base-image
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>base-image</name>
  <uuid>12348899-e829-4217-a0ff-f30eee4d2b31</uuid>
  <title>192.168.1.199</title>
  <memory unit='KiB'>16777216</memory>
  <currentMemory unit='KiB'>8388608</currentMemory>
  <vcpu placement='static' current='2'>4</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Broadwell-IBRS</model>
    <feature policy='require' name='spec-ctrl'/>
  </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='/opt/kvm/data/base-image.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:28:99:22'/>
      <source network='net-br'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </rng>
  </devices>
</domain>

三、批量创建机器脚本

因为需要使用 virt-edit,所以需要安装libguestfs-tools
yum install -y libguestfs-tools

#!/bin/bash
#######################################################
# $Name:        create_kvm_machine.sh
# $Version:     v1.0
# $Function:    create kvm machine
# $Author:      dongjiaxiao
# $Create Date: 2020-04-12
# $Description: shell
######################################################
#set -x
# 检查基础镜像和xml是否存在并生成uuid,mac 信息
function check(){
	if [ ! -f "${xmls_dir}${base_xml}" ]&&[ ! -f "${images_dir}${base_img}" ] # 判断基础xml和基础镜像是否存在
	then
		echo "基础镜像或者基础镜像xml不存在"
		exit 1
	else
   		vir_uuid=`uuidgen`
		vir_mac="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum |sed -r 's/^(..)(..)(..)(..).*$/\1:\2:\3:\4/')"
		vir_name=$machine_name        
	fi 
	
}
#创建磁盘
function create_disk(){
	chattr -i  ${images_dir}$base_img
	qemu-img create -f qcow2 -b ${images_dir}$base_img ${images_dir}${vir_name}.qcow2 ${vir_disk}G  &>/dev/null
}
#添加磁盘
function attach_disk(){
	qemu-img create -f qcow2  ${images_dir}${vir_name}-2.qcow2  ${vir_attach_disk}G  >/dev/null
	virsh attach-disk ${vir_name} ${images_dir}${vir_name}-2.qcow2 vdb --cache writeback --subdriver qcow2 --persistent >/dev/null
	
}

#配置xml ,替换内存cpu 等信息并导入
function conf_xml(){
	cp  ${xmls_dir}${base_xml}  /tmp/${vir_name}.xml
	sed -i "s#<memory unit='KiB'>.*</memory>#<memory unit='KiB'>${vir_mem}</memory>#"  /tmp/${vir_name}.xml 
	sed -i "s#<currentMemory unit='KiB'>.*</currentMemory>#<currentMemory unit='KiB'>${vir_mem}</currentMemory>#"   /tmp/${vir_name}.xml 
	sed -i "s#<vcpu placement='static'>.*</vcpu>#<vcpu placement='static'>${vir_cpu}</vcpu>#" /tmp/${vir_name}.xml
	sed -i "s/<name>.*<\/name>/<name>${vir_name}<\/name>/" /tmp/${vir_name}.xml
	sed -i "s/<uuid>.*<\/uuid>/<uuid>${vir_uuid}<\/uuid>/" /tmp/${vir_name}.xml
	sed -i "s/<title>.*<\/title>/<title>${vir_ip}<\/title>/" /tmp/${vir_name}.xml
	sed -i "s#<source file=.*/>#<source file='${images_dir}${vir_name}.qcow2'/>#" /tmp/${vir_name}.xml
	sed -i "s/<mac address=.*\/>/<mac address='$vir_mac' \/>/" /tmp/${vir_name}.xml
	virsh define /tmp/${vir_name}.xml >/dev/null
	virt-edit -d ${vir_name}  /etc/sysconfig/network-scripts/ifcfg-eth0  -e "s#IPADDR=".*"#IPADDR="${vir_ip}"#"  # 替换ip
}

#启动并设置开机自启
function start_vir(){
	virsh start ${vir_name} >/dev/null
	virsh autostart ${vir_name} >/dev/null
	chattr +i  ${images_dir}$base_img
}
#记录创建日志
function create_vir_log(){
	echo "$(date +%F_%T)  vir_name: ${vir_name} vir_ip: ${vir_ip} !"   >>$vir_log_file
	echo "$(date +%F_%T)  vir_name: ${vir_name} vir_ip: ${vir_ip} 创建成功!" 
}

main(){
    images_dir="/home/kvm/data/" # 镜像存储的位置
    base_img="base-image.qcow2"  # 基础镜像的名称
    xmls_dir="/etc/libvirt/qemu/"  # xml 的位置
    base_xml="base-image.xml"   # 基础xml的名称 
    vir_log_file="/tmp/kvm_create_log.txt" # 创建日志文件
    vir_disk=50 # 磁盘默认为50G  
    vir_cpu=2 # cpu默认为2核
    vir_mem=8388608 # 内存默认为8G
    echo -e "服务器配置选项:\n 1: 2核8G 50G(测试) \n 2: 1核8G 50G(开发) \n 3: 2核8G 100G(数据库) \n 4: 4核20G 200G" 
    read  -t 30 -p  "输入你选择的配置的编号(1-4):" number
    read  -t 90 -p "输入想要创建的IP(192.168.1.1):"  vir_ip
    read  -t 120 -p "输入想要创建的机器名称(haha-biz-1234567-test):"  machine_name
    if [ -z "$vir_ip" ] || [  -z "$machine_name" ] # 判断是否输入ip和机器名称
    then
       echo "请输入ip和机器名称"
       exit -1
    fi
    case "$number" in
        [1] )
            check
            create_disk
            conf_xml
            start_vir
            create_vir_log
        ;;
        [2] )
            vir_cpu=1  # 设置为1核
            check
            create_disk
            conf_xml
            start_vir
            create_vir_log
        ;;
        [3] )
            vir_disk=100 #设置为磁盘100G盘
            check
            create_disk
            conf_xml
            start_vir
            create_vir_log
        ;;
        [4] )
            base_img="dc-base-image.qcow2"
            vir_cpu=4  # 设置为4核
            vir_mem=20971520 #设置内存为20G
            vir_disk=20 #设置为磁盘200G盘
            vir_attach_disk=200 # 设置附加盘的大小
            check
            create_disk
            conf_xml
            start_vir
            attach_disk
            create_vir_log
        ;;
        *) echo "输入编号(1-4)";;
    esac
}
main

四、挂载磁盘多种方式

扩展对象为: 虚拟机

4.1 扩容现有的磁盘

  • 这种方式只能增加不能缩小
  • 需要先把虚拟机关闭。(注意)

虚拟机可以进行两种类型的操作:(以下两步是冲突的,只能2选1)
1、是使用新增空间扩容现有的磁盘
2、是使用新增空间创建新的分区

4.2 添加新的磁盘

五、常用操作命令

标签:xml,name,qcow2,虚拟机,KVM,一键,dev,vir,512
来源: https://www.cnblogs.com/operationhome/p/12813437.html