]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - debian/cloud-tools/hv_set_ifconfig
4b4f49f92b228f875c7532a10036457e443c5e0a
3 # hv_set_ifconfig <config> -- take the hv_kvp_daemon generated configuration
4 # file and apply it to the Ubuntu configuration.
8 # HWADDR=11:22:33:44:55:66
13 # HWADDR=11:22:33:44:55:66
15 # IPADDR=192.168.99.10
16 # GATEWAY=192.168.99.1
18 # IPADDR2=192.168.99.11
19 # IPV6ADDR=2001:DB8:99::10
21 # IPV6_DEFAULTGW=2001:DB8:99::10
23 # set interfaces in hv_kvp_daemon style
32 if_filename
="/etc/network/interfaces"
34 # Drop our output (XXX?)
35 sys
.stdout
= open(os
.devnull
, 'w')
36 sys
.stderr
= open(os
.devnull
, 'w')
38 # Confirm we can open the network configuration.
40 if_file
=open(if_filename
,"r+")
46 # Usage: hv_set_ifconfig <config>
47 if len(sys
.argv
) != 2 :
51 # Here is the format of the ip configuration file:
54 # DEVICE=interface name
55 # BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
56 # or "none" if no boot-time protocol should be used)
60 # IPADDRx=ipaddry (where y = x + 1)
63 # NETMASKx=netmasky (where y = x + 1)
66 # GATEWAYx=ipaddry (where y = x + 1)
68 # DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
70 # IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
71 # tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
75 kvp
=dict(line
.strip().split("=") for line
in fileinput
.input())
77 # Setting the hwaddress to something azure is not expecting is fatal
79 if not "HWADDR" in kvp
:
82 # Confirm we have a device specified.
83 if not "DEVICE" in kvp
:
87 basename
=kvp
["DEVICE"]
89 # DNS entries will go with the first interface and there can be a max
90 # of three. These will be emitted with the first interface.
92 for count
in (1, 2, 3):
93 key
= "DNS" + str(count
)
98 # IPV4 may either be dhcp or static.
99 if ("DHCP" in kvp
and kvp
["DHCP"] == "yes") or \
100 ("BOOTPROTO" in kvp
and kvp
["BOOTPROTO"] == "dhcp"):
101 output
+= ["auto " + basename
]
102 output
+= ["iface " + basename
+ " inet dhcp"]
106 # Matchup the interface specific lines
108 # No real max for the number of interface + aliases ...
109 # only required is the address (but mate everything up that comes in.
111 # IPv4 -- ensure we sort by numeric suffixes.
112 v4names
= [ int(name
[6:]) for name
in kvp
.keys() if name
.startswith("IPADDR") ]
115 for if_count
in v4names
:
117 which
= str(if_count
)
120 ifname
+= ":" + str(if_count
)
125 if not ifname
in autolist
:
128 output
+= [ "iface " + ifname
+ " inet static" ]
129 output
+= [ "\t" + "address " + kvp
["IPADDR" + which
] ]
130 if "NETMASK" + which
in kvp
:
131 output
+= [ "\tnetmask " + kvp
["NETMASK" + which
] ]
132 if "GATEWAY" + which_gw
in kvp
:
133 output
+= ["\tgateway " + kvp
["GATEWAY" + which_gw
]]
137 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
140 # IPv6 requires a netmask
141 # If an ipv6 exists, you'll want to turn off /proc/sys/net/ipv6/conf/all/autoconf with
142 # echo 0 > /proc/sys/net/ipv6/conf/all/autoconf
143 v6names
= [ int(name
[8:]) for name
in kvp
.keys() if name
.startswith("IPV6ADDR") ]
146 for if6_count
in v6names
:
148 which
= str(if6_count
)
151 ifname
+= ":" + str(if6_count
)
156 if not ifname
in autolist
:
159 if "IPV6NETMASK" + which
in kvp
:
160 output
+= [ "iface " + ifname
+ " inet6 static"]
161 output
+= [ "\taddress " + kvp
["IPV6ADDR" + which
]]
162 output
+= [ "\tnetmask " + kvp
["IPV6NETMASK" + which
]]
163 if "IPV6_DEFAULTGW" + which_gw
in kvp
:
164 output
+= [ "\tgateway " + kvp
["IPV6_DEFAULTGW" + which_gw
] ]
167 output
+= ["\tdns-nameservers " + ' '.join(dns
)]
170 # Mark this new interface for automatic up.
171 output
= ["auto "+" ".join(autolist
)] + output
173 print("===================================")
175 print("===================================")
178 # Time to clean out the existing interface file
181 start_mark
= "# The following stanza(s) added by hv_set_ifconfig"
182 end_mark
= "#End of hv_set_ifconfig stanzas"
184 f
=open(if_filename
,"r")
193 if line
.startswith("auto"):
196 newfile
.extend(stanza
)
200 autoline
=line
.strip().split(" ")
201 for word
in autoline
:
202 if (not word
== basename
) and (not word
.startswith(basename
+":")):
204 newline
= newline
.strip()
205 if not newline
== "auto":
206 newfile
+= [newline
.strip()]
207 elif line
.startswith(("iface","mapping","source")):
209 '''A Stanza can also start with allow- ie allow-hotplug'''
212 newfile
.extend(stanza
)
216 autoline
=line
.strip().split(" ")
217 for word
in autoline
:
218 if (word
== basename
) or (word
.startswith(basename
+":")):
221 stanza
+=[line
.strip()]
222 elif line
.strip() in (start_mark
, end_mark
):
225 newfile
.extend(stanza
)
229 # Deduplicate markers.
230 if line
!= prev_line
:
231 newfile
+= [line
.strip()]
235 stanza
+=[line
.strip()]
238 newfile
+= [line
.strip()]
246 os
.write(fd
, output
.encode('utf-8'))
248 # Insert the new output at the end and inside the existing markers if found.
250 fd
, path
= tempfile
.mkstemp()
253 emit("\n".join(output
))
258 emit("\n".join(output
))
262 shutil
.copy(path
,if_filename
)
263 os
.chmod(if_filename
,0o644)
265 #print("TMPFILE is at: " + path)
266 #print("Copied file is at: " + if_filename)
269 retcode
= subprocess
.call("ifdown "+basename
, shell
=True)
271 print("Child was terminated by signal", -retcode
, file=sys
.stderr
)
273 print("Child returned", retcode
, file=sys
.stderr
)
275 print("Execution failed:", e
, file=sys
.stderr
)
278 retcode
= subprocess
.call("ifup "+basename
, shell
=True)
280 print("Child was terminated by signal", -retcode
, file=sys
.stderr
)
282 print("Child returned", retcode
, file=sys
.stderr
)
284 print("Execution failed:", e
, file=sys
.stderr
)