设为首页 收藏本站
查看: 1690|回复: 0

[经验分享] KVM脚本批量添加删除虚拟机版本2

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-15 08:56:38 | 显示全部楼层 |阅读模式
在原有的基础上,做些功能上的添加.
  • 修改虚拟机的主机名

  • 修改虚拟机的MAC
  • 修改虚拟机的IP
  • 虚拟机采用qcow2格式,使用qemu-img的backing_file技术,快速生成虚拟机

这样,虚拟机创建好后,便可以远程管理了。




第2版,create_delete_vm.py代码:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
#coding:utf-8
###################################################################
#Auth:Zhuzhengjun
#LastModified:2015/01/14
#Version 1.0
#Function Description:
#Batch automatically generated/delete VM
#1.Generated VM
##1.1.Create qcow2 vm img file
##1.2.Create VM XML file
###1.2.1.Update UUID
###1.2.2.Update MAC
###1.2.3.Update img path
###1.2.4.Update VM Name
#2.Edit VM
##2.1.Edit HostName
##2.2.Edit Mac
##2.3.Edit IP
#3.Start VM
#4.Delete VM
####################################################################

#import module
import shutil
import os,sys
from virtinst.util import *
import libvirt
import re
import subprocess
import guestfs
import jinja2

if sys.version_info < (2,5):
        import lxml.etree as ET
else:
        import xml.etree.ElementTree as ET

#Define variables

template_img_path="/template/img"
template_xml_path="/template/xml"
vm_img_path="/var/lib/libvirt/images"
vm_xml_path="/etc/libvirt/qemu"
vm_file="/template/scripts/vm.ini"
uri="qemu:///system"
domain=".tc.com"

JINJA = jinja2.Environment(
    loader=jinja2.FileSystemLoader(
        '/template/jinja'
    )
)

def file_exists(file):
    if os.path.exists(file):
        return 1
    else:
        return 0

def create_vm_img_file(src_img_file,dst_img_file):
    command="/usr/bin/qemu-img create -f qcow2 -o cluster_size=2M,backing_file="+src_img_file+" "+dst_img_file+" 20G"
    try:
        subprocess.check_call(command,shell=True)
    except subprocess.CalledProcessError as err:
        print "Error:",err
        sys.exit(1)
    print "Done!"

'''
def copy_vm_img_file(src_img_file,dst_img_file):
    print "Start Copy",src_img_file,"to",dst_img_file
    if file_exists(dst_img_file):
        print "File %s exists, abort" % dst_img_file
        sys.exit(1)
    shutil.copyfile(src_img_file,dst_img_file)
    print "Done!"
'''

def start_vm(vm_xml_file,vm_name):
    try:
        conn = libvirt.open(uri)
    except Exception,e:
        print 'Faild to open connection to the hypervisor'
        sys.exit(1)
    create = True
    if create:
        xmlfile=open(vm_xml_file)
        xmldesc=xmlfile.read()
        xmlfile.close()
    try:
        vmname = conn.defineXML(xmldesc)
    except Exception,e:
        print "Failed to define %s:%s" %(vm_name,e)
        sys.exit(1)
    if vmname is None:
        print 'whoops this shouldnt happen!'
    try:
        vmname.create()
    except Exception,e:
        print "Failed to create %s:%s" %(vm_name,e)
        sys.exit(1)
    try:
        print "Domain 0:id %d running %s" %(vmname.ID(),vmname.name())
    except Exception,e:
        print e
    try:
        conn.close()
    except:
        print "Faild to close the connection!"
        sys.exit(1)
    print "Done!"
    print "="*100

def edit_vm(dst_img_file,vm_mac,vm_ip,hostname):
    g = guestfs.GuestFS(python_return_dict=True)
    g.add_drive_opts(dst_img_file)
    g.launch()
    partions_root=g.inspect_os()
    g.mount(partions_root[0],'/')

    #edit hostname
    hostname_fn='hostname.jinja'
    try:
        template_hostname = JINJA.get_template(hostname_fn)
    except jinja2.exceptions.TemplateNotFound:
        print "error"
        sys.exit(1)
    hostname_context={'hostname':hostname}
    hostname_content=template_hostname.render(**hostname_context)
    g.write('/etc/sysconfig/network',hostname_content)

    #edit mac
    mac_fn='mac.jinja'
    try:
        template_mac = JINJA.get_template(mac_fn)
    except jinja2.exceptions.TemplateNotFound:
        print "error"
        sys.exit(1)
    mac_context={'mac':vm_mac}
    mac_content=template_mac.render(**mac_context)
    g.write('/etc/udev/rules.d/70-persistent-net.rules',mac_content)

    #edit ip
    net_fn='net.jinja'
    try:
        template_net = JINJA.get_template(net_fn)
    except jinja2.exceptions.TemplateNotFound:
        print "error"
        sys.exit(1)
    net_context={'mac':vm_mac,'ip':vm_ip}
    net_content=template_net.render(**net_context)
    g.write('/etc/sysconfig/network-scripts/ifcfg-eth0',net_content)
    g.close()

def create_vm_xml_file(src_xml_file,vm_name,dst_img_file,vm_ip,hostname):
    config = ET.parse(src_xml_file)
    name = config.find('name')
    name.text = vm_name.strip()
    uuid = config.find('uuid')
    uuid.text = uuidToString(randomUUID())
    mac = config.find('devices/interface/mac')
    vm_mac=randomMAC(type='qemu')
    mac.attrib['address'] = vm_mac
    disk = config.find('devices/disk/source')
    disk.attrib['file']=dst_img_file
    vm_xml_name=vm_name.strip() + '.xml'
    vm_xml_file=os.path.join(vm_xml_path,vm_xml_name)
    if file_exists(vm_xml_file):
        print "File %s exists, abort" % vm_xml_file
        sys.exit(1)
    config.write(vm_xml_file)
    print "Created vm config file %s" % vm_xml_file
    #print "Use disk image %s, you must create it from the template disk: %s" % (disk_image, disk_old)
    print "Done!"
    #Function 2 Edit VM
    edit_vm(dst_img_file,vm_mac,vm_ip,hostname)

    #Function 3 Start VM
    print "Start VM "+hostname
    start_vm(vm_xml_file,vm_name)
def delete_file(file_name):
    if file_exists(file_name):
        os.unlink(file_name)
def delete_vm(vm_name):
    vmimg=vm_name+".qcow2"
    vmxml=vm_name+".xml"
    img_file=os.path.join(vm_img_path,vmimg)
    xml_file=os.path.join(vm_xml_path,vmxml)
    try:
        conn = libvirt.open(uri)
    except Exception,e:
        print 'Faild to open connection to the hypervisor'
        sys.exit(1)
    try:
        server=conn.lookupByName(vm_name)
    except Exception,e:
        print e
        sys.exit(1)
    if server.isActive():
        print "VM %s will be shutdown!" %vm_name
        try:
            #server.shutdown()#VM need install acpid
            server.destroy()
        except Exception,e:
            print e
            sys.exit(1)
        print "VM %s will be delete!" %vm_name
        try:
            server.undefine()
        except Exception,e:
            print e
            sys.exit(1)
                                 
        delete_file(img_file)
        delete_file(xml_file)
                                       
        try:
            conn.close()
        except:
            print "Faild to close the connection!"
            sys.exit(1)
    else:
        print "VM %s will be delete!" %vm_name
        try:
            server.undefine()
        except Exception,e:
            print e
            sys.exit(1)
                                       
        delete_file(img_file)
        delete_file(xml_file)
    print "Done"
    print "="*100

###############Main############################################
#Open config file
fh=open(vm_file)
vm_config=fh.readlines()
fh.close()
for line in vm_config:
    passline=re.compile("#.*")
    if re.search(passline,line)!=None:
        continue
    (action,vm_name,src_file,xml_file,vm_ip)=line.strip().split(",")
    hostname=vm_name+domain
    if action=='add':
        src_img_file=os.path.join(template_img_path,src_file)
        dst_img_file=os.path.join(vm_img_path,vm_name.strip()+".qcow2")
        src_xml_file=os.path.join(template_xml_path,xml_file)
        if not (file_exists(src_img_file) and file_exists(src_xml_file)):
            print "File %s or %s not exists,abort!" %(src_img_file,src_xml_file)
            sys.exit(1)
                                
        #Function1.1 Create qcow2 vm img file
        print "Create VM "+hostname+" image file and file type qcow2"
        create_vm_img_file(src_img_file,dst_img_file)
        #Function1.2 Create VM XML file
        print "Create VM "+hostname+"  Xml config file"
        create_vm_xml_file(src_xml_file,vm_name,dst_img_file,vm_ip,hostname)
    elif action=="delete":
        #Function4 Delete VM
        print "Delete VM"
        delete_vm(vm_name)



虚拟机的配置文件:
vm.ini

1
2
3
4
5
#Action,Vm_name,Template_img_file,Template_xml_file,vm_ip
add,web20,Template_Centos63x64.img,Template_Centos63x64.xml,192.168.x.26
add,web30,Template_Centos63x64.img,Template_Centos63x64.xml,192.168.x.27
#delete,web20,none,none,none
#delete,web30,none,none,none



jinja相关的配置文件
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
hostname.jinja
HOSTNAME={{hostname}}
NETWORKING=yes
# IPv4
NETWORKING=yes
NOZEROCONF=yes
# IPv6, necessary for bonding
NETWORKING_IPV6=yes
IPV6INIT=yes

mac.jinja
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x1af4:0x1000 (virtio-pci)
#context={'macs':{'eth0': {'mac': 'mac0'}, 'eth1': {'mac': 'mac1'}}}
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="{{mac}}", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

net.jinja
DEVICE="eth0"
BOOTPROTO="static"
HWADDR={{mac}}
IPADDR={{ip}}
NETMASK=255.255.255.0
GATEWAY=192.168.x.254
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"



批量生成如下:
[iyunv@kvm scripts]# python create_delete_vm.py
Create VM web20.tc.com image file and file type qcow2
Formatting '/var/lib/libvirt/images/web20.qcow2', fmt=qcow2 size=21474836480 backing_file='/template/img/Template_Centos63x64.img' encryption=off cluster_size=2097152
Done!
Create VM web20.tc.com  Xml config file
Created vm config file /etc/libvirt/qemu/web20.xml
Done!
Start VM web20.tc.com
Domain 0:id 60 running web20
Done!
====================================================================================================
Create VM web30.tc.com image file and file type qcow2
Formatting '/var/lib/libvirt/images/web30.qcow2', fmt=qcow2 size=21474836480 backing_file='/template/img/Template_Centos63x64.img' encryption=off cluster_size=2097152
Done!
Create VM web30.tc.com  Xml config file
Created vm config file /etc/libvirt/qemu/web30.xml
Done!
Start VM web30.tc.com
Domain 0:id 61 running web30
Done!
====================================================================================================
供参考!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-40845-1-1.html 上篇帖子: KVM安装虚拟机 下篇帖子: centos搭建kvm 虚拟机 技术 主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表