1 |
#!/usr/bin/python |
2 |
import libvirt |
3 |
import os |
4 |
import IPy |
5 |
|
6 |
# bridge_name |
7 |
|
8 |
# forward -> nat/ route |
9 |
# forward-dev |
10 |
|
11 |
# network |
12 |
# => deduire la gateway , et le range |
13 |
# en dhcp automatiquement |
14 |
|
15 |
# tftp_root |
16 |
|
17 |
# enable_pxelinux |
18 |
|
19 |
|
20 |
bridge_name = os.environ.get('BRIDGE_NAME', 'virbr0') |
21 |
forward = os.environ.get('FORWARD', 'nat') |
22 |
forward_dev = os.environ.get('FORWARD_DEV', 'eth0') |
23 |
|
24 |
network = os.environ.get('NETWORK', '192.168.122.0/24') |
25 |
|
26 |
tftp_root = os.environ.get('TFTP_ROOT', '') |
27 |
disable_pxelinux = os.environ.get('DISABLE_PXE', False) |
28 |
|
29 |
name = os.environ.get('NAME', 'default') |
30 |
|
31 |
|
32 |
ip = IPy.IP(network) |
33 |
gateway = ip[1] |
34 |
dhcp_start = ip[2] |
35 |
dhcp_end = ip[-2] |
36 |
|
37 |
netmask = ip.netmask() |
38 |
tftp_xml = '' |
39 |
pxe_xml = '' |
40 |
|
41 |
if tftp_root: |
42 |
tftp_xml = "<tftp root='" + tftp_root + "' />" |
43 |
if not disable_pxelinux: |
44 |
pxe_xml = "<bootp file='pxelinux.0' />" |
45 |
|
46 |
network_xml = """ |
47 |
<network> |
48 |
<name>%(name)s</name> |
49 |
<bridge name="%(bridge_name)s" /> |
50 |
<forward mode="%(forward)s" dev="%(forward_dev)s"/> |
51 |
<ip address="%(gateway)s" netmask="%(netmask)s"> |
52 |
%(tftp_xml)s |
53 |
<dhcp> |
54 |
<range start="%(dhcp_start)s" end="%(dhcp_end)s" /> |
55 |
%(pxe_xml)s |
56 |
</dhcp> |
57 |
</ip> |
58 |
</network>""" % globals() |
59 |
|
60 |
c=libvirt.open("qemu:///system") |
61 |
c.networkDefineXML(network_xml) |
62 |
|