Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Kvm creates virtual machines in batches and automatically sets ip and server names

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Description

The last thing kvm does not have compared with vmware is the ability to customize ip addresses when deploying virtual machines with templates. For a large number of scenarios that create vm, you need a vnc login to change the ip, which is obviously very tiring. I have encountered this scenario recently (not to mention large cloudstack and openstack architectures).

An idea is to use a shut-down virtual machine as a template, and copy its xml file to change the mac address, so that you can add a script to the template virtual machine through the uniqueness of the mac address to set the corresponding ip address and server name according to a list file written in the virtual machine.

This approach requires the following steps

1. There is a boot script in the template virtual machine, which sets its own ip and server name according to the list file, and establishes a lock file after setting it, so as not to reset it after restarting again.

two。 The list file is written in the template virtual machine, which limits the flexible scene, which is only valid in the pre-set virtual machine creation scene, if you want to temporarily add virtual machines, and the number is small (one or two), it is very difficult for you to spend the function to update the list file in the template virtual machine.

The mac address of the virtual machine created by 3.kvm should be consistent with the list file in the template virtual machine.

two。 Preparation of template virtual machine

The main tasks that the template virtual machine needs to complete are as follows

1. The same virtual machine path as the template used by kvm to create the virtual machine

There is a corresponding startup setup script in 2./etc/rc.local

3. List file

Boot script

[root@linux-host2 ~] # cat init_setting.sh #! / bin/bashsetting_list= "/ root/setting_list" if [!-f $setting_list]; then echo "[$setting_list] not exist" exit 1 filockbacks file= "/ root/setting_lock" if [- f $lock_file]; then echo "this vm has been set.exit" exit 0fimprints 0mac`IP a | awk'/ eth0/ {getline Print}'| grep 'link/ether' | awk' {print $2}'| awk-print $4 ":" $5 ":" $6} '`myinfoline= `cat $setting_list | grep $myinfoline= 0mac` # if [!-f "$myinfoline"] Then# echo "sorry,can't fine info from [$setting_list]" # exit 1roomfimyip`echo $myinfoline | awk'{print $2} '`myhostname= `echo $myinfoline | awk' {print $3} '`sed-ir "s/IPADDR=.*/IPADDR=$myip/" / etc/sysconfig/network-scripts/ifcfg-eth0systemctl restart networkhostnamectl set-hostname $myhostnameecho "this vm has been set" > $lock_file

Add the above script to boot

Echo "/ root/init_setting.sh > / root/init.log 2 > & 1" > > / etc/rc.localchmod 755 / etc/rc.d/rc.local

/ root/setting_list file example, the following corresponds to the last three ip host names of the mac address. If you ask how to generate the mac address, use the following command to generate it. I have only done three digits here.

Openssl rand-hex 3 | sed-r's B/&:/g'4a:b7:01 xxx.xx.xx.201 vm171:5b:32 xxx.xx.xx.202 vm228:34:18 xxx.xx.xx.203 vm3..\ B/&:/g'4a:b7:01 xxx.xx.xx.201 vm171:5b:32 xxx.xx.xx.202 vm228:34:18 xxx.xx.xx.203 vm3

Then shut down the virtual machine for use by kvm clones

3. Kvm batch create virtual machine script [root@localhost ~] # cat lxd/kvm3.sh #! / bin/bash# create a single virtual machine (requires a template virtual machine in the computer) # get the new virtual machine name (single creation mode) get_newname () {while true do read-p "Please enter the new virtual machine name:" newname if [$newname] Then break else echo "*" echo "Please enter a name!" Echo "*" fi done} get_newmemary () {while true do current_free_mem= `free-m | awk'/ ^-/ {print $4} '`mem_total= `free-g | awk' / ^ Mem/ {print $2} '`echo "current total native memory size: ${mem_total} G" Echo "current free memory size is: ${current_free_mem} MB" read-p "Please enter the new virtual machine memory size (in G):" newmemary if [$newmemary] Then if [[$newmemary-le $mem_total]] The value entered by then break else echo "* *" echo "must be less than the current total memory size!" Echo "* *" fi else echo "*" echo "Please enter the new virtual machine memory!" Echo "*" fi done} get_newcpu () {while true do core= `cat / proc/cpuinfo | grep "processor" | wc-l`echo "number of available core: ${core}" read-p "Please enter the number of new virtual machine processor cores:" newcpu if [$newcpu] Then if [$newcpu-le $core]; then break else echo "* *" echo "cannot exceed the number available or enter an error!" Echo "*" fi else echo "*" echo "input cannot be empty!" Echo "*" fi done} # set virtual machine name (custom create multiple virtual machines) set_group_name () {# number of virtual machines read-p "number of new virtual machines:" vir_num free_vir=$vir_num # feature grouping echo "* * after "echo" sets the group name The virtual machine automatically uses [group name 01, group name 01, group name 03, The formal naming of "echo" * "read-p" requires several functional types (greater than 1). ): "vir_type if [${vir_type}-gt ${vir_num}] Then echo "input does not conform to the rules!" Exit fi # set the name, group name + the number kumb1 for ((iTune1 for)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report