且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

saltstack之virt模块创建虚拟机分析之四,创建虚拟机

更新时间:2022-10-04 13:51:33

pillar

/srv/pillar/top.sls 
base:
  'kvm.tc.com':
     - vm.profile


/srv/pillar/vm/profile.sls 
virt: {'nic': {'nics': [{'eth0': {'bridge': 'br0'}}]}, 'disk': {'system': [{'system': {'pool': '/var/lib/libvirt/im
ages', 'format': 'raw'}}, {'data': {'pool': '/var/lib/libvirt/images', 'format': 'raw'}}]}} 


virt.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
def _edit_vm(nics,img_dest,name):
    vm_name=name+".tc.com"
    nic_content={}
    for nic in nics:
        name=nic['name']
        vm_mac=nic['mac']
        vm_ip=nic['ip']
        nic_content[name]={}
        nic_content[name]['ip']=vm_ip
        nic_content[name]['mac']=vm_mac
    = guestfs.GuestFS(python_return_dict=True)
    g.add_drive_opts(img_dest)
    g.launch()
    #partions_root=g.inspect_os() 不能使用此功能,否则我后面写的客户端无提示.
    g.mount('/dev/sda3','/')  #这一块是针对我虚拟机镜像文件格式,如果你的不一样,可以做修改
    #g.mount(partions_root[0],'/')
    #edit vm hostname
    hostname_fn='hostname.jinja'
    try:
        template_hostname = JINJA.get_template(hostname_fn)
    except jinja2.exceptions.TemplateNotFound:
        return False
    hostname_context={'hostname':vm_name}
    hostname_content=template_hostname.render(**hostname_context)
    g.write('/etc/sysconfig/network',hostname_content)
 
    #edit vm mac
    mac_fn='mac.jinja'
    try:
        template_mac = JINJA.get_template(mac_fn)
    except jinja2.exceptions.TemplateNotFound:
        return False
    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 vm ip
    net_fn='net.jinja'
    try:
        template_net = JINJA.get_template(net_fn)
    except jinja2.exceptions.TemplateNotFound:
        return False
    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()
    return True

修改init函数

    for disk in diskp:
        for disk_name, args in disk.items():
            if disk_name=='system':
                fn_ = '{0}.{1}'.format(disk_name, args['format'])
                img_dest = os.path.join(
                    args['pool'],
                    name,
                    fn_
                )   #获取系统镜像文件

    if not _edit_vm(nicp,img_dest,name):
        return False


修改_nic_profile函数:

添加一段获取ip的

vm_ip=kwargs.get('ip','192.168.x.41') #这样,便可以在客户端处获取ip地址信息了.

再在生成mac下,添加:

attributes['ip'] = vm_ip


客户端

1
2
3
4
5
6
#!/usr/bin/env python
import salt.client
local=salt.client.LocalClient()
 
result=local.cmd('kvm.tc.com','virt.init',['web20','4','512','nic=nics','disk=system','ip=192.168.x.31'])
print result


执行后,便会主动生成一个web20的虚拟机.


后期,通过Django开发一个web界面,便可以提交创建虚拟机喽。



本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/1604513,如需转载请自行联系原作者