折腾了两天,终于搞定了,主要还是对python不太熟悉,踩了一些坑,记录下成长。
这里是已经有3种image文件,随机生成虚拟机。最开始是想每起个VM都copy一个image文件,这样很不好,image每个文件至少1G多。
然后小伙伴提醒了有增量虚拟机。然后网上查阅了下增量虚拟机http://www.linuxidc.com/Linux/2012-06/61692.htm
#qemu-imgcreate -b source.img -f qcow2 new.img
新生成的new.img文件小多了!
通过.xml文件启虚拟机,前面说到,需要修改
<name>test</name>
<uuid>
<memory><vcpu>
<devices> -> <disk> -> <source file=""> 指向image的路径
<mac>
<interface> -> <mac> 和 <source bridge="">
等参数。
这里选择通过sed命令修改文件中字符串匹配的内容(每个新生成的xml文件里的这些参数都是固定一样的,方便修改)
sed -i 's/源字符串/要替换的字符串/g' xxxx.xml 灰常好用的命令啊!
在python里通过执行os.system('')来执行shell命令。
在生成单播mac地址的时候,根据第二位的奇偶性来判断。偶数为单播,奇数为组播mac地址。但是这里有个问题,字符’1‘的ascii码的值是49,字符‘a’(十六进制的10)的ascii码的值是97,所以不能直接对第二个字符的ascii码的值对2取余做判断。
解决办法:分情况进行判断
while((ord(macaddr[1])<96 and ord(macaddr[1])%2!=0) or(ord(macaddr[1])>96 and ord(macaddr[1])%2!=1)):
附1 创建vm的试写脚本
#create a vm through virsh
#!/usr/bin/python
import time
import uuid
import os
import commands
import random
image = 'prime95'
#get image
uuid = str(uuid.uuid1())
print uuid
name = image+'_'+uuid
imagename = image+'_image_'+uuid
#os.system('cp /home/ubuntu/jinxingf/ipmidata/virsh-image/' + image+'_image /home/ubuntu/jinxingf/ipmidata/virsh-image/' +imagename)
os.system('qemu-img create -b /home/ubuntu/jinxingf/ipmidata/virsh-image/' + image+'_image' +' -f qcow2 /home/ubuntu/jinxingf/ipmidata/virsh-image/' +imagename)
#uuid = '2f372706-836b-11e5-b8d2-001e67947e90'
xmlname = image +'_'+ uuid+'.xml'
#get .xml
os.system('cp /home/ubuntu/jinxingf/ipmidata/vm-xml/'+image+'.xml /home/ubuntu/jinxingf/ipmidata/vm-xml/' + xmlname)
#change uuid in .xml
os.system('sed -i \'s/c6e32416-fcbe-4c96-9e8d-976d91612bc5/' + uuid + '/g\' /home/ubuntu/jinxingf/ipmidata/vm-xml/'+image+'_'+uuid+'.xml')
Maclist = []
for i in range(1,7):
RANDSTR = "".join(random.sample("0123456789abcdef",2))
Maclist.append(RANDSTR)
macaddr = ":".join(Maclist)
while((ord(macaddr[1])<96 and ord(macaddr[1])%2!=0) or (ord(macaddr[1])>96 and ord(macaddr[1])%2!=1)):
Maclist = []
for i in range(1,7):
RANDSTR = "".join(random.sample("0123456789abcdef",2))
Maclist.append(RANDSTR)
macaddr = ":".join(Maclist)
print macaddr
#change image source in .xml
os.system('sed -i \'s/'+image+'_image'+'/' + imagename + '/g\' /home/ubuntu/jinxingf/ipmidata/vm-xml/'+image+'_'+uuid+'.xml')
#change macaddress in .xml
os.system('sed -i \'s/d4:3d:ab:c9:a3:5f/' + macaddr + '/g\' /home/ubuntu/jinxingf/ipmidata/vm-xml/'+image+'_'+uuid+'.xml')
#change name in .xml
os.system('sed -i \'s/test-image/' + name + '/g\' /home/ubuntu/jinxingf/ipmidata/vm-xml/'+image+'_'+uuid+'.xml')
os.system('virsh define /home/ubuntu/jinxingf/ipmidata/vm-xml/'+xmlname)
os.system('virsh start '+name)
#delete file when virsh vms
import os
import commands
status, output = commands.getstatusoutput('virsh list')
images_del = output.split(' ')
for image_del in images_del:
if image_del.startswith('unix') or image_del.startswith('stress') or image_del.startswith('prime'):
print image_del
os.system('virsh shutdown '+ image_del)
os.system('rm /home/ubuntu/jinxingf/ipmidata/virsh-image/unixbench_image_*')
os.system('rm /home/ubuntu/jinxingf/ipmidata/virsh-image/stress_image_*')
os.system('rm /home/ubuntu/jinxingf/ipmidata/virsh-image/prime95_image_*')
os.system('rm /home/ubuntu/jinxingf/ipmidata/vm-xml/unixbench_*')
os.system('rm /home/ubuntu/jinxingf/ipmidata/vm-xml/stress_*')
os.system('rm /home/ubuntu/jinxingf/ipmidata/vm-xml/prime95_*')