随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

pve配置虚机模板(自动配置IP)

安装好Centos虚机系统,进行标准化配置,最后安装cloud-init,如下:

复制代码
yum -y install qemu-guest-agent

yum -y install cloud-init

systemctl enable cloud-init-local.service
systemctl start cloud-init-local.service
systemctl enable cloud-init.service
systemctl start cloud-init.service
systemctl enable cloud-config.service
systemctl start cloud-config.service
systemctl enable cloud-final.service
systemctl start cloud-final.service

cloud-init init --local  #检查是否可用
复制代码

 配置完成后关机,删除硬件网卡,添加 Cloud-Init 硬件,然后转换为模板

 qm set 1003 --ide2 local-lvm:cloudinit

 

 

 

通过模板克隆新的虚机,自动配置IP,脚本如下:

复制代码
#!/bin/bash
#Function:Clone 1 vm from template
host='10.10.20.11'
vmname='vmtest02'
cpu_cores=8
memory_size=16384
ip='10.10.26.21'
vlanid=26
description='description'

#get vm max id
pve_id_max=`qm list |awk '{print $1}' |tail -n 1`
pve_vm_id=`expr $pve_id_max + 1`

#get cpu cores every cpu socket
cpu_cores_1=`expr $cpu_cores / 2`
storage=`echo $host |awk -F . '{print "dir-"$4}'`
ip_netmask="$ip/24"
gateway=`echo $ip |awk -F . '{print $1"."$2"."$3".254"}'`
vm_hostname="$vmname-$ip"

#clone vm from template(100)
qm clone 100 $pve_vm_id --name $vmname --full --format qcow2 --storage $storage

#configure vm cpu cors and memory size
if [ $cpu_cores -ne 4 -a $memory_size -ne 8192 ];then
  qm set $pve_vm_id --memory $memory_size --sockets 2 --cores $cpu_cores_1
fi

#configure vm description
if [ $description ];then 
  qm set $pve_vm_id --description=$description
fi
qm set $pve_vm_id --onboot #开机自启动
qm set $pve_vm_id --agent #启用vm agent qm set $pve_vm_id --localtime 1 #linux虚机默认为0,会使用utc时间,需要配置为1使用本地时间 qm set $pve_vm_id
-net0 e1000,bridge=vmbr1,firewall=1,tag=$vlanid qm set $pve_vm_id --ipconfig0 ip=$ip_netmask,gw=$gateway
#qm set $pve_vm_id --cipassword:123456 --ciuser:root #为root设置密码
echo 'Start and init vm for about 5 minutes...' #删除clould-init硬件,修改虚机名称 qm start $pve_vm_id sleep 300 qm stop $pve_vm_id sleep 2 qm set $pve_vm_id --delete ide2 sleep 2 qm set $pve_vm_id --name $vm_hostname qm start $pve_vm_id
复制代码

 

posted on   momingliu11  阅读(1071)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-10-09 执行http脚本
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示