KVM 一键批量创建虚拟机 自动设置IP

您所在的位置:网站首页 如何自动配置ip地址 KVM 一键批量创建虚拟机 自动设置IP

KVM 一键批量创建虚拟机 自动设置IP

2024-07-16 13:45:01| 来源: 网络整理| 查看: 265

目录一、原理二、基础镜像2.1、创建基础镜像2.2、 完善基础镜像2.3、基础镜像设置权限3.4 设置 title3.5、基础镜像XML三、批量创建机器脚本四、挂载磁盘多种方式4.1 扩容现有的磁盘4.2 添加新的磁盘五、常用操作命令

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

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

解决的问题:

之前创建机器后需要手动去设置IP. virt-edit 进行编辑网络文件。 创建完机器,我们不知道机器对应的哪个IP. 使用 来进行标记。 使用virsh list --title进行查看 机器和IP对应关系 二、基础镜像 2.1、创建基础镜像 内存为8G 和2核 磁盘为50G vnc 端口使用随机 网络设置为桥接 机器设置为自动启动 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、 完善基础镜像

增加 console 登陆

grubby --update-kernel=ALL --args="console=ttyS0,115200n8"

vnc 端口设置随机

使用桥接网络

设置网络配置

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。

192.168.1.156 3.5、基础镜像XML base-image 12348899-e829-4217-a0ff-f30eee4d2b31 192.168.1.199 16777216 8388608 4 hvm Broadwell-IBRS destroy restart destroy /usr/libexec/qemu-kvm /dev/urandom 三、批量创建机器脚本

因为需要使用 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#.*#${vir_mem}#" /tmp/${vir_name}.xml sed -i "s#.*#${vir_mem}#" /tmp/${vir_name}.xml sed -i "s#.*#${vir_cpu}#" /tmp/${vir_name}.xml sed -i "s/.*/${vir_name}/" /tmp/${vir_name}.xml sed -i "s/.*/${vir_uuid}/" /tmp/${vir_name}.xml sed -i "s/.*/${vir_ip}/" /tmp/${vir_name}.xml sed -i "s###" /tmp/${vir_name}.xml sed -i "s///" /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 扩容现有的磁盘 这种方式只能增加不能缩小 需要先把虚拟机关闭。(注意)

宿主机操作

[root@djx-kvm-123456-test opt]# virsh shutdown djx-mysql-djxmysq-test [root@djx-kvm-123456-test opt]# qemu-img info /home/kvm/data/djx-mysql-djxmysq-test.qcow2 image: /home/kvm/data/djx-mysql-djxmysq-test.qcow2 file format: qcow2 virtual size: 100G (107374182400 bytes) disk size: 131M cluster_size: 65536 backing file: /home/kvm/data/base-image.qcow2 Format specific information: compat: 1.1 lazy refcounts: false [root@djx-kvm-af3r34t-test opt]# qemu-img resize /home/kvm/data/djx-mysql-djxmysq-test.qcow2 +3G Image resized. [root@djx-kvm-af3r34t-test opt]# qemu-img info /home/kvm/data/djx-mysql-djxmysq-test.qcow2 image: /home/kvm/data/djx-mysql-djxmysq-test.qcow2 file format: qcow2 virtual size: 103G (110595407872 bytes) disk size: 254M cluster_size: 65536 backing file: /home/kvm/data/base-image.qcow2 Format specific information: compat: 1.1 lazy refcounts: false

虚拟机操作

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

使用新增空间扩容现有的磁盘

# 内核版本大于3.6.0 # 安装 growpart或者xfsprogs扩容格式化工具 yum install cloud-utils-growpart yum install xfsprogs # 运行fdisk -l命令查看现有云盘大小 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 8.5M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/vda2 50G 1.2G 49G 3% / /dev/vda1 497M 134M 363M 27% /boot tmpfs 783M 0 783M 0% /run/user/0 [root@localhost ~]# fdisk -l Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000a6596 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 104857599 51915776 83 Linux # 查看分区的文件系统类型 [root@localhost ~]# blkid /dev/vda2 /dev/vda2: UUID="be6f97e3-13c3-46ea-9694-78a575272bb8" TYPE="xfs" # 调用growpart为需要扩容的云盘和对应的第几个分区扩容 [root@localhost ~]# growpart /dev/vda 2 CHANGED: partition=2 start=1026048 old: size=103831552 end=104857600 new: size=208689119 end=209715167 # 运行xfs_growfs 命令调用xfs_growfs扩容文件系统。 # xfs 文件系统使用xfs_growfs ,ext4 文件系统使用 resize2fs [root@localhost ~]# xfs_growfs /dev/vda2 meta-data=/dev/vda2 isize=512 agcount=4, agsize=3244736 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=12978944, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=6337, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 12978944 to 26086139

使用新增空间创建新的分区(与上一步冲突的)

[root@localhost ~]# fdisk /dev/vda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition g create a new empty GPT partition table G create an IRIX (SGI) partition table l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): First sector (104857600-209715199, default 104857600): Using default value 104857600 Last sector, +sectors or +size{K,M,G} (104857600-209715199, default 209715199): Using default value 209715199 Partition 3 of type Linux and of size 50 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. [root@localhost ~]# fdisk -l Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000a6596 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 104857599 51915776 83 Linux /dev/vda3 104857600 209715199 52428800 83 Linux # 然后需要重启机器。不然直接挂载的话是挂载不了的。 # 格式化磁盘 [root@localhost ~]mkfs.xfs /dev/vda3 [root@localhost ~]# mount /dev/vda3 /home # 如果没有重启 ,报错 mount: special device /dev/vda3 does not exist # 还需要设置开动挂载 4.2 添加新的磁盘

宿主机操作

[root@k5-m2 ~]# qemu-img create -f qcow2 /home/kvm/data/djx-biz-djx1234-dev-2.qcow2 10G Formatting '/home/kvm/data/djx-biz-djx1234-dev-2.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off [root@k5-m2 ~]# virsh attach-disk djx-biz-djx1234-dev /home/kvm/data/djx-biz-djx1234-dev-2.qcow2 vdb --cache writeback --subdriver qcow2 --persistent Disk attached successfully [root@djx-kvm-af3r34t-test data]# virsh domblklist djx-biz-djx1234-dev Target Source ------------------------------------------------ vda /home/kvm/data/djx-biz-djx1234-dev.qcow2 vdb /home/kvm/data/djx-biz-djx1234-dev-2.qcow2 hda -

虚拟机操作

[root@localhost ~]# fdisk -l Disk /dev/vda: 85.9 GB, 85899345920 bytes, 167772160 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000a6596 Device Boot Start End Blocks Id System /dev/vda1 * 2048 1026047 512000 83 Linux /dev/vda2 1026048 104857599 51915776 83 Linux /dev/vda3 104857600 167772126 31457263+ 83 Linux Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes # 我们可以在系统盘这边看到我们新增加的磁盘,然后我们可以对该磁盘进行初始化和分区,并挂载使用了,注意在挂载的时候需要注意设置开机自动挂载。 具体操作见 7.1 虚拟机操作-使用新增空间创建新的分区 五、常用操作命令

查看虚拟机对应的ip

virsh list --title

登录虚拟机

通过 console

virsh console djx-biz-qawsed4-test

通过 ssh

通过 vnc

vnc 端口为随机。通过查找对应的进程来获取。

virsh list [--all] #查看所有KVM虚拟机

virsh dominfo name # 查看虚拟机 CPU/内存的使用率

virsh start name #启动KVM虚拟机

virsh shutdown name #正常关闭KVM虚拟机、基本是关不掉的

virsh autostart name # 设置开机自启

virsh autostart –disable name # 关闭开机自启

virsh destroy name #强制关闭KVM虚拟机(类似于直接断电)

virsh suspend name #挂起KVM虚拟机

virsh resume name #恢复挂起的KVM虚拟机

virsh dumpxml name #查看KVM虚拟机配置文件

virsh edit name #编辑KVM虚拟机的xml配置文件

virsh undefine name #彻底删除KVM虚拟机,不可逆,如果想找回来,需要备份/etc/libvirt/qemu的xml文件

virsh snapshot-create name #创建快照

virsh snapshot-delete name --current/--snapshotname #删除快照

virsh snapshot-dumpxml name #导出XML

virsh snapshot-list name #查看快照



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭